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 "qemu/main-loop.h" 17 #include "qemu/timer.h" 18 #include "qemu/bitops.h" 19 #include "qemu/crc32c.h" 20 #include "qemu/qemu-print.h" 21 #include "exec/exec-all.h" 22 #include <zlib.h> /* For crc32 */ 23 #include "hw/irq.h" 24 #include "sysemu/cpu-timers.h" 25 #include "sysemu/kvm.h" 26 #include "sysemu/tcg.h" 27 #include "qapi/error.h" 28 #include "qemu/guest-random.h" 29 #ifdef CONFIG_TCG 30 #include "semihosting/common-semi.h" 31 #endif 32 #include "cpregs.h" 33 34 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ 35 36 static void switch_mode(CPUARMState *env, int mode); 37 38 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 39 { 40 assert(ri->fieldoffset); 41 if (cpreg_field_is_64bit(ri)) { 42 return CPREG_FIELD64(env, ri); 43 } else { 44 return CPREG_FIELD32(env, ri); 45 } 46 } 47 48 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 49 { 50 assert(ri->fieldoffset); 51 if (cpreg_field_is_64bit(ri)) { 52 CPREG_FIELD64(env, ri) = value; 53 } else { 54 CPREG_FIELD32(env, ri) = value; 55 } 56 } 57 58 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 59 { 60 return (char *)env + ri->fieldoffset; 61 } 62 63 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 64 { 65 /* Raw read of a coprocessor register (as needed for migration, etc). */ 66 if (ri->type & ARM_CP_CONST) { 67 return ri->resetvalue; 68 } else if (ri->raw_readfn) { 69 return ri->raw_readfn(env, ri); 70 } else if (ri->readfn) { 71 return ri->readfn(env, ri); 72 } else { 73 return raw_read(env, ri); 74 } 75 } 76 77 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 78 uint64_t v) 79 { 80 /* 81 * Raw write of a coprocessor register (as needed for migration, etc). 82 * Note that constant registers are treated as write-ignored; the 83 * caller should check for success by whether a readback gives the 84 * value written. 85 */ 86 if (ri->type & ARM_CP_CONST) { 87 return; 88 } else if (ri->raw_writefn) { 89 ri->raw_writefn(env, ri, v); 90 } else if (ri->writefn) { 91 ri->writefn(env, ri, v); 92 } else { 93 raw_write(env, ri, v); 94 } 95 } 96 97 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 98 { 99 /* 100 * Return true if the regdef would cause an assertion if you called 101 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 102 * program bug for it not to have the NO_RAW flag). 103 * NB that returning false here doesn't necessarily mean that calling 104 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 105 * read/write access functions which are safe for raw use" from "has 106 * read/write access functions which have side effects but has forgotten 107 * to provide raw access functions". 108 * The tests here line up with the conditions in read/write_raw_cp_reg() 109 * and assertions in raw_read()/raw_write(). 110 */ 111 if ((ri->type & ARM_CP_CONST) || 112 ri->fieldoffset || 113 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 114 return false; 115 } 116 return true; 117 } 118 119 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync) 120 { 121 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 122 int i; 123 bool ok = true; 124 125 for (i = 0; i < cpu->cpreg_array_len; i++) { 126 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 127 const ARMCPRegInfo *ri; 128 uint64_t newval; 129 130 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 131 if (!ri) { 132 ok = false; 133 continue; 134 } 135 if (ri->type & ARM_CP_NO_RAW) { 136 continue; 137 } 138 139 newval = read_raw_cp_reg(&cpu->env, ri); 140 if (kvm_sync) { 141 /* 142 * Only sync if the previous list->cpustate sync succeeded. 143 * Rather than tracking the success/failure state for every 144 * item in the list, we just recheck "does the raw write we must 145 * have made in write_list_to_cpustate() read back OK" here. 146 */ 147 uint64_t oldval = cpu->cpreg_values[i]; 148 149 if (oldval == newval) { 150 continue; 151 } 152 153 write_raw_cp_reg(&cpu->env, ri, oldval); 154 if (read_raw_cp_reg(&cpu->env, ri) != oldval) { 155 continue; 156 } 157 158 write_raw_cp_reg(&cpu->env, ri, newval); 159 } 160 cpu->cpreg_values[i] = newval; 161 } 162 return ok; 163 } 164 165 bool write_list_to_cpustate(ARMCPU *cpu) 166 { 167 int i; 168 bool ok = true; 169 170 for (i = 0; i < cpu->cpreg_array_len; i++) { 171 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 172 uint64_t v = cpu->cpreg_values[i]; 173 const ARMCPRegInfo *ri; 174 175 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 176 if (!ri) { 177 ok = false; 178 continue; 179 } 180 if (ri->type & ARM_CP_NO_RAW) { 181 continue; 182 } 183 /* 184 * Write value and confirm it reads back as written 185 * (to catch read-only registers and partially read-only 186 * registers where the incoming migration value doesn't match) 187 */ 188 write_raw_cp_reg(&cpu->env, ri, v); 189 if (read_raw_cp_reg(&cpu->env, ri) != v) { 190 ok = false; 191 } 192 } 193 return ok; 194 } 195 196 static void add_cpreg_to_list(gpointer key, gpointer opaque) 197 { 198 ARMCPU *cpu = opaque; 199 uint32_t regidx = (uintptr_t)key; 200 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 201 202 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) { 203 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 204 /* The value array need not be initialized at this point */ 205 cpu->cpreg_array_len++; 206 } 207 } 208 209 static void count_cpreg(gpointer key, gpointer opaque) 210 { 211 ARMCPU *cpu = opaque; 212 const ARMCPRegInfo *ri; 213 214 ri = g_hash_table_lookup(cpu->cp_regs, key); 215 216 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) { 217 cpu->cpreg_array_len++; 218 } 219 } 220 221 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 222 { 223 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a); 224 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b); 225 226 if (aidx > bidx) { 227 return 1; 228 } 229 if (aidx < bidx) { 230 return -1; 231 } 232 return 0; 233 } 234 235 void init_cpreg_list(ARMCPU *cpu) 236 { 237 /* 238 * Initialise the cpreg_tuples[] array based on the cp_regs hash. 239 * Note that we require cpreg_tuples[] to be sorted by key ID. 240 */ 241 GList *keys; 242 int arraylen; 243 244 keys = g_hash_table_get_keys(cpu->cp_regs); 245 keys = g_list_sort(keys, cpreg_key_compare); 246 247 cpu->cpreg_array_len = 0; 248 249 g_list_foreach(keys, count_cpreg, cpu); 250 251 arraylen = cpu->cpreg_array_len; 252 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 253 cpu->cpreg_values = g_new(uint64_t, arraylen); 254 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 255 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 256 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 257 cpu->cpreg_array_len = 0; 258 259 g_list_foreach(keys, add_cpreg_to_list, cpu); 260 261 assert(cpu->cpreg_array_len == arraylen); 262 263 g_list_free(keys); 264 } 265 266 static bool arm_pan_enabled(CPUARMState *env) 267 { 268 if (is_a64(env)) { 269 if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) { 270 return false; 271 } 272 return env->pstate & PSTATE_PAN; 273 } else { 274 return env->uncached_cpsr & CPSR_PAN; 275 } 276 } 277 278 /* 279 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0. 280 */ 281 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 282 const ARMCPRegInfo *ri, 283 bool isread) 284 { 285 if (!is_a64(env) && arm_current_el(env) == 3 && 286 arm_is_secure_below_el3(env)) { 287 return CP_ACCESS_TRAP_UNCATEGORIZED; 288 } 289 return CP_ACCESS_OK; 290 } 291 292 /* 293 * Some secure-only AArch32 registers trap to EL3 if used from 294 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 295 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 296 * We assume that the .access field is set to PL1_RW. 297 */ 298 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 299 const ARMCPRegInfo *ri, 300 bool isread) 301 { 302 if (arm_current_el(env) == 3) { 303 return CP_ACCESS_OK; 304 } 305 if (arm_is_secure_below_el3(env)) { 306 if (env->cp15.scr_el3 & SCR_EEL2) { 307 return CP_ACCESS_TRAP_EL2; 308 } 309 return CP_ACCESS_TRAP_EL3; 310 } 311 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 312 return CP_ACCESS_TRAP_UNCATEGORIZED; 313 } 314 315 /* 316 * Check for traps to performance monitor registers, which are controlled 317 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 318 */ 319 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 320 bool isread) 321 { 322 int el = arm_current_el(env); 323 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env); 324 325 if (el < 2 && (mdcr_el2 & MDCR_TPM)) { 326 return CP_ACCESS_TRAP_EL2; 327 } 328 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 329 return CP_ACCESS_TRAP_EL3; 330 } 331 return CP_ACCESS_OK; 332 } 333 334 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */ 335 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri, 336 bool isread) 337 { 338 if (arm_current_el(env) == 1) { 339 uint64_t trap = isread ? HCR_TRVM : HCR_TVM; 340 if (arm_hcr_el2_eff(env) & trap) { 341 return CP_ACCESS_TRAP_EL2; 342 } 343 } 344 return CP_ACCESS_OK; 345 } 346 347 /* Check for traps from EL1 due to HCR_EL2.TSW. */ 348 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri, 349 bool isread) 350 { 351 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) { 352 return CP_ACCESS_TRAP_EL2; 353 } 354 return CP_ACCESS_OK; 355 } 356 357 /* Check for traps from EL1 due to HCR_EL2.TACR. */ 358 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri, 359 bool isread) 360 { 361 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) { 362 return CP_ACCESS_TRAP_EL2; 363 } 364 return CP_ACCESS_OK; 365 } 366 367 /* Check for traps from EL1 due to HCR_EL2.TTLB. */ 368 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri, 369 bool isread) 370 { 371 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) { 372 return CP_ACCESS_TRAP_EL2; 373 } 374 return CP_ACCESS_OK; 375 } 376 377 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBIS. */ 378 static CPAccessResult access_ttlbis(CPUARMState *env, const ARMCPRegInfo *ri, 379 bool isread) 380 { 381 if (arm_current_el(env) == 1 && 382 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBIS))) { 383 return CP_ACCESS_TRAP_EL2; 384 } 385 return CP_ACCESS_OK; 386 } 387 388 #ifdef TARGET_AARCH64 389 /* Check for traps from EL1 due to HCR_EL2.TTLB or TTLBOS. */ 390 static CPAccessResult access_ttlbos(CPUARMState *env, const ARMCPRegInfo *ri, 391 bool isread) 392 { 393 if (arm_current_el(env) == 1 && 394 (arm_hcr_el2_eff(env) & (HCR_TTLB | HCR_TTLBOS))) { 395 return CP_ACCESS_TRAP_EL2; 396 } 397 return CP_ACCESS_OK; 398 } 399 #endif 400 401 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 402 { 403 ARMCPU *cpu = env_archcpu(env); 404 405 raw_write(env, ri, value); 406 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 407 } 408 409 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 410 { 411 ARMCPU *cpu = env_archcpu(env); 412 413 if (raw_read(env, ri) != value) { 414 /* 415 * Unlike real hardware the qemu TLB uses virtual addresses, 416 * not modified virtual addresses, so this causes a TLB flush. 417 */ 418 tlb_flush(CPU(cpu)); 419 raw_write(env, ri, value); 420 } 421 } 422 423 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 424 uint64_t value) 425 { 426 ARMCPU *cpu = env_archcpu(env); 427 428 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 429 && !extended_addresses_enabled(env)) { 430 /* 431 * For VMSA (when not using the LPAE long descriptor page table 432 * format) this register includes the ASID, so do a TLB flush. 433 * For PMSA it is purely a process ID and no action is needed. 434 */ 435 tlb_flush(CPU(cpu)); 436 } 437 raw_write(env, ri, value); 438 } 439 440 static int alle1_tlbmask(CPUARMState *env) 441 { 442 /* 443 * Note that the 'ALL' scope must invalidate both stage 1 and 444 * stage 2 translations, whereas most other scopes only invalidate 445 * stage 1 translations. 446 */ 447 return (ARMMMUIdxBit_E10_1 | 448 ARMMMUIdxBit_E10_1_PAN | 449 ARMMMUIdxBit_E10_0 | 450 ARMMMUIdxBit_Stage2 | 451 ARMMMUIdxBit_Stage2_S); 452 } 453 454 455 /* IS variants of TLB operations must affect all cores */ 456 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 457 uint64_t value) 458 { 459 CPUState *cs = env_cpu(env); 460 461 tlb_flush_all_cpus_synced(cs); 462 } 463 464 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 465 uint64_t value) 466 { 467 CPUState *cs = env_cpu(env); 468 469 tlb_flush_all_cpus_synced(cs); 470 } 471 472 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 473 uint64_t value) 474 { 475 CPUState *cs = env_cpu(env); 476 477 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 478 } 479 480 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 481 uint64_t value) 482 { 483 CPUState *cs = env_cpu(env); 484 485 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 486 } 487 488 /* 489 * Non-IS variants of TLB operations are upgraded to 490 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to 491 * force broadcast of these operations. 492 */ 493 static bool tlb_force_broadcast(CPUARMState *env) 494 { 495 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB); 496 } 497 498 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, 499 uint64_t value) 500 { 501 /* Invalidate all (TLBIALL) */ 502 CPUState *cs = env_cpu(env); 503 504 if (tlb_force_broadcast(env)) { 505 tlb_flush_all_cpus_synced(cs); 506 } else { 507 tlb_flush(cs); 508 } 509 } 510 511 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, 512 uint64_t value) 513 { 514 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ 515 CPUState *cs = env_cpu(env); 516 517 value &= TARGET_PAGE_MASK; 518 if (tlb_force_broadcast(env)) { 519 tlb_flush_page_all_cpus_synced(cs, value); 520 } else { 521 tlb_flush_page(cs, value); 522 } 523 } 524 525 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, 526 uint64_t value) 527 { 528 /* Invalidate by ASID (TLBIASID) */ 529 CPUState *cs = env_cpu(env); 530 531 if (tlb_force_broadcast(env)) { 532 tlb_flush_all_cpus_synced(cs); 533 } else { 534 tlb_flush(cs); 535 } 536 } 537 538 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, 539 uint64_t value) 540 { 541 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ 542 CPUState *cs = env_cpu(env); 543 544 value &= TARGET_PAGE_MASK; 545 if (tlb_force_broadcast(env)) { 546 tlb_flush_page_all_cpus_synced(cs, value); 547 } else { 548 tlb_flush_page(cs, value); 549 } 550 } 551 552 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, 553 uint64_t value) 554 { 555 CPUState *cs = env_cpu(env); 556 557 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env)); 558 } 559 560 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 561 uint64_t value) 562 { 563 CPUState *cs = env_cpu(env); 564 565 tlb_flush_by_mmuidx_all_cpus_synced(cs, alle1_tlbmask(env)); 566 } 567 568 569 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 570 uint64_t value) 571 { 572 CPUState *cs = env_cpu(env); 573 574 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2); 575 } 576 577 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 578 uint64_t value) 579 { 580 CPUState *cs = env_cpu(env); 581 582 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2); 583 } 584 585 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 586 uint64_t value) 587 { 588 CPUState *cs = env_cpu(env); 589 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 590 591 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2); 592 } 593 594 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 595 uint64_t value) 596 { 597 CPUState *cs = env_cpu(env); 598 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 599 600 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 601 ARMMMUIdxBit_E2); 602 } 603 604 static void tlbiipas2_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 605 uint64_t value) 606 { 607 CPUState *cs = env_cpu(env); 608 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12; 609 610 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_Stage2); 611 } 612 613 static void tlbiipas2is_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 614 uint64_t value) 615 { 616 CPUState *cs = env_cpu(env); 617 uint64_t pageaddr = (value & MAKE_64BIT_MASK(0, 28)) << 12; 618 619 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, ARMMMUIdxBit_Stage2); 620 } 621 622 static const ARMCPRegInfo cp_reginfo[] = { 623 /* 624 * Define the secure and non-secure FCSE identifier CP registers 625 * separately because there is no secure bank in V8 (no _EL3). This allows 626 * the secure register to be properly reset and migrated. There is also no 627 * v8 EL1 version of the register so the non-secure instance stands alone. 628 */ 629 { .name = "FCSEIDR", 630 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 631 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 632 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 633 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 634 { .name = "FCSEIDR_S", 635 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 636 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 637 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 638 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 639 /* 640 * Define the secure and non-secure context identifier CP registers 641 * separately because there is no secure bank in V8 (no _EL3). This allows 642 * the secure register to be properly reset and migrated. In the 643 * non-secure case, the 32-bit register will have reset and migration 644 * disabled during registration as it is handled by the 64-bit instance. 645 */ 646 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 647 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 648 .access = PL1_RW, .accessfn = access_tvm_trvm, 649 .fgt = FGT_CONTEXTIDR_EL1, 650 .secure = ARM_CP_SECSTATE_NS, 651 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 652 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 653 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 654 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 655 .access = PL1_RW, .accessfn = access_tvm_trvm, 656 .secure = ARM_CP_SECSTATE_S, 657 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 658 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 659 }; 660 661 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 662 /* 663 * NB: Some of these registers exist in v8 but with more precise 664 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 665 */ 666 /* MMU Domain access control / MPU write buffer control */ 667 { .name = "DACR", 668 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 669 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 670 .writefn = dacr_write, .raw_writefn = raw_write, 671 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 672 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 673 /* 674 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 675 * For v6 and v5, these mappings are overly broad. 676 */ 677 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 678 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 679 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 680 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 681 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 682 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 683 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 684 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 685 /* Cache maintenance ops; some of this space may be overridden later. */ 686 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 687 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 688 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 689 }; 690 691 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 692 /* 693 * Not all pre-v6 cores implemented this WFI, so this is slightly 694 * over-broad. 695 */ 696 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 697 .access = PL1_W, .type = ARM_CP_WFI }, 698 }; 699 700 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 701 /* 702 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 703 * is UNPREDICTABLE; we choose to NOP as most implementations do). 704 */ 705 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 706 .access = PL1_W, .type = ARM_CP_WFI }, 707 /* 708 * L1 cache lockdown. Not architectural in v6 and earlier but in practice 709 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 710 * OMAPCP will override this space. 711 */ 712 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 713 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 714 .resetvalue = 0 }, 715 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 716 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 717 .resetvalue = 0 }, 718 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 719 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 720 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 721 .resetvalue = 0 }, 722 /* 723 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 724 * implementing it as RAZ means the "debug architecture version" bits 725 * will read as a reserved value, which should cause Linux to not try 726 * to use the debug hardware. 727 */ 728 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 729 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 730 /* 731 * MMU TLB control. Note that the wildcarding means we cover not just 732 * the unified TLB ops but also the dside/iside/inner-shareable variants. 733 */ 734 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, 735 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, 736 .type = ARM_CP_NO_RAW }, 737 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, 738 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, 739 .type = ARM_CP_NO_RAW }, 740 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, 741 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, 742 .type = ARM_CP_NO_RAW }, 743 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, 744 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, 745 .type = ARM_CP_NO_RAW }, 746 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 747 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 748 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 749 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 750 }; 751 752 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 753 uint64_t value) 754 { 755 uint32_t mask = 0; 756 757 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 758 if (!arm_feature(env, ARM_FEATURE_V8)) { 759 /* 760 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 761 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 762 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 763 */ 764 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) { 765 /* VFP coprocessor: cp10 & cp11 [23:20] */ 766 mask |= R_CPACR_ASEDIS_MASK | 767 R_CPACR_D32DIS_MASK | 768 R_CPACR_CP11_MASK | 769 R_CPACR_CP10_MASK; 770 771 if (!arm_feature(env, ARM_FEATURE_NEON)) { 772 /* ASEDIS [31] bit is RAO/WI */ 773 value |= R_CPACR_ASEDIS_MASK; 774 } 775 776 /* 777 * VFPv3 and upwards with NEON implement 32 double precision 778 * registers (D0-D31). 779 */ 780 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) { 781 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 782 value |= R_CPACR_D32DIS_MASK; 783 } 784 } 785 value &= mask; 786 } 787 788 /* 789 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 790 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 791 */ 792 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 793 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 794 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK; 795 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask); 796 } 797 798 env->cp15.cpacr_el1 = value; 799 } 800 801 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri) 802 { 803 /* 804 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 805 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 806 */ 807 uint64_t value = env->cp15.cpacr_el1; 808 809 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 810 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 811 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK); 812 } 813 return value; 814 } 815 816 817 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 818 { 819 /* 820 * Call cpacr_write() so that we reset with the correct RAO bits set 821 * for our CPU features. 822 */ 823 cpacr_write(env, ri, 0); 824 } 825 826 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 827 bool isread) 828 { 829 if (arm_feature(env, ARM_FEATURE_V8)) { 830 /* Check if CPACR accesses are to be trapped to EL2 */ 831 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) && 832 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) { 833 return CP_ACCESS_TRAP_EL2; 834 /* Check if CPACR accesses are to be trapped to EL3 */ 835 } else if (arm_current_el(env) < 3 && 836 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) { 837 return CP_ACCESS_TRAP_EL3; 838 } 839 } 840 841 return CP_ACCESS_OK; 842 } 843 844 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 845 bool isread) 846 { 847 /* Check if CPTR accesses are set to trap to EL3 */ 848 if (arm_current_el(env) == 2 && 849 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) { 850 return CP_ACCESS_TRAP_EL3; 851 } 852 853 return CP_ACCESS_OK; 854 } 855 856 static const ARMCPRegInfo v6_cp_reginfo[] = { 857 /* prefetch by MVA in v6, NOP in v7 */ 858 { .name = "MVA_prefetch", 859 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 860 .access = PL1_W, .type = ARM_CP_NOP }, 861 /* 862 * We need to break the TB after ISB to execute self-modifying code 863 * correctly and also to take any pending interrupts immediately. 864 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 865 */ 866 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 867 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 868 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 869 .access = PL0_W, .type = ARM_CP_NOP }, 870 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 871 .access = PL0_W, .type = ARM_CP_NOP }, 872 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 873 .access = PL1_RW, .accessfn = access_tvm_trvm, 874 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 875 offsetof(CPUARMState, cp15.ifar_ns) }, 876 .resetvalue = 0, }, 877 /* 878 * Watchpoint Fault Address Register : should actually only be present 879 * for 1136, 1176, 11MPCore. 880 */ 881 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 882 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 883 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 884 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 885 .fgt = FGT_CPACR_EL1, 886 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 887 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read }, 888 }; 889 890 typedef struct pm_event { 891 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */ 892 /* If the event is supported on this CPU (used to generate PMCEID[01]) */ 893 bool (*supported)(CPUARMState *); 894 /* 895 * Retrieve the current count of the underlying event. The programmed 896 * counters hold a difference from the return value from this function 897 */ 898 uint64_t (*get_count)(CPUARMState *); 899 /* 900 * Return how many nanoseconds it will take (at a minimum) for count events 901 * to occur. A negative value indicates the counter will never overflow, or 902 * that the counter has otherwise arranged for the overflow bit to be set 903 * and the PMU interrupt to be raised on overflow. 904 */ 905 int64_t (*ns_per_count)(uint64_t); 906 } pm_event; 907 908 static bool event_always_supported(CPUARMState *env) 909 { 910 return true; 911 } 912 913 static uint64_t swinc_get_count(CPUARMState *env) 914 { 915 /* 916 * SW_INCR events are written directly to the pmevcntr's by writes to 917 * PMSWINC, so there is no underlying count maintained by the PMU itself 918 */ 919 return 0; 920 } 921 922 static int64_t swinc_ns_per(uint64_t ignored) 923 { 924 return -1; 925 } 926 927 /* 928 * Return the underlying cycle count for the PMU cycle counters. If we're in 929 * usermode, simply return 0. 930 */ 931 static uint64_t cycles_get_count(CPUARMState *env) 932 { 933 #ifndef CONFIG_USER_ONLY 934 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 935 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 936 #else 937 return cpu_get_host_ticks(); 938 #endif 939 } 940 941 #ifndef CONFIG_USER_ONLY 942 static int64_t cycles_ns_per(uint64_t cycles) 943 { 944 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles; 945 } 946 947 static bool instructions_supported(CPUARMState *env) 948 { 949 return icount_enabled() == 1; /* Precise instruction counting */ 950 } 951 952 static uint64_t instructions_get_count(CPUARMState *env) 953 { 954 return (uint64_t)icount_get_raw(); 955 } 956 957 static int64_t instructions_ns_per(uint64_t icount) 958 { 959 return icount_to_ns((int64_t)icount); 960 } 961 #endif 962 963 static bool pmuv3p1_events_supported(CPUARMState *env) 964 { 965 /* For events which are supported in any v8.1 PMU */ 966 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env)); 967 } 968 969 static bool pmuv3p4_events_supported(CPUARMState *env) 970 { 971 /* For events which are supported in any v8.1 PMU */ 972 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env)); 973 } 974 975 static uint64_t zero_event_get_count(CPUARMState *env) 976 { 977 /* For events which on QEMU never fire, so their count is always zero */ 978 return 0; 979 } 980 981 static int64_t zero_event_ns_per(uint64_t cycles) 982 { 983 /* An event which never fires can never overflow */ 984 return -1; 985 } 986 987 static const pm_event pm_events[] = { 988 { .number = 0x000, /* SW_INCR */ 989 .supported = event_always_supported, 990 .get_count = swinc_get_count, 991 .ns_per_count = swinc_ns_per, 992 }, 993 #ifndef CONFIG_USER_ONLY 994 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */ 995 .supported = instructions_supported, 996 .get_count = instructions_get_count, 997 .ns_per_count = instructions_ns_per, 998 }, 999 { .number = 0x011, /* CPU_CYCLES, Cycle */ 1000 .supported = event_always_supported, 1001 .get_count = cycles_get_count, 1002 .ns_per_count = cycles_ns_per, 1003 }, 1004 #endif 1005 { .number = 0x023, /* STALL_FRONTEND */ 1006 .supported = pmuv3p1_events_supported, 1007 .get_count = zero_event_get_count, 1008 .ns_per_count = zero_event_ns_per, 1009 }, 1010 { .number = 0x024, /* STALL_BACKEND */ 1011 .supported = pmuv3p1_events_supported, 1012 .get_count = zero_event_get_count, 1013 .ns_per_count = zero_event_ns_per, 1014 }, 1015 { .number = 0x03c, /* STALL */ 1016 .supported = pmuv3p4_events_supported, 1017 .get_count = zero_event_get_count, 1018 .ns_per_count = zero_event_ns_per, 1019 }, 1020 }; 1021 1022 /* 1023 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of 1024 * events (i.e. the statistical profiling extension), this implementation 1025 * should first be updated to something sparse instead of the current 1026 * supported_event_map[] array. 1027 */ 1028 #define MAX_EVENT_ID 0x3c 1029 #define UNSUPPORTED_EVENT UINT16_MAX 1030 static uint16_t supported_event_map[MAX_EVENT_ID + 1]; 1031 1032 /* 1033 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map 1034 * of ARM event numbers to indices in our pm_events array. 1035 * 1036 * Note: Events in the 0x40XX range are not currently supported. 1037 */ 1038 void pmu_init(ARMCPU *cpu) 1039 { 1040 unsigned int i; 1041 1042 /* 1043 * Empty supported_event_map and cpu->pmceid[01] before adding supported 1044 * events to them 1045 */ 1046 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) { 1047 supported_event_map[i] = UNSUPPORTED_EVENT; 1048 } 1049 cpu->pmceid0 = 0; 1050 cpu->pmceid1 = 0; 1051 1052 for (i = 0; i < ARRAY_SIZE(pm_events); i++) { 1053 const pm_event *cnt = &pm_events[i]; 1054 assert(cnt->number <= MAX_EVENT_ID); 1055 /* We do not currently support events in the 0x40xx range */ 1056 assert(cnt->number <= 0x3f); 1057 1058 if (cnt->supported(&cpu->env)) { 1059 supported_event_map[cnt->number] = i; 1060 uint64_t event_mask = 1ULL << (cnt->number & 0x1f); 1061 if (cnt->number & 0x20) { 1062 cpu->pmceid1 |= event_mask; 1063 } else { 1064 cpu->pmceid0 |= event_mask; 1065 } 1066 } 1067 } 1068 } 1069 1070 /* 1071 * Check at runtime whether a PMU event is supported for the current machine 1072 */ 1073 static bool event_supported(uint16_t number) 1074 { 1075 if (number > MAX_EVENT_ID) { 1076 return false; 1077 } 1078 return supported_event_map[number] != UNSUPPORTED_EVENT; 1079 } 1080 1081 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 1082 bool isread) 1083 { 1084 /* 1085 * Performance monitor registers user accessibility is controlled 1086 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 1087 * trapping to EL2 or EL3 for other accesses. 1088 */ 1089 int el = arm_current_el(env); 1090 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env); 1091 1092 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 1093 return CP_ACCESS_TRAP; 1094 } 1095 if (el < 2 && (mdcr_el2 & MDCR_TPM)) { 1096 return CP_ACCESS_TRAP_EL2; 1097 } 1098 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 1099 return CP_ACCESS_TRAP_EL3; 1100 } 1101 1102 return CP_ACCESS_OK; 1103 } 1104 1105 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 1106 const ARMCPRegInfo *ri, 1107 bool isread) 1108 { 1109 /* ER: event counter read trap control */ 1110 if (arm_feature(env, ARM_FEATURE_V8) 1111 && arm_current_el(env) == 0 1112 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 1113 && isread) { 1114 return CP_ACCESS_OK; 1115 } 1116 1117 return pmreg_access(env, ri, isread); 1118 } 1119 1120 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 1121 const ARMCPRegInfo *ri, 1122 bool isread) 1123 { 1124 /* SW: software increment write trap control */ 1125 if (arm_feature(env, ARM_FEATURE_V8) 1126 && arm_current_el(env) == 0 1127 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 1128 && !isread) { 1129 return CP_ACCESS_OK; 1130 } 1131 1132 return pmreg_access(env, ri, isread); 1133 } 1134 1135 static CPAccessResult pmreg_access_selr(CPUARMState *env, 1136 const ARMCPRegInfo *ri, 1137 bool isread) 1138 { 1139 /* ER: event counter read trap control */ 1140 if (arm_feature(env, ARM_FEATURE_V8) 1141 && arm_current_el(env) == 0 1142 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 1143 return CP_ACCESS_OK; 1144 } 1145 1146 return pmreg_access(env, ri, isread); 1147 } 1148 1149 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 1150 const ARMCPRegInfo *ri, 1151 bool isread) 1152 { 1153 /* CR: cycle counter read trap control */ 1154 if (arm_feature(env, ARM_FEATURE_V8) 1155 && arm_current_el(env) == 0 1156 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 1157 && isread) { 1158 return CP_ACCESS_OK; 1159 } 1160 1161 return pmreg_access(env, ri, isread); 1162 } 1163 1164 /* 1165 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at. 1166 * We use these to decide whether we need to wrap a write to MDCR_EL2 1167 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls. 1168 */ 1169 #define MDCR_EL2_PMU_ENABLE_BITS \ 1170 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP) 1171 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD) 1172 1173 /* 1174 * Returns true if the counter (pass 31 for PMCCNTR) should count events using 1175 * the current EL, security state, and register configuration. 1176 */ 1177 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter) 1178 { 1179 uint64_t filter; 1180 bool e, p, u, nsk, nsu, nsh, m; 1181 bool enabled, prohibited = false, filtered; 1182 bool secure = arm_is_secure(env); 1183 int el = arm_current_el(env); 1184 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env); 1185 uint8_t hpmn = mdcr_el2 & MDCR_HPMN; 1186 1187 if (!arm_feature(env, ARM_FEATURE_PMU)) { 1188 return false; 1189 } 1190 1191 if (!arm_feature(env, ARM_FEATURE_EL2) || 1192 (counter < hpmn || counter == 31)) { 1193 e = env->cp15.c9_pmcr & PMCRE; 1194 } else { 1195 e = mdcr_el2 & MDCR_HPME; 1196 } 1197 enabled = e && (env->cp15.c9_pmcnten & (1 << counter)); 1198 1199 /* Is event counting prohibited? */ 1200 if (el == 2 && (counter < hpmn || counter == 31)) { 1201 prohibited = mdcr_el2 & MDCR_HPMD; 1202 } 1203 if (secure) { 1204 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME); 1205 } 1206 1207 if (counter == 31) { 1208 /* 1209 * The cycle counter defaults to running. PMCR.DP says "disable 1210 * the cycle counter when event counting is prohibited". 1211 * Some MDCR bits disable the cycle counter specifically. 1212 */ 1213 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP; 1214 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1215 if (secure) { 1216 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD); 1217 } 1218 if (el == 2) { 1219 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD); 1220 } 1221 } 1222 } 1223 1224 if (counter == 31) { 1225 filter = env->cp15.pmccfiltr_el0; 1226 } else { 1227 filter = env->cp15.c14_pmevtyper[counter]; 1228 } 1229 1230 p = filter & PMXEVTYPER_P; 1231 u = filter & PMXEVTYPER_U; 1232 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK); 1233 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU); 1234 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH); 1235 m = arm_el_is_aa64(env, 1) && 1236 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M); 1237 1238 if (el == 0) { 1239 filtered = secure ? u : u != nsu; 1240 } else if (el == 1) { 1241 filtered = secure ? p : p != nsk; 1242 } else if (el == 2) { 1243 filtered = !nsh; 1244 } else { /* EL3 */ 1245 filtered = m != p; 1246 } 1247 1248 if (counter != 31) { 1249 /* 1250 * If not checking PMCCNTR, ensure the counter is setup to an event we 1251 * support 1252 */ 1253 uint16_t event = filter & PMXEVTYPER_EVTCOUNT; 1254 if (!event_supported(event)) { 1255 return false; 1256 } 1257 } 1258 1259 return enabled && !prohibited && !filtered; 1260 } 1261 1262 static void pmu_update_irq(CPUARMState *env) 1263 { 1264 ARMCPU *cpu = env_archcpu(env); 1265 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) && 1266 (env->cp15.c9_pminten & env->cp15.c9_pmovsr)); 1267 } 1268 1269 static bool pmccntr_clockdiv_enabled(CPUARMState *env) 1270 { 1271 /* 1272 * Return true if the clock divider is enabled and the cycle counter 1273 * is supposed to tick only once every 64 clock cycles. This is 1274 * controlled by PMCR.D, but if PMCR.LC is set to enable the long 1275 * (64-bit) cycle counter PMCR.D has no effect. 1276 */ 1277 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD; 1278 } 1279 1280 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter) 1281 { 1282 /* Return true if the specified event counter is configured to be 64 bit */ 1283 1284 /* This isn't intended to be used with the cycle counter */ 1285 assert(counter < 31); 1286 1287 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1288 return false; 1289 } 1290 1291 if (arm_feature(env, ARM_FEATURE_EL2)) { 1292 /* 1293 * MDCR_EL2.HLP still applies even when EL2 is disabled in the 1294 * current security state, so we don't use arm_mdcr_el2_eff() here. 1295 */ 1296 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP; 1297 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN; 1298 1299 if (counter >= hpmn) { 1300 return hlp; 1301 } 1302 } 1303 return env->cp15.c9_pmcr & PMCRLP; 1304 } 1305 1306 /* 1307 * Ensure c15_ccnt is the guest-visible count so that operations such as 1308 * enabling/disabling the counter or filtering, modifying the count itself, 1309 * etc. can be done logically. This is essentially a no-op if the counter is 1310 * not enabled at the time of the call. 1311 */ 1312 static void pmccntr_op_start(CPUARMState *env) 1313 { 1314 uint64_t cycles = cycles_get_count(env); 1315 1316 if (pmu_counter_enabled(env, 31)) { 1317 uint64_t eff_cycles = cycles; 1318 if (pmccntr_clockdiv_enabled(env)) { 1319 eff_cycles /= 64; 1320 } 1321 1322 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta; 1323 1324 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \ 1325 1ull << 63 : 1ull << 31; 1326 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) { 1327 env->cp15.c9_pmovsr |= (1ULL << 31); 1328 pmu_update_irq(env); 1329 } 1330 1331 env->cp15.c15_ccnt = new_pmccntr; 1332 } 1333 env->cp15.c15_ccnt_delta = cycles; 1334 } 1335 1336 /* 1337 * If PMCCNTR is enabled, recalculate the delta between the clock and the 1338 * guest-visible count. A call to pmccntr_op_finish should follow every call to 1339 * pmccntr_op_start. 1340 */ 1341 static void pmccntr_op_finish(CPUARMState *env) 1342 { 1343 if (pmu_counter_enabled(env, 31)) { 1344 #ifndef CONFIG_USER_ONLY 1345 /* Calculate when the counter will next overflow */ 1346 uint64_t remaining_cycles = -env->cp15.c15_ccnt; 1347 if (!(env->cp15.c9_pmcr & PMCRLC)) { 1348 remaining_cycles = (uint32_t)remaining_cycles; 1349 } 1350 int64_t overflow_in = cycles_ns_per(remaining_cycles); 1351 1352 if (overflow_in > 0) { 1353 int64_t overflow_at; 1354 1355 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1356 overflow_in, &overflow_at)) { 1357 ARMCPU *cpu = env_archcpu(env); 1358 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1359 } 1360 } 1361 #endif 1362 1363 uint64_t prev_cycles = env->cp15.c15_ccnt_delta; 1364 if (pmccntr_clockdiv_enabled(env)) { 1365 prev_cycles /= 64; 1366 } 1367 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt; 1368 } 1369 } 1370 1371 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter) 1372 { 1373 1374 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1375 uint64_t count = 0; 1376 if (event_supported(event)) { 1377 uint16_t event_idx = supported_event_map[event]; 1378 count = pm_events[event_idx].get_count(env); 1379 } 1380 1381 if (pmu_counter_enabled(env, counter)) { 1382 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter]; 1383 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ? 1384 1ULL << 63 : 1ULL << 31; 1385 1386 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) { 1387 env->cp15.c9_pmovsr |= (1 << counter); 1388 pmu_update_irq(env); 1389 } 1390 env->cp15.c14_pmevcntr[counter] = new_pmevcntr; 1391 } 1392 env->cp15.c14_pmevcntr_delta[counter] = count; 1393 } 1394 1395 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter) 1396 { 1397 if (pmu_counter_enabled(env, counter)) { 1398 #ifndef CONFIG_USER_ONLY 1399 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1400 uint16_t event_idx = supported_event_map[event]; 1401 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1); 1402 int64_t overflow_in; 1403 1404 if (!pmevcntr_is_64_bit(env, counter)) { 1405 delta = (uint32_t)delta; 1406 } 1407 overflow_in = pm_events[event_idx].ns_per_count(delta); 1408 1409 if (overflow_in > 0) { 1410 int64_t overflow_at; 1411 1412 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1413 overflow_in, &overflow_at)) { 1414 ARMCPU *cpu = env_archcpu(env); 1415 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1416 } 1417 } 1418 #endif 1419 1420 env->cp15.c14_pmevcntr_delta[counter] -= 1421 env->cp15.c14_pmevcntr[counter]; 1422 } 1423 } 1424 1425 void pmu_op_start(CPUARMState *env) 1426 { 1427 unsigned int i; 1428 pmccntr_op_start(env); 1429 for (i = 0; i < pmu_num_counters(env); i++) { 1430 pmevcntr_op_start(env, i); 1431 } 1432 } 1433 1434 void pmu_op_finish(CPUARMState *env) 1435 { 1436 unsigned int i; 1437 pmccntr_op_finish(env); 1438 for (i = 0; i < pmu_num_counters(env); i++) { 1439 pmevcntr_op_finish(env, i); 1440 } 1441 } 1442 1443 void pmu_pre_el_change(ARMCPU *cpu, void *ignored) 1444 { 1445 pmu_op_start(&cpu->env); 1446 } 1447 1448 void pmu_post_el_change(ARMCPU *cpu, void *ignored) 1449 { 1450 pmu_op_finish(&cpu->env); 1451 } 1452 1453 void arm_pmu_timer_cb(void *opaque) 1454 { 1455 ARMCPU *cpu = opaque; 1456 1457 /* 1458 * Update all the counter values based on the current underlying counts, 1459 * triggering interrupts to be raised, if necessary. pmu_op_finish() also 1460 * has the effect of setting the cpu->pmu_timer to the next earliest time a 1461 * counter may expire. 1462 */ 1463 pmu_op_start(&cpu->env); 1464 pmu_op_finish(&cpu->env); 1465 } 1466 1467 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1468 uint64_t value) 1469 { 1470 pmu_op_start(env); 1471 1472 if (value & PMCRC) { 1473 /* The counter has been reset */ 1474 env->cp15.c15_ccnt = 0; 1475 } 1476 1477 if (value & PMCRP) { 1478 unsigned int i; 1479 for (i = 0; i < pmu_num_counters(env); i++) { 1480 env->cp15.c14_pmevcntr[i] = 0; 1481 } 1482 } 1483 1484 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK; 1485 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK); 1486 1487 pmu_op_finish(env); 1488 } 1489 1490 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1491 { 1492 uint64_t pmcr = env->cp15.c9_pmcr; 1493 1494 /* 1495 * If EL2 is implemented and enabled for the current security state, reads 1496 * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN. 1497 */ 1498 if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) { 1499 pmcr &= ~PMCRN_MASK; 1500 pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT; 1501 } 1502 1503 return pmcr; 1504 } 1505 1506 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri, 1507 uint64_t value) 1508 { 1509 unsigned int i; 1510 uint64_t overflow_mask, new_pmswinc; 1511 1512 for (i = 0; i < pmu_num_counters(env); i++) { 1513 /* Increment a counter's count iff: */ 1514 if ((value & (1 << i)) && /* counter's bit is set */ 1515 /* counter is enabled and not filtered */ 1516 pmu_counter_enabled(env, i) && 1517 /* counter is SW_INCR */ 1518 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) { 1519 pmevcntr_op_start(env, i); 1520 1521 /* 1522 * Detect if this write causes an overflow since we can't predict 1523 * PMSWINC overflows like we can for other events 1524 */ 1525 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1; 1526 1527 overflow_mask = pmevcntr_is_64_bit(env, i) ? 1528 1ULL << 63 : 1ULL << 31; 1529 1530 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) { 1531 env->cp15.c9_pmovsr |= (1 << i); 1532 pmu_update_irq(env); 1533 } 1534 1535 env->cp15.c14_pmevcntr[i] = new_pmswinc; 1536 1537 pmevcntr_op_finish(env, i); 1538 } 1539 } 1540 } 1541 1542 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1543 { 1544 uint64_t ret; 1545 pmccntr_op_start(env); 1546 ret = env->cp15.c15_ccnt; 1547 pmccntr_op_finish(env); 1548 return ret; 1549 } 1550 1551 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1552 uint64_t value) 1553 { 1554 /* 1555 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1556 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1557 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1558 * accessed. 1559 */ 1560 env->cp15.c9_pmselr = value & 0x1f; 1561 } 1562 1563 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1564 uint64_t value) 1565 { 1566 pmccntr_op_start(env); 1567 env->cp15.c15_ccnt = value; 1568 pmccntr_op_finish(env); 1569 } 1570 1571 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1572 uint64_t value) 1573 { 1574 uint64_t cur_val = pmccntr_read(env, NULL); 1575 1576 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1577 } 1578 1579 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1580 uint64_t value) 1581 { 1582 pmccntr_op_start(env); 1583 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0; 1584 pmccntr_op_finish(env); 1585 } 1586 1587 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri, 1588 uint64_t value) 1589 { 1590 pmccntr_op_start(env); 1591 /* M is not accessible from AArch32 */ 1592 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) | 1593 (value & PMCCFILTR); 1594 pmccntr_op_finish(env); 1595 } 1596 1597 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri) 1598 { 1599 /* M is not visible in AArch32 */ 1600 return env->cp15.pmccfiltr_el0 & PMCCFILTR; 1601 } 1602 1603 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1604 uint64_t value) 1605 { 1606 pmu_op_start(env); 1607 value &= pmu_counter_mask(env); 1608 env->cp15.c9_pmcnten |= value; 1609 pmu_op_finish(env); 1610 } 1611 1612 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1613 uint64_t value) 1614 { 1615 pmu_op_start(env); 1616 value &= pmu_counter_mask(env); 1617 env->cp15.c9_pmcnten &= ~value; 1618 pmu_op_finish(env); 1619 } 1620 1621 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1622 uint64_t value) 1623 { 1624 value &= pmu_counter_mask(env); 1625 env->cp15.c9_pmovsr &= ~value; 1626 pmu_update_irq(env); 1627 } 1628 1629 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1630 uint64_t value) 1631 { 1632 value &= pmu_counter_mask(env); 1633 env->cp15.c9_pmovsr |= value; 1634 pmu_update_irq(env); 1635 } 1636 1637 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1638 uint64_t value, const uint8_t counter) 1639 { 1640 if (counter == 31) { 1641 pmccfiltr_write(env, ri, value); 1642 } else if (counter < pmu_num_counters(env)) { 1643 pmevcntr_op_start(env, counter); 1644 1645 /* 1646 * If this counter's event type is changing, store the current 1647 * underlying count for the new type in c14_pmevcntr_delta[counter] so 1648 * pmevcntr_op_finish has the correct baseline when it converts back to 1649 * a delta. 1650 */ 1651 uint16_t old_event = env->cp15.c14_pmevtyper[counter] & 1652 PMXEVTYPER_EVTCOUNT; 1653 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT; 1654 if (old_event != new_event) { 1655 uint64_t count = 0; 1656 if (event_supported(new_event)) { 1657 uint16_t event_idx = supported_event_map[new_event]; 1658 count = pm_events[event_idx].get_count(env); 1659 } 1660 env->cp15.c14_pmevcntr_delta[counter] = count; 1661 } 1662 1663 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK; 1664 pmevcntr_op_finish(env, counter); 1665 } 1666 /* 1667 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1668 * PMSELR value is equal to or greater than the number of implemented 1669 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1670 */ 1671 } 1672 1673 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri, 1674 const uint8_t counter) 1675 { 1676 if (counter == 31) { 1677 return env->cp15.pmccfiltr_el0; 1678 } else if (counter < pmu_num_counters(env)) { 1679 return env->cp15.c14_pmevtyper[counter]; 1680 } else { 1681 /* 1682 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1683 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write(). 1684 */ 1685 return 0; 1686 } 1687 } 1688 1689 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1690 uint64_t value) 1691 { 1692 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1693 pmevtyper_write(env, ri, value, counter); 1694 } 1695 1696 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1697 uint64_t value) 1698 { 1699 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1700 env->cp15.c14_pmevtyper[counter] = value; 1701 1702 /* 1703 * pmevtyper_rawwrite is called between a pair of pmu_op_start and 1704 * pmu_op_finish calls when loading saved state for a migration. Because 1705 * we're potentially updating the type of event here, the value written to 1706 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a 1707 * different counter type. Therefore, we need to set this value to the 1708 * current count for the counter type we're writing so that pmu_op_finish 1709 * has the correct count for its calculation. 1710 */ 1711 uint16_t event = value & PMXEVTYPER_EVTCOUNT; 1712 if (event_supported(event)) { 1713 uint16_t event_idx = supported_event_map[event]; 1714 env->cp15.c14_pmevcntr_delta[counter] = 1715 pm_events[event_idx].get_count(env); 1716 } 1717 } 1718 1719 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1720 { 1721 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1722 return pmevtyper_read(env, ri, counter); 1723 } 1724 1725 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1726 uint64_t value) 1727 { 1728 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31); 1729 } 1730 1731 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1732 { 1733 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31); 1734 } 1735 1736 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1737 uint64_t value, uint8_t counter) 1738 { 1739 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1740 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */ 1741 value &= MAKE_64BIT_MASK(0, 32); 1742 } 1743 if (counter < pmu_num_counters(env)) { 1744 pmevcntr_op_start(env, counter); 1745 env->cp15.c14_pmevcntr[counter] = value; 1746 pmevcntr_op_finish(env, counter); 1747 } 1748 /* 1749 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1750 * are CONSTRAINED UNPREDICTABLE. 1751 */ 1752 } 1753 1754 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri, 1755 uint8_t counter) 1756 { 1757 if (counter < pmu_num_counters(env)) { 1758 uint64_t ret; 1759 pmevcntr_op_start(env, counter); 1760 ret = env->cp15.c14_pmevcntr[counter]; 1761 pmevcntr_op_finish(env, counter); 1762 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1763 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */ 1764 ret &= MAKE_64BIT_MASK(0, 32); 1765 } 1766 return ret; 1767 } else { 1768 /* 1769 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1770 * are CONSTRAINED UNPREDICTABLE. 1771 */ 1772 return 0; 1773 } 1774 } 1775 1776 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1777 uint64_t value) 1778 { 1779 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1780 pmevcntr_write(env, ri, value, counter); 1781 } 1782 1783 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1784 { 1785 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1786 return pmevcntr_read(env, ri, counter); 1787 } 1788 1789 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1790 uint64_t value) 1791 { 1792 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1793 assert(counter < pmu_num_counters(env)); 1794 env->cp15.c14_pmevcntr[counter] = value; 1795 pmevcntr_write(env, ri, value, counter); 1796 } 1797 1798 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri) 1799 { 1800 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1801 assert(counter < pmu_num_counters(env)); 1802 return env->cp15.c14_pmevcntr[counter]; 1803 } 1804 1805 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1806 uint64_t value) 1807 { 1808 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31); 1809 } 1810 1811 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1812 { 1813 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31); 1814 } 1815 1816 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1817 uint64_t value) 1818 { 1819 if (arm_feature(env, ARM_FEATURE_V8)) { 1820 env->cp15.c9_pmuserenr = value & 0xf; 1821 } else { 1822 env->cp15.c9_pmuserenr = value & 1; 1823 } 1824 } 1825 1826 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1827 uint64_t value) 1828 { 1829 /* We have no event counters so only the C bit can be changed */ 1830 value &= pmu_counter_mask(env); 1831 env->cp15.c9_pminten |= value; 1832 pmu_update_irq(env); 1833 } 1834 1835 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1836 uint64_t value) 1837 { 1838 value &= pmu_counter_mask(env); 1839 env->cp15.c9_pminten &= ~value; 1840 pmu_update_irq(env); 1841 } 1842 1843 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 1844 uint64_t value) 1845 { 1846 /* 1847 * Note that even though the AArch64 view of this register has bits 1848 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 1849 * architectural requirements for bits which are RES0 only in some 1850 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 1851 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 1852 */ 1853 raw_write(env, ri, value & ~0x1FULL); 1854 } 1855 1856 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 1857 { 1858 /* Begin with base v8.0 state. */ 1859 uint64_t valid_mask = 0x3fff; 1860 ARMCPU *cpu = env_archcpu(env); 1861 uint64_t changed; 1862 1863 /* 1864 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always 1865 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64. 1866 * Instead, choose the format based on the mode of EL3. 1867 */ 1868 if (arm_el_is_aa64(env, 3)) { 1869 value |= SCR_FW | SCR_AW; /* RES1 */ 1870 valid_mask &= ~SCR_NET; /* RES0 */ 1871 1872 if (!cpu_isar_feature(aa64_aa32_el1, cpu) && 1873 !cpu_isar_feature(aa64_aa32_el2, cpu)) { 1874 value |= SCR_RW; /* RAO/WI */ 1875 } 1876 if (cpu_isar_feature(aa64_ras, cpu)) { 1877 valid_mask |= SCR_TERR; 1878 } 1879 if (cpu_isar_feature(aa64_lor, cpu)) { 1880 valid_mask |= SCR_TLOR; 1881 } 1882 if (cpu_isar_feature(aa64_pauth, cpu)) { 1883 valid_mask |= SCR_API | SCR_APK; 1884 } 1885 if (cpu_isar_feature(aa64_sel2, cpu)) { 1886 valid_mask |= SCR_EEL2; 1887 } else if (cpu_isar_feature(aa64_rme, cpu)) { 1888 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */ 1889 value |= SCR_NS; 1890 } 1891 if (cpu_isar_feature(aa64_mte, cpu)) { 1892 valid_mask |= SCR_ATA; 1893 } 1894 if (cpu_isar_feature(aa64_scxtnum, cpu)) { 1895 valid_mask |= SCR_ENSCXT; 1896 } 1897 if (cpu_isar_feature(aa64_doublefault, cpu)) { 1898 valid_mask |= SCR_EASE | SCR_NMEA; 1899 } 1900 if (cpu_isar_feature(aa64_sme, cpu)) { 1901 valid_mask |= SCR_ENTP2; 1902 } 1903 if (cpu_isar_feature(aa64_hcx, cpu)) { 1904 valid_mask |= SCR_HXEN; 1905 } 1906 if (cpu_isar_feature(aa64_fgt, cpu)) { 1907 valid_mask |= SCR_FGTEN; 1908 } 1909 if (cpu_isar_feature(aa64_rme, cpu)) { 1910 valid_mask |= SCR_NSE | SCR_GPF; 1911 } 1912 } else { 1913 valid_mask &= ~(SCR_RW | SCR_ST); 1914 if (cpu_isar_feature(aa32_ras, cpu)) { 1915 valid_mask |= SCR_TERR; 1916 } 1917 } 1918 1919 if (!arm_feature(env, ARM_FEATURE_EL2)) { 1920 valid_mask &= ~SCR_HCE; 1921 1922 /* 1923 * On ARMv7, SMD (or SCD as it is called in v7) is only 1924 * supported if EL2 exists. The bit is UNK/SBZP when 1925 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 1926 * when EL2 is unavailable. 1927 * On ARMv8, this bit is always available. 1928 */ 1929 if (arm_feature(env, ARM_FEATURE_V7) && 1930 !arm_feature(env, ARM_FEATURE_V8)) { 1931 valid_mask &= ~SCR_SMD; 1932 } 1933 } 1934 1935 /* Clear all-context RES0 bits. */ 1936 value &= valid_mask; 1937 changed = env->cp15.scr_el3 ^ value; 1938 env->cp15.scr_el3 = value; 1939 1940 /* 1941 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state, 1942 * we must invalidate all TLBs below EL3. 1943 */ 1944 if (changed & (SCR_NS | SCR_NSE)) { 1945 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 | 1946 ARMMMUIdxBit_E20_0 | 1947 ARMMMUIdxBit_E10_1 | 1948 ARMMMUIdxBit_E20_2 | 1949 ARMMMUIdxBit_E10_1_PAN | 1950 ARMMMUIdxBit_E20_2_PAN | 1951 ARMMMUIdxBit_E2)); 1952 } 1953 } 1954 1955 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1956 { 1957 /* 1958 * scr_write will set the RES1 bits on an AArch64-only CPU. 1959 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise. 1960 */ 1961 scr_write(env, ri, 0); 1962 } 1963 1964 static CPAccessResult access_tid4(CPUARMState *env, 1965 const ARMCPRegInfo *ri, 1966 bool isread) 1967 { 1968 if (arm_current_el(env) == 1 && 1969 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) { 1970 return CP_ACCESS_TRAP_EL2; 1971 } 1972 1973 return CP_ACCESS_OK; 1974 } 1975 1976 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1977 { 1978 ARMCPU *cpu = env_archcpu(env); 1979 1980 /* 1981 * Acquire the CSSELR index from the bank corresponding to the CCSIDR 1982 * bank 1983 */ 1984 uint32_t index = A32_BANKED_REG_GET(env, csselr, 1985 ri->secure & ARM_CP_SECSTATE_S); 1986 1987 return cpu->ccsidr[index]; 1988 } 1989 1990 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1991 uint64_t value) 1992 { 1993 raw_write(env, ri, value & 0xf); 1994 } 1995 1996 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1997 { 1998 CPUState *cs = env_cpu(env); 1999 bool el1 = arm_current_el(env) == 1; 2000 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0; 2001 uint64_t ret = 0; 2002 2003 if (hcr_el2 & HCR_IMO) { 2004 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 2005 ret |= CPSR_I; 2006 } 2007 } else { 2008 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 2009 ret |= CPSR_I; 2010 } 2011 } 2012 2013 if (hcr_el2 & HCR_FMO) { 2014 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 2015 ret |= CPSR_F; 2016 } 2017 } else { 2018 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 2019 ret |= CPSR_F; 2020 } 2021 } 2022 2023 if (hcr_el2 & HCR_AMO) { 2024 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) { 2025 ret |= CPSR_A; 2026 } 2027 } 2028 2029 return ret; 2030 } 2031 2032 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri, 2033 bool isread) 2034 { 2035 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) { 2036 return CP_ACCESS_TRAP_EL2; 2037 } 2038 2039 return CP_ACCESS_OK; 2040 } 2041 2042 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri, 2043 bool isread) 2044 { 2045 if (arm_feature(env, ARM_FEATURE_V8)) { 2046 return access_aa64_tid1(env, ri, isread); 2047 } 2048 2049 return CP_ACCESS_OK; 2050 } 2051 2052 static const ARMCPRegInfo v7_cp_reginfo[] = { 2053 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 2054 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 2055 .access = PL1_W, .type = ARM_CP_NOP }, 2056 /* 2057 * Performance monitors are implementation defined in v7, 2058 * but with an ARM recommended set of registers, which we 2059 * follow. 2060 * 2061 * Performance registers fall into three categories: 2062 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 2063 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 2064 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 2065 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 2066 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 2067 */ 2068 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 2069 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO, 2070 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 2071 .writefn = pmcntenset_write, 2072 .accessfn = pmreg_access, 2073 .fgt = FGT_PMCNTEN, 2074 .raw_writefn = raw_write }, 2075 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO, 2076 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 2077 .access = PL0_RW, .accessfn = pmreg_access, 2078 .fgt = FGT_PMCNTEN, 2079 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 2080 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 2081 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 2082 .access = PL0_RW, 2083 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 2084 .accessfn = pmreg_access, 2085 .fgt = FGT_PMCNTEN, 2086 .writefn = pmcntenclr_write, 2087 .type = ARM_CP_ALIAS | ARM_CP_IO }, 2088 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 2089 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 2090 .access = PL0_RW, .accessfn = pmreg_access, 2091 .fgt = FGT_PMCNTEN, 2092 .type = ARM_CP_ALIAS | ARM_CP_IO, 2093 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 2094 .writefn = pmcntenclr_write }, 2095 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 2096 .access = PL0_RW, .type = ARM_CP_IO, 2097 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2098 .accessfn = pmreg_access, 2099 .fgt = FGT_PMOVS, 2100 .writefn = pmovsr_write, 2101 .raw_writefn = raw_write }, 2102 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 2103 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 2104 .access = PL0_RW, .accessfn = pmreg_access, 2105 .fgt = FGT_PMOVS, 2106 .type = ARM_CP_ALIAS | ARM_CP_IO, 2107 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2108 .writefn = pmovsr_write, 2109 .raw_writefn = raw_write }, 2110 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 2111 .access = PL0_W, .accessfn = pmreg_access_swinc, 2112 .fgt = FGT_PMSWINC_EL0, 2113 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2114 .writefn = pmswinc_write }, 2115 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64, 2116 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4, 2117 .access = PL0_W, .accessfn = pmreg_access_swinc, 2118 .fgt = FGT_PMSWINC_EL0, 2119 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2120 .writefn = pmswinc_write }, 2121 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 2122 .access = PL0_RW, .type = ARM_CP_ALIAS, 2123 .fgt = FGT_PMSELR_EL0, 2124 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 2125 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 2126 .raw_writefn = raw_write}, 2127 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 2128 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 2129 .access = PL0_RW, .accessfn = pmreg_access_selr, 2130 .fgt = FGT_PMSELR_EL0, 2131 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 2132 .writefn = pmselr_write, .raw_writefn = raw_write, }, 2133 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 2134 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 2135 .fgt = FGT_PMCCNTR_EL0, 2136 .readfn = pmccntr_read, .writefn = pmccntr_write32, 2137 .accessfn = pmreg_access_ccntr }, 2138 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 2139 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 2140 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 2141 .fgt = FGT_PMCCNTR_EL0, 2142 .type = ARM_CP_IO, 2143 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt), 2144 .readfn = pmccntr_read, .writefn = pmccntr_write, 2145 .raw_readfn = raw_read, .raw_writefn = raw_write, }, 2146 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7, 2147 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32, 2148 .access = PL0_RW, .accessfn = pmreg_access, 2149 .fgt = FGT_PMCCFILTR_EL0, 2150 .type = ARM_CP_ALIAS | ARM_CP_IO, 2151 .resetvalue = 0, }, 2152 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 2153 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 2154 .writefn = pmccfiltr_write, .raw_writefn = raw_write, 2155 .access = PL0_RW, .accessfn = pmreg_access, 2156 .fgt = FGT_PMCCFILTR_EL0, 2157 .type = ARM_CP_IO, 2158 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 2159 .resetvalue = 0, }, 2160 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 2161 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2162 .accessfn = pmreg_access, 2163 .fgt = FGT_PMEVTYPERN_EL0, 2164 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2165 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 2166 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 2167 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2168 .accessfn = pmreg_access, 2169 .fgt = FGT_PMEVTYPERN_EL0, 2170 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2171 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 2172 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2173 .accessfn = pmreg_access_xevcntr, 2174 .fgt = FGT_PMEVCNTRN_EL0, 2175 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2176 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64, 2177 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2, 2178 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2179 .accessfn = pmreg_access_xevcntr, 2180 .fgt = FGT_PMEVCNTRN_EL0, 2181 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2182 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 2183 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 2184 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 2185 .resetvalue = 0, 2186 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2187 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 2188 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 2189 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 2190 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 2191 .resetvalue = 0, 2192 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2193 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 2194 .access = PL1_RW, .accessfn = access_tpm, 2195 .fgt = FGT_PMINTEN, 2196 .type = ARM_CP_ALIAS | ARM_CP_IO, 2197 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 2198 .resetvalue = 0, 2199 .writefn = pmintenset_write, .raw_writefn = raw_write }, 2200 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 2201 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 2202 .access = PL1_RW, .accessfn = access_tpm, 2203 .fgt = FGT_PMINTEN, 2204 .type = ARM_CP_IO, 2205 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2206 .writefn = pmintenset_write, .raw_writefn = raw_write, 2207 .resetvalue = 0x0 }, 2208 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 2209 .access = PL1_RW, .accessfn = access_tpm, 2210 .fgt = FGT_PMINTEN, 2211 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW, 2212 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2213 .writefn = pmintenclr_write, }, 2214 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 2215 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 2216 .access = PL1_RW, .accessfn = access_tpm, 2217 .fgt = FGT_PMINTEN, 2218 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW, 2219 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2220 .writefn = pmintenclr_write }, 2221 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 2222 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 2223 .access = PL1_R, 2224 .accessfn = access_tid4, 2225 .fgt = FGT_CCSIDR_EL1, 2226 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 2227 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 2228 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 2229 .access = PL1_RW, 2230 .accessfn = access_tid4, 2231 .fgt = FGT_CSSELR_EL1, 2232 .writefn = csselr_write, .resetvalue = 0, 2233 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 2234 offsetof(CPUARMState, cp15.csselr_ns) } }, 2235 /* 2236 * Auxiliary ID register: this actually has an IMPDEF value but for now 2237 * just RAZ for all cores: 2238 */ 2239 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 2240 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 2241 .access = PL1_R, .type = ARM_CP_CONST, 2242 .accessfn = access_aa64_tid1, 2243 .fgt = FGT_AIDR_EL1, 2244 .resetvalue = 0 }, 2245 /* 2246 * Auxiliary fault status registers: these also are IMPDEF, and we 2247 * choose to RAZ/WI for all cores. 2248 */ 2249 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 2250 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 2251 .access = PL1_RW, .accessfn = access_tvm_trvm, 2252 .fgt = FGT_AFSR0_EL1, 2253 .type = ARM_CP_CONST, .resetvalue = 0 }, 2254 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 2255 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 2256 .access = PL1_RW, .accessfn = access_tvm_trvm, 2257 .fgt = FGT_AFSR1_EL1, 2258 .type = ARM_CP_CONST, .resetvalue = 0 }, 2259 /* 2260 * MAIR can just read-as-written because we don't implement caches 2261 * and so don't need to care about memory attributes. 2262 */ 2263 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 2264 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2265 .access = PL1_RW, .accessfn = access_tvm_trvm, 2266 .fgt = FGT_MAIR_EL1, 2267 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 2268 .resetvalue = 0 }, 2269 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 2270 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 2271 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 2272 .resetvalue = 0 }, 2273 /* 2274 * For non-long-descriptor page tables these are PRRR and NMRR; 2275 * regardless they still act as reads-as-written for QEMU. 2276 */ 2277 /* 2278 * MAIR0/1 are defined separately from their 64-bit counterpart which 2279 * allows them to assign the correct fieldoffset based on the endianness 2280 * handled in the field definitions. 2281 */ 2282 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 2283 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2284 .access = PL1_RW, .accessfn = access_tvm_trvm, 2285 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 2286 offsetof(CPUARMState, cp15.mair0_ns) }, 2287 .resetfn = arm_cp_reset_ignore }, 2288 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 2289 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, 2290 .access = PL1_RW, .accessfn = access_tvm_trvm, 2291 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 2292 offsetof(CPUARMState, cp15.mair1_ns) }, 2293 .resetfn = arm_cp_reset_ignore }, 2294 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 2295 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 2296 .fgt = FGT_ISR_EL1, 2297 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 2298 /* 32 bit ITLB invalidates */ 2299 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, 2300 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2301 .writefn = tlbiall_write }, 2302 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 2303 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2304 .writefn = tlbimva_write }, 2305 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, 2306 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2307 .writefn = tlbiasid_write }, 2308 /* 32 bit DTLB invalidates */ 2309 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, 2310 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2311 .writefn = tlbiall_write }, 2312 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 2313 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2314 .writefn = tlbimva_write }, 2315 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, 2316 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2317 .writefn = tlbiasid_write }, 2318 /* 32 bit TLB invalidates */ 2319 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 2320 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2321 .writefn = tlbiall_write }, 2322 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 2323 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2324 .writefn = tlbimva_write }, 2325 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 2326 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2327 .writefn = tlbiasid_write }, 2328 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 2329 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2330 .writefn = tlbimvaa_write }, 2331 }; 2332 2333 static const ARMCPRegInfo v7mp_cp_reginfo[] = { 2334 /* 32 bit TLB invalidates, Inner Shareable */ 2335 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 2336 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis, 2337 .writefn = tlbiall_is_write }, 2338 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 2339 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis, 2340 .writefn = tlbimva_is_write }, 2341 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 2342 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis, 2343 .writefn = tlbiasid_is_write }, 2344 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 2345 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis, 2346 .writefn = tlbimvaa_is_write }, 2347 }; 2348 2349 static const ARMCPRegInfo pmovsset_cp_reginfo[] = { 2350 /* PMOVSSET is not implemented in v7 before v7ve */ 2351 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3, 2352 .access = PL0_RW, .accessfn = pmreg_access, 2353 .fgt = FGT_PMOVS, 2354 .type = ARM_CP_ALIAS | ARM_CP_IO, 2355 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2356 .writefn = pmovsset_write, 2357 .raw_writefn = raw_write }, 2358 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64, 2359 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3, 2360 .access = PL0_RW, .accessfn = pmreg_access, 2361 .fgt = FGT_PMOVS, 2362 .type = ARM_CP_ALIAS | ARM_CP_IO, 2363 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2364 .writefn = pmovsset_write, 2365 .raw_writefn = raw_write }, 2366 }; 2367 2368 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2369 uint64_t value) 2370 { 2371 value &= 1; 2372 env->teecr = value; 2373 } 2374 2375 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2376 bool isread) 2377 { 2378 /* 2379 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE 2380 * at all, so we don't need to check whether we're v8A. 2381 */ 2382 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) && 2383 (env->cp15.hstr_el2 & HSTR_TTEE)) { 2384 return CP_ACCESS_TRAP_EL2; 2385 } 2386 return CP_ACCESS_OK; 2387 } 2388 2389 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2390 bool isread) 2391 { 2392 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 2393 return CP_ACCESS_TRAP; 2394 } 2395 return teecr_access(env, ri, isread); 2396 } 2397 2398 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 2399 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 2400 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 2401 .resetvalue = 0, 2402 .writefn = teecr_write, .accessfn = teecr_access }, 2403 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 2404 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 2405 .accessfn = teehbr_access, .resetvalue = 0 }, 2406 }; 2407 2408 static const ARMCPRegInfo v6k_cp_reginfo[] = { 2409 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 2410 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 2411 .access = PL0_RW, 2412 .fgt = FGT_TPIDR_EL0, 2413 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 2414 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 2415 .access = PL0_RW, 2416 .fgt = FGT_TPIDR_EL0, 2417 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 2418 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 2419 .resetfn = arm_cp_reset_ignore }, 2420 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 2421 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 2422 .access = PL0_R | PL1_W, 2423 .fgt = FGT_TPIDRRO_EL0, 2424 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 2425 .resetvalue = 0}, 2426 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 2427 .access = PL0_R | PL1_W, 2428 .fgt = FGT_TPIDRRO_EL0, 2429 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 2430 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 2431 .resetfn = arm_cp_reset_ignore }, 2432 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 2433 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 2434 .access = PL1_RW, 2435 .fgt = FGT_TPIDR_EL1, 2436 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 2437 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 2438 .access = PL1_RW, 2439 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 2440 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 2441 .resetvalue = 0 }, 2442 }; 2443 2444 #ifndef CONFIG_USER_ONLY 2445 2446 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 2447 bool isread) 2448 { 2449 /* 2450 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 2451 * Writable only at the highest implemented exception level. 2452 */ 2453 int el = arm_current_el(env); 2454 uint64_t hcr; 2455 uint32_t cntkctl; 2456 2457 switch (el) { 2458 case 0: 2459 hcr = arm_hcr_el2_eff(env); 2460 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2461 cntkctl = env->cp15.cnthctl_el2; 2462 } else { 2463 cntkctl = env->cp15.c14_cntkctl; 2464 } 2465 if (!extract32(cntkctl, 0, 2)) { 2466 return CP_ACCESS_TRAP; 2467 } 2468 break; 2469 case 1: 2470 if (!isread && ri->state == ARM_CP_STATE_AA32 && 2471 arm_is_secure_below_el3(env)) { 2472 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 2473 return CP_ACCESS_TRAP_UNCATEGORIZED; 2474 } 2475 break; 2476 case 2: 2477 case 3: 2478 break; 2479 } 2480 2481 if (!isread && el < arm_highest_el(env)) { 2482 return CP_ACCESS_TRAP_UNCATEGORIZED; 2483 } 2484 2485 return CP_ACCESS_OK; 2486 } 2487 2488 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 2489 bool isread) 2490 { 2491 unsigned int cur_el = arm_current_el(env); 2492 bool has_el2 = arm_is_el2_enabled(env); 2493 uint64_t hcr = arm_hcr_el2_eff(env); 2494 2495 switch (cur_el) { 2496 case 0: 2497 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */ 2498 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2499 return (extract32(env->cp15.cnthctl_el2, timeridx, 1) 2500 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2); 2501 } 2502 2503 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */ 2504 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 2505 return CP_ACCESS_TRAP; 2506 } 2507 /* fall through */ 2508 case 1: 2509 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */ 2510 if (has_el2 && timeridx == GTIMER_PHYS && 2511 (hcr & HCR_E2H 2512 ? !extract32(env->cp15.cnthctl_el2, 10, 1) 2513 : !extract32(env->cp15.cnthctl_el2, 0, 1))) { 2514 return CP_ACCESS_TRAP_EL2; 2515 } 2516 break; 2517 } 2518 return CP_ACCESS_OK; 2519 } 2520 2521 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 2522 bool isread) 2523 { 2524 unsigned int cur_el = arm_current_el(env); 2525 bool has_el2 = arm_is_el2_enabled(env); 2526 uint64_t hcr = arm_hcr_el2_eff(env); 2527 2528 switch (cur_el) { 2529 case 0: 2530 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2531 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */ 2532 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1) 2533 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2); 2534 } 2535 2536 /* 2537 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from 2538 * EL0 if EL0[PV]TEN is zero. 2539 */ 2540 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 2541 return CP_ACCESS_TRAP; 2542 } 2543 /* fall through */ 2544 2545 case 1: 2546 if (has_el2 && timeridx == GTIMER_PHYS) { 2547 if (hcr & HCR_E2H) { 2548 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */ 2549 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) { 2550 return CP_ACCESS_TRAP_EL2; 2551 } 2552 } else { 2553 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */ 2554 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) { 2555 return CP_ACCESS_TRAP_EL2; 2556 } 2557 } 2558 } 2559 break; 2560 } 2561 return CP_ACCESS_OK; 2562 } 2563 2564 static CPAccessResult gt_pct_access(CPUARMState *env, 2565 const ARMCPRegInfo *ri, 2566 bool isread) 2567 { 2568 return gt_counter_access(env, GTIMER_PHYS, isread); 2569 } 2570 2571 static CPAccessResult gt_vct_access(CPUARMState *env, 2572 const ARMCPRegInfo *ri, 2573 bool isread) 2574 { 2575 return gt_counter_access(env, GTIMER_VIRT, isread); 2576 } 2577 2578 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2579 bool isread) 2580 { 2581 return gt_timer_access(env, GTIMER_PHYS, isread); 2582 } 2583 2584 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2585 bool isread) 2586 { 2587 return gt_timer_access(env, GTIMER_VIRT, isread); 2588 } 2589 2590 static CPAccessResult gt_stimer_access(CPUARMState *env, 2591 const ARMCPRegInfo *ri, 2592 bool isread) 2593 { 2594 /* 2595 * The AArch64 register view of the secure physical timer is 2596 * always accessible from EL3, and configurably accessible from 2597 * Secure EL1. 2598 */ 2599 switch (arm_current_el(env)) { 2600 case 1: 2601 if (!arm_is_secure(env)) { 2602 return CP_ACCESS_TRAP; 2603 } 2604 if (!(env->cp15.scr_el3 & SCR_ST)) { 2605 return CP_ACCESS_TRAP_EL3; 2606 } 2607 return CP_ACCESS_OK; 2608 case 0: 2609 case 2: 2610 return CP_ACCESS_TRAP; 2611 case 3: 2612 return CP_ACCESS_OK; 2613 default: 2614 g_assert_not_reached(); 2615 } 2616 } 2617 2618 static uint64_t gt_get_countervalue(CPUARMState *env) 2619 { 2620 ARMCPU *cpu = env_archcpu(env); 2621 2622 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu); 2623 } 2624 2625 static void gt_update_irq(ARMCPU *cpu, int timeridx) 2626 { 2627 CPUARMState *env = &cpu->env; 2628 uint64_t cnthctl = env->cp15.cnthctl_el2; 2629 ARMSecuritySpace ss = arm_security_space(env); 2630 /* ISTATUS && !IMASK */ 2631 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4; 2632 2633 /* 2634 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK. 2635 * It is RES0 in Secure and NonSecure state. 2636 */ 2637 if ((ss == ARMSS_Root || ss == ARMSS_Realm) && 2638 ((timeridx == GTIMER_VIRT && (cnthctl & CNTHCTL_CNTVMASK)) || 2639 (timeridx == GTIMER_PHYS && (cnthctl & CNTHCTL_CNTPMASK)))) { 2640 irqstate = 0; 2641 } 2642 2643 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2644 trace_arm_gt_update_irq(timeridx, irqstate); 2645 } 2646 2647 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored) 2648 { 2649 /* 2650 * Changing security state between Root and Secure/NonSecure, which may 2651 * happen when switching EL, can change the effective value of CNTHCTL_EL2 2652 * mask bits. Update the IRQ state accordingly. 2653 */ 2654 gt_update_irq(cpu, GTIMER_VIRT); 2655 gt_update_irq(cpu, GTIMER_PHYS); 2656 } 2657 2658 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 2659 { 2660 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 2661 2662 if (gt->ctl & 1) { 2663 /* 2664 * Timer enabled: calculate and set current ISTATUS, irq, and 2665 * reset timer to when ISTATUS next has to change 2666 */ 2667 uint64_t offset = timeridx == GTIMER_VIRT ? 2668 cpu->env.cp15.cntvoff_el2 : 0; 2669 uint64_t count = gt_get_countervalue(&cpu->env); 2670 /* Note that this must be unsigned 64 bit arithmetic: */ 2671 int istatus = count - offset >= gt->cval; 2672 uint64_t nexttick; 2673 2674 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 2675 2676 if (istatus) { 2677 /* 2678 * Next transition is when (count - offset) rolls back over to 0. 2679 * If offset > count then this is when count == offset; 2680 * if offset <= count then this is when count == offset + 2^64 2681 * For the latter case we set nexttick to an "as far in future 2682 * as possible" value and let the code below handle it. 2683 */ 2684 if (offset > count) { 2685 nexttick = offset; 2686 } else { 2687 nexttick = UINT64_MAX; 2688 } 2689 } else { 2690 /* 2691 * Next transition is when (count - offset) == cval, i.e. 2692 * when count == (cval + offset). 2693 * If that would overflow, then again we set up the next interrupt 2694 * for "as far in the future as possible" for the code below. 2695 */ 2696 if (uadd64_overflow(gt->cval, offset, &nexttick)) { 2697 nexttick = UINT64_MAX; 2698 } 2699 } 2700 /* 2701 * Note that the desired next expiry time might be beyond the 2702 * signed-64-bit range of a QEMUTimer -- in this case we just 2703 * set the timer for as far in the future as possible. When the 2704 * timer expires we will reset the timer for any remaining period. 2705 */ 2706 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) { 2707 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX); 2708 } else { 2709 timer_mod(cpu->gt_timer[timeridx], nexttick); 2710 } 2711 trace_arm_gt_recalc(timeridx, nexttick); 2712 } else { 2713 /* Timer disabled: ISTATUS and timer output always clear */ 2714 gt->ctl &= ~4; 2715 timer_del(cpu->gt_timer[timeridx]); 2716 trace_arm_gt_recalc_disabled(timeridx); 2717 } 2718 gt_update_irq(cpu, timeridx); 2719 } 2720 2721 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 2722 int timeridx) 2723 { 2724 ARMCPU *cpu = env_archcpu(env); 2725 2726 timer_del(cpu->gt_timer[timeridx]); 2727 } 2728 2729 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2730 { 2731 return gt_get_countervalue(env); 2732 } 2733 2734 static uint64_t gt_virt_cnt_offset(CPUARMState *env) 2735 { 2736 uint64_t hcr; 2737 2738 switch (arm_current_el(env)) { 2739 case 2: 2740 hcr = arm_hcr_el2_eff(env); 2741 if (hcr & HCR_E2H) { 2742 return 0; 2743 } 2744 break; 2745 case 0: 2746 hcr = arm_hcr_el2_eff(env); 2747 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2748 return 0; 2749 } 2750 break; 2751 } 2752 2753 return env->cp15.cntvoff_el2; 2754 } 2755 2756 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2757 { 2758 return gt_get_countervalue(env) - gt_virt_cnt_offset(env); 2759 } 2760 2761 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2762 int timeridx, 2763 uint64_t value) 2764 { 2765 trace_arm_gt_cval_write(timeridx, value); 2766 env->cp15.c14_timer[timeridx].cval = value; 2767 gt_recalc_timer(env_archcpu(env), timeridx); 2768 } 2769 2770 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 2771 int timeridx) 2772 { 2773 uint64_t offset = 0; 2774 2775 switch (timeridx) { 2776 case GTIMER_VIRT: 2777 case GTIMER_HYPVIRT: 2778 offset = gt_virt_cnt_offset(env); 2779 break; 2780 } 2781 2782 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 2783 (gt_get_countervalue(env) - offset)); 2784 } 2785 2786 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2787 int timeridx, 2788 uint64_t value) 2789 { 2790 uint64_t offset = 0; 2791 2792 switch (timeridx) { 2793 case GTIMER_VIRT: 2794 case GTIMER_HYPVIRT: 2795 offset = gt_virt_cnt_offset(env); 2796 break; 2797 } 2798 2799 trace_arm_gt_tval_write(timeridx, value); 2800 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 2801 sextract64(value, 0, 32); 2802 gt_recalc_timer(env_archcpu(env), timeridx); 2803 } 2804 2805 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2806 int timeridx, 2807 uint64_t value) 2808 { 2809 ARMCPU *cpu = env_archcpu(env); 2810 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 2811 2812 trace_arm_gt_ctl_write(timeridx, value); 2813 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 2814 if ((oldval ^ value) & 1) { 2815 /* Enable toggled */ 2816 gt_recalc_timer(cpu, timeridx); 2817 } else if ((oldval ^ value) & 2) { 2818 /* 2819 * IMASK toggled: don't need to recalculate, 2820 * just set the interrupt line based on ISTATUS 2821 */ 2822 trace_arm_gt_imask_toggle(timeridx); 2823 gt_update_irq(cpu, timeridx); 2824 } 2825 } 2826 2827 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2828 { 2829 gt_timer_reset(env, ri, GTIMER_PHYS); 2830 } 2831 2832 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2833 uint64_t value) 2834 { 2835 gt_cval_write(env, ri, GTIMER_PHYS, value); 2836 } 2837 2838 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2839 { 2840 return gt_tval_read(env, ri, GTIMER_PHYS); 2841 } 2842 2843 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2844 uint64_t value) 2845 { 2846 gt_tval_write(env, ri, GTIMER_PHYS, value); 2847 } 2848 2849 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2850 uint64_t value) 2851 { 2852 gt_ctl_write(env, ri, GTIMER_PHYS, value); 2853 } 2854 2855 static int gt_phys_redir_timeridx(CPUARMState *env) 2856 { 2857 switch (arm_mmu_idx(env)) { 2858 case ARMMMUIdx_E20_0: 2859 case ARMMMUIdx_E20_2: 2860 case ARMMMUIdx_E20_2_PAN: 2861 return GTIMER_HYP; 2862 default: 2863 return GTIMER_PHYS; 2864 } 2865 } 2866 2867 static int gt_virt_redir_timeridx(CPUARMState *env) 2868 { 2869 switch (arm_mmu_idx(env)) { 2870 case ARMMMUIdx_E20_0: 2871 case ARMMMUIdx_E20_2: 2872 case ARMMMUIdx_E20_2_PAN: 2873 return GTIMER_HYPVIRT; 2874 default: 2875 return GTIMER_VIRT; 2876 } 2877 } 2878 2879 static uint64_t gt_phys_redir_cval_read(CPUARMState *env, 2880 const ARMCPRegInfo *ri) 2881 { 2882 int timeridx = gt_phys_redir_timeridx(env); 2883 return env->cp15.c14_timer[timeridx].cval; 2884 } 2885 2886 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2887 uint64_t value) 2888 { 2889 int timeridx = gt_phys_redir_timeridx(env); 2890 gt_cval_write(env, ri, timeridx, value); 2891 } 2892 2893 static uint64_t gt_phys_redir_tval_read(CPUARMState *env, 2894 const ARMCPRegInfo *ri) 2895 { 2896 int timeridx = gt_phys_redir_timeridx(env); 2897 return gt_tval_read(env, ri, timeridx); 2898 } 2899 2900 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2901 uint64_t value) 2902 { 2903 int timeridx = gt_phys_redir_timeridx(env); 2904 gt_tval_write(env, ri, timeridx, value); 2905 } 2906 2907 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env, 2908 const ARMCPRegInfo *ri) 2909 { 2910 int timeridx = gt_phys_redir_timeridx(env); 2911 return env->cp15.c14_timer[timeridx].ctl; 2912 } 2913 2914 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2915 uint64_t value) 2916 { 2917 int timeridx = gt_phys_redir_timeridx(env); 2918 gt_ctl_write(env, ri, timeridx, value); 2919 } 2920 2921 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2922 { 2923 gt_timer_reset(env, ri, GTIMER_VIRT); 2924 } 2925 2926 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2927 uint64_t value) 2928 { 2929 gt_cval_write(env, ri, GTIMER_VIRT, value); 2930 } 2931 2932 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2933 { 2934 return gt_tval_read(env, ri, GTIMER_VIRT); 2935 } 2936 2937 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2938 uint64_t value) 2939 { 2940 gt_tval_write(env, ri, GTIMER_VIRT, value); 2941 } 2942 2943 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2944 uint64_t value) 2945 { 2946 gt_ctl_write(env, ri, GTIMER_VIRT, value); 2947 } 2948 2949 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2950 uint64_t value) 2951 { 2952 ARMCPU *cpu = env_archcpu(env); 2953 uint32_t oldval = env->cp15.cnthctl_el2; 2954 2955 raw_write(env, ri, value); 2956 2957 if ((oldval ^ value) & CNTHCTL_CNTVMASK) { 2958 gt_update_irq(cpu, GTIMER_VIRT); 2959 } else if ((oldval ^ value) & CNTHCTL_CNTPMASK) { 2960 gt_update_irq(cpu, GTIMER_PHYS); 2961 } 2962 } 2963 2964 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 2965 uint64_t value) 2966 { 2967 ARMCPU *cpu = env_archcpu(env); 2968 2969 trace_arm_gt_cntvoff_write(value); 2970 raw_write(env, ri, value); 2971 gt_recalc_timer(cpu, GTIMER_VIRT); 2972 } 2973 2974 static uint64_t gt_virt_redir_cval_read(CPUARMState *env, 2975 const ARMCPRegInfo *ri) 2976 { 2977 int timeridx = gt_virt_redir_timeridx(env); 2978 return env->cp15.c14_timer[timeridx].cval; 2979 } 2980 2981 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2982 uint64_t value) 2983 { 2984 int timeridx = gt_virt_redir_timeridx(env); 2985 gt_cval_write(env, ri, timeridx, value); 2986 } 2987 2988 static uint64_t gt_virt_redir_tval_read(CPUARMState *env, 2989 const ARMCPRegInfo *ri) 2990 { 2991 int timeridx = gt_virt_redir_timeridx(env); 2992 return gt_tval_read(env, ri, timeridx); 2993 } 2994 2995 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2996 uint64_t value) 2997 { 2998 int timeridx = gt_virt_redir_timeridx(env); 2999 gt_tval_write(env, ri, timeridx, value); 3000 } 3001 3002 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env, 3003 const ARMCPRegInfo *ri) 3004 { 3005 int timeridx = gt_virt_redir_timeridx(env); 3006 return env->cp15.c14_timer[timeridx].ctl; 3007 } 3008 3009 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3010 uint64_t value) 3011 { 3012 int timeridx = gt_virt_redir_timeridx(env); 3013 gt_ctl_write(env, ri, timeridx, value); 3014 } 3015 3016 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3017 { 3018 gt_timer_reset(env, ri, GTIMER_HYP); 3019 } 3020 3021 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3022 uint64_t value) 3023 { 3024 gt_cval_write(env, ri, GTIMER_HYP, value); 3025 } 3026 3027 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3028 { 3029 return gt_tval_read(env, ri, GTIMER_HYP); 3030 } 3031 3032 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3033 uint64_t value) 3034 { 3035 gt_tval_write(env, ri, GTIMER_HYP, value); 3036 } 3037 3038 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3039 uint64_t value) 3040 { 3041 gt_ctl_write(env, ri, GTIMER_HYP, value); 3042 } 3043 3044 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3045 { 3046 gt_timer_reset(env, ri, GTIMER_SEC); 3047 } 3048 3049 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3050 uint64_t value) 3051 { 3052 gt_cval_write(env, ri, GTIMER_SEC, value); 3053 } 3054 3055 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3056 { 3057 return gt_tval_read(env, ri, GTIMER_SEC); 3058 } 3059 3060 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3061 uint64_t value) 3062 { 3063 gt_tval_write(env, ri, GTIMER_SEC, value); 3064 } 3065 3066 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3067 uint64_t value) 3068 { 3069 gt_ctl_write(env, ri, GTIMER_SEC, value); 3070 } 3071 3072 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3073 { 3074 gt_timer_reset(env, ri, GTIMER_HYPVIRT); 3075 } 3076 3077 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3078 uint64_t value) 3079 { 3080 gt_cval_write(env, ri, GTIMER_HYPVIRT, value); 3081 } 3082 3083 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3084 { 3085 return gt_tval_read(env, ri, GTIMER_HYPVIRT); 3086 } 3087 3088 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3089 uint64_t value) 3090 { 3091 gt_tval_write(env, ri, GTIMER_HYPVIRT, value); 3092 } 3093 3094 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3095 uint64_t value) 3096 { 3097 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value); 3098 } 3099 3100 void arm_gt_ptimer_cb(void *opaque) 3101 { 3102 ARMCPU *cpu = opaque; 3103 3104 gt_recalc_timer(cpu, GTIMER_PHYS); 3105 } 3106 3107 void arm_gt_vtimer_cb(void *opaque) 3108 { 3109 ARMCPU *cpu = opaque; 3110 3111 gt_recalc_timer(cpu, GTIMER_VIRT); 3112 } 3113 3114 void arm_gt_htimer_cb(void *opaque) 3115 { 3116 ARMCPU *cpu = opaque; 3117 3118 gt_recalc_timer(cpu, GTIMER_HYP); 3119 } 3120 3121 void arm_gt_stimer_cb(void *opaque) 3122 { 3123 ARMCPU *cpu = opaque; 3124 3125 gt_recalc_timer(cpu, GTIMER_SEC); 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 void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque) 3136 { 3137 ARMCPU *cpu = env_archcpu(env); 3138 3139 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz; 3140 } 3141 3142 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 3143 /* 3144 * Note that CNTFRQ is purely reads-as-written for the benefit 3145 * of software; writing it doesn't actually change the timer frequency. 3146 * Our reset value matches the fixed frequency we implement the timer at. 3147 */ 3148 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 3149 .type = ARM_CP_ALIAS, 3150 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 3151 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 3152 }, 3153 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 3154 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 3155 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 3156 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 3157 .resetfn = arm_gt_cntfrq_reset, 3158 }, 3159 /* overall control: mostly access permissions */ 3160 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 3161 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 3162 .access = PL1_RW, 3163 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 3164 .resetvalue = 0, 3165 }, 3166 /* per-timer control */ 3167 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 3168 .secure = ARM_CP_SECSTATE_NS, 3169 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3170 .accessfn = gt_ptimer_access, 3171 .fieldoffset = offsetoflow32(CPUARMState, 3172 cp15.c14_timer[GTIMER_PHYS].ctl), 3173 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read, 3174 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write, 3175 }, 3176 { .name = "CNTP_CTL_S", 3177 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 3178 .secure = ARM_CP_SECSTATE_S, 3179 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3180 .accessfn = gt_ptimer_access, 3181 .fieldoffset = offsetoflow32(CPUARMState, 3182 cp15.c14_timer[GTIMER_SEC].ctl), 3183 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 3184 }, 3185 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 3186 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 3187 .type = ARM_CP_IO, .access = PL0_RW, 3188 .accessfn = gt_ptimer_access, 3189 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 3190 .resetvalue = 0, 3191 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read, 3192 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write, 3193 }, 3194 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 3195 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3196 .accessfn = gt_vtimer_access, 3197 .fieldoffset = offsetoflow32(CPUARMState, 3198 cp15.c14_timer[GTIMER_VIRT].ctl), 3199 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read, 3200 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write, 3201 }, 3202 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 3203 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 3204 .type = ARM_CP_IO, .access = PL0_RW, 3205 .accessfn = gt_vtimer_access, 3206 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 3207 .resetvalue = 0, 3208 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read, 3209 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write, 3210 }, 3211 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 3212 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 3213 .secure = ARM_CP_SECSTATE_NS, 3214 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3215 .accessfn = gt_ptimer_access, 3216 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write, 3217 }, 3218 { .name = "CNTP_TVAL_S", 3219 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 3220 .secure = ARM_CP_SECSTATE_S, 3221 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3222 .accessfn = gt_ptimer_access, 3223 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 3224 }, 3225 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 3226 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 3227 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3228 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 3229 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write, 3230 }, 3231 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 3232 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3233 .accessfn = gt_vtimer_access, 3234 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write, 3235 }, 3236 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 3237 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 3238 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3239 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 3240 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write, 3241 }, 3242 /* The counter itself */ 3243 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 3244 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3245 .accessfn = gt_pct_access, 3246 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 3247 }, 3248 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 3249 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 3250 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3251 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 3252 }, 3253 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 3254 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3255 .accessfn = gt_vct_access, 3256 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 3257 }, 3258 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 3259 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 3260 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3261 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 3262 }, 3263 /* Comparison value, indicating when the timer goes off */ 3264 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 3265 .secure = ARM_CP_SECSTATE_NS, 3266 .access = PL0_RW, 3267 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3268 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 3269 .accessfn = gt_ptimer_access, 3270 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read, 3271 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write, 3272 }, 3273 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 3274 .secure = ARM_CP_SECSTATE_S, 3275 .access = PL0_RW, 3276 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3277 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 3278 .accessfn = gt_ptimer_access, 3279 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 3280 }, 3281 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 3282 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 3283 .access = PL0_RW, 3284 .type = ARM_CP_IO, 3285 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 3286 .resetvalue = 0, .accessfn = gt_ptimer_access, 3287 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read, 3288 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write, 3289 }, 3290 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 3291 .access = PL0_RW, 3292 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3293 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 3294 .accessfn = gt_vtimer_access, 3295 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read, 3296 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write, 3297 }, 3298 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 3299 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 3300 .access = PL0_RW, 3301 .type = ARM_CP_IO, 3302 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 3303 .resetvalue = 0, .accessfn = gt_vtimer_access, 3304 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read, 3305 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write, 3306 }, 3307 /* 3308 * Secure timer -- this is actually restricted to only EL3 3309 * and configurably Secure-EL1 via the accessfn. 3310 */ 3311 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 3312 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 3313 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 3314 .accessfn = gt_stimer_access, 3315 .readfn = gt_sec_tval_read, 3316 .writefn = gt_sec_tval_write, 3317 .resetfn = gt_sec_timer_reset, 3318 }, 3319 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 3320 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 3321 .type = ARM_CP_IO, .access = PL1_RW, 3322 .accessfn = gt_stimer_access, 3323 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 3324 .resetvalue = 0, 3325 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 3326 }, 3327 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 3328 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 3329 .type = ARM_CP_IO, .access = PL1_RW, 3330 .accessfn = gt_stimer_access, 3331 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 3332 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 3333 }, 3334 }; 3335 3336 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri, 3337 bool isread) 3338 { 3339 if (arm_current_el(env) == 1) { 3340 /* This must be a FEAT_NV access */ 3341 /* TODO: FEAT_ECV will need to check CNTHCTL_EL2 here */ 3342 return CP_ACCESS_OK; 3343 } 3344 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) { 3345 return CP_ACCESS_TRAP; 3346 } 3347 return CP_ACCESS_OK; 3348 } 3349 3350 #else 3351 3352 /* 3353 * In user-mode most of the generic timer registers are inaccessible 3354 * however modern kernels (4.12+) allow access to cntvct_el0 3355 */ 3356 3357 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 3358 { 3359 ARMCPU *cpu = env_archcpu(env); 3360 3361 /* 3362 * Currently we have no support for QEMUTimer in linux-user so we 3363 * can't call gt_get_countervalue(env), instead we directly 3364 * call the lower level functions. 3365 */ 3366 return cpu_get_clock() / gt_cntfrq_period_ns(cpu); 3367 } 3368 3369 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 3370 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 3371 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 3372 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 3373 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 3374 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, 3375 }, 3376 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 3377 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 3378 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3379 .readfn = gt_virt_cnt_read, 3380 }, 3381 }; 3382 3383 #endif 3384 3385 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3386 { 3387 if (arm_feature(env, ARM_FEATURE_LPAE)) { 3388 raw_write(env, ri, value); 3389 } else if (arm_feature(env, ARM_FEATURE_V7)) { 3390 raw_write(env, ri, value & 0xfffff6ff); 3391 } else { 3392 raw_write(env, ri, value & 0xfffff1ff); 3393 } 3394 } 3395 3396 #ifndef CONFIG_USER_ONLY 3397 /* get_phys_addr() isn't present for user-mode-only targets */ 3398 3399 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 3400 bool isread) 3401 { 3402 if (ri->opc2 & 4) { 3403 /* 3404 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in 3405 * Secure EL1 (which can only happen if EL3 is AArch64). 3406 * They are simply UNDEF if executed from NS EL1. 3407 * They function normally from EL2 or EL3. 3408 */ 3409 if (arm_current_el(env) == 1) { 3410 if (arm_is_secure_below_el3(env)) { 3411 if (env->cp15.scr_el3 & SCR_EEL2) { 3412 return CP_ACCESS_TRAP_EL2; 3413 } 3414 return CP_ACCESS_TRAP_EL3; 3415 } 3416 return CP_ACCESS_TRAP_UNCATEGORIZED; 3417 } 3418 } 3419 return CP_ACCESS_OK; 3420 } 3421 3422 #ifdef CONFIG_TCG 3423 static int par_el1_shareability(GetPhysAddrResult *res) 3424 { 3425 /* 3426 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC 3427 * memory -- see pseudocode PAREncodeShareability(). 3428 */ 3429 if (((res->cacheattrs.attrs & 0xf0) == 0) || 3430 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) { 3431 return 2; 3432 } 3433 return res->cacheattrs.shareability; 3434 } 3435 3436 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 3437 MMUAccessType access_type, ARMMMUIdx mmu_idx, 3438 ARMSecuritySpace ss) 3439 { 3440 bool ret; 3441 uint64_t par64; 3442 bool format64 = false; 3443 ARMMMUFaultInfo fi = {}; 3444 GetPhysAddrResult res = {}; 3445 3446 /* 3447 * I_MXTJT: Granule protection checks are not performed on the final address 3448 * of a successful translation. 3449 */ 3450 ret = get_phys_addr_with_space_nogpc(env, value, access_type, mmu_idx, ss, 3451 &res, &fi); 3452 3453 /* 3454 * ATS operations only do S1 or S1+S2 translations, so we never 3455 * have to deal with the ARMCacheAttrs format for S2 only. 3456 */ 3457 assert(!res.cacheattrs.is_s2_format); 3458 3459 if (ret) { 3460 /* 3461 * Some kinds of translation fault must cause exceptions rather 3462 * than being reported in the PAR. 3463 */ 3464 int current_el = arm_current_el(env); 3465 int target_el; 3466 uint32_t syn, fsr, fsc; 3467 bool take_exc = false; 3468 3469 if (fi.s1ptw && current_el == 1 3470 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) { 3471 /* 3472 * Synchronous stage 2 fault on an access made as part of the 3473 * translation table walk for AT S1E0* or AT S1E1* insn 3474 * executed from NS EL1. If this is a synchronous external abort 3475 * and SCR_EL3.EA == 1, then we take a synchronous external abort 3476 * to EL3. Otherwise the fault is taken as an exception to EL2, 3477 * and HPFAR_EL2 holds the faulting IPA. 3478 */ 3479 if (fi.type == ARMFault_SyncExternalOnWalk && 3480 (env->cp15.scr_el3 & SCR_EA)) { 3481 target_el = 3; 3482 } else { 3483 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4; 3484 if (arm_is_secure_below_el3(env) && fi.s1ns) { 3485 env->cp15.hpfar_el2 |= HPFAR_NS; 3486 } 3487 target_el = 2; 3488 } 3489 take_exc = true; 3490 } else if (fi.type == ARMFault_SyncExternalOnWalk) { 3491 /* 3492 * Synchronous external aborts during a translation table walk 3493 * are taken as Data Abort exceptions. 3494 */ 3495 if (fi.stage2) { 3496 if (current_el == 3) { 3497 target_el = 3; 3498 } else { 3499 target_el = 2; 3500 } 3501 } else { 3502 target_el = exception_target_el(env); 3503 } 3504 take_exc = true; 3505 } 3506 3507 if (take_exc) { 3508 /* Construct FSR and FSC using same logic as arm_deliver_fault() */ 3509 if (target_el == 2 || arm_el_is_aa64(env, target_el) || 3510 arm_s1_regime_using_lpae_format(env, mmu_idx)) { 3511 fsr = arm_fi_to_lfsc(&fi); 3512 fsc = extract32(fsr, 0, 6); 3513 } else { 3514 fsr = arm_fi_to_sfsc(&fi); 3515 fsc = 0x3f; 3516 } 3517 /* 3518 * Report exception with ESR indicating a fault due to a 3519 * translation table walk for a cache maintenance instruction. 3520 */ 3521 syn = syn_data_abort_no_iss(current_el == target_el, 0, 3522 fi.ea, 1, fi.s1ptw, 1, fsc); 3523 env->exception.vaddress = value; 3524 env->exception.fsr = fsr; 3525 raise_exception(env, EXCP_DATA_ABORT, syn, target_el); 3526 } 3527 } 3528 3529 if (is_a64(env)) { 3530 format64 = true; 3531 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 3532 /* 3533 * ATS1Cxx: 3534 * * TTBCR.EAE determines whether the result is returned using the 3535 * 32-bit or the 64-bit PAR format 3536 * * Instructions executed in Hyp mode always use the 64bit format 3537 * 3538 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 3539 * * The Non-secure TTBCR.EAE bit is set to 1 3540 * * The implementation includes EL2, and the value of HCR.VM is 1 3541 * 3542 * (Note that HCR.DC makes HCR.VM behave as if it is 1.) 3543 * 3544 * ATS1Hx always uses the 64bit format. 3545 */ 3546 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 3547 3548 if (arm_feature(env, ARM_FEATURE_EL2)) { 3549 if (mmu_idx == ARMMMUIdx_E10_0 || 3550 mmu_idx == ARMMMUIdx_E10_1 || 3551 mmu_idx == ARMMMUIdx_E10_1_PAN) { 3552 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC); 3553 } else { 3554 format64 |= arm_current_el(env) == 2; 3555 } 3556 } 3557 } 3558 3559 if (format64) { 3560 /* Create a 64-bit PAR */ 3561 par64 = (1 << 11); /* LPAE bit always set */ 3562 if (!ret) { 3563 par64 |= res.f.phys_addr & ~0xfffULL; 3564 if (!res.f.attrs.secure) { 3565 par64 |= (1 << 9); /* NS */ 3566 } 3567 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */ 3568 par64 |= par_el1_shareability(&res) << 7; /* SH */ 3569 } else { 3570 uint32_t fsr = arm_fi_to_lfsc(&fi); 3571 3572 par64 |= 1; /* F */ 3573 par64 |= (fsr & 0x3f) << 1; /* FS */ 3574 if (fi.stage2) { 3575 par64 |= (1 << 9); /* S */ 3576 } 3577 if (fi.s1ptw) { 3578 par64 |= (1 << 8); /* PTW */ 3579 } 3580 } 3581 } else { 3582 /* 3583 * fsr is a DFSR/IFSR value for the short descriptor 3584 * translation table format (with WnR always clear). 3585 * Convert it to a 32-bit PAR. 3586 */ 3587 if (!ret) { 3588 /* We do not set any attribute bits in the PAR */ 3589 if (res.f.lg_page_size == 24 3590 && arm_feature(env, ARM_FEATURE_V7)) { 3591 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1); 3592 } else { 3593 par64 = res.f.phys_addr & 0xfffff000; 3594 } 3595 if (!res.f.attrs.secure) { 3596 par64 |= (1 << 9); /* NS */ 3597 } 3598 } else { 3599 uint32_t fsr = arm_fi_to_sfsc(&fi); 3600 3601 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 3602 ((fsr & 0xf) << 1) | 1; 3603 } 3604 } 3605 return par64; 3606 } 3607 #endif /* CONFIG_TCG */ 3608 3609 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3610 { 3611 #ifdef CONFIG_TCG 3612 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3613 uint64_t par64; 3614 ARMMMUIdx mmu_idx; 3615 int el = arm_current_el(env); 3616 ARMSecuritySpace ss = arm_security_space(env); 3617 3618 switch (ri->opc2 & 6) { 3619 case 0: 3620 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */ 3621 switch (el) { 3622 case 3: 3623 mmu_idx = ARMMMUIdx_E3; 3624 break; 3625 case 2: 3626 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */ 3627 /* fall through */ 3628 case 1: 3629 if (ri->crm == 9 && arm_pan_enabled(env)) { 3630 mmu_idx = ARMMMUIdx_Stage1_E1_PAN; 3631 } else { 3632 mmu_idx = ARMMMUIdx_Stage1_E1; 3633 } 3634 break; 3635 default: 3636 g_assert_not_reached(); 3637 } 3638 break; 3639 case 2: 3640 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 3641 switch (el) { 3642 case 3: 3643 mmu_idx = ARMMMUIdx_E10_0; 3644 break; 3645 case 2: 3646 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */ 3647 mmu_idx = ARMMMUIdx_Stage1_E0; 3648 break; 3649 case 1: 3650 mmu_idx = ARMMMUIdx_Stage1_E0; 3651 break; 3652 default: 3653 g_assert_not_reached(); 3654 } 3655 break; 3656 case 4: 3657 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 3658 mmu_idx = ARMMMUIdx_E10_1; 3659 ss = ARMSS_NonSecure; 3660 break; 3661 case 6: 3662 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 3663 mmu_idx = ARMMMUIdx_E10_0; 3664 ss = ARMSS_NonSecure; 3665 break; 3666 default: 3667 g_assert_not_reached(); 3668 } 3669 3670 par64 = do_ats_write(env, value, access_type, mmu_idx, ss); 3671 3672 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3673 #else 3674 /* Handled by hardware accelerator. */ 3675 g_assert_not_reached(); 3676 #endif /* CONFIG_TCG */ 3677 } 3678 3679 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 3680 uint64_t value) 3681 { 3682 #ifdef CONFIG_TCG 3683 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3684 uint64_t par64; 3685 3686 /* There is no SecureEL2 for AArch32. */ 3687 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, 3688 ARMSS_NonSecure); 3689 3690 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3691 #else 3692 /* Handled by hardware accelerator. */ 3693 g_assert_not_reached(); 3694 #endif /* CONFIG_TCG */ 3695 } 3696 3697 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri, 3698 bool isread) 3699 { 3700 /* 3701 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level 3702 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can 3703 * only happen when executing at EL3 because that combination also causes an 3704 * illegal exception return. We don't need to check FEAT_RME either, because 3705 * scr_write() ensures that the NSE bit is not set otherwise. 3706 */ 3707 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) { 3708 return CP_ACCESS_TRAP; 3709 } 3710 return CP_ACCESS_OK; 3711 } 3712 3713 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 3714 bool isread) 3715 { 3716 if (arm_current_el(env) == 3 && 3717 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) { 3718 return CP_ACCESS_TRAP; 3719 } 3720 return at_e012_access(env, ri, isread); 3721 } 3722 3723 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri, 3724 bool isread) 3725 { 3726 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) { 3727 return CP_ACCESS_TRAP_EL2; 3728 } 3729 return at_e012_access(env, ri, isread); 3730 } 3731 3732 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 3733 uint64_t value) 3734 { 3735 #ifdef CONFIG_TCG 3736 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3737 ARMMMUIdx mmu_idx; 3738 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 3739 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE); 3740 3741 switch (ri->opc2 & 6) { 3742 case 0: 3743 switch (ri->opc1) { 3744 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */ 3745 if (ri->crm == 9 && arm_pan_enabled(env)) { 3746 mmu_idx = regime_e20 ? 3747 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN; 3748 } else { 3749 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1; 3750 } 3751 break; 3752 case 4: /* AT S1E2R, AT S1E2W */ 3753 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2; 3754 break; 3755 case 6: /* AT S1E3R, AT S1E3W */ 3756 mmu_idx = ARMMMUIdx_E3; 3757 break; 3758 default: 3759 g_assert_not_reached(); 3760 } 3761 break; 3762 case 2: /* AT S1E0R, AT S1E0W */ 3763 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0; 3764 break; 3765 case 4: /* AT S12E1R, AT S12E1W */ 3766 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1; 3767 break; 3768 case 6: /* AT S12E0R, AT S12E0W */ 3769 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0; 3770 break; 3771 default: 3772 g_assert_not_reached(); 3773 } 3774 3775 env->cp15.par_el[1] = do_ats_write(env, value, access_type, 3776 mmu_idx, arm_security_space(env)); 3777 #else 3778 /* Handled by hardware accelerator. */ 3779 g_assert_not_reached(); 3780 #endif /* CONFIG_TCG */ 3781 } 3782 #endif 3783 3784 /* Return basic MPU access permission bits. */ 3785 static uint32_t simple_mpu_ap_bits(uint32_t val) 3786 { 3787 uint32_t ret; 3788 uint32_t mask; 3789 int i; 3790 ret = 0; 3791 mask = 3; 3792 for (i = 0; i < 16; i += 2) { 3793 ret |= (val >> i) & mask; 3794 mask <<= 2; 3795 } 3796 return ret; 3797 } 3798 3799 /* Pad basic MPU access permission bits to extended format. */ 3800 static uint32_t extended_mpu_ap_bits(uint32_t val) 3801 { 3802 uint32_t ret; 3803 uint32_t mask; 3804 int i; 3805 ret = 0; 3806 mask = 3; 3807 for (i = 0; i < 16; i += 2) { 3808 ret |= (val & mask) << i; 3809 mask <<= 2; 3810 } 3811 return ret; 3812 } 3813 3814 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3815 uint64_t value) 3816 { 3817 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 3818 } 3819 3820 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3821 { 3822 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 3823 } 3824 3825 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3826 uint64_t value) 3827 { 3828 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 3829 } 3830 3831 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3832 { 3833 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 3834 } 3835 3836 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 3837 { 3838 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3839 3840 if (!u32p) { 3841 return 0; 3842 } 3843 3844 u32p += env->pmsav7.rnr[M_REG_NS]; 3845 return *u32p; 3846 } 3847 3848 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 3849 uint64_t value) 3850 { 3851 ARMCPU *cpu = env_archcpu(env); 3852 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3853 3854 if (!u32p) { 3855 return; 3856 } 3857 3858 u32p += env->pmsav7.rnr[M_REG_NS]; 3859 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3860 *u32p = value; 3861 } 3862 3863 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3864 uint64_t value) 3865 { 3866 ARMCPU *cpu = env_archcpu(env); 3867 uint32_t nrgs = cpu->pmsav7_dregion; 3868 3869 if (value >= nrgs) { 3870 qemu_log_mask(LOG_GUEST_ERROR, 3871 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 3872 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 3873 return; 3874 } 3875 3876 raw_write(env, ri, value); 3877 } 3878 3879 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3880 uint64_t value) 3881 { 3882 ARMCPU *cpu = env_archcpu(env); 3883 3884 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3885 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value; 3886 } 3887 3888 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3889 { 3890 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]]; 3891 } 3892 3893 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3894 uint64_t value) 3895 { 3896 ARMCPU *cpu = env_archcpu(env); 3897 3898 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3899 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value; 3900 } 3901 3902 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3903 { 3904 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]]; 3905 } 3906 3907 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3908 uint64_t value) 3909 { 3910 ARMCPU *cpu = env_archcpu(env); 3911 3912 /* 3913 * Ignore writes that would select not implemented region. 3914 * This is architecturally UNPREDICTABLE. 3915 */ 3916 if (value >= cpu->pmsav7_dregion) { 3917 return; 3918 } 3919 3920 env->pmsav7.rnr[M_REG_NS] = value; 3921 } 3922 3923 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3924 uint64_t value) 3925 { 3926 ARMCPU *cpu = env_archcpu(env); 3927 3928 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3929 env->pmsav8.hprbar[env->pmsav8.hprselr] = value; 3930 } 3931 3932 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3933 { 3934 return env->pmsav8.hprbar[env->pmsav8.hprselr]; 3935 } 3936 3937 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3938 uint64_t value) 3939 { 3940 ARMCPU *cpu = env_archcpu(env); 3941 3942 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3943 env->pmsav8.hprlar[env->pmsav8.hprselr] = value; 3944 } 3945 3946 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3947 { 3948 return env->pmsav8.hprlar[env->pmsav8.hprselr]; 3949 } 3950 3951 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3952 uint64_t value) 3953 { 3954 uint32_t n; 3955 uint32_t bit; 3956 ARMCPU *cpu = env_archcpu(env); 3957 3958 /* Ignore writes to unimplemented regions */ 3959 int rmax = MIN(cpu->pmsav8r_hdregion, 32); 3960 value &= MAKE_64BIT_MASK(0, rmax); 3961 3962 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3963 3964 /* Register alias is only valid for first 32 indexes */ 3965 for (n = 0; n < rmax; ++n) { 3966 bit = extract32(value, n, 1); 3967 env->pmsav8.hprlar[n] = deposit32( 3968 env->pmsav8.hprlar[n], 0, 1, bit); 3969 } 3970 } 3971 3972 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3973 { 3974 uint32_t n; 3975 uint32_t result = 0x0; 3976 ARMCPU *cpu = env_archcpu(env); 3977 3978 /* Register alias is only valid for first 32 indexes */ 3979 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) { 3980 if (env->pmsav8.hprlar[n] & 0x1) { 3981 result |= (0x1 << n); 3982 } 3983 } 3984 return result; 3985 } 3986 3987 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3988 uint64_t value) 3989 { 3990 ARMCPU *cpu = env_archcpu(env); 3991 3992 /* 3993 * Ignore writes that would select not implemented region. 3994 * This is architecturally UNPREDICTABLE. 3995 */ 3996 if (value >= cpu->pmsav8r_hdregion) { 3997 return; 3998 } 3999 4000 env->pmsav8.hprselr = value; 4001 } 4002 4003 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri, 4004 uint64_t value) 4005 { 4006 ARMCPU *cpu = env_archcpu(env); 4007 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) | 4008 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1); 4009 4010 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 4011 4012 if (ri->opc1 & 4) { 4013 if (index >= cpu->pmsav8r_hdregion) { 4014 return; 4015 } 4016 if (ri->opc2 & 0x1) { 4017 env->pmsav8.hprlar[index] = value; 4018 } else { 4019 env->pmsav8.hprbar[index] = value; 4020 } 4021 } else { 4022 if (index >= cpu->pmsav7_dregion) { 4023 return; 4024 } 4025 if (ri->opc2 & 0x1) { 4026 env->pmsav8.rlar[M_REG_NS][index] = value; 4027 } else { 4028 env->pmsav8.rbar[M_REG_NS][index] = value; 4029 } 4030 } 4031 } 4032 4033 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri) 4034 { 4035 ARMCPU *cpu = env_archcpu(env); 4036 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) | 4037 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1); 4038 4039 if (ri->opc1 & 4) { 4040 if (index >= cpu->pmsav8r_hdregion) { 4041 return 0x0; 4042 } 4043 if (ri->opc2 & 0x1) { 4044 return env->pmsav8.hprlar[index]; 4045 } else { 4046 return env->pmsav8.hprbar[index]; 4047 } 4048 } else { 4049 if (index >= cpu->pmsav7_dregion) { 4050 return 0x0; 4051 } 4052 if (ri->opc2 & 0x1) { 4053 return env->pmsav8.rlar[M_REG_NS][index]; 4054 } else { 4055 return env->pmsav8.rbar[M_REG_NS][index]; 4056 } 4057 } 4058 } 4059 4060 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = { 4061 { .name = "PRBAR", 4062 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0, 4063 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4064 .accessfn = access_tvm_trvm, 4065 .readfn = prbar_read, .writefn = prbar_write }, 4066 { .name = "PRLAR", 4067 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1, 4068 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4069 .accessfn = access_tvm_trvm, 4070 .readfn = prlar_read, .writefn = prlar_write }, 4071 { .name = "PRSELR", .resetvalue = 0, 4072 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1, 4073 .access = PL1_RW, .accessfn = access_tvm_trvm, 4074 .writefn = prselr_write, 4075 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) }, 4076 { .name = "HPRBAR", .resetvalue = 0, 4077 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0, 4078 .access = PL2_RW, .type = ARM_CP_NO_RAW, 4079 .readfn = hprbar_read, .writefn = hprbar_write }, 4080 { .name = "HPRLAR", 4081 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1, 4082 .access = PL2_RW, .type = ARM_CP_NO_RAW, 4083 .readfn = hprlar_read, .writefn = hprlar_write }, 4084 { .name = "HPRSELR", .resetvalue = 0, 4085 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1, 4086 .access = PL2_RW, 4087 .writefn = hprselr_write, 4088 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) }, 4089 { .name = "HPRENR", 4090 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1, 4091 .access = PL2_RW, .type = ARM_CP_NO_RAW, 4092 .readfn = hprenr_read, .writefn = hprenr_write }, 4093 }; 4094 4095 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 4096 /* 4097 * Reset for all these registers is handled in arm_cpu_reset(), 4098 * because the PMSAv7 is also used by M-profile CPUs, which do 4099 * not register cpregs but still need the state to be reset. 4100 */ 4101 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 4102 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4103 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 4104 .readfn = pmsav7_read, .writefn = pmsav7_write, 4105 .resetfn = arm_cp_reset_ignore }, 4106 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 4107 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4108 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 4109 .readfn = pmsav7_read, .writefn = pmsav7_write, 4110 .resetfn = arm_cp_reset_ignore }, 4111 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 4112 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4113 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 4114 .readfn = pmsav7_read, .writefn = pmsav7_write, 4115 .resetfn = arm_cp_reset_ignore }, 4116 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 4117 .access = PL1_RW, 4118 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 4119 .writefn = pmsav7_rgnr_write, 4120 .resetfn = arm_cp_reset_ignore }, 4121 }; 4122 4123 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 4124 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 4125 .access = PL1_RW, .type = ARM_CP_ALIAS, 4126 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 4127 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 4128 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 4129 .access = PL1_RW, .type = ARM_CP_ALIAS, 4130 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 4131 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 4132 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 4133 .access = PL1_RW, 4134 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 4135 .resetvalue = 0, }, 4136 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 4137 .access = PL1_RW, 4138 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 4139 .resetvalue = 0, }, 4140 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 4141 .access = PL1_RW, 4142 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 4143 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 4144 .access = PL1_RW, 4145 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 4146 /* Protection region base and size registers */ 4147 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 4148 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4149 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 4150 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 4151 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4152 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 4153 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 4154 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4155 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 4156 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 4157 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4158 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 4159 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 4160 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4161 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 4162 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 4163 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4164 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 4165 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 4166 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4167 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 4168 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 4169 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4170 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 4171 }; 4172 4173 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4174 uint64_t value) 4175 { 4176 ARMCPU *cpu = env_archcpu(env); 4177 4178 if (!arm_feature(env, ARM_FEATURE_V8)) { 4179 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 4180 /* 4181 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 4182 * using Long-descriptor translation table format 4183 */ 4184 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 4185 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 4186 /* 4187 * In an implementation that includes the Security Extensions 4188 * TTBCR has additional fields PD0 [4] and PD1 [5] for 4189 * Short-descriptor translation table format. 4190 */ 4191 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 4192 } else { 4193 value &= TTBCR_N; 4194 } 4195 } 4196 4197 if (arm_feature(env, ARM_FEATURE_LPAE)) { 4198 /* 4199 * With LPAE the TTBCR could result in a change of ASID 4200 * via the TTBCR.A1 bit, so do a TLB flush. 4201 */ 4202 tlb_flush(CPU(cpu)); 4203 } 4204 raw_write(env, ri, value); 4205 } 4206 4207 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri, 4208 uint64_t value) 4209 { 4210 ARMCPU *cpu = env_archcpu(env); 4211 4212 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 4213 tlb_flush(CPU(cpu)); 4214 raw_write(env, ri, value); 4215 } 4216 4217 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4218 uint64_t value) 4219 { 4220 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */ 4221 if (cpreg_field_is_64bit(ri) && 4222 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 4223 ARMCPU *cpu = env_archcpu(env); 4224 tlb_flush(CPU(cpu)); 4225 } 4226 raw_write(env, ri, value); 4227 } 4228 4229 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4230 uint64_t value) 4231 { 4232 /* 4233 * If we are running with E2&0 regime, then an ASID is active. 4234 * Flush if that might be changing. Note we're not checking 4235 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that 4236 * holds the active ASID, only checking the field that might. 4237 */ 4238 if (extract64(raw_read(env, ri) ^ value, 48, 16) && 4239 (arm_hcr_el2_eff(env) & HCR_E2H)) { 4240 uint16_t mask = ARMMMUIdxBit_E20_2 | 4241 ARMMMUIdxBit_E20_2_PAN | 4242 ARMMMUIdxBit_E20_0; 4243 tlb_flush_by_mmuidx(env_cpu(env), mask); 4244 } 4245 raw_write(env, ri, value); 4246 } 4247 4248 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4249 uint64_t value) 4250 { 4251 ARMCPU *cpu = env_archcpu(env); 4252 CPUState *cs = CPU(cpu); 4253 4254 /* 4255 * A change in VMID to the stage2 page table (Stage2) invalidates 4256 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0). 4257 */ 4258 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 4259 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env)); 4260 } 4261 raw_write(env, ri, value); 4262 } 4263 4264 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 4265 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 4266 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS, 4267 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 4268 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 4269 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 4270 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 4271 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 4272 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 4273 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 4274 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 4275 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 4276 offsetof(CPUARMState, cp15.dfar_ns) } }, 4277 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 4278 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 4279 .access = PL1_RW, .accessfn = access_tvm_trvm, 4280 .fgt = FGT_FAR_EL1, 4281 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 4282 .resetvalue = 0, }, 4283 }; 4284 4285 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 4286 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 4287 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 4288 .access = PL1_RW, .accessfn = access_tvm_trvm, 4289 .fgt = FGT_ESR_EL1, 4290 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 4291 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 4292 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 4293 .access = PL1_RW, .accessfn = access_tvm_trvm, 4294 .fgt = FGT_TTBR0_EL1, 4295 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write, 4296 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 4297 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 4298 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 4299 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 4300 .access = PL1_RW, .accessfn = access_tvm_trvm, 4301 .fgt = FGT_TTBR1_EL1, 4302 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write, 4303 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 4304 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 4305 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 4306 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 4307 .access = PL1_RW, .accessfn = access_tvm_trvm, 4308 .fgt = FGT_TCR_EL1, 4309 .writefn = vmsa_tcr_el12_write, 4310 .raw_writefn = raw_write, 4311 .resetvalue = 0, 4312 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 4313 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 4314 .access = PL1_RW, .accessfn = access_tvm_trvm, 4315 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 4316 .raw_writefn = raw_write, 4317 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 4318 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 4319 }; 4320 4321 /* 4322 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing 4323 * qemu tlbs nor adjusting cached masks. 4324 */ 4325 static const ARMCPRegInfo ttbcr2_reginfo = { 4326 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3, 4327 .access = PL1_RW, .accessfn = access_tvm_trvm, 4328 .type = ARM_CP_ALIAS, 4329 .bank_fieldoffsets = { 4330 offsetofhigh32(CPUARMState, cp15.tcr_el[3]), 4331 offsetofhigh32(CPUARMState, cp15.tcr_el[1]), 4332 }, 4333 }; 4334 4335 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 4336 uint64_t value) 4337 { 4338 env->cp15.c15_ticonfig = value & 0xe7; 4339 /* The OS_TYPE bit in this register changes the reported CPUID! */ 4340 env->cp15.c0_cpuid = (value & (1 << 5)) ? 4341 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 4342 } 4343 4344 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 4345 uint64_t value) 4346 { 4347 env->cp15.c15_threadid = value & 0xffff; 4348 } 4349 4350 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 4351 uint64_t value) 4352 { 4353 /* Wait-for-interrupt (deprecated) */ 4354 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT); 4355 } 4356 4357 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 4358 uint64_t value) 4359 { 4360 /* 4361 * On OMAP there are registers indicating the max/min index of dcache lines 4362 * containing a dirty line; cache flush operations have to reset these. 4363 */ 4364 env->cp15.c15_i_max = 0x000; 4365 env->cp15.c15_i_min = 0xff0; 4366 } 4367 4368 static const ARMCPRegInfo omap_cp_reginfo[] = { 4369 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 4370 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 4371 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 4372 .resetvalue = 0, }, 4373 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 4374 .access = PL1_RW, .type = ARM_CP_NOP }, 4375 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 4376 .access = PL1_RW, 4377 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 4378 .writefn = omap_ticonfig_write }, 4379 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 4380 .access = PL1_RW, 4381 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 4382 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 4383 .access = PL1_RW, .resetvalue = 0xff0, 4384 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 4385 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 4386 .access = PL1_RW, 4387 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 4388 .writefn = omap_threadid_write }, 4389 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 4390 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 4391 .type = ARM_CP_NO_RAW, 4392 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 4393 /* 4394 * TODO: Peripheral port remap register: 4395 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 4396 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 4397 * when MMU is off. 4398 */ 4399 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 4400 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 4401 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 4402 .writefn = omap_cachemaint_write }, 4403 { .name = "C9", .cp = 15, .crn = 9, 4404 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 4405 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 4406 }; 4407 4408 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 4409 uint64_t value) 4410 { 4411 env->cp15.c15_cpar = value & 0x3fff; 4412 } 4413 4414 static const ARMCPRegInfo xscale_cp_reginfo[] = { 4415 { .name = "XSCALE_CPAR", 4416 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 4417 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 4418 .writefn = xscale_cpar_write, }, 4419 { .name = "XSCALE_AUXCR", 4420 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 4421 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 4422 .resetvalue = 0, }, 4423 /* 4424 * XScale specific cache-lockdown: since we have no cache we NOP these 4425 * and hope the guest does not really rely on cache behaviour. 4426 */ 4427 { .name = "XSCALE_LOCK_ICACHE_LINE", 4428 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 4429 .access = PL1_W, .type = ARM_CP_NOP }, 4430 { .name = "XSCALE_UNLOCK_ICACHE", 4431 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 4432 .access = PL1_W, .type = ARM_CP_NOP }, 4433 { .name = "XSCALE_DCACHE_LOCK", 4434 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 4435 .access = PL1_RW, .type = ARM_CP_NOP }, 4436 { .name = "XSCALE_UNLOCK_DCACHE", 4437 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 4438 .access = PL1_W, .type = ARM_CP_NOP }, 4439 }; 4440 4441 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 4442 /* 4443 * RAZ/WI the whole crn=15 space, when we don't have a more specific 4444 * implementation of this implementation-defined space. 4445 * Ideally this should eventually disappear in favour of actually 4446 * implementing the correct behaviour for all cores. 4447 */ 4448 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 4449 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 4450 .access = PL1_RW, 4451 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 4452 .resetvalue = 0 }, 4453 }; 4454 4455 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 4456 /* Cache status: RAZ because we have no cache so it's always clean */ 4457 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 4458 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4459 .resetvalue = 0 }, 4460 }; 4461 4462 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 4463 /* We never have a block transfer operation in progress */ 4464 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 4465 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4466 .resetvalue = 0 }, 4467 /* The cache ops themselves: these all NOP for QEMU */ 4468 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 4469 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4470 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 4471 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4472 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 4473 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4474 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 4475 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4476 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 4477 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4478 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 4479 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4480 }; 4481 4482 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 4483 /* 4484 * The cache test-and-clean instructions always return (1 << 30) 4485 * to indicate that there are no dirty cache lines. 4486 */ 4487 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 4488 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4489 .resetvalue = (1 << 30) }, 4490 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 4491 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4492 .resetvalue = (1 << 30) }, 4493 }; 4494 4495 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 4496 /* Ignore ReadBuffer accesses */ 4497 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 4498 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 4499 .access = PL1_RW, .resetvalue = 0, 4500 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 4501 }; 4502 4503 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4504 { 4505 unsigned int cur_el = arm_current_el(env); 4506 4507 if (arm_is_el2_enabled(env) && cur_el == 1) { 4508 return env->cp15.vpidr_el2; 4509 } 4510 return raw_read(env, ri); 4511 } 4512 4513 static uint64_t mpidr_read_val(CPUARMState *env) 4514 { 4515 ARMCPU *cpu = env_archcpu(env); 4516 uint64_t mpidr = cpu->mp_affinity; 4517 4518 if (arm_feature(env, ARM_FEATURE_V7MP)) { 4519 mpidr |= (1U << 31); 4520 /* 4521 * Cores which are uniprocessor (non-coherent) 4522 * but still implement the MP extensions set 4523 * bit 30. (For instance, Cortex-R5). 4524 */ 4525 if (cpu->mp_is_up) { 4526 mpidr |= (1u << 30); 4527 } 4528 } 4529 return mpidr; 4530 } 4531 4532 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4533 { 4534 unsigned int cur_el = arm_current_el(env); 4535 4536 if (arm_is_el2_enabled(env) && cur_el == 1) { 4537 return env->cp15.vmpidr_el2; 4538 } 4539 return mpidr_read_val(env); 4540 } 4541 4542 static const ARMCPRegInfo lpae_cp_reginfo[] = { 4543 /* NOP AMAIR0/1 */ 4544 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 4545 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 4546 .access = PL1_RW, .accessfn = access_tvm_trvm, 4547 .fgt = FGT_AMAIR_EL1, 4548 .type = ARM_CP_CONST, .resetvalue = 0 }, 4549 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 4550 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 4551 .access = PL1_RW, .accessfn = access_tvm_trvm, 4552 .type = ARM_CP_CONST, .resetvalue = 0 }, 4553 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 4554 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 4555 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 4556 offsetof(CPUARMState, cp15.par_ns)} }, 4557 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 4558 .access = PL1_RW, .accessfn = access_tvm_trvm, 4559 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4560 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 4561 offsetof(CPUARMState, cp15.ttbr0_ns) }, 4562 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write }, 4563 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 4564 .access = PL1_RW, .accessfn = access_tvm_trvm, 4565 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4566 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 4567 offsetof(CPUARMState, cp15.ttbr1_ns) }, 4568 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write }, 4569 }; 4570 4571 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4572 { 4573 return vfp_get_fpcr(env); 4574 } 4575 4576 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4577 uint64_t value) 4578 { 4579 vfp_set_fpcr(env, value); 4580 } 4581 4582 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4583 { 4584 return vfp_get_fpsr(env); 4585 } 4586 4587 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4588 uint64_t value) 4589 { 4590 vfp_set_fpsr(env, value); 4591 } 4592 4593 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 4594 bool isread) 4595 { 4596 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) { 4597 return CP_ACCESS_TRAP; 4598 } 4599 return CP_ACCESS_OK; 4600 } 4601 4602 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 4603 uint64_t value) 4604 { 4605 env->daif = value & PSTATE_DAIF; 4606 } 4607 4608 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri) 4609 { 4610 return env->pstate & PSTATE_PAN; 4611 } 4612 4613 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri, 4614 uint64_t value) 4615 { 4616 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN); 4617 } 4618 4619 static const ARMCPRegInfo pan_reginfo = { 4620 .name = "PAN", .state = ARM_CP_STATE_AA64, 4621 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3, 4622 .type = ARM_CP_NO_RAW, .access = PL1_RW, 4623 .readfn = aa64_pan_read, .writefn = aa64_pan_write 4624 }; 4625 4626 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri) 4627 { 4628 return env->pstate & PSTATE_UAO; 4629 } 4630 4631 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri, 4632 uint64_t value) 4633 { 4634 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO); 4635 } 4636 4637 static const ARMCPRegInfo uao_reginfo = { 4638 .name = "UAO", .state = ARM_CP_STATE_AA64, 4639 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4, 4640 .type = ARM_CP_NO_RAW, .access = PL1_RW, 4641 .readfn = aa64_uao_read, .writefn = aa64_uao_write 4642 }; 4643 4644 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri) 4645 { 4646 return env->pstate & PSTATE_DIT; 4647 } 4648 4649 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri, 4650 uint64_t value) 4651 { 4652 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT); 4653 } 4654 4655 static const ARMCPRegInfo dit_reginfo = { 4656 .name = "DIT", .state = ARM_CP_STATE_AA64, 4657 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5, 4658 .type = ARM_CP_NO_RAW, .access = PL0_RW, 4659 .readfn = aa64_dit_read, .writefn = aa64_dit_write 4660 }; 4661 4662 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri) 4663 { 4664 return env->pstate & PSTATE_SSBS; 4665 } 4666 4667 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri, 4668 uint64_t value) 4669 { 4670 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS); 4671 } 4672 4673 static const ARMCPRegInfo ssbs_reginfo = { 4674 .name = "SSBS", .state = ARM_CP_STATE_AA64, 4675 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6, 4676 .type = ARM_CP_NO_RAW, .access = PL0_RW, 4677 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write 4678 }; 4679 4680 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env, 4681 const ARMCPRegInfo *ri, 4682 bool isread) 4683 { 4684 /* Cache invalidate/clean to Point of Coherency or Persistence... */ 4685 switch (arm_current_el(env)) { 4686 case 0: 4687 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */ 4688 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) { 4689 return CP_ACCESS_TRAP; 4690 } 4691 /* fall through */ 4692 case 1: 4693 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */ 4694 if (arm_hcr_el2_eff(env) & HCR_TPCP) { 4695 return CP_ACCESS_TRAP_EL2; 4696 } 4697 break; 4698 } 4699 return CP_ACCESS_OK; 4700 } 4701 4702 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags) 4703 { 4704 /* Cache invalidate/clean to Point of Unification... */ 4705 switch (arm_current_el(env)) { 4706 case 0: 4707 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */ 4708 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) { 4709 return CP_ACCESS_TRAP; 4710 } 4711 /* fall through */ 4712 case 1: 4713 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */ 4714 if (arm_hcr_el2_eff(env) & hcrflags) { 4715 return CP_ACCESS_TRAP_EL2; 4716 } 4717 break; 4718 } 4719 return CP_ACCESS_OK; 4720 } 4721 4722 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri, 4723 bool isread) 4724 { 4725 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU); 4726 } 4727 4728 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri, 4729 bool isread) 4730 { 4731 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU); 4732 } 4733 4734 /* 4735 * See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions 4736 * Page D4-1736 (DDI0487A.b) 4737 */ 4738 4739 static int vae1_tlbmask(CPUARMState *env) 4740 { 4741 uint64_t hcr = arm_hcr_el2_eff(env); 4742 uint16_t mask; 4743 4744 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 4745 mask = ARMMMUIdxBit_E20_2 | 4746 ARMMMUIdxBit_E20_2_PAN | 4747 ARMMMUIdxBit_E20_0; 4748 } else { 4749 mask = ARMMMUIdxBit_E10_1 | 4750 ARMMMUIdxBit_E10_1_PAN | 4751 ARMMMUIdxBit_E10_0; 4752 } 4753 return mask; 4754 } 4755 4756 static int vae2_tlbmask(CPUARMState *env) 4757 { 4758 uint64_t hcr = arm_hcr_el2_eff(env); 4759 uint16_t mask; 4760 4761 if (hcr & HCR_E2H) { 4762 mask = ARMMMUIdxBit_E20_2 | 4763 ARMMMUIdxBit_E20_2_PAN | 4764 ARMMMUIdxBit_E20_0; 4765 } else { 4766 mask = ARMMMUIdxBit_E2; 4767 } 4768 return mask; 4769 } 4770 4771 /* Return 56 if TBI is enabled, 64 otherwise. */ 4772 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx, 4773 uint64_t addr) 4774 { 4775 uint64_t tcr = regime_tcr(env, mmu_idx); 4776 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx); 4777 int select = extract64(addr, 55, 1); 4778 4779 return (tbi >> select) & 1 ? 56 : 64; 4780 } 4781 4782 static int vae1_tlbbits(CPUARMState *env, uint64_t addr) 4783 { 4784 uint64_t hcr = arm_hcr_el2_eff(env); 4785 ARMMMUIdx mmu_idx; 4786 4787 /* Only the regime of the mmu_idx below is significant. */ 4788 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 4789 mmu_idx = ARMMMUIdx_E20_0; 4790 } else { 4791 mmu_idx = ARMMMUIdx_E10_0; 4792 } 4793 4794 return tlbbits_for_regime(env, mmu_idx, addr); 4795 } 4796 4797 static int vae2_tlbbits(CPUARMState *env, uint64_t addr) 4798 { 4799 uint64_t hcr = arm_hcr_el2_eff(env); 4800 ARMMMUIdx mmu_idx; 4801 4802 /* 4803 * Only the regime of the mmu_idx below is significant. 4804 * Regime EL2&0 has two ranges with separate TBI configuration, while EL2 4805 * only has one. 4806 */ 4807 if (hcr & HCR_E2H) { 4808 mmu_idx = ARMMMUIdx_E20_2; 4809 } else { 4810 mmu_idx = ARMMMUIdx_E2; 4811 } 4812 4813 return tlbbits_for_regime(env, mmu_idx, addr); 4814 } 4815 4816 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4817 uint64_t value) 4818 { 4819 CPUState *cs = env_cpu(env); 4820 int mask = vae1_tlbmask(env); 4821 4822 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4823 } 4824 4825 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4826 uint64_t value) 4827 { 4828 CPUState *cs = env_cpu(env); 4829 int mask = vae1_tlbmask(env); 4830 4831 if (tlb_force_broadcast(env)) { 4832 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4833 } else { 4834 tlb_flush_by_mmuidx(cs, mask); 4835 } 4836 } 4837 4838 static int e2_tlbmask(CPUARMState *env) 4839 { 4840 return (ARMMMUIdxBit_E20_0 | 4841 ARMMMUIdxBit_E20_2 | 4842 ARMMMUIdxBit_E20_2_PAN | 4843 ARMMMUIdxBit_E2); 4844 } 4845 4846 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4847 uint64_t value) 4848 { 4849 CPUState *cs = env_cpu(env); 4850 int mask = alle1_tlbmask(env); 4851 4852 tlb_flush_by_mmuidx(cs, mask); 4853 } 4854 4855 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4856 uint64_t value) 4857 { 4858 CPUState *cs = env_cpu(env); 4859 int mask = e2_tlbmask(env); 4860 4861 tlb_flush_by_mmuidx(cs, mask); 4862 } 4863 4864 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, 4865 uint64_t value) 4866 { 4867 ARMCPU *cpu = env_archcpu(env); 4868 CPUState *cs = CPU(cpu); 4869 4870 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E3); 4871 } 4872 4873 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4874 uint64_t value) 4875 { 4876 CPUState *cs = env_cpu(env); 4877 int mask = alle1_tlbmask(env); 4878 4879 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4880 } 4881 4882 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4883 uint64_t value) 4884 { 4885 CPUState *cs = env_cpu(env); 4886 int mask = e2_tlbmask(env); 4887 4888 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4889 } 4890 4891 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4892 uint64_t value) 4893 { 4894 CPUState *cs = env_cpu(env); 4895 4896 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E3); 4897 } 4898 4899 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4900 uint64_t value) 4901 { 4902 /* 4903 * Invalidate by VA, EL2 4904 * Currently handles both VAE2 and VALE2, since we don't support 4905 * flush-last-level-only. 4906 */ 4907 CPUState *cs = env_cpu(env); 4908 int mask = vae2_tlbmask(env); 4909 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4910 int bits = vae2_tlbbits(env, pageaddr); 4911 4912 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits); 4913 } 4914 4915 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, 4916 uint64_t value) 4917 { 4918 /* 4919 * Invalidate by VA, EL3 4920 * Currently handles both VAE3 and VALE3, since we don't support 4921 * flush-last-level-only. 4922 */ 4923 ARMCPU *cpu = env_archcpu(env); 4924 CPUState *cs = CPU(cpu); 4925 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4926 4927 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E3); 4928 } 4929 4930 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4931 uint64_t value) 4932 { 4933 CPUState *cs = env_cpu(env); 4934 int mask = vae1_tlbmask(env); 4935 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4936 int bits = vae1_tlbbits(env, pageaddr); 4937 4938 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits); 4939 } 4940 4941 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4942 uint64_t value) 4943 { 4944 /* 4945 * Invalidate by VA, EL1&0 (AArch64 version). 4946 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, 4947 * since we don't support flush-for-specific-ASID-only or 4948 * flush-last-level-only. 4949 */ 4950 CPUState *cs = env_cpu(env); 4951 int mask = vae1_tlbmask(env); 4952 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4953 int bits = vae1_tlbbits(env, pageaddr); 4954 4955 if (tlb_force_broadcast(env)) { 4956 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits); 4957 } else { 4958 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits); 4959 } 4960 } 4961 4962 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4963 uint64_t value) 4964 { 4965 CPUState *cs = env_cpu(env); 4966 int mask = vae2_tlbmask(env); 4967 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4968 int bits = vae2_tlbbits(env, pageaddr); 4969 4970 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits); 4971 } 4972 4973 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4974 uint64_t value) 4975 { 4976 CPUState *cs = env_cpu(env); 4977 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4978 int bits = tlbbits_for_regime(env, ARMMMUIdx_E3, pageaddr); 4979 4980 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, 4981 ARMMMUIdxBit_E3, bits); 4982 } 4983 4984 static int ipas2e1_tlbmask(CPUARMState *env, int64_t value) 4985 { 4986 /* 4987 * The MSB of value is the NS field, which only applies if SEL2 4988 * is implemented and SCR_EL3.NS is not set (i.e. in secure mode). 4989 */ 4990 return (value >= 0 4991 && cpu_isar_feature(aa64_sel2, env_archcpu(env)) 4992 && arm_is_secure_below_el3(env) 4993 ? ARMMMUIdxBit_Stage2_S 4994 : ARMMMUIdxBit_Stage2); 4995 } 4996 4997 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4998 uint64_t value) 4999 { 5000 CPUState *cs = env_cpu(env); 5001 int mask = ipas2e1_tlbmask(env, value); 5002 uint64_t pageaddr = sextract64(value << 12, 0, 56); 5003 5004 if (tlb_force_broadcast(env)) { 5005 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask); 5006 } else { 5007 tlb_flush_page_by_mmuidx(cs, pageaddr, mask); 5008 } 5009 } 5010 5011 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 5012 uint64_t value) 5013 { 5014 CPUState *cs = env_cpu(env); 5015 int mask = ipas2e1_tlbmask(env, value); 5016 uint64_t pageaddr = sextract64(value << 12, 0, 56); 5017 5018 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, mask); 5019 } 5020 5021 #ifdef TARGET_AARCH64 5022 typedef struct { 5023 uint64_t base; 5024 uint64_t length; 5025 } TLBIRange; 5026 5027 static ARMGranuleSize tlbi_range_tg_to_gran_size(int tg) 5028 { 5029 /* 5030 * Note that the TLBI range TG field encoding differs from both 5031 * TG0 and TG1 encodings. 5032 */ 5033 switch (tg) { 5034 case 1: 5035 return Gran4K; 5036 case 2: 5037 return Gran16K; 5038 case 3: 5039 return Gran64K; 5040 default: 5041 return GranInvalid; 5042 } 5043 } 5044 5045 static TLBIRange tlbi_aa64_get_range(CPUARMState *env, ARMMMUIdx mmuidx, 5046 uint64_t value) 5047 { 5048 unsigned int page_size_granule, page_shift, num, scale, exponent; 5049 /* Extract one bit to represent the va selector in use. */ 5050 uint64_t select = sextract64(value, 36, 1); 5051 ARMVAParameters param = aa64_va_parameters(env, select, mmuidx, true, false); 5052 TLBIRange ret = { }; 5053 ARMGranuleSize gran; 5054 5055 page_size_granule = extract64(value, 46, 2); 5056 gran = tlbi_range_tg_to_gran_size(page_size_granule); 5057 5058 /* The granule encoded in value must match the granule in use. */ 5059 if (gran != param.gran) { 5060 qemu_log_mask(LOG_GUEST_ERROR, "Invalid tlbi page size granule %d\n", 5061 page_size_granule); 5062 return ret; 5063 } 5064 5065 page_shift = arm_granule_bits(gran); 5066 num = extract64(value, 39, 5); 5067 scale = extract64(value, 44, 2); 5068 exponent = (5 * scale) + 1; 5069 5070 ret.length = (num + 1) << (exponent + page_shift); 5071 5072 if (param.select) { 5073 ret.base = sextract64(value, 0, 37); 5074 } else { 5075 ret.base = extract64(value, 0, 37); 5076 } 5077 if (param.ds) { 5078 /* 5079 * With DS=1, BaseADDR is always shifted 16 so that it is able 5080 * to address all 52 va bits. The input address is perforce 5081 * aligned on a 64k boundary regardless of translation granule. 5082 */ 5083 page_shift = 16; 5084 } 5085 ret.base <<= page_shift; 5086 5087 return ret; 5088 } 5089 5090 static void do_rvae_write(CPUARMState *env, uint64_t value, 5091 int idxmap, bool synced) 5092 { 5093 ARMMMUIdx one_idx = ARM_MMU_IDX_A | ctz32(idxmap); 5094 TLBIRange range; 5095 int bits; 5096 5097 range = tlbi_aa64_get_range(env, one_idx, value); 5098 bits = tlbbits_for_regime(env, one_idx, range.base); 5099 5100 if (synced) { 5101 tlb_flush_range_by_mmuidx_all_cpus_synced(env_cpu(env), 5102 range.base, 5103 range.length, 5104 idxmap, 5105 bits); 5106 } else { 5107 tlb_flush_range_by_mmuidx(env_cpu(env), range.base, 5108 range.length, idxmap, bits); 5109 } 5110 } 5111 5112 static void tlbi_aa64_rvae1_write(CPUARMState *env, 5113 const ARMCPRegInfo *ri, 5114 uint64_t value) 5115 { 5116 /* 5117 * Invalidate by VA range, EL1&0. 5118 * Currently handles all of RVAE1, RVAAE1, RVAALE1 and RVALE1, 5119 * since we don't support flush-for-specific-ASID-only or 5120 * flush-last-level-only. 5121 */ 5122 5123 do_rvae_write(env, value, vae1_tlbmask(env), 5124 tlb_force_broadcast(env)); 5125 } 5126 5127 static void tlbi_aa64_rvae1is_write(CPUARMState *env, 5128 const ARMCPRegInfo *ri, 5129 uint64_t value) 5130 { 5131 /* 5132 * Invalidate by VA range, Inner/Outer Shareable EL1&0. 5133 * Currently handles all of RVAE1IS, RVAE1OS, RVAAE1IS, RVAAE1OS, 5134 * RVAALE1IS, RVAALE1OS, RVALE1IS and RVALE1OS, since we don't support 5135 * flush-for-specific-ASID-only, flush-last-level-only or inner/outer 5136 * shareable specific flushes. 5137 */ 5138 5139 do_rvae_write(env, value, vae1_tlbmask(env), true); 5140 } 5141 5142 static void tlbi_aa64_rvae2_write(CPUARMState *env, 5143 const ARMCPRegInfo *ri, 5144 uint64_t value) 5145 { 5146 /* 5147 * Invalidate by VA range, EL2. 5148 * Currently handles all of RVAE2 and RVALE2, 5149 * since we don't support flush-for-specific-ASID-only or 5150 * flush-last-level-only. 5151 */ 5152 5153 do_rvae_write(env, value, vae2_tlbmask(env), 5154 tlb_force_broadcast(env)); 5155 5156 5157 } 5158 5159 static void tlbi_aa64_rvae2is_write(CPUARMState *env, 5160 const ARMCPRegInfo *ri, 5161 uint64_t value) 5162 { 5163 /* 5164 * Invalidate by VA range, Inner/Outer Shareable, EL2. 5165 * Currently handles all of RVAE2IS, RVAE2OS, RVALE2IS and RVALE2OS, 5166 * since we don't support flush-for-specific-ASID-only, 5167 * flush-last-level-only or inner/outer shareable specific flushes. 5168 */ 5169 5170 do_rvae_write(env, value, vae2_tlbmask(env), true); 5171 5172 } 5173 5174 static void tlbi_aa64_rvae3_write(CPUARMState *env, 5175 const ARMCPRegInfo *ri, 5176 uint64_t value) 5177 { 5178 /* 5179 * Invalidate by VA range, EL3. 5180 * Currently handles all of RVAE3 and RVALE3, 5181 * since we don't support flush-for-specific-ASID-only or 5182 * flush-last-level-only. 5183 */ 5184 5185 do_rvae_write(env, value, ARMMMUIdxBit_E3, tlb_force_broadcast(env)); 5186 } 5187 5188 static void tlbi_aa64_rvae3is_write(CPUARMState *env, 5189 const ARMCPRegInfo *ri, 5190 uint64_t value) 5191 { 5192 /* 5193 * Invalidate by VA range, EL3, Inner/Outer Shareable. 5194 * Currently handles all of RVAE3IS, RVAE3OS, RVALE3IS and RVALE3OS, 5195 * since we don't support flush-for-specific-ASID-only, 5196 * flush-last-level-only or inner/outer specific flushes. 5197 */ 5198 5199 do_rvae_write(env, value, ARMMMUIdxBit_E3, true); 5200 } 5201 5202 static void tlbi_aa64_ripas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, 5203 uint64_t value) 5204 { 5205 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), 5206 tlb_force_broadcast(env)); 5207 } 5208 5209 static void tlbi_aa64_ripas2e1is_write(CPUARMState *env, 5210 const ARMCPRegInfo *ri, 5211 uint64_t value) 5212 { 5213 do_rvae_write(env, value, ipas2e1_tlbmask(env, value), true); 5214 } 5215 #endif 5216 5217 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 5218 bool isread) 5219 { 5220 int cur_el = arm_current_el(env); 5221 5222 if (cur_el < 2) { 5223 uint64_t hcr = arm_hcr_el2_eff(env); 5224 5225 if (cur_el == 0) { 5226 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 5227 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) { 5228 return CP_ACCESS_TRAP_EL2; 5229 } 5230 } else { 5231 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 5232 return CP_ACCESS_TRAP; 5233 } 5234 if (hcr & HCR_TDZ) { 5235 return CP_ACCESS_TRAP_EL2; 5236 } 5237 } 5238 } else if (hcr & HCR_TDZ) { 5239 return CP_ACCESS_TRAP_EL2; 5240 } 5241 } 5242 return CP_ACCESS_OK; 5243 } 5244 5245 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 5246 { 5247 ARMCPU *cpu = env_archcpu(env); 5248 int dzp_bit = 1 << 4; 5249 5250 /* DZP indicates whether DC ZVA access is allowed */ 5251 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 5252 dzp_bit = 0; 5253 } 5254 return cpu->dcz_blocksize | dzp_bit; 5255 } 5256 5257 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 5258 bool isread) 5259 { 5260 if (!(env->pstate & PSTATE_SP)) { 5261 /* 5262 * Access to SP_EL0 is undefined if it's being used as 5263 * the stack pointer. 5264 */ 5265 return CP_ACCESS_TRAP_UNCATEGORIZED; 5266 } 5267 return CP_ACCESS_OK; 5268 } 5269 5270 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 5271 { 5272 return env->pstate & PSTATE_SP; 5273 } 5274 5275 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 5276 { 5277 update_spsel(env, val); 5278 } 5279 5280 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5281 uint64_t value) 5282 { 5283 ARMCPU *cpu = env_archcpu(env); 5284 5285 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 5286 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 5287 value &= ~SCTLR_M; 5288 } 5289 5290 /* ??? Lots of these bits are not implemented. */ 5291 5292 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) { 5293 if (ri->opc1 == 6) { /* SCTLR_EL3 */ 5294 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA); 5295 } else { 5296 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF | 5297 SCTLR_ATA0 | SCTLR_ATA); 5298 } 5299 } 5300 5301 if (raw_read(env, ri) == value) { 5302 /* 5303 * Skip the TLB flush if nothing actually changed; Linux likes 5304 * to do a lot of pointless SCTLR writes. 5305 */ 5306 return; 5307 } 5308 5309 raw_write(env, ri, value); 5310 5311 /* This may enable/disable the MMU, so do a TLB flush. */ 5312 tlb_flush(CPU(cpu)); 5313 5314 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) { 5315 /* 5316 * Normally we would always end the TB on an SCTLR write; see the 5317 * comment in ARMCPRegInfo sctlr initialization below for why Xscale 5318 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild 5319 * of hflags from the translator, so do it here. 5320 */ 5321 arm_rebuild_hflags(env); 5322 } 5323 } 5324 5325 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri, 5326 uint64_t value) 5327 { 5328 /* 5329 * Some MDCR_EL3 bits affect whether PMU counters are running: 5330 * if we are trying to change any of those then we must 5331 * bracket this update with PMU start/finish calls. 5332 */ 5333 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS; 5334 5335 if (pmu_op) { 5336 pmu_op_start(env); 5337 } 5338 env->cp15.mdcr_el3 = value; 5339 if (pmu_op) { 5340 pmu_op_finish(env); 5341 } 5342 } 5343 5344 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5345 uint64_t value) 5346 { 5347 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */ 5348 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK); 5349 } 5350 5351 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 5352 uint64_t value) 5353 { 5354 /* 5355 * Some MDCR_EL2 bits affect whether PMU counters are running: 5356 * if we are trying to change any of those then we must 5357 * bracket this update with PMU start/finish calls. 5358 */ 5359 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS; 5360 5361 if (pmu_op) { 5362 pmu_op_start(env); 5363 } 5364 env->cp15.mdcr_el2 = value; 5365 if (pmu_op) { 5366 pmu_op_finish(env); 5367 } 5368 } 5369 5370 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri, 5371 bool isread) 5372 { 5373 if (arm_current_el(env) == 1) { 5374 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2); 5375 5376 if (hcr_nv == (HCR_NV | HCR_NV1)) { 5377 return CP_ACCESS_TRAP_EL2; 5378 } 5379 } 5380 return CP_ACCESS_OK; 5381 } 5382 5383 #ifdef CONFIG_USER_ONLY 5384 /* 5385 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their 5386 * code to get around W^X restrictions, where one region is writable and the 5387 * other is executable. 5388 * 5389 * Since the executable region is never written to we cannot detect code 5390 * changes when running in user mode, and rely on the emulated JIT telling us 5391 * that the code has changed by executing this instruction. 5392 */ 5393 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri, 5394 uint64_t value) 5395 { 5396 uint64_t icache_line_mask, start_address, end_address; 5397 const ARMCPU *cpu; 5398 5399 cpu = env_archcpu(env); 5400 5401 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1; 5402 start_address = value & ~icache_line_mask; 5403 end_address = value | icache_line_mask; 5404 5405 mmap_lock(); 5406 5407 tb_invalidate_phys_range(start_address, end_address); 5408 5409 mmap_unlock(); 5410 } 5411 #endif 5412 5413 static const ARMCPRegInfo v8_cp_reginfo[] = { 5414 /* 5415 * Minimal set of EL0-visible registers. This will need to be expanded 5416 * significantly for system emulation of AArch64 CPUs. 5417 */ 5418 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 5419 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 5420 .access = PL0_RW, .type = ARM_CP_NZCV }, 5421 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 5422 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 5423 .type = ARM_CP_NO_RAW, 5424 .access = PL0_RW, .accessfn = aa64_daif_access, 5425 .fieldoffset = offsetof(CPUARMState, daif), 5426 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 5427 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 5428 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 5429 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 5430 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 5431 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 5432 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 5433 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 5434 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 5435 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 5436 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 5437 .access = PL0_R, .type = ARM_CP_NO_RAW, 5438 .fgt = FGT_DCZID_EL0, 5439 .readfn = aa64_dczid_read }, 5440 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 5441 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 5442 .access = PL0_W, .type = ARM_CP_DC_ZVA, 5443 #ifndef CONFIG_USER_ONLY 5444 /* Avoid overhead of an access check that always passes in user-mode */ 5445 .accessfn = aa64_zva_access, 5446 .fgt = FGT_DCZVA, 5447 #endif 5448 }, 5449 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 5450 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 5451 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 5452 /* 5453 * Instruction cache ops. All of these except `IC IVAU` NOP because we 5454 * don't emulate caches. 5455 */ 5456 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 5457 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 5458 .access = PL1_W, .type = ARM_CP_NOP, 5459 .fgt = FGT_ICIALLUIS, 5460 .accessfn = access_ticab }, 5461 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 5462 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 5463 .access = PL1_W, .type = ARM_CP_NOP, 5464 .fgt = FGT_ICIALLU, 5465 .accessfn = access_tocu }, 5466 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 5467 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 5468 .access = PL0_W, 5469 .fgt = FGT_ICIVAU, 5470 .accessfn = access_tocu, 5471 #ifdef CONFIG_USER_ONLY 5472 .type = ARM_CP_NO_RAW, 5473 .writefn = ic_ivau_write 5474 #else 5475 .type = ARM_CP_NOP 5476 #endif 5477 }, 5478 /* Cache ops: all NOPs since we don't emulate caches */ 5479 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 5480 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 5481 .access = PL1_W, .accessfn = aa64_cacheop_poc_access, 5482 .fgt = FGT_DCIVAC, 5483 .type = ARM_CP_NOP }, 5484 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 5485 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 5486 .fgt = FGT_DCISW, 5487 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 5488 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 5489 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 5490 .access = PL0_W, .type = ARM_CP_NOP, 5491 .fgt = FGT_DCCVAC, 5492 .accessfn = aa64_cacheop_poc_access }, 5493 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 5494 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 5495 .fgt = FGT_DCCSW, 5496 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 5497 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 5498 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 5499 .access = PL0_W, .type = ARM_CP_NOP, 5500 .fgt = FGT_DCCVAU, 5501 .accessfn = access_tocu }, 5502 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 5503 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 5504 .access = PL0_W, .type = ARM_CP_NOP, 5505 .fgt = FGT_DCCIVAC, 5506 .accessfn = aa64_cacheop_poc_access }, 5507 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 5508 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 5509 .fgt = FGT_DCCISW, 5510 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 5511 /* TLBI operations */ 5512 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, 5513 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 5514 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 5515 .fgt = FGT_TLBIVMALLE1IS, 5516 .writefn = tlbi_aa64_vmalle1is_write }, 5517 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, 5518 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 5519 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 5520 .fgt = FGT_TLBIVAE1IS, 5521 .writefn = tlbi_aa64_vae1is_write }, 5522 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, 5523 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 5524 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 5525 .fgt = FGT_TLBIASIDE1IS, 5526 .writefn = tlbi_aa64_vmalle1is_write }, 5527 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, 5528 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 5529 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 5530 .fgt = FGT_TLBIVAAE1IS, 5531 .writefn = tlbi_aa64_vae1is_write }, 5532 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, 5533 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 5534 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 5535 .fgt = FGT_TLBIVALE1IS, 5536 .writefn = tlbi_aa64_vae1is_write }, 5537 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, 5538 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 5539 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 5540 .fgt = FGT_TLBIVAALE1IS, 5541 .writefn = tlbi_aa64_vae1is_write }, 5542 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, 5543 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 5544 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 5545 .fgt = FGT_TLBIVMALLE1, 5546 .writefn = tlbi_aa64_vmalle1_write }, 5547 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, 5548 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 5549 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 5550 .fgt = FGT_TLBIVAE1, 5551 .writefn = tlbi_aa64_vae1_write }, 5552 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, 5553 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 5554 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 5555 .fgt = FGT_TLBIASIDE1, 5556 .writefn = tlbi_aa64_vmalle1_write }, 5557 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, 5558 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 5559 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 5560 .fgt = FGT_TLBIVAAE1, 5561 .writefn = tlbi_aa64_vae1_write }, 5562 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, 5563 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 5564 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 5565 .fgt = FGT_TLBIVALE1, 5566 .writefn = tlbi_aa64_vae1_write }, 5567 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, 5568 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 5569 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 5570 .fgt = FGT_TLBIVAALE1, 5571 .writefn = tlbi_aa64_vae1_write }, 5572 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, 5573 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 5574 .access = PL2_W, .type = ARM_CP_NO_RAW, 5575 .writefn = tlbi_aa64_ipas2e1is_write }, 5576 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, 5577 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 5578 .access = PL2_W, .type = ARM_CP_NO_RAW, 5579 .writefn = tlbi_aa64_ipas2e1is_write }, 5580 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, 5581 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 5582 .access = PL2_W, .type = ARM_CP_NO_RAW, 5583 .writefn = tlbi_aa64_alle1is_write }, 5584 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, 5585 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, 5586 .access = PL2_W, .type = ARM_CP_NO_RAW, 5587 .writefn = tlbi_aa64_alle1is_write }, 5588 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, 5589 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 5590 .access = PL2_W, .type = ARM_CP_NO_RAW, 5591 .writefn = tlbi_aa64_ipas2e1_write }, 5592 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, 5593 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 5594 .access = PL2_W, .type = ARM_CP_NO_RAW, 5595 .writefn = tlbi_aa64_ipas2e1_write }, 5596 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, 5597 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 5598 .access = PL2_W, .type = ARM_CP_NO_RAW, 5599 .writefn = tlbi_aa64_alle1_write }, 5600 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, 5601 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, 5602 .access = PL2_W, .type = ARM_CP_NO_RAW, 5603 .writefn = tlbi_aa64_alle1is_write }, 5604 #ifndef CONFIG_USER_ONLY 5605 /* 64 bit address translation operations */ 5606 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 5607 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 5608 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5609 .fgt = FGT_ATS1E1R, 5610 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5611 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 5612 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 5613 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5614 .fgt = FGT_ATS1E1W, 5615 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5616 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 5617 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 5618 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5619 .fgt = FGT_ATS1E0R, 5620 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5621 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 5622 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 5623 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5624 .fgt = FGT_ATS1E0W, 5625 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5626 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 5627 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 5628 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5629 .accessfn = at_e012_access, .writefn = ats_write64 }, 5630 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 5631 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 5632 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5633 .accessfn = at_e012_access, .writefn = ats_write64 }, 5634 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 5635 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 5636 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5637 .accessfn = at_e012_access, .writefn = ats_write64 }, 5638 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 5639 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 5640 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5641 .accessfn = at_e012_access, .writefn = ats_write64 }, 5642 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 5643 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 5644 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 5645 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5646 .writefn = ats_write64 }, 5647 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 5648 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 5649 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5650 .writefn = ats_write64 }, 5651 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 5652 .type = ARM_CP_ALIAS, 5653 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 5654 .access = PL1_RW, .resetvalue = 0, 5655 .fgt = FGT_PAR_EL1, 5656 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 5657 .writefn = par_write }, 5658 #endif 5659 /* TLB invalidate last level of translation table walk */ 5660 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 5661 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis, 5662 .writefn = tlbimva_is_write }, 5663 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 5664 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlbis, 5665 .writefn = tlbimvaa_is_write }, 5666 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 5667 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 5668 .writefn = tlbimva_write }, 5669 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 5670 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 5671 .writefn = tlbimvaa_write }, 5672 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 5673 .type = ARM_CP_NO_RAW, .access = PL2_W, 5674 .writefn = tlbimva_hyp_write }, 5675 { .name = "TLBIMVALHIS", 5676 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 5677 .type = ARM_CP_NO_RAW, .access = PL2_W, 5678 .writefn = tlbimva_hyp_is_write }, 5679 { .name = "TLBIIPAS2", 5680 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 5681 .type = ARM_CP_NO_RAW, .access = PL2_W, 5682 .writefn = tlbiipas2_hyp_write }, 5683 { .name = "TLBIIPAS2IS", 5684 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 5685 .type = ARM_CP_NO_RAW, .access = PL2_W, 5686 .writefn = tlbiipas2is_hyp_write }, 5687 { .name = "TLBIIPAS2L", 5688 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 5689 .type = ARM_CP_NO_RAW, .access = PL2_W, 5690 .writefn = tlbiipas2_hyp_write }, 5691 { .name = "TLBIIPAS2LIS", 5692 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 5693 .type = ARM_CP_NO_RAW, .access = PL2_W, 5694 .writefn = tlbiipas2is_hyp_write }, 5695 /* 32 bit cache operations */ 5696 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 5697 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab }, 5698 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 5699 .type = ARM_CP_NOP, .access = PL1_W }, 5700 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 5701 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, 5702 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 5703 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, 5704 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 5705 .type = ARM_CP_NOP, .access = PL1_W }, 5706 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 5707 .type = ARM_CP_NOP, .access = PL1_W }, 5708 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 5709 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5710 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 5711 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5712 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 5713 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5714 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 5715 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5716 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 5717 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, 5718 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 5719 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5720 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 5721 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5722 /* MMU Domain access control / MPU write buffer control */ 5723 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 5724 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 5725 .writefn = dacr_write, .raw_writefn = raw_write, 5726 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 5727 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 5728 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 5729 .type = ARM_CP_ALIAS, 5730 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 5731 .access = PL1_RW, .accessfn = access_nv1, 5732 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 5733 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 5734 .type = ARM_CP_ALIAS, 5735 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 5736 .access = PL1_RW, .accessfn = access_nv1, 5737 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 5738 /* 5739 * We rely on the access checks not allowing the guest to write to the 5740 * state field when SPSel indicates that it's being used as the stack 5741 * pointer. 5742 */ 5743 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 5744 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 5745 .access = PL1_RW, .accessfn = sp_el0_access, 5746 .type = ARM_CP_ALIAS, 5747 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 5748 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 5749 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 5750 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP, 5751 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 5752 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 5753 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 5754 .type = ARM_CP_NO_RAW, 5755 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 5756 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 5757 .type = ARM_CP_ALIAS, 5758 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 5759 .access = PL2_RW, 5760 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 5761 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 5762 .type = ARM_CP_ALIAS, 5763 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 5764 .access = PL2_RW, 5765 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 5766 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 5767 .type = ARM_CP_ALIAS, 5768 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 5769 .access = PL2_RW, 5770 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 5771 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 5772 .type = ARM_CP_ALIAS, 5773 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 5774 .access = PL2_RW, 5775 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 5776 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 5777 .type = ARM_CP_IO, 5778 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 5779 .resetvalue = 0, 5780 .access = PL3_RW, 5781 .writefn = mdcr_el3_write, 5782 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 5783 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO, 5784 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 5785 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5786 .writefn = sdcr_write, 5787 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 5788 }; 5789 5790 /* These are present only when EL1 supports AArch32 */ 5791 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = { 5792 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 5793 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 5794 .access = PL2_RW, 5795 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP, 5796 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) }, 5797 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 5798 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 5799 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP, 5800 .writefn = dacr_write, .raw_writefn = raw_write, 5801 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 5802 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 5803 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 5804 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP, 5805 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 5806 }; 5807 5808 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask) 5809 { 5810 ARMCPU *cpu = env_archcpu(env); 5811 5812 if (arm_feature(env, ARM_FEATURE_V8)) { 5813 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */ 5814 } else { 5815 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */ 5816 } 5817 5818 if (arm_feature(env, ARM_FEATURE_EL3)) { 5819 valid_mask &= ~HCR_HCD; 5820 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 5821 /* 5822 * Architecturally HCR.TSC is RES0 if EL3 is not implemented. 5823 * However, if we're using the SMC PSCI conduit then QEMU is 5824 * effectively acting like EL3 firmware and so the guest at 5825 * EL2 should retain the ability to prevent EL1 from being 5826 * able to make SMC calls into the ersatz firmware, so in 5827 * that case HCR.TSC should be read/write. 5828 */ 5829 valid_mask &= ~HCR_TSC; 5830 } 5831 5832 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5833 if (cpu_isar_feature(aa64_vh, cpu)) { 5834 valid_mask |= HCR_E2H; 5835 } 5836 if (cpu_isar_feature(aa64_ras, cpu)) { 5837 valid_mask |= HCR_TERR | HCR_TEA; 5838 } 5839 if (cpu_isar_feature(aa64_lor, cpu)) { 5840 valid_mask |= HCR_TLOR; 5841 } 5842 if (cpu_isar_feature(aa64_pauth, cpu)) { 5843 valid_mask |= HCR_API | HCR_APK; 5844 } 5845 if (cpu_isar_feature(aa64_mte, cpu)) { 5846 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5; 5847 } 5848 if (cpu_isar_feature(aa64_scxtnum, cpu)) { 5849 valid_mask |= HCR_ENSCXT; 5850 } 5851 if (cpu_isar_feature(aa64_fwb, cpu)) { 5852 valid_mask |= HCR_FWB; 5853 } 5854 if (cpu_isar_feature(aa64_rme, cpu)) { 5855 valid_mask |= HCR_GPF; 5856 } 5857 if (cpu_isar_feature(aa64_nv, cpu)) { 5858 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT; 5859 } 5860 } 5861 5862 if (cpu_isar_feature(any_evt, cpu)) { 5863 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4; 5864 } else if (cpu_isar_feature(any_half_evt, cpu)) { 5865 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4; 5866 } 5867 5868 /* Clear RES0 bits. */ 5869 value &= valid_mask; 5870 5871 /* 5872 * These bits change the MMU setup: 5873 * HCR_VM enables stage 2 translation 5874 * HCR_PTW forbids certain page-table setups 5875 * HCR_DC disables stage1 and enables stage2 translation 5876 * HCR_DCT enables tagging on (disabled) stage1 translation 5877 * HCR_FWB changes the interpretation of stage2 descriptor bits 5878 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits 5879 */ 5880 if ((env->cp15.hcr_el2 ^ value) & 5881 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) { 5882 tlb_flush(CPU(cpu)); 5883 } 5884 env->cp15.hcr_el2 = value; 5885 5886 /* 5887 * Updates to VI and VF require us to update the status of 5888 * virtual interrupts, which are the logical OR of these bits 5889 * and the state of the input lines from the GIC. (This requires 5890 * that we have the BQL, which is done by marking the 5891 * reginfo structs as ARM_CP_IO.) 5892 * Note that if a write to HCR pends a VIRQ or VFIQ it is never 5893 * possible for it to be taken immediately, because VIRQ and 5894 * VFIQ are masked unless running at EL0 or EL1, and HCR 5895 * can only be written at EL2. 5896 */ 5897 g_assert(bql_locked()); 5898 arm_cpu_update_virq(cpu); 5899 arm_cpu_update_vfiq(cpu); 5900 arm_cpu_update_vserr(cpu); 5901 } 5902 5903 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 5904 { 5905 do_hcr_write(env, value, 0); 5906 } 5907 5908 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri, 5909 uint64_t value) 5910 { 5911 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */ 5912 value = deposit64(env->cp15.hcr_el2, 32, 32, value); 5913 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32)); 5914 } 5915 5916 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri, 5917 uint64_t value) 5918 { 5919 /* Handle HCR write, i.e. write to low half of HCR_EL2 */ 5920 value = deposit64(env->cp15.hcr_el2, 0, 32, value); 5921 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32)); 5922 } 5923 5924 /* 5925 * Return the effective value of HCR_EL2, at the given security state. 5926 * Bits that are not included here: 5927 * RW (read from SCR_EL3.RW as needed) 5928 */ 5929 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space) 5930 { 5931 uint64_t ret = env->cp15.hcr_el2; 5932 5933 assert(space != ARMSS_Root); 5934 5935 if (!arm_is_el2_enabled_secstate(env, space)) { 5936 /* 5937 * "This register has no effect if EL2 is not enabled in the 5938 * current Security state". This is ARMv8.4-SecEL2 speak for 5939 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1). 5940 * 5941 * Prior to that, the language was "In an implementation that 5942 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves 5943 * as if this field is 0 for all purposes other than a direct 5944 * read or write access of HCR_EL2". With lots of enumeration 5945 * on a per-field basis. In current QEMU, this is condition 5946 * is arm_is_secure_below_el3. 5947 * 5948 * Since the v8.4 language applies to the entire register, and 5949 * appears to be backward compatible, use that. 5950 */ 5951 return 0; 5952 } 5953 5954 /* 5955 * For a cpu that supports both aarch64 and aarch32, we can set bits 5956 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32. 5957 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32. 5958 */ 5959 if (!arm_el_is_aa64(env, 2)) { 5960 uint64_t aa32_valid; 5961 5962 /* 5963 * These bits are up-to-date as of ARMv8.6. 5964 * For HCR, it's easiest to list just the 2 bits that are invalid. 5965 * For HCR2, list those that are valid. 5966 */ 5967 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ); 5968 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE | 5969 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS); 5970 ret &= aa32_valid; 5971 } 5972 5973 if (ret & HCR_TGE) { 5974 /* These bits are up-to-date as of ARMv8.6. */ 5975 if (ret & HCR_E2H) { 5976 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO | 5977 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE | 5978 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU | 5979 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE | 5980 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT | 5981 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5); 5982 } else { 5983 ret |= HCR_FMO | HCR_IMO | HCR_AMO; 5984 } 5985 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE | 5986 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR | 5987 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM | 5988 HCR_TLOR); 5989 } 5990 5991 return ret; 5992 } 5993 5994 uint64_t arm_hcr_el2_eff(CPUARMState *env) 5995 { 5996 if (arm_feature(env, ARM_FEATURE_M)) { 5997 return 0; 5998 } 5999 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env)); 6000 } 6001 6002 /* 6003 * Corresponds to ARM pseudocode function ELIsInHost(). 6004 */ 6005 bool el_is_in_host(CPUARMState *env, int el) 6006 { 6007 uint64_t mask; 6008 6009 /* 6010 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff(). 6011 * Perform the simplest bit tests first, and validate EL2 afterward. 6012 */ 6013 if (el & 1) { 6014 return false; /* EL1 or EL3 */ 6015 } 6016 6017 /* 6018 * Note that hcr_write() checks isar_feature_aa64_vh(), 6019 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set. 6020 */ 6021 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE; 6022 if ((env->cp15.hcr_el2 & mask) != mask) { 6023 return false; 6024 } 6025 6026 /* TGE and/or E2H set: double check those bits are currently legal. */ 6027 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2); 6028 } 6029 6030 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri, 6031 uint64_t value) 6032 { 6033 uint64_t valid_mask = 0; 6034 6035 /* FEAT_MOPS adds MSCEn and MCE2 */ 6036 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) { 6037 valid_mask |= HCRX_MSCEN | HCRX_MCE2; 6038 } 6039 6040 /* Clear RES0 bits. */ 6041 env->cp15.hcrx_el2 = value & valid_mask; 6042 } 6043 6044 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri, 6045 bool isread) 6046 { 6047 if (arm_current_el(env) == 2 6048 && arm_feature(env, ARM_FEATURE_EL3) 6049 && !(env->cp15.scr_el3 & SCR_HXEN)) { 6050 return CP_ACCESS_TRAP_EL3; 6051 } 6052 return CP_ACCESS_OK; 6053 } 6054 6055 static const ARMCPRegInfo hcrx_el2_reginfo = { 6056 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64, 6057 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2, 6058 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen, 6059 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2), 6060 }; 6061 6062 /* Return the effective value of HCRX_EL2. */ 6063 uint64_t arm_hcrx_el2_eff(CPUARMState *env) 6064 { 6065 /* 6066 * The bits in this register behave as 0 for all purposes other than 6067 * direct reads of the register if SCR_EL3.HXEn is 0. 6068 * If EL2 is not enabled in the current security state, then the 6069 * bit may behave as if 0, or as if 1, depending on the bit. 6070 * For the moment, we treat the EL2-disabled case as taking 6071 * priority over the HXEn-disabled case. This is true for the only 6072 * bit for a feature which we implement where the answer is different 6073 * for the two cases (MSCEn for FEAT_MOPS). 6074 * This may need to be revisited for future bits. 6075 */ 6076 if (!arm_is_el2_enabled(env)) { 6077 uint64_t hcrx = 0; 6078 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) { 6079 /* MSCEn behaves as 1 if EL2 is not enabled */ 6080 hcrx |= HCRX_MSCEN; 6081 } 6082 return hcrx; 6083 } 6084 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) { 6085 return 0; 6086 } 6087 return env->cp15.hcrx_el2; 6088 } 6089 6090 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 6091 uint64_t value) 6092 { 6093 /* 6094 * For A-profile AArch32 EL3, if NSACR.CP10 6095 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 6096 */ 6097 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 6098 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 6099 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK; 6100 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask); 6101 } 6102 env->cp15.cptr_el[2] = value; 6103 } 6104 6105 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri) 6106 { 6107 /* 6108 * For A-profile AArch32 EL3, if NSACR.CP10 6109 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 6110 */ 6111 uint64_t value = env->cp15.cptr_el[2]; 6112 6113 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 6114 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 6115 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK; 6116 } 6117 return value; 6118 } 6119 6120 static const ARMCPRegInfo el2_cp_reginfo[] = { 6121 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 6122 .type = ARM_CP_IO, 6123 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 6124 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 6125 .writefn = hcr_write, .raw_writefn = raw_write }, 6126 { .name = "HCR", .state = ARM_CP_STATE_AA32, 6127 .type = ARM_CP_ALIAS | ARM_CP_IO, 6128 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 6129 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 6130 .writefn = hcr_writelow }, 6131 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 6132 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 6133 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 6134 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 6135 .type = ARM_CP_ALIAS, 6136 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 6137 .access = PL2_RW, 6138 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 6139 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 6140 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 6141 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 6142 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 6143 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 6144 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 6145 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 6146 .type = ARM_CP_ALIAS, 6147 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 6148 .access = PL2_RW, 6149 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) }, 6150 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 6151 .type = ARM_CP_ALIAS, 6152 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 6153 .access = PL2_RW, 6154 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 6155 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 6156 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 6157 .access = PL2_RW, .writefn = vbar_write, 6158 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 6159 .resetvalue = 0 }, 6160 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 6161 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 6162 .access = PL3_RW, .type = ARM_CP_ALIAS, 6163 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 6164 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 6165 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 6166 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 6167 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]), 6168 .readfn = cptr_el2_read, .writefn = cptr_el2_write }, 6169 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 6170 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 6171 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 6172 .resetvalue = 0 }, 6173 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 6174 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 6175 .access = PL2_RW, .type = ARM_CP_ALIAS, 6176 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 6177 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 6178 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 6179 .access = PL2_RW, .type = ARM_CP_CONST, 6180 .resetvalue = 0 }, 6181 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 6182 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 6183 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 6184 .access = PL2_RW, .type = ARM_CP_CONST, 6185 .resetvalue = 0 }, 6186 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 6187 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 6188 .access = PL2_RW, .type = ARM_CP_CONST, 6189 .resetvalue = 0 }, 6190 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 6191 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 6192 .access = PL2_RW, .type = ARM_CP_CONST, 6193 .resetvalue = 0 }, 6194 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 6195 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 6196 .access = PL2_RW, .writefn = vmsa_tcr_el12_write, 6197 .raw_writefn = raw_write, 6198 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 6199 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 6200 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 6201 .type = ARM_CP_ALIAS, 6202 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6203 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) }, 6204 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 6205 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 6206 .access = PL2_RW, 6207 /* no .writefn needed as this can't cause an ASID change */ 6208 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 6209 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 6210 .cp = 15, .opc1 = 6, .crm = 2, 6211 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 6212 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6213 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 6214 .writefn = vttbr_write, .raw_writefn = raw_write }, 6215 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 6216 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 6217 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write, 6218 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 6219 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 6220 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 6221 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 6222 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 6223 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 6224 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 6225 .access = PL2_RW, .resetvalue = 0, 6226 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 6227 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 6228 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 6229 .access = PL2_RW, .resetvalue = 0, 6230 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write, 6231 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 6232 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 6233 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 6234 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 6235 { .name = "TLBIALLNSNH", 6236 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 6237 .type = ARM_CP_NO_RAW, .access = PL2_W, 6238 .writefn = tlbiall_nsnh_write }, 6239 { .name = "TLBIALLNSNHIS", 6240 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 6241 .type = ARM_CP_NO_RAW, .access = PL2_W, 6242 .writefn = tlbiall_nsnh_is_write }, 6243 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 6244 .type = ARM_CP_NO_RAW, .access = PL2_W, 6245 .writefn = tlbiall_hyp_write }, 6246 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 6247 .type = ARM_CP_NO_RAW, .access = PL2_W, 6248 .writefn = tlbiall_hyp_is_write }, 6249 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 6250 .type = ARM_CP_NO_RAW, .access = PL2_W, 6251 .writefn = tlbimva_hyp_write }, 6252 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 6253 .type = ARM_CP_NO_RAW, .access = PL2_W, 6254 .writefn = tlbimva_hyp_is_write }, 6255 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, 6256 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 6257 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 6258 .writefn = tlbi_aa64_alle2_write }, 6259 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, 6260 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 6261 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 6262 .writefn = tlbi_aa64_vae2_write }, 6263 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, 6264 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 6265 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 6266 .writefn = tlbi_aa64_vae2_write }, 6267 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, 6268 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 6269 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 6270 .writefn = tlbi_aa64_alle2is_write }, 6271 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, 6272 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 6273 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 6274 .writefn = tlbi_aa64_vae2is_write }, 6275 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, 6276 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 6277 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 6278 .writefn = tlbi_aa64_vae2is_write }, 6279 #ifndef CONFIG_USER_ONLY 6280 /* 6281 * Unlike the other EL2-related AT operations, these must 6282 * UNDEF from EL3 if EL2 is not implemented, which is why we 6283 * define them here rather than with the rest of the AT ops. 6284 */ 6285 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 6286 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 6287 .access = PL2_W, .accessfn = at_s1e2_access, 6288 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF, 6289 .writefn = ats_write64 }, 6290 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 6291 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 6292 .access = PL2_W, .accessfn = at_s1e2_access, 6293 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF, 6294 .writefn = ats_write64 }, 6295 /* 6296 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 6297 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 6298 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 6299 * to behave as if SCR.NS was 1. 6300 */ 6301 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 6302 .access = PL2_W, 6303 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 6304 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 6305 .access = PL2_W, 6306 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 6307 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 6308 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 6309 /* 6310 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 6311 * reset values as IMPDEF. We choose to reset to 3 to comply with 6312 * both ARMv7 and ARMv8. 6313 */ 6314 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3, 6315 .writefn = gt_cnthctl_write, .raw_writefn = raw_write, 6316 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 6317 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 6318 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 6319 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 6320 .writefn = gt_cntvoff_write, 6321 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 6322 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 6323 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 6324 .writefn = gt_cntvoff_write, 6325 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 6326 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 6327 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 6328 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 6329 .type = ARM_CP_IO, .access = PL2_RW, 6330 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 6331 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 6332 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 6333 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 6334 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 6335 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 6336 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 6337 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 6338 .resetfn = gt_hyp_timer_reset, 6339 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 6340 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 6341 .type = ARM_CP_IO, 6342 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 6343 .access = PL2_RW, 6344 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 6345 .resetvalue = 0, 6346 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 6347 #endif 6348 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 6349 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 6350 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6351 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 6352 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 6353 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 6354 .access = PL2_RW, 6355 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 6356 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 6357 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 6358 .access = PL2_RW, 6359 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 6360 }; 6361 6362 static const ARMCPRegInfo el2_v8_cp_reginfo[] = { 6363 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 6364 .type = ARM_CP_ALIAS | ARM_CP_IO, 6365 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 6366 .access = PL2_RW, 6367 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2), 6368 .writefn = hcr_writehigh }, 6369 }; 6370 6371 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri, 6372 bool isread) 6373 { 6374 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) { 6375 return CP_ACCESS_OK; 6376 } 6377 return CP_ACCESS_TRAP_UNCATEGORIZED; 6378 } 6379 6380 static const ARMCPRegInfo el2_sec_cp_reginfo[] = { 6381 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64, 6382 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0, 6383 .access = PL2_RW, .accessfn = sel2_access, 6384 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) }, 6385 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64, 6386 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2, 6387 .access = PL2_RW, .accessfn = sel2_access, 6388 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) }, 6389 }; 6390 6391 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 6392 bool isread) 6393 { 6394 /* 6395 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 6396 * At Secure EL1 it traps to EL3 or EL2. 6397 */ 6398 if (arm_current_el(env) == 3) { 6399 return CP_ACCESS_OK; 6400 } 6401 if (arm_is_secure_below_el3(env)) { 6402 if (env->cp15.scr_el3 & SCR_EEL2) { 6403 return CP_ACCESS_TRAP_EL2; 6404 } 6405 return CP_ACCESS_TRAP_EL3; 6406 } 6407 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 6408 if (isread) { 6409 return CP_ACCESS_OK; 6410 } 6411 return CP_ACCESS_TRAP_UNCATEGORIZED; 6412 } 6413 6414 static const ARMCPRegInfo el3_cp_reginfo[] = { 6415 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 6416 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 6417 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 6418 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write }, 6419 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL, 6420 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 6421 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 6422 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 6423 .writefn = scr_write, .raw_writefn = raw_write }, 6424 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 6425 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 6426 .access = PL3_RW, .resetvalue = 0, 6427 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 6428 { .name = "SDER", 6429 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 6430 .access = PL3_RW, .resetvalue = 0, 6431 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 6432 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 6433 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 6434 .writefn = vbar_write, .resetvalue = 0, 6435 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 6436 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 6437 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 6438 .access = PL3_RW, .resetvalue = 0, 6439 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 6440 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 6441 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 6442 .access = PL3_RW, 6443 /* no .writefn needed as this can't cause an ASID change */ 6444 .resetvalue = 0, 6445 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 6446 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 6447 .type = ARM_CP_ALIAS, 6448 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 6449 .access = PL3_RW, 6450 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 6451 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 6452 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 6453 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 6454 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 6455 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 6456 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 6457 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 6458 .type = ARM_CP_ALIAS, 6459 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 6460 .access = PL3_RW, 6461 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 6462 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 6463 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 6464 .access = PL3_RW, .writefn = vbar_write, 6465 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 6466 .resetvalue = 0 }, 6467 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 6468 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 6469 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 6470 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 6471 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 6472 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 6473 .access = PL3_RW, .resetvalue = 0, 6474 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 6475 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 6476 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 6477 .access = PL3_RW, .type = ARM_CP_CONST, 6478 .resetvalue = 0 }, 6479 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 6480 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 6481 .access = PL3_RW, .type = ARM_CP_CONST, 6482 .resetvalue = 0 }, 6483 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 6484 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 6485 .access = PL3_RW, .type = ARM_CP_CONST, 6486 .resetvalue = 0 }, 6487 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, 6488 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, 6489 .access = PL3_W, .type = ARM_CP_NO_RAW, 6490 .writefn = tlbi_aa64_alle3is_write }, 6491 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, 6492 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, 6493 .access = PL3_W, .type = ARM_CP_NO_RAW, 6494 .writefn = tlbi_aa64_vae3is_write }, 6495 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, 6496 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, 6497 .access = PL3_W, .type = ARM_CP_NO_RAW, 6498 .writefn = tlbi_aa64_vae3is_write }, 6499 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, 6500 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, 6501 .access = PL3_W, .type = ARM_CP_NO_RAW, 6502 .writefn = tlbi_aa64_alle3_write }, 6503 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, 6504 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, 6505 .access = PL3_W, .type = ARM_CP_NO_RAW, 6506 .writefn = tlbi_aa64_vae3_write }, 6507 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, 6508 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, 6509 .access = PL3_W, .type = ARM_CP_NO_RAW, 6510 .writefn = tlbi_aa64_vae3_write }, 6511 }; 6512 6513 #ifndef CONFIG_USER_ONLY 6514 /* Test if system register redirection is to occur in the current state. */ 6515 static bool redirect_for_e2h(CPUARMState *env) 6516 { 6517 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H); 6518 } 6519 6520 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri) 6521 { 6522 CPReadFn *readfn; 6523 6524 if (redirect_for_e2h(env)) { 6525 /* Switch to the saved EL2 version of the register. */ 6526 ri = ri->opaque; 6527 readfn = ri->readfn; 6528 } else { 6529 readfn = ri->orig_readfn; 6530 } 6531 if (readfn == NULL) { 6532 readfn = raw_read; 6533 } 6534 return readfn(env, ri); 6535 } 6536 6537 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri, 6538 uint64_t value) 6539 { 6540 CPWriteFn *writefn; 6541 6542 if (redirect_for_e2h(env)) { 6543 /* Switch to the saved EL2 version of the register. */ 6544 ri = ri->opaque; 6545 writefn = ri->writefn; 6546 } else { 6547 writefn = ri->orig_writefn; 6548 } 6549 if (writefn == NULL) { 6550 writefn = raw_write; 6551 } 6552 writefn(env, ri, value); 6553 } 6554 6555 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri) 6556 { 6557 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */ 6558 return ri->orig_readfn(env, ri->opaque); 6559 } 6560 6561 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri, 6562 uint64_t value) 6563 { 6564 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */ 6565 return ri->orig_writefn(env, ri->opaque, value); 6566 } 6567 6568 static CPAccessResult el2_e2h_e12_access(CPUARMState *env, 6569 const ARMCPRegInfo *ri, 6570 bool isread) 6571 { 6572 if (arm_current_el(env) == 1) { 6573 /* 6574 * This must be a FEAT_NV access (will either trap or redirect 6575 * to memory). None of the registers with _EL12 aliases want to 6576 * apply their trap controls for this kind of access, so don't 6577 * call the orig_accessfn or do the "UNDEF when E2H is 0" check. 6578 */ 6579 return CP_ACCESS_OK; 6580 } 6581 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */ 6582 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) { 6583 return CP_ACCESS_TRAP_UNCATEGORIZED; 6584 } 6585 if (ri->orig_accessfn) { 6586 return ri->orig_accessfn(env, ri->opaque, isread); 6587 } 6588 return CP_ACCESS_OK; 6589 } 6590 6591 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu) 6592 { 6593 struct E2HAlias { 6594 uint32_t src_key, dst_key, new_key; 6595 const char *src_name, *dst_name, *new_name; 6596 bool (*feature)(const ARMISARegisters *id); 6597 }; 6598 6599 #define K(op0, op1, crn, crm, op2) \ 6600 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2) 6601 6602 static const struct E2HAlias aliases[] = { 6603 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0), 6604 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" }, 6605 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2), 6606 "CPACR", "CPTR_EL2", "CPACR_EL12" }, 6607 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0), 6608 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" }, 6609 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1), 6610 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" }, 6611 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2), 6612 "TCR_EL1", "TCR_EL2", "TCR_EL12" }, 6613 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0), 6614 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" }, 6615 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1), 6616 "ELR_EL1", "ELR_EL2", "ELR_EL12" }, 6617 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0), 6618 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" }, 6619 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1), 6620 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" }, 6621 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0), 6622 "ESR_EL1", "ESR_EL2", "ESR_EL12" }, 6623 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0), 6624 "FAR_EL1", "FAR_EL2", "FAR_EL12" }, 6625 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0), 6626 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" }, 6627 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0), 6628 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" }, 6629 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0), 6630 "VBAR", "VBAR_EL2", "VBAR_EL12" }, 6631 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1), 6632 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" }, 6633 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0), 6634 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" }, 6635 6636 /* 6637 * Note that redirection of ZCR is mentioned in the description 6638 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but 6639 * not in the summary table. 6640 */ 6641 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0), 6642 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve }, 6643 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6), 6644 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme }, 6645 6646 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0), 6647 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte }, 6648 6649 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7), 6650 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12", 6651 isar_feature_aa64_scxtnum }, 6652 6653 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */ 6654 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */ 6655 }; 6656 #undef K 6657 6658 size_t i; 6659 6660 for (i = 0; i < ARRAY_SIZE(aliases); i++) { 6661 const struct E2HAlias *a = &aliases[i]; 6662 ARMCPRegInfo *src_reg, *dst_reg, *new_reg; 6663 bool ok; 6664 6665 if (a->feature && !a->feature(&cpu->isar)) { 6666 continue; 6667 } 6668 6669 src_reg = g_hash_table_lookup(cpu->cp_regs, 6670 (gpointer)(uintptr_t)a->src_key); 6671 dst_reg = g_hash_table_lookup(cpu->cp_regs, 6672 (gpointer)(uintptr_t)a->dst_key); 6673 g_assert(src_reg != NULL); 6674 g_assert(dst_reg != NULL); 6675 6676 /* Cross-compare names to detect typos in the keys. */ 6677 g_assert(strcmp(src_reg->name, a->src_name) == 0); 6678 g_assert(strcmp(dst_reg->name, a->dst_name) == 0); 6679 6680 /* None of the core system registers use opaque; we will. */ 6681 g_assert(src_reg->opaque == NULL); 6682 6683 /* Create alias before redirection so we dup the right data. */ 6684 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo)); 6685 6686 new_reg->name = a->new_name; 6687 new_reg->type |= ARM_CP_ALIAS; 6688 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */ 6689 new_reg->access &= PL2_RW | PL3_RW; 6690 /* The new_reg op fields are as per new_key, not the target reg */ 6691 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK) 6692 >> CP_REG_ARM64_SYSREG_CRN_SHIFT; 6693 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK) 6694 >> CP_REG_ARM64_SYSREG_CRM_SHIFT; 6695 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK) 6696 >> CP_REG_ARM64_SYSREG_OP0_SHIFT; 6697 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK) 6698 >> CP_REG_ARM64_SYSREG_OP1_SHIFT; 6699 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK) 6700 >> CP_REG_ARM64_SYSREG_OP2_SHIFT; 6701 new_reg->opaque = src_reg; 6702 new_reg->orig_readfn = src_reg->readfn ?: raw_read; 6703 new_reg->orig_writefn = src_reg->writefn ?: raw_write; 6704 new_reg->orig_accessfn = src_reg->accessfn; 6705 if (!new_reg->raw_readfn) { 6706 new_reg->raw_readfn = raw_read; 6707 } 6708 if (!new_reg->raw_writefn) { 6709 new_reg->raw_writefn = raw_write; 6710 } 6711 new_reg->readfn = el2_e2h_e12_read; 6712 new_reg->writefn = el2_e2h_e12_write; 6713 new_reg->accessfn = el2_e2h_e12_access; 6714 6715 ok = g_hash_table_insert(cpu->cp_regs, 6716 (gpointer)(uintptr_t)a->new_key, new_reg); 6717 g_assert(ok); 6718 6719 src_reg->opaque = dst_reg; 6720 src_reg->orig_readfn = src_reg->readfn ?: raw_read; 6721 src_reg->orig_writefn = src_reg->writefn ?: raw_write; 6722 if (!src_reg->raw_readfn) { 6723 src_reg->raw_readfn = raw_read; 6724 } 6725 if (!src_reg->raw_writefn) { 6726 src_reg->raw_writefn = raw_write; 6727 } 6728 src_reg->readfn = el2_e2h_read; 6729 src_reg->writefn = el2_e2h_write; 6730 } 6731 } 6732 #endif 6733 6734 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 6735 bool isread) 6736 { 6737 int cur_el = arm_current_el(env); 6738 6739 if (cur_el < 2) { 6740 uint64_t hcr = arm_hcr_el2_eff(env); 6741 6742 if (cur_el == 0) { 6743 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 6744 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) { 6745 return CP_ACCESS_TRAP_EL2; 6746 } 6747 } else { 6748 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 6749 return CP_ACCESS_TRAP; 6750 } 6751 if (hcr & HCR_TID2) { 6752 return CP_ACCESS_TRAP_EL2; 6753 } 6754 } 6755 } else if (hcr & HCR_TID2) { 6756 return CP_ACCESS_TRAP_EL2; 6757 } 6758 } 6759 6760 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) { 6761 return CP_ACCESS_TRAP_EL2; 6762 } 6763 6764 return CP_ACCESS_OK; 6765 } 6766 6767 /* 6768 * Check for traps to RAS registers, which are controlled 6769 * by HCR_EL2.TERR and SCR_EL3.TERR. 6770 */ 6771 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri, 6772 bool isread) 6773 { 6774 int el = arm_current_el(env); 6775 6776 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) { 6777 return CP_ACCESS_TRAP_EL2; 6778 } 6779 if (el < 3 && (env->cp15.scr_el3 & SCR_TERR)) { 6780 return CP_ACCESS_TRAP_EL3; 6781 } 6782 return CP_ACCESS_OK; 6783 } 6784 6785 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri) 6786 { 6787 int el = arm_current_el(env); 6788 6789 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) { 6790 return env->cp15.vdisr_el2; 6791 } 6792 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) { 6793 return 0; /* RAZ/WI */ 6794 } 6795 return env->cp15.disr_el1; 6796 } 6797 6798 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 6799 { 6800 int el = arm_current_el(env); 6801 6802 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) { 6803 env->cp15.vdisr_el2 = val; 6804 return; 6805 } 6806 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) { 6807 return; /* RAZ/WI */ 6808 } 6809 env->cp15.disr_el1 = val; 6810 } 6811 6812 /* 6813 * Minimal RAS implementation with no Error Records. 6814 * Which means that all of the Error Record registers: 6815 * ERXADDR_EL1 6816 * ERXCTLR_EL1 6817 * ERXFR_EL1 6818 * ERXMISC0_EL1 6819 * ERXMISC1_EL1 6820 * ERXMISC2_EL1 6821 * ERXMISC3_EL1 6822 * ERXPFGCDN_EL1 (RASv1p1) 6823 * ERXPFGCTL_EL1 (RASv1p1) 6824 * ERXPFGF_EL1 (RASv1p1) 6825 * ERXSTATUS_EL1 6826 * and 6827 * ERRSELR_EL1 6828 * may generate UNDEFINED, which is the effect we get by not 6829 * listing them at all. 6830 * 6831 * These registers have fine-grained trap bits, but UNDEF-to-EL1 6832 * is higher priority than FGT-to-EL2 so we do not need to list them 6833 * in order to check for an FGT. 6834 */ 6835 static const ARMCPRegInfo minimal_ras_reginfo[] = { 6836 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH, 6837 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1, 6838 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1), 6839 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write }, 6840 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH, 6841 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0, 6842 .access = PL1_R, .accessfn = access_terr, 6843 .fgt = FGT_ERRIDR_EL1, 6844 .type = ARM_CP_CONST, .resetvalue = 0 }, 6845 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH, 6846 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1, 6847 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) }, 6848 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH, 6849 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3, 6850 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) }, 6851 }; 6852 6853 /* 6854 * Return the exception level to which exceptions should be taken 6855 * via SVEAccessTrap. This excludes the check for whether the exception 6856 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily 6857 * be found by testing 0 < fp_exception_el < sve_exception_el. 6858 * 6859 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the 6860 * pseudocode does *not* separate out the FP trap checks, but has them 6861 * all in one function. 6862 */ 6863 int sve_exception_el(CPUARMState *env, int el) 6864 { 6865 #ifndef CONFIG_USER_ONLY 6866 if (el <= 1 && !el_is_in_host(env, el)) { 6867 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) { 6868 case 1: 6869 if (el != 0) { 6870 break; 6871 } 6872 /* fall through */ 6873 case 0: 6874 case 2: 6875 return 1; 6876 } 6877 } 6878 6879 if (el <= 2 && arm_is_el2_enabled(env)) { 6880 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */ 6881 if (env->cp15.hcr_el2 & HCR_E2H) { 6882 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) { 6883 case 1: 6884 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) { 6885 break; 6886 } 6887 /* fall through */ 6888 case 0: 6889 case 2: 6890 return 2; 6891 } 6892 } else { 6893 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) { 6894 return 2; 6895 } 6896 } 6897 } 6898 6899 /* CPTR_EL3. Since EZ is negative we must check for EL3. */ 6900 if (arm_feature(env, ARM_FEATURE_EL3) 6901 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) { 6902 return 3; 6903 } 6904 #endif 6905 return 0; 6906 } 6907 6908 /* 6909 * Return the exception level to which exceptions should be taken for SME. 6910 * C.f. the ARM pseudocode function CheckSMEAccess. 6911 */ 6912 int sme_exception_el(CPUARMState *env, int el) 6913 { 6914 #ifndef CONFIG_USER_ONLY 6915 if (el <= 1 && !el_is_in_host(env, el)) { 6916 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) { 6917 case 1: 6918 if (el != 0) { 6919 break; 6920 } 6921 /* fall through */ 6922 case 0: 6923 case 2: 6924 return 1; 6925 } 6926 } 6927 6928 if (el <= 2 && arm_is_el2_enabled(env)) { 6929 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */ 6930 if (env->cp15.hcr_el2 & HCR_E2H) { 6931 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) { 6932 case 1: 6933 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) { 6934 break; 6935 } 6936 /* fall through */ 6937 case 0: 6938 case 2: 6939 return 2; 6940 } 6941 } else { 6942 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) { 6943 return 2; 6944 } 6945 } 6946 } 6947 6948 /* CPTR_EL3. Since ESM is negative we must check for EL3. */ 6949 if (arm_feature(env, ARM_FEATURE_EL3) 6950 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) { 6951 return 3; 6952 } 6953 #endif 6954 return 0; 6955 } 6956 6957 /* 6958 * Given that SVE is enabled, return the vector length for EL. 6959 */ 6960 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm) 6961 { 6962 ARMCPU *cpu = env_archcpu(env); 6963 uint64_t *cr = env->vfp.zcr_el; 6964 uint32_t map = cpu->sve_vq.map; 6965 uint32_t len = ARM_MAX_VQ - 1; 6966 6967 if (sm) { 6968 cr = env->vfp.smcr_el; 6969 map = cpu->sme_vq.map; 6970 } 6971 6972 if (el <= 1 && !el_is_in_host(env, el)) { 6973 len = MIN(len, 0xf & (uint32_t)cr[1]); 6974 } 6975 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) { 6976 len = MIN(len, 0xf & (uint32_t)cr[2]); 6977 } 6978 if (arm_feature(env, ARM_FEATURE_EL3)) { 6979 len = MIN(len, 0xf & (uint32_t)cr[3]); 6980 } 6981 6982 map &= MAKE_64BIT_MASK(0, len + 1); 6983 if (map != 0) { 6984 return 31 - clz32(map); 6985 } 6986 6987 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */ 6988 assert(sm); 6989 return ctz32(cpu->sme_vq.map); 6990 } 6991 6992 uint32_t sve_vqm1_for_el(CPUARMState *env, int el) 6993 { 6994 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM)); 6995 } 6996 6997 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6998 uint64_t value) 6999 { 7000 int cur_el = arm_current_el(env); 7001 int old_len = sve_vqm1_for_el(env, cur_el); 7002 int new_len; 7003 7004 /* Bits other than [3:0] are RAZ/WI. */ 7005 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16); 7006 raw_write(env, ri, value & 0xf); 7007 7008 /* 7009 * Because we arrived here, we know both FP and SVE are enabled; 7010 * otherwise we would have trapped access to the ZCR_ELn register. 7011 */ 7012 new_len = sve_vqm1_for_el(env, cur_el); 7013 if (new_len < old_len) { 7014 aarch64_sve_narrow_vq(env, new_len + 1); 7015 } 7016 } 7017 7018 static const ARMCPRegInfo zcr_reginfo[] = { 7019 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 7020 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 7021 .access = PL1_RW, .type = ARM_CP_SVE, 7022 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 7023 .writefn = zcr_write, .raw_writefn = raw_write }, 7024 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 7025 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 7026 .access = PL2_RW, .type = ARM_CP_SVE, 7027 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 7028 .writefn = zcr_write, .raw_writefn = raw_write }, 7029 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 7030 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 7031 .access = PL3_RW, .type = ARM_CP_SVE, 7032 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 7033 .writefn = zcr_write, .raw_writefn = raw_write }, 7034 }; 7035 7036 #ifdef TARGET_AARCH64 7037 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri, 7038 bool isread) 7039 { 7040 int el = arm_current_el(env); 7041 7042 if (el == 0) { 7043 uint64_t sctlr = arm_sctlr(env, el); 7044 if (!(sctlr & SCTLR_EnTP2)) { 7045 return CP_ACCESS_TRAP; 7046 } 7047 } 7048 /* TODO: FEAT_FGT */ 7049 if (el < 3 7050 && arm_feature(env, ARM_FEATURE_EL3) 7051 && !(env->cp15.scr_el3 & SCR_ENTP2)) { 7052 return CP_ACCESS_TRAP_EL3; 7053 } 7054 return CP_ACCESS_OK; 7055 } 7056 7057 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri, 7058 bool isread) 7059 { 7060 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */ 7061 if (arm_current_el(env) == 2 7062 && arm_feature(env, ARM_FEATURE_EL3) 7063 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) { 7064 return CP_ACCESS_TRAP_EL3; 7065 } 7066 return CP_ACCESS_OK; 7067 } 7068 7069 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri, 7070 bool isread) 7071 { 7072 if (arm_current_el(env) < 3 7073 && arm_feature(env, ARM_FEATURE_EL3) 7074 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) { 7075 return CP_ACCESS_TRAP_EL3; 7076 } 7077 return CP_ACCESS_OK; 7078 } 7079 7080 /* ResetSVEState */ 7081 static void arm_reset_sve_state(CPUARMState *env) 7082 { 7083 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs)); 7084 /* Recall that FFR is stored as pregs[16]. */ 7085 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs)); 7086 vfp_set_fpcr(env, 0x0800009f); 7087 } 7088 7089 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask) 7090 { 7091 uint64_t change = (env->svcr ^ new) & mask; 7092 7093 if (change == 0) { 7094 return; 7095 } 7096 env->svcr ^= change; 7097 7098 if (change & R_SVCR_SM_MASK) { 7099 arm_reset_sve_state(env); 7100 } 7101 7102 /* 7103 * ResetSMEState. 7104 * 7105 * SetPSTATE_ZA zeros on enable and disable. We can zero this only 7106 * on enable: while disabled, the storage is inaccessible and the 7107 * value does not matter. We're not saving the storage in vmstate 7108 * when disabled either. 7109 */ 7110 if (change & new & R_SVCR_ZA_MASK) { 7111 memset(env->zarray, 0, sizeof(env->zarray)); 7112 } 7113 7114 if (tcg_enabled()) { 7115 arm_rebuild_hflags(env); 7116 } 7117 } 7118 7119 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 7120 uint64_t value) 7121 { 7122 aarch64_set_svcr(env, value, -1); 7123 } 7124 7125 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 7126 uint64_t value) 7127 { 7128 int cur_el = arm_current_el(env); 7129 int old_len = sve_vqm1_for_el(env, cur_el); 7130 int new_len; 7131 7132 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1); 7133 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK; 7134 raw_write(env, ri, value); 7135 7136 /* 7137 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage 7138 * when SVL is widened (old values kept, or zeros). Choose to keep the 7139 * current values for simplicity. But for QEMU internals, we must still 7140 * apply the narrower SVL to the Zregs and Pregs -- see the comment 7141 * above aarch64_sve_narrow_vq. 7142 */ 7143 new_len = sve_vqm1_for_el(env, cur_el); 7144 if (new_len < old_len) { 7145 aarch64_sve_narrow_vq(env, new_len + 1); 7146 } 7147 } 7148 7149 static const ARMCPRegInfo sme_reginfo[] = { 7150 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64, 7151 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5, 7152 .access = PL0_RW, .accessfn = access_tpidr2, 7153 .fgt = FGT_NTPIDR2_EL0, 7154 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) }, 7155 { .name = "SVCR", .state = ARM_CP_STATE_AA64, 7156 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2, 7157 .access = PL0_RW, .type = ARM_CP_SME, 7158 .fieldoffset = offsetof(CPUARMState, svcr), 7159 .writefn = svcr_write, .raw_writefn = raw_write }, 7160 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64, 7161 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6, 7162 .access = PL1_RW, .type = ARM_CP_SME, 7163 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]), 7164 .writefn = smcr_write, .raw_writefn = raw_write }, 7165 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64, 7166 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6, 7167 .access = PL2_RW, .type = ARM_CP_SME, 7168 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]), 7169 .writefn = smcr_write, .raw_writefn = raw_write }, 7170 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64, 7171 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6, 7172 .access = PL3_RW, .type = ARM_CP_SME, 7173 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]), 7174 .writefn = smcr_write, .raw_writefn = raw_write }, 7175 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64, 7176 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6, 7177 .access = PL1_R, .accessfn = access_aa64_tid1, 7178 /* 7179 * IMPLEMENTOR = 0 (software) 7180 * REVISION = 0 (implementation defined) 7181 * SMPS = 0 (no streaming execution priority in QEMU) 7182 * AFFINITY = 0 (streaming sve mode not shared with other PEs) 7183 */ 7184 .type = ARM_CP_CONST, .resetvalue = 0, }, 7185 /* 7186 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0. 7187 */ 7188 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64, 7189 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4, 7190 .access = PL1_RW, .accessfn = access_smpri, 7191 .fgt = FGT_NSMPRI_EL1, 7192 .type = ARM_CP_CONST, .resetvalue = 0 }, 7193 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64, 7194 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5, 7195 .access = PL2_RW, .accessfn = access_smprimap, 7196 .type = ARM_CP_CONST, .resetvalue = 0 }, 7197 }; 7198 7199 static void tlbi_aa64_paall_write(CPUARMState *env, const ARMCPRegInfo *ri, 7200 uint64_t value) 7201 { 7202 CPUState *cs = env_cpu(env); 7203 7204 tlb_flush(cs); 7205 } 7206 7207 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri, 7208 uint64_t value) 7209 { 7210 /* L0GPTSZ is RO; other bits not mentioned are RES0. */ 7211 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK | 7212 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK | 7213 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK; 7214 7215 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask); 7216 } 7217 7218 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 7219 { 7220 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ, 7221 env_archcpu(env)->reset_l0gptsz); 7222 } 7223 7224 static void tlbi_aa64_paallos_write(CPUARMState *env, const ARMCPRegInfo *ri, 7225 uint64_t value) 7226 { 7227 CPUState *cs = env_cpu(env); 7228 7229 tlb_flush_all_cpus_synced(cs); 7230 } 7231 7232 static const ARMCPRegInfo rme_reginfo[] = { 7233 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64, 7234 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6, 7235 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset, 7236 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) }, 7237 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64, 7238 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4, 7239 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) }, 7240 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64, 7241 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5, 7242 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) }, 7243 { .name = "TLBI_PAALL", .state = ARM_CP_STATE_AA64, 7244 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 4, 7245 .access = PL3_W, .type = ARM_CP_NO_RAW, 7246 .writefn = tlbi_aa64_paall_write }, 7247 { .name = "TLBI_PAALLOS", .state = ARM_CP_STATE_AA64, 7248 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 4, 7249 .access = PL3_W, .type = ARM_CP_NO_RAW, 7250 .writefn = tlbi_aa64_paallos_write }, 7251 /* 7252 * QEMU does not have a way to invalidate by physical address, thus 7253 * invalidating a range of physical addresses is accomplished by 7254 * flushing all tlb entries in the outer shareable domain, 7255 * just like PAALLOS. 7256 */ 7257 { .name = "TLBI_RPALOS", .state = ARM_CP_STATE_AA64, 7258 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 7, 7259 .access = PL3_W, .type = ARM_CP_NO_RAW, 7260 .writefn = tlbi_aa64_paallos_write }, 7261 { .name = "TLBI_RPAOS", .state = ARM_CP_STATE_AA64, 7262 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 4, .opc2 = 3, 7263 .access = PL3_W, .type = ARM_CP_NO_RAW, 7264 .writefn = tlbi_aa64_paallos_write }, 7265 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64, 7266 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1, 7267 .access = PL3_W, .type = ARM_CP_NOP }, 7268 }; 7269 7270 static const ARMCPRegInfo rme_mte_reginfo[] = { 7271 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64, 7272 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5, 7273 .access = PL3_W, .type = ARM_CP_NOP }, 7274 }; 7275 #endif /* TARGET_AARCH64 */ 7276 7277 static void define_pmu_regs(ARMCPU *cpu) 7278 { 7279 /* 7280 * v7 performance monitor control register: same implementor 7281 * field as main ID register, and we implement four counters in 7282 * addition to the cycle count register. 7283 */ 7284 unsigned int i, pmcrn = pmu_num_counters(&cpu->env); 7285 ARMCPRegInfo pmcr = { 7286 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 7287 .access = PL0_RW, 7288 .fgt = FGT_PMCR_EL0, 7289 .type = ARM_CP_IO | ARM_CP_ALIAS, 7290 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 7291 .accessfn = pmreg_access, 7292 .readfn = pmcr_read, .raw_readfn = raw_read, 7293 .writefn = pmcr_write, .raw_writefn = raw_write, 7294 }; 7295 ARMCPRegInfo pmcr64 = { 7296 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 7297 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 7298 .access = PL0_RW, .accessfn = pmreg_access, 7299 .fgt = FGT_PMCR_EL0, 7300 .type = ARM_CP_IO, 7301 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 7302 .resetvalue = cpu->isar.reset_pmcr_el0, 7303 .readfn = pmcr_read, .raw_readfn = raw_read, 7304 .writefn = pmcr_write, .raw_writefn = raw_write, 7305 }; 7306 7307 define_one_arm_cp_reg(cpu, &pmcr); 7308 define_one_arm_cp_reg(cpu, &pmcr64); 7309 for (i = 0; i < pmcrn; i++) { 7310 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i); 7311 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i); 7312 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i); 7313 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i); 7314 ARMCPRegInfo pmev_regs[] = { 7315 { .name = pmevcntr_name, .cp = 15, .crn = 14, 7316 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 7317 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 7318 .fgt = FGT_PMEVCNTRN_EL0, 7319 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 7320 .accessfn = pmreg_access_xevcntr }, 7321 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64, 7322 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)), 7323 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr, 7324 .type = ARM_CP_IO, 7325 .fgt = FGT_PMEVCNTRN_EL0, 7326 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 7327 .raw_readfn = pmevcntr_rawread, 7328 .raw_writefn = pmevcntr_rawwrite }, 7329 { .name = pmevtyper_name, .cp = 15, .crn = 14, 7330 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 7331 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 7332 .fgt = FGT_PMEVTYPERN_EL0, 7333 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 7334 .accessfn = pmreg_access }, 7335 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64, 7336 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)), 7337 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 7338 .fgt = FGT_PMEVTYPERN_EL0, 7339 .type = ARM_CP_IO, 7340 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 7341 .raw_writefn = pmevtyper_rawwrite }, 7342 }; 7343 define_arm_cp_regs(cpu, pmev_regs); 7344 g_free(pmevcntr_name); 7345 g_free(pmevcntr_el0_name); 7346 g_free(pmevtyper_name); 7347 g_free(pmevtyper_el0_name); 7348 } 7349 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) { 7350 ARMCPRegInfo v81_pmu_regs[] = { 7351 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32, 7352 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4, 7353 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7354 .fgt = FGT_PMCEIDN_EL0, 7355 .resetvalue = extract64(cpu->pmceid0, 32, 32) }, 7356 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32, 7357 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5, 7358 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7359 .fgt = FGT_PMCEIDN_EL0, 7360 .resetvalue = extract64(cpu->pmceid1, 32, 32) }, 7361 }; 7362 define_arm_cp_regs(cpu, v81_pmu_regs); 7363 } 7364 if (cpu_isar_feature(any_pmuv3p4, cpu)) { 7365 static const ARMCPRegInfo v84_pmmir = { 7366 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH, 7367 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6, 7368 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7369 .fgt = FGT_PMMIR_EL1, 7370 .resetvalue = 0 7371 }; 7372 define_one_arm_cp_reg(cpu, &v84_pmmir); 7373 } 7374 } 7375 7376 #ifndef CONFIG_USER_ONLY 7377 /* 7378 * We don't know until after realize whether there's a GICv3 7379 * attached, and that is what registers the gicv3 sysregs. 7380 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 7381 * at runtime. 7382 */ 7383 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 7384 { 7385 ARMCPU *cpu = env_archcpu(env); 7386 uint64_t pfr1 = cpu->isar.id_pfr1; 7387 7388 if (env->gicv3state) { 7389 pfr1 |= 1 << 28; 7390 } 7391 return pfr1; 7392 } 7393 7394 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 7395 { 7396 ARMCPU *cpu = env_archcpu(env); 7397 uint64_t pfr0 = cpu->isar.id_aa64pfr0; 7398 7399 if (env->gicv3state) { 7400 pfr0 |= 1 << 24; 7401 } 7402 return pfr0; 7403 } 7404 #endif 7405 7406 /* 7407 * Shared logic between LORID and the rest of the LOR* registers. 7408 * Secure state exclusion has already been dealt with. 7409 */ 7410 static CPAccessResult access_lor_ns(CPUARMState *env, 7411 const ARMCPRegInfo *ri, bool isread) 7412 { 7413 int el = arm_current_el(env); 7414 7415 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) { 7416 return CP_ACCESS_TRAP_EL2; 7417 } 7418 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) { 7419 return CP_ACCESS_TRAP_EL3; 7420 } 7421 return CP_ACCESS_OK; 7422 } 7423 7424 static CPAccessResult access_lor_other(CPUARMState *env, 7425 const ARMCPRegInfo *ri, bool isread) 7426 { 7427 if (arm_is_secure_below_el3(env)) { 7428 /* Access denied in secure mode. */ 7429 return CP_ACCESS_TRAP; 7430 } 7431 return access_lor_ns(env, ri, isread); 7432 } 7433 7434 /* 7435 * A trivial implementation of ARMv8.1-LOR leaves all of these 7436 * registers fixed at 0, which indicates that there are zero 7437 * supported Limited Ordering regions. 7438 */ 7439 static const ARMCPRegInfo lor_reginfo[] = { 7440 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64, 7441 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0, 7442 .access = PL1_RW, .accessfn = access_lor_other, 7443 .fgt = FGT_LORSA_EL1, 7444 .type = ARM_CP_CONST, .resetvalue = 0 }, 7445 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64, 7446 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1, 7447 .access = PL1_RW, .accessfn = access_lor_other, 7448 .fgt = FGT_LOREA_EL1, 7449 .type = ARM_CP_CONST, .resetvalue = 0 }, 7450 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64, 7451 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2, 7452 .access = PL1_RW, .accessfn = access_lor_other, 7453 .fgt = FGT_LORN_EL1, 7454 .type = ARM_CP_CONST, .resetvalue = 0 }, 7455 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64, 7456 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3, 7457 .access = PL1_RW, .accessfn = access_lor_other, 7458 .fgt = FGT_LORC_EL1, 7459 .type = ARM_CP_CONST, .resetvalue = 0 }, 7460 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64, 7461 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7, 7462 .access = PL1_R, .accessfn = access_lor_ns, 7463 .fgt = FGT_LORID_EL1, 7464 .type = ARM_CP_CONST, .resetvalue = 0 }, 7465 }; 7466 7467 #ifdef TARGET_AARCH64 7468 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri, 7469 bool isread) 7470 { 7471 int el = arm_current_el(env); 7472 7473 if (el < 2 && 7474 arm_is_el2_enabled(env) && 7475 !(arm_hcr_el2_eff(env) & HCR_APK)) { 7476 return CP_ACCESS_TRAP_EL2; 7477 } 7478 if (el < 3 && 7479 arm_feature(env, ARM_FEATURE_EL3) && 7480 !(env->cp15.scr_el3 & SCR_APK)) { 7481 return CP_ACCESS_TRAP_EL3; 7482 } 7483 return CP_ACCESS_OK; 7484 } 7485 7486 static const ARMCPRegInfo pauth_reginfo[] = { 7487 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7488 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0, 7489 .access = PL1_RW, .accessfn = access_pauth, 7490 .fgt = FGT_APDAKEY, 7491 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) }, 7492 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7493 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1, 7494 .access = PL1_RW, .accessfn = access_pauth, 7495 .fgt = FGT_APDAKEY, 7496 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) }, 7497 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7498 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2, 7499 .access = PL1_RW, .accessfn = access_pauth, 7500 .fgt = FGT_APDBKEY, 7501 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) }, 7502 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7503 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3, 7504 .access = PL1_RW, .accessfn = access_pauth, 7505 .fgt = FGT_APDBKEY, 7506 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) }, 7507 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7508 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0, 7509 .access = PL1_RW, .accessfn = access_pauth, 7510 .fgt = FGT_APGAKEY, 7511 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) }, 7512 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7513 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1, 7514 .access = PL1_RW, .accessfn = access_pauth, 7515 .fgt = FGT_APGAKEY, 7516 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) }, 7517 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7518 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0, 7519 .access = PL1_RW, .accessfn = access_pauth, 7520 .fgt = FGT_APIAKEY, 7521 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) }, 7522 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7523 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1, 7524 .access = PL1_RW, .accessfn = access_pauth, 7525 .fgt = FGT_APIAKEY, 7526 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) }, 7527 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7528 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2, 7529 .access = PL1_RW, .accessfn = access_pauth, 7530 .fgt = FGT_APIBKEY, 7531 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) }, 7532 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7533 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3, 7534 .access = PL1_RW, .accessfn = access_pauth, 7535 .fgt = FGT_APIBKEY, 7536 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) }, 7537 }; 7538 7539 static const ARMCPRegInfo tlbirange_reginfo[] = { 7540 { .name = "TLBI_RVAE1IS", .state = ARM_CP_STATE_AA64, 7541 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 1, 7542 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 7543 .fgt = FGT_TLBIRVAE1IS, 7544 .writefn = tlbi_aa64_rvae1is_write }, 7545 { .name = "TLBI_RVAAE1IS", .state = ARM_CP_STATE_AA64, 7546 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 3, 7547 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 7548 .fgt = FGT_TLBIRVAAE1IS, 7549 .writefn = tlbi_aa64_rvae1is_write }, 7550 { .name = "TLBI_RVALE1IS", .state = ARM_CP_STATE_AA64, 7551 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 5, 7552 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 7553 .fgt = FGT_TLBIRVALE1IS, 7554 .writefn = tlbi_aa64_rvae1is_write }, 7555 { .name = "TLBI_RVAALE1IS", .state = ARM_CP_STATE_AA64, 7556 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 2, .opc2 = 7, 7557 .access = PL1_W, .accessfn = access_ttlbis, .type = ARM_CP_NO_RAW, 7558 .fgt = FGT_TLBIRVAALE1IS, 7559 .writefn = tlbi_aa64_rvae1is_write }, 7560 { .name = "TLBI_RVAE1OS", .state = ARM_CP_STATE_AA64, 7561 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 7562 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7563 .fgt = FGT_TLBIRVAE1OS, 7564 .writefn = tlbi_aa64_rvae1is_write }, 7565 { .name = "TLBI_RVAAE1OS", .state = ARM_CP_STATE_AA64, 7566 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 3, 7567 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7568 .fgt = FGT_TLBIRVAAE1OS, 7569 .writefn = tlbi_aa64_rvae1is_write }, 7570 { .name = "TLBI_RVALE1OS", .state = ARM_CP_STATE_AA64, 7571 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 5, 7572 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7573 .fgt = FGT_TLBIRVALE1OS, 7574 .writefn = tlbi_aa64_rvae1is_write }, 7575 { .name = "TLBI_RVAALE1OS", .state = ARM_CP_STATE_AA64, 7576 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 7, 7577 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7578 .fgt = FGT_TLBIRVAALE1OS, 7579 .writefn = tlbi_aa64_rvae1is_write }, 7580 { .name = "TLBI_RVAE1", .state = ARM_CP_STATE_AA64, 7581 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 7582 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 7583 .fgt = FGT_TLBIRVAE1, 7584 .writefn = tlbi_aa64_rvae1_write }, 7585 { .name = "TLBI_RVAAE1", .state = ARM_CP_STATE_AA64, 7586 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 3, 7587 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 7588 .fgt = FGT_TLBIRVAAE1, 7589 .writefn = tlbi_aa64_rvae1_write }, 7590 { .name = "TLBI_RVALE1", .state = ARM_CP_STATE_AA64, 7591 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 5, 7592 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 7593 .fgt = FGT_TLBIRVALE1, 7594 .writefn = tlbi_aa64_rvae1_write }, 7595 { .name = "TLBI_RVAALE1", .state = ARM_CP_STATE_AA64, 7596 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 7, 7597 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 7598 .fgt = FGT_TLBIRVAALE1, 7599 .writefn = tlbi_aa64_rvae1_write }, 7600 { .name = "TLBI_RIPAS2E1IS", .state = ARM_CP_STATE_AA64, 7601 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 2, 7602 .access = PL2_W, .type = ARM_CP_NO_RAW, 7603 .writefn = tlbi_aa64_ripas2e1is_write }, 7604 { .name = "TLBI_RIPAS2LE1IS", .state = ARM_CP_STATE_AA64, 7605 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 6, 7606 .access = PL2_W, .type = ARM_CP_NO_RAW, 7607 .writefn = tlbi_aa64_ripas2e1is_write }, 7608 { .name = "TLBI_RVAE2IS", .state = ARM_CP_STATE_AA64, 7609 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 1, 7610 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7611 .writefn = tlbi_aa64_rvae2is_write }, 7612 { .name = "TLBI_RVALE2IS", .state = ARM_CP_STATE_AA64, 7613 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 2, .opc2 = 5, 7614 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7615 .writefn = tlbi_aa64_rvae2is_write }, 7616 { .name = "TLBI_RIPAS2E1", .state = ARM_CP_STATE_AA64, 7617 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 2, 7618 .access = PL2_W, .type = ARM_CP_NO_RAW, 7619 .writefn = tlbi_aa64_ripas2e1_write }, 7620 { .name = "TLBI_RIPAS2LE1", .state = ARM_CP_STATE_AA64, 7621 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 6, 7622 .access = PL2_W, .type = ARM_CP_NO_RAW, 7623 .writefn = tlbi_aa64_ripas2e1_write }, 7624 { .name = "TLBI_RVAE2OS", .state = ARM_CP_STATE_AA64, 7625 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 1, 7626 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7627 .writefn = tlbi_aa64_rvae2is_write }, 7628 { .name = "TLBI_RVALE2OS", .state = ARM_CP_STATE_AA64, 7629 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 5, .opc2 = 5, 7630 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7631 .writefn = tlbi_aa64_rvae2is_write }, 7632 { .name = "TLBI_RVAE2", .state = ARM_CP_STATE_AA64, 7633 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 1, 7634 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7635 .writefn = tlbi_aa64_rvae2_write }, 7636 { .name = "TLBI_RVALE2", .state = ARM_CP_STATE_AA64, 7637 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 6, .opc2 = 5, 7638 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7639 .writefn = tlbi_aa64_rvae2_write }, 7640 { .name = "TLBI_RVAE3IS", .state = ARM_CP_STATE_AA64, 7641 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 1, 7642 .access = PL3_W, .type = ARM_CP_NO_RAW, 7643 .writefn = tlbi_aa64_rvae3is_write }, 7644 { .name = "TLBI_RVALE3IS", .state = ARM_CP_STATE_AA64, 7645 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 2, .opc2 = 5, 7646 .access = PL3_W, .type = ARM_CP_NO_RAW, 7647 .writefn = tlbi_aa64_rvae3is_write }, 7648 { .name = "TLBI_RVAE3OS", .state = ARM_CP_STATE_AA64, 7649 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 1, 7650 .access = PL3_W, .type = ARM_CP_NO_RAW, 7651 .writefn = tlbi_aa64_rvae3is_write }, 7652 { .name = "TLBI_RVALE3OS", .state = ARM_CP_STATE_AA64, 7653 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 5, .opc2 = 5, 7654 .access = PL3_W, .type = ARM_CP_NO_RAW, 7655 .writefn = tlbi_aa64_rvae3is_write }, 7656 { .name = "TLBI_RVAE3", .state = ARM_CP_STATE_AA64, 7657 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 1, 7658 .access = PL3_W, .type = ARM_CP_NO_RAW, 7659 .writefn = tlbi_aa64_rvae3_write }, 7660 { .name = "TLBI_RVALE3", .state = ARM_CP_STATE_AA64, 7661 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 6, .opc2 = 5, 7662 .access = PL3_W, .type = ARM_CP_NO_RAW, 7663 .writefn = tlbi_aa64_rvae3_write }, 7664 }; 7665 7666 static const ARMCPRegInfo tlbios_reginfo[] = { 7667 { .name = "TLBI_VMALLE1OS", .state = ARM_CP_STATE_AA64, 7668 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 0, 7669 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7670 .fgt = FGT_TLBIVMALLE1OS, 7671 .writefn = tlbi_aa64_vmalle1is_write }, 7672 { .name = "TLBI_VAE1OS", .state = ARM_CP_STATE_AA64, 7673 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 1, 7674 .fgt = FGT_TLBIVAE1OS, 7675 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7676 .writefn = tlbi_aa64_vae1is_write }, 7677 { .name = "TLBI_ASIDE1OS", .state = ARM_CP_STATE_AA64, 7678 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 2, 7679 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7680 .fgt = FGT_TLBIASIDE1OS, 7681 .writefn = tlbi_aa64_vmalle1is_write }, 7682 { .name = "TLBI_VAAE1OS", .state = ARM_CP_STATE_AA64, 7683 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 3, 7684 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7685 .fgt = FGT_TLBIVAAE1OS, 7686 .writefn = tlbi_aa64_vae1is_write }, 7687 { .name = "TLBI_VALE1OS", .state = ARM_CP_STATE_AA64, 7688 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 5, 7689 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7690 .fgt = FGT_TLBIVALE1OS, 7691 .writefn = tlbi_aa64_vae1is_write }, 7692 { .name = "TLBI_VAALE1OS", .state = ARM_CP_STATE_AA64, 7693 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 1, .opc2 = 7, 7694 .access = PL1_W, .accessfn = access_ttlbos, .type = ARM_CP_NO_RAW, 7695 .fgt = FGT_TLBIVAALE1OS, 7696 .writefn = tlbi_aa64_vae1is_write }, 7697 { .name = "TLBI_ALLE2OS", .state = ARM_CP_STATE_AA64, 7698 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 0, 7699 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7700 .writefn = tlbi_aa64_alle2is_write }, 7701 { .name = "TLBI_VAE2OS", .state = ARM_CP_STATE_AA64, 7702 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 1, 7703 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7704 .writefn = tlbi_aa64_vae2is_write }, 7705 { .name = "TLBI_ALLE1OS", .state = ARM_CP_STATE_AA64, 7706 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 4, 7707 .access = PL2_W, .type = ARM_CP_NO_RAW, 7708 .writefn = tlbi_aa64_alle1is_write }, 7709 { .name = "TLBI_VALE2OS", .state = ARM_CP_STATE_AA64, 7710 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 5, 7711 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_EL3_NO_EL2_UNDEF, 7712 .writefn = tlbi_aa64_vae2is_write }, 7713 { .name = "TLBI_VMALLS12E1OS", .state = ARM_CP_STATE_AA64, 7714 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 1, .opc2 = 6, 7715 .access = PL2_W, .type = ARM_CP_NO_RAW, 7716 .writefn = tlbi_aa64_alle1is_write }, 7717 { .name = "TLBI_IPAS2E1OS", .state = ARM_CP_STATE_AA64, 7718 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 0, 7719 .access = PL2_W, .type = ARM_CP_NOP }, 7720 { .name = "TLBI_RIPAS2E1OS", .state = ARM_CP_STATE_AA64, 7721 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 3, 7722 .access = PL2_W, .type = ARM_CP_NOP }, 7723 { .name = "TLBI_IPAS2LE1OS", .state = ARM_CP_STATE_AA64, 7724 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 4, 7725 .access = PL2_W, .type = ARM_CP_NOP }, 7726 { .name = "TLBI_RIPAS2LE1OS", .state = ARM_CP_STATE_AA64, 7727 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 7, 7728 .access = PL2_W, .type = ARM_CP_NOP }, 7729 { .name = "TLBI_ALLE3OS", .state = ARM_CP_STATE_AA64, 7730 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 0, 7731 .access = PL3_W, .type = ARM_CP_NO_RAW, 7732 .writefn = tlbi_aa64_alle3is_write }, 7733 { .name = "TLBI_VAE3OS", .state = ARM_CP_STATE_AA64, 7734 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 1, 7735 .access = PL3_W, .type = ARM_CP_NO_RAW, 7736 .writefn = tlbi_aa64_vae3is_write }, 7737 { .name = "TLBI_VALE3OS", .state = ARM_CP_STATE_AA64, 7738 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 1, .opc2 = 5, 7739 .access = PL3_W, .type = ARM_CP_NO_RAW, 7740 .writefn = tlbi_aa64_vae3is_write }, 7741 }; 7742 7743 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 7744 { 7745 Error *err = NULL; 7746 uint64_t ret; 7747 7748 /* Success sets NZCV = 0000. */ 7749 env->NF = env->CF = env->VF = 0, env->ZF = 1; 7750 7751 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) { 7752 /* 7753 * ??? Failed, for unknown reasons in the crypto subsystem. 7754 * The best we can do is log the reason and return the 7755 * timed-out indication to the guest. There is no reason 7756 * we know to expect this failure to be transitory, so the 7757 * guest may well hang retrying the operation. 7758 */ 7759 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s", 7760 ri->name, error_get_pretty(err)); 7761 error_free(err); 7762 7763 env->ZF = 0; /* NZCF = 0100 */ 7764 return 0; 7765 } 7766 return ret; 7767 } 7768 7769 /* We do not support re-seeding, so the two registers operate the same. */ 7770 static const ARMCPRegInfo rndr_reginfo[] = { 7771 { .name = "RNDR", .state = ARM_CP_STATE_AA64, 7772 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 7773 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0, 7774 .access = PL0_R, .readfn = rndr_readfn }, 7775 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64, 7776 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 7777 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1, 7778 .access = PL0_R, .readfn = rndr_readfn }, 7779 }; 7780 7781 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque, 7782 uint64_t value) 7783 { 7784 #ifdef CONFIG_TCG 7785 ARMCPU *cpu = env_archcpu(env); 7786 /* CTR_EL0 System register -> DminLine, bits [19:16] */ 7787 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF); 7788 uint64_t vaddr_in = (uint64_t) value; 7789 uint64_t vaddr = vaddr_in & ~(dline_size - 1); 7790 void *haddr; 7791 int mem_idx = cpu_mmu_index(env, false); 7792 7793 /* This won't be crossing page boundaries */ 7794 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC()); 7795 if (haddr) { 7796 #ifndef CONFIG_USER_ONLY 7797 7798 ram_addr_t offset; 7799 MemoryRegion *mr; 7800 7801 /* RCU lock is already being held */ 7802 mr = memory_region_from_host(haddr, &offset); 7803 7804 if (mr) { 7805 memory_region_writeback(mr, offset, dline_size); 7806 } 7807 #endif /*CONFIG_USER_ONLY*/ 7808 } 7809 #else 7810 /* Handled by hardware accelerator. */ 7811 g_assert_not_reached(); 7812 #endif /* CONFIG_TCG */ 7813 } 7814 7815 static const ARMCPRegInfo dcpop_reg[] = { 7816 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64, 7817 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1, 7818 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END, 7819 .fgt = FGT_DCCVAP, 7820 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn }, 7821 }; 7822 7823 static const ARMCPRegInfo dcpodp_reg[] = { 7824 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64, 7825 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1, 7826 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END, 7827 .fgt = FGT_DCCVADP, 7828 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn }, 7829 }; 7830 7831 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri, 7832 bool isread) 7833 { 7834 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) { 7835 return CP_ACCESS_TRAP_EL2; 7836 } 7837 7838 return CP_ACCESS_OK; 7839 } 7840 7841 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri, 7842 bool isread) 7843 { 7844 int el = arm_current_el(env); 7845 if (el < 2 && arm_is_el2_enabled(env)) { 7846 uint64_t hcr = arm_hcr_el2_eff(env); 7847 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) { 7848 return CP_ACCESS_TRAP_EL2; 7849 } 7850 } 7851 if (el < 3 && 7852 arm_feature(env, ARM_FEATURE_EL3) && 7853 !(env->cp15.scr_el3 & SCR_ATA)) { 7854 return CP_ACCESS_TRAP_EL3; 7855 } 7856 return CP_ACCESS_OK; 7857 } 7858 7859 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri, 7860 bool isread) 7861 { 7862 CPAccessResult nv1 = access_nv1(env, ri, isread); 7863 7864 if (nv1 != CP_ACCESS_OK) { 7865 return nv1; 7866 } 7867 return access_mte(env, ri, isread); 7868 } 7869 7870 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri, 7871 bool isread) 7872 { 7873 /* 7874 * TFSR_EL2: similar to generic access_mte(), but we need to 7875 * account for FEAT_NV. At EL1 this must be a FEAT_NV access; 7876 * we will trap to EL2 and the HCR/SCR traps do not apply. 7877 */ 7878 int el = arm_current_el(env); 7879 7880 if (el == 1) { 7881 return CP_ACCESS_OK; 7882 } 7883 if (el < 2 && arm_is_el2_enabled(env)) { 7884 uint64_t hcr = arm_hcr_el2_eff(env); 7885 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) { 7886 return CP_ACCESS_TRAP_EL2; 7887 } 7888 } 7889 if (el < 3 && 7890 arm_feature(env, ARM_FEATURE_EL3) && 7891 !(env->cp15.scr_el3 & SCR_ATA)) { 7892 return CP_ACCESS_TRAP_EL3; 7893 } 7894 return CP_ACCESS_OK; 7895 } 7896 7897 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri) 7898 { 7899 return env->pstate & PSTATE_TCO; 7900 } 7901 7902 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 7903 { 7904 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO); 7905 } 7906 7907 static const ARMCPRegInfo mte_reginfo[] = { 7908 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64, 7909 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1, 7910 .access = PL1_RW, .accessfn = access_mte, 7911 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) }, 7912 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64, 7913 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0, 7914 .access = PL1_RW, .accessfn = access_tfsr_el1, 7915 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) }, 7916 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64, 7917 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0, 7918 .access = PL2_RW, .accessfn = access_tfsr_el2, 7919 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) }, 7920 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64, 7921 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0, 7922 .access = PL3_RW, 7923 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) }, 7924 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64, 7925 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5, 7926 .access = PL1_RW, .accessfn = access_mte, 7927 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) }, 7928 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64, 7929 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6, 7930 .access = PL1_RW, .accessfn = access_mte, 7931 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) }, 7932 { .name = "TCO", .state = ARM_CP_STATE_AA64, 7933 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7, 7934 .type = ARM_CP_NO_RAW, 7935 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write }, 7936 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64, 7937 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3, 7938 .type = ARM_CP_NOP, .access = PL1_W, 7939 .fgt = FGT_DCIVAC, 7940 .accessfn = aa64_cacheop_poc_access }, 7941 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64, 7942 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4, 7943 .fgt = FGT_DCISW, 7944 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7945 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64, 7946 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5, 7947 .type = ARM_CP_NOP, .access = PL1_W, 7948 .fgt = FGT_DCIVAC, 7949 .accessfn = aa64_cacheop_poc_access }, 7950 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64, 7951 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6, 7952 .fgt = FGT_DCISW, 7953 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7954 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64, 7955 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4, 7956 .fgt = FGT_DCCSW, 7957 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7958 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64, 7959 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6, 7960 .fgt = FGT_DCCSW, 7961 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7962 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64, 7963 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4, 7964 .fgt = FGT_DCCISW, 7965 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7966 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64, 7967 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6, 7968 .fgt = FGT_DCCISW, 7969 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7970 }; 7971 7972 static const ARMCPRegInfo mte_tco_ro_reginfo[] = { 7973 { .name = "TCO", .state = ARM_CP_STATE_AA64, 7974 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7, 7975 .type = ARM_CP_CONST, .access = PL0_RW, }, 7976 }; 7977 7978 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = { 7979 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64, 7980 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3, 7981 .type = ARM_CP_NOP, .access = PL0_W, 7982 .fgt = FGT_DCCVAC, 7983 .accessfn = aa64_cacheop_poc_access }, 7984 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64, 7985 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5, 7986 .type = ARM_CP_NOP, .access = PL0_W, 7987 .fgt = FGT_DCCVAC, 7988 .accessfn = aa64_cacheop_poc_access }, 7989 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64, 7990 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3, 7991 .type = ARM_CP_NOP, .access = PL0_W, 7992 .fgt = FGT_DCCVAP, 7993 .accessfn = aa64_cacheop_poc_access }, 7994 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64, 7995 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5, 7996 .type = ARM_CP_NOP, .access = PL0_W, 7997 .fgt = FGT_DCCVAP, 7998 .accessfn = aa64_cacheop_poc_access }, 7999 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64, 8000 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3, 8001 .type = ARM_CP_NOP, .access = PL0_W, 8002 .fgt = FGT_DCCVADP, 8003 .accessfn = aa64_cacheop_poc_access }, 8004 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64, 8005 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5, 8006 .type = ARM_CP_NOP, .access = PL0_W, 8007 .fgt = FGT_DCCVADP, 8008 .accessfn = aa64_cacheop_poc_access }, 8009 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64, 8010 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3, 8011 .type = ARM_CP_NOP, .access = PL0_W, 8012 .fgt = FGT_DCCIVAC, 8013 .accessfn = aa64_cacheop_poc_access }, 8014 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64, 8015 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5, 8016 .type = ARM_CP_NOP, .access = PL0_W, 8017 .fgt = FGT_DCCIVAC, 8018 .accessfn = aa64_cacheop_poc_access }, 8019 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64, 8020 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3, 8021 .access = PL0_W, .type = ARM_CP_DC_GVA, 8022 #ifndef CONFIG_USER_ONLY 8023 /* Avoid overhead of an access check that always passes in user-mode */ 8024 .accessfn = aa64_zva_access, 8025 .fgt = FGT_DCZVA, 8026 #endif 8027 }, 8028 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64, 8029 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4, 8030 .access = PL0_W, .type = ARM_CP_DC_GZVA, 8031 #ifndef CONFIG_USER_ONLY 8032 /* Avoid overhead of an access check that always passes in user-mode */ 8033 .accessfn = aa64_zva_access, 8034 .fgt = FGT_DCZVA, 8035 #endif 8036 }, 8037 }; 8038 8039 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri, 8040 bool isread) 8041 { 8042 uint64_t hcr = arm_hcr_el2_eff(env); 8043 int el = arm_current_el(env); 8044 8045 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) { 8046 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) { 8047 if (hcr & HCR_TGE) { 8048 return CP_ACCESS_TRAP_EL2; 8049 } 8050 return CP_ACCESS_TRAP; 8051 } 8052 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) { 8053 return CP_ACCESS_TRAP_EL2; 8054 } 8055 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) { 8056 return CP_ACCESS_TRAP_EL2; 8057 } 8058 if (el < 3 8059 && arm_feature(env, ARM_FEATURE_EL3) 8060 && !(env->cp15.scr_el3 & SCR_ENSCXT)) { 8061 return CP_ACCESS_TRAP_EL3; 8062 } 8063 return CP_ACCESS_OK; 8064 } 8065 8066 static CPAccessResult access_scxtnum_el1(CPUARMState *env, 8067 const ARMCPRegInfo *ri, 8068 bool isread) 8069 { 8070 CPAccessResult nv1 = access_nv1(env, ri, isread); 8071 8072 if (nv1 != CP_ACCESS_OK) { 8073 return nv1; 8074 } 8075 return access_scxtnum(env, ri, isread); 8076 } 8077 8078 static const ARMCPRegInfo scxtnum_reginfo[] = { 8079 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64, 8080 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7, 8081 .access = PL0_RW, .accessfn = access_scxtnum, 8082 .fgt = FGT_SCXTNUM_EL0, 8083 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) }, 8084 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64, 8085 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7, 8086 .access = PL1_RW, .accessfn = access_scxtnum_el1, 8087 .fgt = FGT_SCXTNUM_EL1, 8088 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) }, 8089 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64, 8090 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7, 8091 .access = PL2_RW, .accessfn = access_scxtnum, 8092 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) }, 8093 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64, 8094 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7, 8095 .access = PL3_RW, 8096 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) }, 8097 }; 8098 8099 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri, 8100 bool isread) 8101 { 8102 if (arm_current_el(env) == 2 && 8103 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) { 8104 return CP_ACCESS_TRAP_EL3; 8105 } 8106 return CP_ACCESS_OK; 8107 } 8108 8109 static const ARMCPRegInfo fgt_reginfo[] = { 8110 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64, 8111 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 8112 .access = PL2_RW, .accessfn = access_fgt, 8113 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) }, 8114 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64, 8115 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5, 8116 .access = PL2_RW, .accessfn = access_fgt, 8117 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) }, 8118 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64, 8119 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4, 8120 .access = PL2_RW, .accessfn = access_fgt, 8121 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) }, 8122 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64, 8123 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5, 8124 .access = PL2_RW, .accessfn = access_fgt, 8125 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) }, 8126 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64, 8127 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6, 8128 .access = PL2_RW, .accessfn = access_fgt, 8129 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) }, 8130 }; 8131 #endif /* TARGET_AARCH64 */ 8132 8133 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri, 8134 bool isread) 8135 { 8136 int el = arm_current_el(env); 8137 8138 if (el == 0) { 8139 uint64_t sctlr = arm_sctlr(env, el); 8140 if (!(sctlr & SCTLR_EnRCTX)) { 8141 return CP_ACCESS_TRAP; 8142 } 8143 } else if (el == 1) { 8144 uint64_t hcr = arm_hcr_el2_eff(env); 8145 if (hcr & HCR_NV) { 8146 return CP_ACCESS_TRAP_EL2; 8147 } 8148 } 8149 return CP_ACCESS_OK; 8150 } 8151 8152 static const ARMCPRegInfo predinv_reginfo[] = { 8153 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64, 8154 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4, 8155 .fgt = FGT_CFPRCTX, 8156 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 8157 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64, 8158 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5, 8159 .fgt = FGT_DVPRCTX, 8160 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 8161 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64, 8162 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7, 8163 .fgt = FGT_CPPRCTX, 8164 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 8165 /* 8166 * Note the AArch32 opcodes have a different OPC1. 8167 */ 8168 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32, 8169 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4, 8170 .fgt = FGT_CFPRCTX, 8171 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 8172 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32, 8173 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5, 8174 .fgt = FGT_DVPRCTX, 8175 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 8176 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32, 8177 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7, 8178 .fgt = FGT_CPPRCTX, 8179 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 8180 }; 8181 8182 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri) 8183 { 8184 /* Read the high 32 bits of the current CCSIDR */ 8185 return extract64(ccsidr_read(env, ri), 32, 32); 8186 } 8187 8188 static const ARMCPRegInfo ccsidr2_reginfo[] = { 8189 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH, 8190 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2, 8191 .access = PL1_R, 8192 .accessfn = access_tid4, 8193 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW }, 8194 }; 8195 8196 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri, 8197 bool isread) 8198 { 8199 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) { 8200 return CP_ACCESS_TRAP_EL2; 8201 } 8202 8203 return CP_ACCESS_OK; 8204 } 8205 8206 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri, 8207 bool isread) 8208 { 8209 if (arm_feature(env, ARM_FEATURE_V8)) { 8210 return access_aa64_tid3(env, ri, isread); 8211 } 8212 8213 return CP_ACCESS_OK; 8214 } 8215 8216 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri, 8217 bool isread) 8218 { 8219 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) { 8220 return CP_ACCESS_TRAP_EL2; 8221 } 8222 8223 return CP_ACCESS_OK; 8224 } 8225 8226 static CPAccessResult access_joscr_jmcr(CPUARMState *env, 8227 const ARMCPRegInfo *ri, bool isread) 8228 { 8229 /* 8230 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only 8231 * in v7A, not in v8A. 8232 */ 8233 if (!arm_feature(env, ARM_FEATURE_V8) && 8234 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) && 8235 (env->cp15.hstr_el2 & HSTR_TJDBX)) { 8236 return CP_ACCESS_TRAP_EL2; 8237 } 8238 return CP_ACCESS_OK; 8239 } 8240 8241 static const ARMCPRegInfo jazelle_regs[] = { 8242 { .name = "JIDR", 8243 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0, 8244 .access = PL1_R, .accessfn = access_jazelle, 8245 .type = ARM_CP_CONST, .resetvalue = 0 }, 8246 { .name = "JOSCR", 8247 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0, 8248 .accessfn = access_joscr_jmcr, 8249 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 8250 { .name = "JMCR", 8251 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0, 8252 .accessfn = access_joscr_jmcr, 8253 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 8254 }; 8255 8256 static const ARMCPRegInfo contextidr_el2 = { 8257 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64, 8258 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1, 8259 .access = PL2_RW, 8260 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) 8261 }; 8262 8263 static const ARMCPRegInfo vhe_reginfo[] = { 8264 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64, 8265 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1, 8266 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write, 8267 .raw_writefn = raw_write, 8268 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) }, 8269 #ifndef CONFIG_USER_ONLY 8270 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64, 8271 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2, 8272 .fieldoffset = 8273 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval), 8274 .type = ARM_CP_IO, .access = PL2_RW, 8275 .writefn = gt_hv_cval_write, .raw_writefn = raw_write }, 8276 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 8277 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0, 8278 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 8279 .resetfn = gt_hv_timer_reset, 8280 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write }, 8281 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH, 8282 .type = ARM_CP_IO, 8283 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1, 8284 .access = PL2_RW, 8285 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl), 8286 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write }, 8287 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64, 8288 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1, 8289 .type = ARM_CP_IO | ARM_CP_ALIAS, 8290 .access = PL2_RW, .accessfn = e2h_access, 8291 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 8292 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write }, 8293 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64, 8294 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1, 8295 .type = ARM_CP_IO | ARM_CP_ALIAS, 8296 .access = PL2_RW, .accessfn = e2h_access, 8297 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 8298 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write }, 8299 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64, 8300 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0, 8301 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS, 8302 .access = PL2_RW, .accessfn = e2h_access, 8303 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write }, 8304 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64, 8305 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0, 8306 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS, 8307 .access = PL2_RW, .accessfn = e2h_access, 8308 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write }, 8309 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64, 8310 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2, 8311 .type = ARM_CP_IO | ARM_CP_ALIAS, 8312 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 8313 .access = PL2_RW, .accessfn = e2h_access, 8314 .writefn = gt_phys_cval_write, .raw_writefn = raw_write }, 8315 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64, 8316 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2, 8317 .type = ARM_CP_IO | ARM_CP_ALIAS, 8318 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 8319 .access = PL2_RW, .accessfn = e2h_access, 8320 .writefn = gt_virt_cval_write, .raw_writefn = raw_write }, 8321 #endif 8322 }; 8323 8324 #ifndef CONFIG_USER_ONLY 8325 static const ARMCPRegInfo ats1e1_reginfo[] = { 8326 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64, 8327 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0, 8328 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 8329 .fgt = FGT_ATS1E1RP, 8330 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 8331 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64, 8332 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1, 8333 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 8334 .fgt = FGT_ATS1E1WP, 8335 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 8336 }; 8337 8338 static const ARMCPRegInfo ats1cp_reginfo[] = { 8339 { .name = "ATS1CPRP", 8340 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0, 8341 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 8342 .writefn = ats_write }, 8343 { .name = "ATS1CPWP", 8344 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1, 8345 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 8346 .writefn = ats_write }, 8347 }; 8348 #endif 8349 8350 /* 8351 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and 8352 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field 8353 * is non-zero, which is never for ARMv7, optionally in ARMv8 8354 * and mandatorily for ARMv8.2 and up. 8355 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's 8356 * implementation is RAZ/WI we can ignore this detail, as we 8357 * do for ACTLR. 8358 */ 8359 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = { 8360 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32, 8361 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3, 8362 .access = PL1_RW, .accessfn = access_tacr, 8363 .type = ARM_CP_CONST, .resetvalue = 0 }, 8364 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32, 8365 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3, 8366 .access = PL2_RW, .type = ARM_CP_CONST, 8367 .resetvalue = 0 }, 8368 }; 8369 8370 void register_cp_regs_for_features(ARMCPU *cpu) 8371 { 8372 /* Register all the coprocessor registers based on feature bits */ 8373 CPUARMState *env = &cpu->env; 8374 if (arm_feature(env, ARM_FEATURE_M)) { 8375 /* M profile has no coprocessor registers */ 8376 return; 8377 } 8378 8379 define_arm_cp_regs(cpu, cp_reginfo); 8380 if (!arm_feature(env, ARM_FEATURE_V8)) { 8381 /* 8382 * Must go early as it is full of wildcards that may be 8383 * overridden by later definitions. 8384 */ 8385 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 8386 } 8387 8388 if (arm_feature(env, ARM_FEATURE_V6)) { 8389 /* The ID registers all have impdef reset values */ 8390 ARMCPRegInfo v6_idregs[] = { 8391 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 8392 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 8393 .access = PL1_R, .type = ARM_CP_CONST, 8394 .accessfn = access_aa32_tid3, 8395 .resetvalue = cpu->isar.id_pfr0 }, 8396 /* 8397 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know 8398 * the value of the GIC field until after we define these regs. 8399 */ 8400 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 8401 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 8402 .access = PL1_R, .type = ARM_CP_NO_RAW, 8403 .accessfn = access_aa32_tid3, 8404 #ifdef CONFIG_USER_ONLY 8405 .type = ARM_CP_CONST, 8406 .resetvalue = cpu->isar.id_pfr1, 8407 #else 8408 .type = ARM_CP_NO_RAW, 8409 .accessfn = access_aa32_tid3, 8410 .readfn = id_pfr1_read, 8411 .writefn = arm_cp_write_ignore 8412 #endif 8413 }, 8414 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 8415 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 8416 .access = PL1_R, .type = ARM_CP_CONST, 8417 .accessfn = access_aa32_tid3, 8418 .resetvalue = cpu->isar.id_dfr0 }, 8419 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 8420 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 8421 .access = PL1_R, .type = ARM_CP_CONST, 8422 .accessfn = access_aa32_tid3, 8423 .resetvalue = cpu->id_afr0 }, 8424 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 8425 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 8426 .access = PL1_R, .type = ARM_CP_CONST, 8427 .accessfn = access_aa32_tid3, 8428 .resetvalue = cpu->isar.id_mmfr0 }, 8429 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 8430 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 8431 .access = PL1_R, .type = ARM_CP_CONST, 8432 .accessfn = access_aa32_tid3, 8433 .resetvalue = cpu->isar.id_mmfr1 }, 8434 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 8435 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 8436 .access = PL1_R, .type = ARM_CP_CONST, 8437 .accessfn = access_aa32_tid3, 8438 .resetvalue = cpu->isar.id_mmfr2 }, 8439 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 8440 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 8441 .access = PL1_R, .type = ARM_CP_CONST, 8442 .accessfn = access_aa32_tid3, 8443 .resetvalue = cpu->isar.id_mmfr3 }, 8444 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 8445 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 8446 .access = PL1_R, .type = ARM_CP_CONST, 8447 .accessfn = access_aa32_tid3, 8448 .resetvalue = cpu->isar.id_isar0 }, 8449 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 8450 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 8451 .access = PL1_R, .type = ARM_CP_CONST, 8452 .accessfn = access_aa32_tid3, 8453 .resetvalue = cpu->isar.id_isar1 }, 8454 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 8455 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 8456 .access = PL1_R, .type = ARM_CP_CONST, 8457 .accessfn = access_aa32_tid3, 8458 .resetvalue = cpu->isar.id_isar2 }, 8459 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 8460 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 8461 .access = PL1_R, .type = ARM_CP_CONST, 8462 .accessfn = access_aa32_tid3, 8463 .resetvalue = cpu->isar.id_isar3 }, 8464 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 8465 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 8466 .access = PL1_R, .type = ARM_CP_CONST, 8467 .accessfn = access_aa32_tid3, 8468 .resetvalue = cpu->isar.id_isar4 }, 8469 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 8470 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 8471 .access = PL1_R, .type = ARM_CP_CONST, 8472 .accessfn = access_aa32_tid3, 8473 .resetvalue = cpu->isar.id_isar5 }, 8474 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 8475 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 8476 .access = PL1_R, .type = ARM_CP_CONST, 8477 .accessfn = access_aa32_tid3, 8478 .resetvalue = cpu->isar.id_mmfr4 }, 8479 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 8480 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 8481 .access = PL1_R, .type = ARM_CP_CONST, 8482 .accessfn = access_aa32_tid3, 8483 .resetvalue = cpu->isar.id_isar6 }, 8484 }; 8485 define_arm_cp_regs(cpu, v6_idregs); 8486 define_arm_cp_regs(cpu, v6_cp_reginfo); 8487 } else { 8488 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 8489 } 8490 if (arm_feature(env, ARM_FEATURE_V6K)) { 8491 define_arm_cp_regs(cpu, v6k_cp_reginfo); 8492 } 8493 if (arm_feature(env, ARM_FEATURE_V7MP) && 8494 !arm_feature(env, ARM_FEATURE_PMSA)) { 8495 define_arm_cp_regs(cpu, v7mp_cp_reginfo); 8496 } 8497 if (arm_feature(env, ARM_FEATURE_V7VE)) { 8498 define_arm_cp_regs(cpu, pmovsset_cp_reginfo); 8499 } 8500 if (arm_feature(env, ARM_FEATURE_V7)) { 8501 ARMCPRegInfo clidr = { 8502 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 8503 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 8504 .access = PL1_R, .type = ARM_CP_CONST, 8505 .accessfn = access_tid4, 8506 .fgt = FGT_CLIDR_EL1, 8507 .resetvalue = cpu->clidr 8508 }; 8509 define_one_arm_cp_reg(cpu, &clidr); 8510 define_arm_cp_regs(cpu, v7_cp_reginfo); 8511 define_debug_regs(cpu); 8512 define_pmu_regs(cpu); 8513 } else { 8514 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 8515 } 8516 if (arm_feature(env, ARM_FEATURE_V8)) { 8517 /* 8518 * v8 ID registers, which all have impdef reset values. 8519 * Note that within the ID register ranges the unused slots 8520 * must all RAZ, not UNDEF; future architecture versions may 8521 * define new registers here. 8522 * ID registers which are AArch64 views of the AArch32 ID registers 8523 * which already existed in v6 and v7 are handled elsewhere, 8524 * in v6_idregs[]. 8525 */ 8526 int i; 8527 ARMCPRegInfo v8_idregs[] = { 8528 /* 8529 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system 8530 * emulation because we don't know the right value for the 8531 * GIC field until after we define these regs. 8532 */ 8533 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 8534 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 8535 .access = PL1_R, 8536 #ifdef CONFIG_USER_ONLY 8537 .type = ARM_CP_CONST, 8538 .resetvalue = cpu->isar.id_aa64pfr0 8539 #else 8540 .type = ARM_CP_NO_RAW, 8541 .accessfn = access_aa64_tid3, 8542 .readfn = id_aa64pfr0_read, 8543 .writefn = arm_cp_write_ignore 8544 #endif 8545 }, 8546 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 8547 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 8548 .access = PL1_R, .type = ARM_CP_CONST, 8549 .accessfn = access_aa64_tid3, 8550 .resetvalue = cpu->isar.id_aa64pfr1}, 8551 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8552 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 8553 .access = PL1_R, .type = ARM_CP_CONST, 8554 .accessfn = access_aa64_tid3, 8555 .resetvalue = 0 }, 8556 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8557 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 8558 .access = PL1_R, .type = ARM_CP_CONST, 8559 .accessfn = access_aa64_tid3, 8560 .resetvalue = 0 }, 8561 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64, 8562 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 8563 .access = PL1_R, .type = ARM_CP_CONST, 8564 .accessfn = access_aa64_tid3, 8565 .resetvalue = cpu->isar.id_aa64zfr0 }, 8566 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64, 8567 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 8568 .access = PL1_R, .type = ARM_CP_CONST, 8569 .accessfn = access_aa64_tid3, 8570 .resetvalue = cpu->isar.id_aa64smfr0 }, 8571 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8572 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 8573 .access = PL1_R, .type = ARM_CP_CONST, 8574 .accessfn = access_aa64_tid3, 8575 .resetvalue = 0 }, 8576 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8577 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 8578 .access = PL1_R, .type = ARM_CP_CONST, 8579 .accessfn = access_aa64_tid3, 8580 .resetvalue = 0 }, 8581 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 8582 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 8583 .access = PL1_R, .type = ARM_CP_CONST, 8584 .accessfn = access_aa64_tid3, 8585 .resetvalue = cpu->isar.id_aa64dfr0 }, 8586 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 8587 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 8588 .access = PL1_R, .type = ARM_CP_CONST, 8589 .accessfn = access_aa64_tid3, 8590 .resetvalue = cpu->isar.id_aa64dfr1 }, 8591 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8592 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 8593 .access = PL1_R, .type = ARM_CP_CONST, 8594 .accessfn = access_aa64_tid3, 8595 .resetvalue = 0 }, 8596 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8597 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 8598 .access = PL1_R, .type = ARM_CP_CONST, 8599 .accessfn = access_aa64_tid3, 8600 .resetvalue = 0 }, 8601 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 8602 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 8603 .access = PL1_R, .type = ARM_CP_CONST, 8604 .accessfn = access_aa64_tid3, 8605 .resetvalue = cpu->id_aa64afr0 }, 8606 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 8607 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 8608 .access = PL1_R, .type = ARM_CP_CONST, 8609 .accessfn = access_aa64_tid3, 8610 .resetvalue = cpu->id_aa64afr1 }, 8611 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8612 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 8613 .access = PL1_R, .type = ARM_CP_CONST, 8614 .accessfn = access_aa64_tid3, 8615 .resetvalue = 0 }, 8616 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8617 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 8618 .access = PL1_R, .type = ARM_CP_CONST, 8619 .accessfn = access_aa64_tid3, 8620 .resetvalue = 0 }, 8621 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 8622 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 8623 .access = PL1_R, .type = ARM_CP_CONST, 8624 .accessfn = access_aa64_tid3, 8625 .resetvalue = cpu->isar.id_aa64isar0 }, 8626 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 8627 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 8628 .access = PL1_R, .type = ARM_CP_CONST, 8629 .accessfn = access_aa64_tid3, 8630 .resetvalue = cpu->isar.id_aa64isar1 }, 8631 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64, 8632 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 8633 .access = PL1_R, .type = ARM_CP_CONST, 8634 .accessfn = access_aa64_tid3, 8635 .resetvalue = cpu->isar.id_aa64isar2 }, 8636 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8637 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 8638 .access = PL1_R, .type = ARM_CP_CONST, 8639 .accessfn = access_aa64_tid3, 8640 .resetvalue = 0 }, 8641 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8642 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 8643 .access = PL1_R, .type = ARM_CP_CONST, 8644 .accessfn = access_aa64_tid3, 8645 .resetvalue = 0 }, 8646 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8647 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 8648 .access = PL1_R, .type = ARM_CP_CONST, 8649 .accessfn = access_aa64_tid3, 8650 .resetvalue = 0 }, 8651 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8652 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 8653 .access = PL1_R, .type = ARM_CP_CONST, 8654 .accessfn = access_aa64_tid3, 8655 .resetvalue = 0 }, 8656 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8657 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 8658 .access = PL1_R, .type = ARM_CP_CONST, 8659 .accessfn = access_aa64_tid3, 8660 .resetvalue = 0 }, 8661 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 8662 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 8663 .access = PL1_R, .type = ARM_CP_CONST, 8664 .accessfn = access_aa64_tid3, 8665 .resetvalue = cpu->isar.id_aa64mmfr0 }, 8666 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 8667 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 8668 .access = PL1_R, .type = ARM_CP_CONST, 8669 .accessfn = access_aa64_tid3, 8670 .resetvalue = cpu->isar.id_aa64mmfr1 }, 8671 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64, 8672 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 8673 .access = PL1_R, .type = ARM_CP_CONST, 8674 .accessfn = access_aa64_tid3, 8675 .resetvalue = cpu->isar.id_aa64mmfr2 }, 8676 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8677 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 8678 .access = PL1_R, .type = ARM_CP_CONST, 8679 .accessfn = access_aa64_tid3, 8680 .resetvalue = 0 }, 8681 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8682 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 8683 .access = PL1_R, .type = ARM_CP_CONST, 8684 .accessfn = access_aa64_tid3, 8685 .resetvalue = 0 }, 8686 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8687 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 8688 .access = PL1_R, .type = ARM_CP_CONST, 8689 .accessfn = access_aa64_tid3, 8690 .resetvalue = 0 }, 8691 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8692 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 8693 .access = PL1_R, .type = ARM_CP_CONST, 8694 .accessfn = access_aa64_tid3, 8695 .resetvalue = 0 }, 8696 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8697 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 8698 .access = PL1_R, .type = ARM_CP_CONST, 8699 .accessfn = access_aa64_tid3, 8700 .resetvalue = 0 }, 8701 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 8702 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 8703 .access = PL1_R, .type = ARM_CP_CONST, 8704 .accessfn = access_aa64_tid3, 8705 .resetvalue = cpu->isar.mvfr0 }, 8706 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 8707 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 8708 .access = PL1_R, .type = ARM_CP_CONST, 8709 .accessfn = access_aa64_tid3, 8710 .resetvalue = cpu->isar.mvfr1 }, 8711 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 8712 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 8713 .access = PL1_R, .type = ARM_CP_CONST, 8714 .accessfn = access_aa64_tid3, 8715 .resetvalue = cpu->isar.mvfr2 }, 8716 /* 8717 * "0, c0, c3, {0,1,2}" are the encodings corresponding to 8718 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding 8719 * as RAZ, since it is in the "reserved for future ID 8720 * registers, RAZ" part of the AArch32 encoding space. 8721 */ 8722 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32, 8723 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 8724 .access = PL1_R, .type = ARM_CP_CONST, 8725 .accessfn = access_aa64_tid3, 8726 .resetvalue = 0 }, 8727 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32, 8728 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 8729 .access = PL1_R, .type = ARM_CP_CONST, 8730 .accessfn = access_aa64_tid3, 8731 .resetvalue = 0 }, 8732 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32, 8733 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 8734 .access = PL1_R, .type = ARM_CP_CONST, 8735 .accessfn = access_aa64_tid3, 8736 .resetvalue = 0 }, 8737 /* 8738 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because 8739 * they're also RAZ for AArch64, and in v8 are gradually 8740 * being filled with AArch64-view-of-AArch32-ID-register 8741 * for new ID registers. 8742 */ 8743 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH, 8744 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 8745 .access = PL1_R, .type = ARM_CP_CONST, 8746 .accessfn = access_aa64_tid3, 8747 .resetvalue = 0 }, 8748 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH, 8749 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 8750 .access = PL1_R, .type = ARM_CP_CONST, 8751 .accessfn = access_aa64_tid3, 8752 .resetvalue = cpu->isar.id_pfr2 }, 8753 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH, 8754 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 8755 .access = PL1_R, .type = ARM_CP_CONST, 8756 .accessfn = access_aa64_tid3, 8757 .resetvalue = cpu->isar.id_dfr1 }, 8758 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH, 8759 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 8760 .access = PL1_R, .type = ARM_CP_CONST, 8761 .accessfn = access_aa64_tid3, 8762 .resetvalue = cpu->isar.id_mmfr5 }, 8763 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH, 8764 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 8765 .access = PL1_R, .type = ARM_CP_CONST, 8766 .accessfn = access_aa64_tid3, 8767 .resetvalue = 0 }, 8768 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 8769 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 8770 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8771 .fgt = FGT_PMCEIDN_EL0, 8772 .resetvalue = extract64(cpu->pmceid0, 0, 32) }, 8773 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 8774 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 8775 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8776 .fgt = FGT_PMCEIDN_EL0, 8777 .resetvalue = cpu->pmceid0 }, 8778 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 8779 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 8780 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8781 .fgt = FGT_PMCEIDN_EL0, 8782 .resetvalue = extract64(cpu->pmceid1, 0, 32) }, 8783 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 8784 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 8785 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8786 .fgt = FGT_PMCEIDN_EL0, 8787 .resetvalue = cpu->pmceid1 }, 8788 }; 8789 #ifdef CONFIG_USER_ONLY 8790 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = { 8791 { .name = "ID_AA64PFR0_EL1", 8792 .exported_bits = R_ID_AA64PFR0_FP_MASK | 8793 R_ID_AA64PFR0_ADVSIMD_MASK | 8794 R_ID_AA64PFR0_SVE_MASK | 8795 R_ID_AA64PFR0_DIT_MASK, 8796 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) | 8797 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) }, 8798 { .name = "ID_AA64PFR1_EL1", 8799 .exported_bits = R_ID_AA64PFR1_BT_MASK | 8800 R_ID_AA64PFR1_SSBS_MASK | 8801 R_ID_AA64PFR1_MTE_MASK | 8802 R_ID_AA64PFR1_SME_MASK }, 8803 { .name = "ID_AA64PFR*_EL1_RESERVED", 8804 .is_glob = true }, 8805 { .name = "ID_AA64ZFR0_EL1", 8806 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK | 8807 R_ID_AA64ZFR0_AES_MASK | 8808 R_ID_AA64ZFR0_BITPERM_MASK | 8809 R_ID_AA64ZFR0_BFLOAT16_MASK | 8810 R_ID_AA64ZFR0_SHA3_MASK | 8811 R_ID_AA64ZFR0_SM4_MASK | 8812 R_ID_AA64ZFR0_I8MM_MASK | 8813 R_ID_AA64ZFR0_F32MM_MASK | 8814 R_ID_AA64ZFR0_F64MM_MASK }, 8815 { .name = "ID_AA64SMFR0_EL1", 8816 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK | 8817 R_ID_AA64SMFR0_BI32I32_MASK | 8818 R_ID_AA64SMFR0_B16F32_MASK | 8819 R_ID_AA64SMFR0_F16F32_MASK | 8820 R_ID_AA64SMFR0_I8I32_MASK | 8821 R_ID_AA64SMFR0_F16F16_MASK | 8822 R_ID_AA64SMFR0_B16B16_MASK | 8823 R_ID_AA64SMFR0_I16I32_MASK | 8824 R_ID_AA64SMFR0_F64F64_MASK | 8825 R_ID_AA64SMFR0_I16I64_MASK | 8826 R_ID_AA64SMFR0_SMEVER_MASK | 8827 R_ID_AA64SMFR0_FA64_MASK }, 8828 { .name = "ID_AA64MMFR0_EL1", 8829 .exported_bits = R_ID_AA64MMFR0_ECV_MASK, 8830 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) | 8831 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) }, 8832 { .name = "ID_AA64MMFR1_EL1", 8833 .exported_bits = R_ID_AA64MMFR1_AFP_MASK }, 8834 { .name = "ID_AA64MMFR2_EL1", 8835 .exported_bits = R_ID_AA64MMFR2_AT_MASK }, 8836 { .name = "ID_AA64MMFR*_EL1_RESERVED", 8837 .is_glob = true }, 8838 { .name = "ID_AA64DFR0_EL1", 8839 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) }, 8840 { .name = "ID_AA64DFR1_EL1" }, 8841 { .name = "ID_AA64DFR*_EL1_RESERVED", 8842 .is_glob = true }, 8843 { .name = "ID_AA64AFR*", 8844 .is_glob = true }, 8845 { .name = "ID_AA64ISAR0_EL1", 8846 .exported_bits = R_ID_AA64ISAR0_AES_MASK | 8847 R_ID_AA64ISAR0_SHA1_MASK | 8848 R_ID_AA64ISAR0_SHA2_MASK | 8849 R_ID_AA64ISAR0_CRC32_MASK | 8850 R_ID_AA64ISAR0_ATOMIC_MASK | 8851 R_ID_AA64ISAR0_RDM_MASK | 8852 R_ID_AA64ISAR0_SHA3_MASK | 8853 R_ID_AA64ISAR0_SM3_MASK | 8854 R_ID_AA64ISAR0_SM4_MASK | 8855 R_ID_AA64ISAR0_DP_MASK | 8856 R_ID_AA64ISAR0_FHM_MASK | 8857 R_ID_AA64ISAR0_TS_MASK | 8858 R_ID_AA64ISAR0_RNDR_MASK }, 8859 { .name = "ID_AA64ISAR1_EL1", 8860 .exported_bits = R_ID_AA64ISAR1_DPB_MASK | 8861 R_ID_AA64ISAR1_APA_MASK | 8862 R_ID_AA64ISAR1_API_MASK | 8863 R_ID_AA64ISAR1_JSCVT_MASK | 8864 R_ID_AA64ISAR1_FCMA_MASK | 8865 R_ID_AA64ISAR1_LRCPC_MASK | 8866 R_ID_AA64ISAR1_GPA_MASK | 8867 R_ID_AA64ISAR1_GPI_MASK | 8868 R_ID_AA64ISAR1_FRINTTS_MASK | 8869 R_ID_AA64ISAR1_SB_MASK | 8870 R_ID_AA64ISAR1_BF16_MASK | 8871 R_ID_AA64ISAR1_DGH_MASK | 8872 R_ID_AA64ISAR1_I8MM_MASK }, 8873 { .name = "ID_AA64ISAR2_EL1", 8874 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK | 8875 R_ID_AA64ISAR2_RPRES_MASK | 8876 R_ID_AA64ISAR2_GPA3_MASK | 8877 R_ID_AA64ISAR2_APA3_MASK | 8878 R_ID_AA64ISAR2_MOPS_MASK | 8879 R_ID_AA64ISAR2_BC_MASK | 8880 R_ID_AA64ISAR2_RPRFM_MASK | 8881 R_ID_AA64ISAR2_CSSC_MASK }, 8882 { .name = "ID_AA64ISAR*_EL1_RESERVED", 8883 .is_glob = true }, 8884 }; 8885 modify_arm_cp_regs(v8_idregs, v8_user_idregs); 8886 #endif 8887 /* 8888 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL. 8889 * TODO: For RMR, a write with bit 1 set should do something with 8890 * cpu_reset(). In the meantime, "the bit is strictly a request", 8891 * so we are in spec just ignoring writes. 8892 */ 8893 if (!arm_feature(env, ARM_FEATURE_EL3) && 8894 !arm_feature(env, ARM_FEATURE_EL2)) { 8895 ARMCPRegInfo el1_reset_regs[] = { 8896 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH, 8897 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 8898 .access = PL1_R, 8899 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) }, 8900 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH, 8901 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2, 8902 .access = PL1_RW, .type = ARM_CP_CONST, 8903 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) } 8904 }; 8905 define_arm_cp_regs(cpu, el1_reset_regs); 8906 } 8907 define_arm_cp_regs(cpu, v8_idregs); 8908 define_arm_cp_regs(cpu, v8_cp_reginfo); 8909 if (cpu_isar_feature(aa64_aa32_el1, cpu)) { 8910 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo); 8911 } 8912 8913 for (i = 4; i < 16; i++) { 8914 /* 8915 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32. 8916 * For pre-v8 cores there are RAZ patterns for these in 8917 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here. 8918 * v8 extends the "must RAZ" part of the ID register space 8919 * to also cover c0, 0, c{8-15}, {0-7}. 8920 * These are STATE_AA32 because in the AArch64 sysreg space 8921 * c4-c7 is where the AArch64 ID registers live (and we've 8922 * already defined those in v8_idregs[]), and c8-c15 are not 8923 * "must RAZ" for AArch64. 8924 */ 8925 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i); 8926 ARMCPRegInfo v8_aa32_raz_idregs = { 8927 .name = name, 8928 .state = ARM_CP_STATE_AA32, 8929 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY, 8930 .access = PL1_R, .type = ARM_CP_CONST, 8931 .accessfn = access_aa64_tid3, 8932 .resetvalue = 0 }; 8933 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs); 8934 } 8935 } 8936 8937 /* 8938 * Register the base EL2 cpregs. 8939 * Pre v8, these registers are implemented only as part of the 8940 * Virtualization Extensions (EL2 present). Beginning with v8, 8941 * if EL2 is missing but EL3 is enabled, mostly these become 8942 * RES0 from EL3, with some specific exceptions. 8943 */ 8944 if (arm_feature(env, ARM_FEATURE_EL2) 8945 || (arm_feature(env, ARM_FEATURE_EL3) 8946 && arm_feature(env, ARM_FEATURE_V8))) { 8947 uint64_t vmpidr_def = mpidr_read_val(env); 8948 ARMCPRegInfo vpidr_regs[] = { 8949 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 8950 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 8951 .access = PL2_RW, .accessfn = access_el3_aa32ns, 8952 .resetvalue = cpu->midr, 8953 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ, 8954 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 8955 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 8956 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 8957 .access = PL2_RW, .resetvalue = cpu->midr, 8958 .type = ARM_CP_EL3_NO_EL2_C_NZ, 8959 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 8960 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 8961 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 8962 .access = PL2_RW, .accessfn = access_el3_aa32ns, 8963 .resetvalue = vmpidr_def, 8964 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ, 8965 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 8966 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 8967 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 8968 .access = PL2_RW, .resetvalue = vmpidr_def, 8969 .type = ARM_CP_EL3_NO_EL2_C_NZ, 8970 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 8971 }; 8972 /* 8973 * The only field of MDCR_EL2 that has a defined architectural reset 8974 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N. 8975 */ 8976 ARMCPRegInfo mdcr_el2 = { 8977 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO, 8978 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 8979 .writefn = mdcr_el2_write, 8980 .access = PL2_RW, .resetvalue = pmu_num_counters(env), 8981 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), 8982 }; 8983 define_one_arm_cp_reg(cpu, &mdcr_el2); 8984 define_arm_cp_regs(cpu, vpidr_regs); 8985 define_arm_cp_regs(cpu, el2_cp_reginfo); 8986 if (arm_feature(env, ARM_FEATURE_V8)) { 8987 define_arm_cp_regs(cpu, el2_v8_cp_reginfo); 8988 } 8989 if (cpu_isar_feature(aa64_sel2, cpu)) { 8990 define_arm_cp_regs(cpu, el2_sec_cp_reginfo); 8991 } 8992 /* 8993 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL. 8994 * See commentary near RMR_EL1. 8995 */ 8996 if (!arm_feature(env, ARM_FEATURE_EL3)) { 8997 static const ARMCPRegInfo el2_reset_regs[] = { 8998 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 8999 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 9000 .access = PL2_R, 9001 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) }, 9002 { .name = "RVBAR", .type = ARM_CP_ALIAS, 9003 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 9004 .access = PL2_R, 9005 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) }, 9006 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64, 9007 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2, 9008 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 }, 9009 }; 9010 define_arm_cp_regs(cpu, el2_reset_regs); 9011 } 9012 } 9013 9014 /* Register the base EL3 cpregs. */ 9015 if (arm_feature(env, ARM_FEATURE_EL3)) { 9016 define_arm_cp_regs(cpu, el3_cp_reginfo); 9017 ARMCPRegInfo el3_regs[] = { 9018 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 9019 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 9020 .access = PL3_R, 9021 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), }, 9022 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64, 9023 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2, 9024 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 }, 9025 { .name = "RMR", .state = ARM_CP_STATE_AA32, 9026 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2, 9027 .access = PL3_RW, .type = ARM_CP_CONST, 9028 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }, 9029 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 9030 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 9031 .access = PL3_RW, 9032 .raw_writefn = raw_write, .writefn = sctlr_write, 9033 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 9034 .resetvalue = cpu->reset_sctlr }, 9035 }; 9036 9037 define_arm_cp_regs(cpu, el3_regs); 9038 } 9039 /* 9040 * The behaviour of NSACR is sufficiently various that we don't 9041 * try to describe it in a single reginfo: 9042 * if EL3 is 64 bit, then trap to EL3 from S EL1, 9043 * reads as constant 0xc00 from NS EL1 and NS EL2 9044 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 9045 * if v7 without EL3, register doesn't exist 9046 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 9047 */ 9048 if (arm_feature(env, ARM_FEATURE_EL3)) { 9049 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 9050 static const ARMCPRegInfo nsacr = { 9051 .name = "NSACR", .type = ARM_CP_CONST, 9052 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 9053 .access = PL1_RW, .accessfn = nsacr_access, 9054 .resetvalue = 0xc00 9055 }; 9056 define_one_arm_cp_reg(cpu, &nsacr); 9057 } else { 9058 static const ARMCPRegInfo nsacr = { 9059 .name = "NSACR", 9060 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 9061 .access = PL3_RW | PL1_R, 9062 .resetvalue = 0, 9063 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 9064 }; 9065 define_one_arm_cp_reg(cpu, &nsacr); 9066 } 9067 } else { 9068 if (arm_feature(env, ARM_FEATURE_V8)) { 9069 static const ARMCPRegInfo nsacr = { 9070 .name = "NSACR", .type = ARM_CP_CONST, 9071 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 9072 .access = PL1_R, 9073 .resetvalue = 0xc00 9074 }; 9075 define_one_arm_cp_reg(cpu, &nsacr); 9076 } 9077 } 9078 9079 if (arm_feature(env, ARM_FEATURE_PMSA)) { 9080 if (arm_feature(env, ARM_FEATURE_V6)) { 9081 /* PMSAv6 not implemented */ 9082 assert(arm_feature(env, ARM_FEATURE_V7)); 9083 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 9084 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 9085 } else { 9086 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 9087 } 9088 } else { 9089 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 9090 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 9091 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */ 9092 if (cpu_isar_feature(aa32_hpd, cpu)) { 9093 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo); 9094 } 9095 } 9096 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 9097 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 9098 } 9099 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 9100 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 9101 } 9102 if (arm_feature(env, ARM_FEATURE_VAPA)) { 9103 ARMCPRegInfo vapa_cp_reginfo[] = { 9104 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 9105 .access = PL1_RW, .resetvalue = 0, 9106 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 9107 offsetoflow32(CPUARMState, cp15.par_ns) }, 9108 .writefn = par_write}, 9109 #ifndef CONFIG_USER_ONLY 9110 /* This underdecoding is safe because the reginfo is NO_RAW. */ 9111 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 9112 .access = PL1_W, .accessfn = ats_access, 9113 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 9114 #endif 9115 }; 9116 9117 /* 9118 * When LPAE exists this 32-bit PAR register is an alias of the 9119 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[] 9120 */ 9121 if (arm_feature(env, ARM_FEATURE_LPAE)) { 9122 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB; 9123 } 9124 define_arm_cp_regs(cpu, vapa_cp_reginfo); 9125 } 9126 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 9127 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 9128 } 9129 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 9130 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 9131 } 9132 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 9133 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 9134 } 9135 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 9136 define_arm_cp_regs(cpu, omap_cp_reginfo); 9137 } 9138 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 9139 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 9140 } 9141 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 9142 define_arm_cp_regs(cpu, xscale_cp_reginfo); 9143 } 9144 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 9145 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 9146 } 9147 if (arm_feature(env, ARM_FEATURE_LPAE)) { 9148 define_arm_cp_regs(cpu, lpae_cp_reginfo); 9149 } 9150 if (cpu_isar_feature(aa32_jazelle, cpu)) { 9151 define_arm_cp_regs(cpu, jazelle_regs); 9152 } 9153 /* 9154 * Slightly awkwardly, the OMAP and StrongARM cores need all of 9155 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 9156 * be read-only (ie write causes UNDEF exception). 9157 */ 9158 { 9159 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 9160 /* 9161 * Pre-v8 MIDR space. 9162 * Note that the MIDR isn't a simple constant register because 9163 * of the TI925 behaviour where writes to another register can 9164 * cause the MIDR value to change. 9165 * 9166 * Unimplemented registers in the c15 0 0 0 space default to 9167 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 9168 * and friends override accordingly. 9169 */ 9170 { .name = "MIDR", 9171 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 9172 .access = PL1_R, .resetvalue = cpu->midr, 9173 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 9174 .readfn = midr_read, 9175 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 9176 .type = ARM_CP_OVERRIDE }, 9177 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 9178 { .name = "DUMMY", 9179 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 9180 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 9181 { .name = "DUMMY", 9182 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 9183 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 9184 { .name = "DUMMY", 9185 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 9186 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 9187 { .name = "DUMMY", 9188 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 9189 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 9190 { .name = "DUMMY", 9191 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 9192 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 9193 }; 9194 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 9195 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 9196 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 9197 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 9198 .fgt = FGT_MIDR_EL1, 9199 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 9200 .readfn = midr_read }, 9201 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */ 9202 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 9203 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 9204 .access = PL1_R, .resetvalue = cpu->midr }, 9205 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 9206 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 9207 .access = PL1_R, 9208 .accessfn = access_aa64_tid1, 9209 .fgt = FGT_REVIDR_EL1, 9210 .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 9211 }; 9212 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = { 9213 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB, 9214 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 9215 .access = PL1_R, .resetvalue = cpu->midr 9216 }; 9217 ARMCPRegInfo id_cp_reginfo[] = { 9218 /* These are common to v8 and pre-v8 */ 9219 { .name = "CTR", 9220 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 9221 .access = PL1_R, .accessfn = ctr_el0_access, 9222 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 9223 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 9224 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 9225 .access = PL0_R, .accessfn = ctr_el0_access, 9226 .fgt = FGT_CTR_EL0, 9227 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 9228 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 9229 { .name = "TCMTR", 9230 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 9231 .access = PL1_R, 9232 .accessfn = access_aa32_tid1, 9233 .type = ARM_CP_CONST, .resetvalue = 0 }, 9234 }; 9235 /* TLBTR is specific to VMSA */ 9236 ARMCPRegInfo id_tlbtr_reginfo = { 9237 .name = "TLBTR", 9238 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 9239 .access = PL1_R, 9240 .accessfn = access_aa32_tid1, 9241 .type = ARM_CP_CONST, .resetvalue = 0, 9242 }; 9243 /* MPUIR is specific to PMSA V6+ */ 9244 ARMCPRegInfo id_mpuir_reginfo = { 9245 .name = "MPUIR", 9246 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 9247 .access = PL1_R, .type = ARM_CP_CONST, 9248 .resetvalue = cpu->pmsav7_dregion << 8 9249 }; 9250 /* HMPUIR is specific to PMSA V8 */ 9251 ARMCPRegInfo id_hmpuir_reginfo = { 9252 .name = "HMPUIR", 9253 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4, 9254 .access = PL2_R, .type = ARM_CP_CONST, 9255 .resetvalue = cpu->pmsav8r_hdregion 9256 }; 9257 static const ARMCPRegInfo crn0_wi_reginfo = { 9258 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 9259 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 9260 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 9261 }; 9262 #ifdef CONFIG_USER_ONLY 9263 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = { 9264 { .name = "MIDR_EL1", 9265 .exported_bits = R_MIDR_EL1_REVISION_MASK | 9266 R_MIDR_EL1_PARTNUM_MASK | 9267 R_MIDR_EL1_ARCHITECTURE_MASK | 9268 R_MIDR_EL1_VARIANT_MASK | 9269 R_MIDR_EL1_IMPLEMENTER_MASK }, 9270 { .name = "REVIDR_EL1" }, 9271 }; 9272 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo); 9273 #endif 9274 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 9275 arm_feature(env, ARM_FEATURE_STRONGARM)) { 9276 size_t i; 9277 /* 9278 * Register the blanket "writes ignored" value first to cover the 9279 * whole space. Then update the specific ID registers to allow write 9280 * access, so that they ignore writes rather than causing them to 9281 * UNDEF. 9282 */ 9283 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 9284 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) { 9285 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW; 9286 } 9287 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) { 9288 id_cp_reginfo[i].access = PL1_RW; 9289 } 9290 id_mpuir_reginfo.access = PL1_RW; 9291 id_tlbtr_reginfo.access = PL1_RW; 9292 } 9293 if (arm_feature(env, ARM_FEATURE_V8)) { 9294 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 9295 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 9296 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo); 9297 } 9298 } else { 9299 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 9300 } 9301 define_arm_cp_regs(cpu, id_cp_reginfo); 9302 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 9303 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 9304 } else if (arm_feature(env, ARM_FEATURE_PMSA) && 9305 arm_feature(env, ARM_FEATURE_V8)) { 9306 uint32_t i = 0; 9307 char *tmp_string; 9308 9309 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 9310 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo); 9311 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo); 9312 9313 /* Register alias is only valid for first 32 indexes */ 9314 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) { 9315 uint8_t crm = 0b1000 | extract32(i, 1, 3); 9316 uint8_t opc1 = extract32(i, 4, 1); 9317 uint8_t opc2 = extract32(i, 0, 1) << 2; 9318 9319 tmp_string = g_strdup_printf("PRBAR%u", i); 9320 ARMCPRegInfo tmp_prbarn_reginfo = { 9321 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW, 9322 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 9323 .access = PL1_RW, .resetvalue = 0, 9324 .accessfn = access_tvm_trvm, 9325 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 9326 }; 9327 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo); 9328 g_free(tmp_string); 9329 9330 opc2 = extract32(i, 0, 1) << 2 | 0x1; 9331 tmp_string = g_strdup_printf("PRLAR%u", i); 9332 ARMCPRegInfo tmp_prlarn_reginfo = { 9333 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW, 9334 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 9335 .access = PL1_RW, .resetvalue = 0, 9336 .accessfn = access_tvm_trvm, 9337 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 9338 }; 9339 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo); 9340 g_free(tmp_string); 9341 } 9342 9343 /* Register alias is only valid for first 32 indexes */ 9344 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) { 9345 uint8_t crm = 0b1000 | extract32(i, 1, 3); 9346 uint8_t opc1 = 0b100 | extract32(i, 4, 1); 9347 uint8_t opc2 = extract32(i, 0, 1) << 2; 9348 9349 tmp_string = g_strdup_printf("HPRBAR%u", i); 9350 ARMCPRegInfo tmp_hprbarn_reginfo = { 9351 .name = tmp_string, 9352 .type = ARM_CP_NO_RAW, 9353 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 9354 .access = PL2_RW, .resetvalue = 0, 9355 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 9356 }; 9357 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo); 9358 g_free(tmp_string); 9359 9360 opc2 = extract32(i, 0, 1) << 2 | 0x1; 9361 tmp_string = g_strdup_printf("HPRLAR%u", i); 9362 ARMCPRegInfo tmp_hprlarn_reginfo = { 9363 .name = tmp_string, 9364 .type = ARM_CP_NO_RAW, 9365 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 9366 .access = PL2_RW, .resetvalue = 0, 9367 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 9368 }; 9369 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo); 9370 g_free(tmp_string); 9371 } 9372 } else if (arm_feature(env, ARM_FEATURE_V7)) { 9373 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 9374 } 9375 } 9376 9377 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 9378 ARMCPRegInfo mpidr_cp_reginfo[] = { 9379 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH, 9380 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 9381 .fgt = FGT_MPIDR_EL1, 9382 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 9383 }; 9384 #ifdef CONFIG_USER_ONLY 9385 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = { 9386 { .name = "MPIDR_EL1", 9387 .fixed_bits = 0x0000000080000000 }, 9388 }; 9389 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo); 9390 #endif 9391 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 9392 } 9393 9394 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 9395 ARMCPRegInfo auxcr_reginfo[] = { 9396 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 9397 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 9398 .access = PL1_RW, .accessfn = access_tacr, 9399 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr }, 9400 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 9401 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 9402 .access = PL2_RW, .type = ARM_CP_CONST, 9403 .resetvalue = 0 }, 9404 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 9405 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 9406 .access = PL3_RW, .type = ARM_CP_CONST, 9407 .resetvalue = 0 }, 9408 }; 9409 define_arm_cp_regs(cpu, auxcr_reginfo); 9410 if (cpu_isar_feature(aa32_ac2, cpu)) { 9411 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo); 9412 } 9413 } 9414 9415 if (arm_feature(env, ARM_FEATURE_CBAR)) { 9416 /* 9417 * CBAR is IMPDEF, but common on Arm Cortex-A implementations. 9418 * There are two flavours: 9419 * (1) older 32-bit only cores have a simple 32-bit CBAR 9420 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a 9421 * 32-bit register visible to AArch32 at a different encoding 9422 * to the "flavour 1" register and with the bits rearranged to 9423 * be able to squash a 64-bit address into the 32-bit view. 9424 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but 9425 * in future if we support AArch32-only configs of some of the 9426 * AArch64 cores we might need to add a specific feature flag 9427 * to indicate cores with "flavour 2" CBAR. 9428 */ 9429 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 9430 /* 32 bit view is [31:18] 0...0 [43:32]. */ 9431 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 9432 | extract64(cpu->reset_cbar, 32, 12); 9433 ARMCPRegInfo cbar_reginfo[] = { 9434 { .name = "CBAR", 9435 .type = ARM_CP_CONST, 9436 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0, 9437 .access = PL1_R, .resetvalue = cbar32 }, 9438 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 9439 .type = ARM_CP_CONST, 9440 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 9441 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 9442 }; 9443 /* We don't implement a r/w 64 bit CBAR currently */ 9444 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 9445 define_arm_cp_regs(cpu, cbar_reginfo); 9446 } else { 9447 ARMCPRegInfo cbar = { 9448 .name = "CBAR", 9449 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 9450 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar, 9451 .fieldoffset = offsetof(CPUARMState, 9452 cp15.c15_config_base_address) 9453 }; 9454 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 9455 cbar.access = PL1_R; 9456 cbar.fieldoffset = 0; 9457 cbar.type = ARM_CP_CONST; 9458 } 9459 define_one_arm_cp_reg(cpu, &cbar); 9460 } 9461 } 9462 9463 if (arm_feature(env, ARM_FEATURE_VBAR)) { 9464 static const ARMCPRegInfo vbar_cp_reginfo[] = { 9465 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 9466 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 9467 .access = PL1_RW, .writefn = vbar_write, 9468 .accessfn = access_nv1, 9469 .fgt = FGT_VBAR_EL1, 9470 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 9471 offsetof(CPUARMState, cp15.vbar_ns) }, 9472 .resetvalue = 0 }, 9473 }; 9474 define_arm_cp_regs(cpu, vbar_cp_reginfo); 9475 } 9476 9477 /* Generic registers whose values depend on the implementation */ 9478 { 9479 ARMCPRegInfo sctlr = { 9480 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 9481 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 9482 .access = PL1_RW, .accessfn = access_tvm_trvm, 9483 .fgt = FGT_SCTLR_EL1, 9484 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 9485 offsetof(CPUARMState, cp15.sctlr_ns) }, 9486 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 9487 .raw_writefn = raw_write, 9488 }; 9489 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 9490 /* 9491 * Normally we would always end the TB on an SCTLR write, but Linux 9492 * arch/arm/mach-pxa/sleep.S expects two instructions following 9493 * an MMU enable to execute from cache. Imitate this behaviour. 9494 */ 9495 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 9496 } 9497 define_one_arm_cp_reg(cpu, &sctlr); 9498 9499 if (arm_feature(env, ARM_FEATURE_PMSA) && 9500 arm_feature(env, ARM_FEATURE_V8)) { 9501 ARMCPRegInfo vsctlr = { 9502 .name = "VSCTLR", .state = ARM_CP_STATE_AA32, 9503 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 9504 .access = PL2_RW, .resetvalue = 0x0, 9505 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr), 9506 }; 9507 define_one_arm_cp_reg(cpu, &vsctlr); 9508 } 9509 } 9510 9511 if (cpu_isar_feature(aa64_lor, cpu)) { 9512 define_arm_cp_regs(cpu, lor_reginfo); 9513 } 9514 if (cpu_isar_feature(aa64_pan, cpu)) { 9515 define_one_arm_cp_reg(cpu, &pan_reginfo); 9516 } 9517 #ifndef CONFIG_USER_ONLY 9518 if (cpu_isar_feature(aa64_ats1e1, cpu)) { 9519 define_arm_cp_regs(cpu, ats1e1_reginfo); 9520 } 9521 if (cpu_isar_feature(aa32_ats1e1, cpu)) { 9522 define_arm_cp_regs(cpu, ats1cp_reginfo); 9523 } 9524 #endif 9525 if (cpu_isar_feature(aa64_uao, cpu)) { 9526 define_one_arm_cp_reg(cpu, &uao_reginfo); 9527 } 9528 9529 if (cpu_isar_feature(aa64_dit, cpu)) { 9530 define_one_arm_cp_reg(cpu, &dit_reginfo); 9531 } 9532 if (cpu_isar_feature(aa64_ssbs, cpu)) { 9533 define_one_arm_cp_reg(cpu, &ssbs_reginfo); 9534 } 9535 if (cpu_isar_feature(any_ras, cpu)) { 9536 define_arm_cp_regs(cpu, minimal_ras_reginfo); 9537 } 9538 9539 if (cpu_isar_feature(aa64_vh, cpu) || 9540 cpu_isar_feature(aa64_debugv8p2, cpu)) { 9541 define_one_arm_cp_reg(cpu, &contextidr_el2); 9542 } 9543 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) { 9544 define_arm_cp_regs(cpu, vhe_reginfo); 9545 } 9546 9547 if (cpu_isar_feature(aa64_sve, cpu)) { 9548 define_arm_cp_regs(cpu, zcr_reginfo); 9549 } 9550 9551 if (cpu_isar_feature(aa64_hcx, cpu)) { 9552 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo); 9553 } 9554 9555 #ifdef TARGET_AARCH64 9556 if (cpu_isar_feature(aa64_sme, cpu)) { 9557 define_arm_cp_regs(cpu, sme_reginfo); 9558 } 9559 if (cpu_isar_feature(aa64_pauth, cpu)) { 9560 define_arm_cp_regs(cpu, pauth_reginfo); 9561 } 9562 if (cpu_isar_feature(aa64_rndr, cpu)) { 9563 define_arm_cp_regs(cpu, rndr_reginfo); 9564 } 9565 if (cpu_isar_feature(aa64_tlbirange, cpu)) { 9566 define_arm_cp_regs(cpu, tlbirange_reginfo); 9567 } 9568 if (cpu_isar_feature(aa64_tlbios, cpu)) { 9569 define_arm_cp_regs(cpu, tlbios_reginfo); 9570 } 9571 /* Data Cache clean instructions up to PoP */ 9572 if (cpu_isar_feature(aa64_dcpop, cpu)) { 9573 define_one_arm_cp_reg(cpu, dcpop_reg); 9574 9575 if (cpu_isar_feature(aa64_dcpodp, cpu)) { 9576 define_one_arm_cp_reg(cpu, dcpodp_reg); 9577 } 9578 } 9579 9580 /* 9581 * If full MTE is enabled, add all of the system registers. 9582 * If only "instructions available at EL0" are enabled, 9583 * then define only a RAZ/WI version of PSTATE.TCO. 9584 */ 9585 if (cpu_isar_feature(aa64_mte, cpu)) { 9586 ARMCPRegInfo gmid_reginfo = { 9587 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64, 9588 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4, 9589 .access = PL1_R, .accessfn = access_aa64_tid5, 9590 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize, 9591 }; 9592 define_one_arm_cp_reg(cpu, &gmid_reginfo); 9593 define_arm_cp_regs(cpu, mte_reginfo); 9594 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo); 9595 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) { 9596 define_arm_cp_regs(cpu, mte_tco_ro_reginfo); 9597 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo); 9598 } 9599 9600 if (cpu_isar_feature(aa64_scxtnum, cpu)) { 9601 define_arm_cp_regs(cpu, scxtnum_reginfo); 9602 } 9603 9604 if (cpu_isar_feature(aa64_fgt, cpu)) { 9605 define_arm_cp_regs(cpu, fgt_reginfo); 9606 } 9607 9608 if (cpu_isar_feature(aa64_rme, cpu)) { 9609 define_arm_cp_regs(cpu, rme_reginfo); 9610 if (cpu_isar_feature(aa64_mte, cpu)) { 9611 define_arm_cp_regs(cpu, rme_mte_reginfo); 9612 } 9613 } 9614 #endif 9615 9616 if (cpu_isar_feature(any_predinv, cpu)) { 9617 define_arm_cp_regs(cpu, predinv_reginfo); 9618 } 9619 9620 if (cpu_isar_feature(any_ccidx, cpu)) { 9621 define_arm_cp_regs(cpu, ccsidr2_reginfo); 9622 } 9623 9624 #ifndef CONFIG_USER_ONLY 9625 /* 9626 * Register redirections and aliases must be done last, 9627 * after the registers from the other extensions have been defined. 9628 */ 9629 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) { 9630 define_arm_vh_e2h_redirects_aliases(cpu); 9631 } 9632 #endif 9633 } 9634 9635 /* 9636 * Private utility function for define_one_arm_cp_reg_with_opaque(): 9637 * add a single reginfo struct to the hash table. 9638 */ 9639 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 9640 void *opaque, CPState state, 9641 CPSecureState secstate, 9642 int crm, int opc1, int opc2, 9643 const char *name) 9644 { 9645 CPUARMState *env = &cpu->env; 9646 uint32_t key; 9647 ARMCPRegInfo *r2; 9648 bool is64 = r->type & ARM_CP_64BIT; 9649 bool ns = secstate & ARM_CP_SECSTATE_NS; 9650 int cp = r->cp; 9651 size_t name_len; 9652 bool make_const; 9653 9654 switch (state) { 9655 case ARM_CP_STATE_AA32: 9656 /* We assume it is a cp15 register if the .cp field is left unset. */ 9657 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) { 9658 cp = 15; 9659 } 9660 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2); 9661 break; 9662 case ARM_CP_STATE_AA64: 9663 /* 9664 * To allow abbreviation of ARMCPRegInfo definitions, we treat 9665 * cp == 0 as equivalent to the value for "standard guest-visible 9666 * sysreg". STATE_BOTH definitions are also always "standard sysreg" 9667 * in their AArch64 view (the .cp value may be non-zero for the 9668 * benefit of the AArch32 view). 9669 */ 9670 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) { 9671 cp = CP_REG_ARM64_SYSREG_CP; 9672 } 9673 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2); 9674 break; 9675 default: 9676 g_assert_not_reached(); 9677 } 9678 9679 /* Overriding of an existing definition must be explicitly requested. */ 9680 if (!(r->type & ARM_CP_OVERRIDE)) { 9681 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key); 9682 if (oldreg) { 9683 assert(oldreg->type & ARM_CP_OVERRIDE); 9684 } 9685 } 9686 9687 /* 9688 * Eliminate registers that are not present because the EL is missing. 9689 * Doing this here makes it easier to put all registers for a given 9690 * feature into the same ARMCPRegInfo array and define them all at once. 9691 */ 9692 make_const = false; 9693 if (arm_feature(env, ARM_FEATURE_EL3)) { 9694 /* 9695 * An EL2 register without EL2 but with EL3 is (usually) RES0. 9696 * See rule RJFFP in section D1.1.3 of DDI0487H.a. 9697 */ 9698 int min_el = ctz32(r->access) / 2; 9699 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) { 9700 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) { 9701 return; 9702 } 9703 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP); 9704 } 9705 } else { 9706 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2) 9707 ? PL2_RW : PL1_RW); 9708 if ((r->access & max_el) == 0) { 9709 return; 9710 } 9711 } 9712 9713 /* Combine cpreg and name into one allocation. */ 9714 name_len = strlen(name) + 1; 9715 r2 = g_malloc(sizeof(*r2) + name_len); 9716 *r2 = *r; 9717 r2->name = memcpy(r2 + 1, name, name_len); 9718 9719 /* 9720 * Update fields to match the instantiation, overwiting wildcards 9721 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH. 9722 */ 9723 r2->cp = cp; 9724 r2->crm = crm; 9725 r2->opc1 = opc1; 9726 r2->opc2 = opc2; 9727 r2->state = state; 9728 r2->secure = secstate; 9729 if (opaque) { 9730 r2->opaque = opaque; 9731 } 9732 9733 if (make_const) { 9734 /* This should not have been a very special register to begin. */ 9735 int old_special = r2->type & ARM_CP_SPECIAL_MASK; 9736 assert(old_special == 0 || old_special == ARM_CP_NOP); 9737 /* 9738 * Set the special function to CONST, retaining the other flags. 9739 * This is important for e.g. ARM_CP_SVE so that we still 9740 * take the SVE trap if CPTR_EL3.EZ == 0. 9741 */ 9742 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST; 9743 /* 9744 * Usually, these registers become RES0, but there are a few 9745 * special cases like VPIDR_EL2 which have a constant non-zero 9746 * value with writes ignored. 9747 */ 9748 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) { 9749 r2->resetvalue = 0; 9750 } 9751 /* 9752 * ARM_CP_CONST has precedence, so removing the callbacks and 9753 * offsets are not strictly necessary, but it is potentially 9754 * less confusing to debug later. 9755 */ 9756 r2->readfn = NULL; 9757 r2->writefn = NULL; 9758 r2->raw_readfn = NULL; 9759 r2->raw_writefn = NULL; 9760 r2->resetfn = NULL; 9761 r2->fieldoffset = 0; 9762 r2->bank_fieldoffsets[0] = 0; 9763 r2->bank_fieldoffsets[1] = 0; 9764 } else { 9765 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]; 9766 9767 if (isbanked) { 9768 /* 9769 * Register is banked (using both entries in array). 9770 * Overwriting fieldoffset as the array is only used to define 9771 * banked registers but later only fieldoffset is used. 9772 */ 9773 r2->fieldoffset = r->bank_fieldoffsets[ns]; 9774 } 9775 if (state == ARM_CP_STATE_AA32) { 9776 if (isbanked) { 9777 /* 9778 * If the register is banked then we don't need to migrate or 9779 * reset the 32-bit instance in certain cases: 9780 * 9781 * 1) If the register has both 32-bit and 64-bit instances 9782 * then we can count on the 64-bit instance taking care 9783 * of the non-secure bank. 9784 * 2) If ARMv8 is enabled then we can count on a 64-bit 9785 * version taking care of the secure bank. This requires 9786 * that separate 32 and 64-bit definitions are provided. 9787 */ 9788 if ((r->state == ARM_CP_STATE_BOTH && ns) || 9789 (arm_feature(env, ARM_FEATURE_V8) && !ns)) { 9790 r2->type |= ARM_CP_ALIAS; 9791 } 9792 } else if ((secstate != r->secure) && !ns) { 9793 /* 9794 * The register is not banked so we only want to allow 9795 * migration of the non-secure instance. 9796 */ 9797 r2->type |= ARM_CP_ALIAS; 9798 } 9799 9800 if (HOST_BIG_ENDIAN && 9801 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) { 9802 r2->fieldoffset += sizeof(uint32_t); 9803 } 9804 } 9805 } 9806 9807 /* 9808 * By convention, for wildcarded registers only the first 9809 * entry is used for migration; the others are marked as 9810 * ALIAS so we don't try to transfer the register 9811 * multiple times. Special registers (ie NOP/WFI) are 9812 * never migratable and not even raw-accessible. 9813 */ 9814 if (r2->type & ARM_CP_SPECIAL_MASK) { 9815 r2->type |= ARM_CP_NO_RAW; 9816 } 9817 if (((r->crm == CP_ANY) && crm != 0) || 9818 ((r->opc1 == CP_ANY) && opc1 != 0) || 9819 ((r->opc2 == CP_ANY) && opc2 != 0)) { 9820 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 9821 } 9822 9823 /* 9824 * Check that raw accesses are either forbidden or handled. Note that 9825 * we can't assert this earlier because the setup of fieldoffset for 9826 * banked registers has to be done first. 9827 */ 9828 if (!(r2->type & ARM_CP_NO_RAW)) { 9829 assert(!raw_accessors_invalid(r2)); 9830 } 9831 9832 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2); 9833 } 9834 9835 9836 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 9837 const ARMCPRegInfo *r, void *opaque) 9838 { 9839 /* 9840 * Define implementations of coprocessor registers. 9841 * We store these in a hashtable because typically 9842 * there are less than 150 registers in a space which 9843 * is 16*16*16*8*8 = 262144 in size. 9844 * Wildcarding is supported for the crm, opc1 and opc2 fields. 9845 * If a register is defined twice then the second definition is 9846 * used, so this can be used to define some generic registers and 9847 * then override them with implementation specific variations. 9848 * At least one of the original and the second definition should 9849 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 9850 * against accidental use. 9851 * 9852 * The state field defines whether the register is to be 9853 * visible in the AArch32 or AArch64 execution state. If the 9854 * state is set to ARM_CP_STATE_BOTH then we synthesise a 9855 * reginfo structure for the AArch32 view, which sees the lower 9856 * 32 bits of the 64 bit register. 9857 * 9858 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 9859 * be wildcarded. AArch64 registers are always considered to be 64 9860 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 9861 * the register, if any. 9862 */ 9863 int crm, opc1, opc2; 9864 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 9865 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 9866 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 9867 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 9868 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 9869 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 9870 CPState state; 9871 9872 /* 64 bit registers have only CRm and Opc1 fields */ 9873 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 9874 /* op0 only exists in the AArch64 encodings */ 9875 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 9876 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 9877 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 9878 /* 9879 * This API is only for Arm's system coprocessors (14 and 15) or 9880 * (M-profile or v7A-and-earlier only) for implementation defined 9881 * coprocessors in the range 0..7. Our decode assumes this, since 9882 * 8..13 can be used for other insns including VFP and Neon. See 9883 * valid_cp() in translate.c. Assert here that we haven't tried 9884 * to use an invalid coprocessor number. 9885 */ 9886 switch (r->state) { 9887 case ARM_CP_STATE_BOTH: 9888 /* 0 has a special meaning, but otherwise the same rules as AA32. */ 9889 if (r->cp == 0) { 9890 break; 9891 } 9892 /* fall through */ 9893 case ARM_CP_STATE_AA32: 9894 if (arm_feature(&cpu->env, ARM_FEATURE_V8) && 9895 !arm_feature(&cpu->env, ARM_FEATURE_M)) { 9896 assert(r->cp >= 14 && r->cp <= 15); 9897 } else { 9898 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15)); 9899 } 9900 break; 9901 case ARM_CP_STATE_AA64: 9902 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP); 9903 break; 9904 default: 9905 g_assert_not_reached(); 9906 } 9907 /* 9908 * The AArch64 pseudocode CheckSystemAccess() specifies that op1 9909 * encodes a minimum access level for the register. We roll this 9910 * runtime check into our general permission check code, so check 9911 * here that the reginfo's specified permissions are strict enough 9912 * to encompass the generic architectural permission check. 9913 */ 9914 if (r->state != ARM_CP_STATE_AA32) { 9915 CPAccessRights mask; 9916 switch (r->opc1) { 9917 case 0: 9918 /* min_EL EL1, but some accessible to EL0 via kernel ABI */ 9919 mask = PL0U_R | PL1_RW; 9920 break; 9921 case 1: case 2: 9922 /* min_EL EL1 */ 9923 mask = PL1_RW; 9924 break; 9925 case 3: 9926 /* min_EL EL0 */ 9927 mask = PL0_RW; 9928 break; 9929 case 4: 9930 case 5: 9931 /* min_EL EL2 */ 9932 mask = PL2_RW; 9933 break; 9934 case 6: 9935 /* min_EL EL3 */ 9936 mask = PL3_RW; 9937 break; 9938 case 7: 9939 /* min_EL EL1, secure mode only (we don't check the latter) */ 9940 mask = PL1_RW; 9941 break; 9942 default: 9943 /* broken reginfo with out-of-range opc1 */ 9944 g_assert_not_reached(); 9945 } 9946 /* assert our permissions are not too lax (stricter is fine) */ 9947 assert((r->access & ~mask) == 0); 9948 } 9949 9950 /* 9951 * Check that the register definition has enough info to handle 9952 * reads and writes if they are permitted. 9953 */ 9954 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) { 9955 if (r->access & PL3_R) { 9956 assert((r->fieldoffset || 9957 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 9958 r->readfn); 9959 } 9960 if (r->access & PL3_W) { 9961 assert((r->fieldoffset || 9962 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 9963 r->writefn); 9964 } 9965 } 9966 9967 for (crm = crmmin; crm <= crmmax; crm++) { 9968 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 9969 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 9970 for (state = ARM_CP_STATE_AA32; 9971 state <= ARM_CP_STATE_AA64; state++) { 9972 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 9973 continue; 9974 } 9975 if (state == ARM_CP_STATE_AA32) { 9976 /* 9977 * Under AArch32 CP registers can be common 9978 * (same for secure and non-secure world) or banked. 9979 */ 9980 char *name; 9981 9982 switch (r->secure) { 9983 case ARM_CP_SECSTATE_S: 9984 case ARM_CP_SECSTATE_NS: 9985 add_cpreg_to_hashtable(cpu, r, opaque, state, 9986 r->secure, crm, opc1, opc2, 9987 r->name); 9988 break; 9989 case ARM_CP_SECSTATE_BOTH: 9990 name = g_strdup_printf("%s_S", r->name); 9991 add_cpreg_to_hashtable(cpu, r, opaque, state, 9992 ARM_CP_SECSTATE_S, 9993 crm, opc1, opc2, name); 9994 g_free(name); 9995 add_cpreg_to_hashtable(cpu, r, opaque, state, 9996 ARM_CP_SECSTATE_NS, 9997 crm, opc1, opc2, r->name); 9998 break; 9999 default: 10000 g_assert_not_reached(); 10001 } 10002 } else { 10003 /* 10004 * AArch64 registers get mapped to non-secure instance 10005 * of AArch32 10006 */ 10007 add_cpreg_to_hashtable(cpu, r, opaque, state, 10008 ARM_CP_SECSTATE_NS, 10009 crm, opc1, opc2, r->name); 10010 } 10011 } 10012 } 10013 } 10014 } 10015 } 10016 10017 /* Define a whole list of registers */ 10018 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs, 10019 void *opaque, size_t len) 10020 { 10021 size_t i; 10022 for (i = 0; i < len; ++i) { 10023 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque); 10024 } 10025 } 10026 10027 /* 10028 * Modify ARMCPRegInfo for access from userspace. 10029 * 10030 * This is a data driven modification directed by 10031 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as 10032 * user-space cannot alter any values and dynamic values pertaining to 10033 * execution state are hidden from user space view anyway. 10034 */ 10035 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len, 10036 const ARMCPRegUserSpaceInfo *mods, 10037 size_t mods_len) 10038 { 10039 for (size_t mi = 0; mi < mods_len; ++mi) { 10040 const ARMCPRegUserSpaceInfo *m = mods + mi; 10041 GPatternSpec *pat = NULL; 10042 10043 if (m->is_glob) { 10044 pat = g_pattern_spec_new(m->name); 10045 } 10046 for (size_t ri = 0; ri < regs_len; ++ri) { 10047 ARMCPRegInfo *r = regs + ri; 10048 10049 if (pat && g_pattern_match_string(pat, r->name)) { 10050 r->type = ARM_CP_CONST; 10051 r->access = PL0U_R; 10052 r->resetvalue = 0; 10053 /* continue */ 10054 } else if (strcmp(r->name, m->name) == 0) { 10055 r->type = ARM_CP_CONST; 10056 r->access = PL0U_R; 10057 r->resetvalue &= m->exported_bits; 10058 r->resetvalue |= m->fixed_bits; 10059 break; 10060 } 10061 } 10062 if (pat) { 10063 g_pattern_spec_free(pat); 10064 } 10065 } 10066 } 10067 10068 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 10069 { 10070 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp); 10071 } 10072 10073 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 10074 uint64_t value) 10075 { 10076 /* Helper coprocessor write function for write-ignore registers */ 10077 } 10078 10079 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 10080 { 10081 /* Helper coprocessor write function for read-as-zero registers */ 10082 return 0; 10083 } 10084 10085 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 10086 { 10087 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 10088 } 10089 10090 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 10091 { 10092 /* 10093 * Return true if it is not valid for us to switch to 10094 * this CPU mode (ie all the UNPREDICTABLE cases in 10095 * the ARM ARM CPSRWriteByInstr pseudocode). 10096 */ 10097 10098 /* Changes to or from Hyp via MSR and CPS are illegal. */ 10099 if (write_type == CPSRWriteByInstr && 10100 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 10101 mode == ARM_CPU_MODE_HYP)) { 10102 return 1; 10103 } 10104 10105 switch (mode) { 10106 case ARM_CPU_MODE_USR: 10107 return 0; 10108 case ARM_CPU_MODE_SYS: 10109 case ARM_CPU_MODE_SVC: 10110 case ARM_CPU_MODE_ABT: 10111 case ARM_CPU_MODE_UND: 10112 case ARM_CPU_MODE_IRQ: 10113 case ARM_CPU_MODE_FIQ: 10114 /* 10115 * Note that we don't implement the IMPDEF NSACR.RFR which in v7 10116 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 10117 */ 10118 /* 10119 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 10120 * and CPS are treated as illegal mode changes. 10121 */ 10122 if (write_type == CPSRWriteByInstr && 10123 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 10124 (arm_hcr_el2_eff(env) & HCR_TGE)) { 10125 return 1; 10126 } 10127 return 0; 10128 case ARM_CPU_MODE_HYP: 10129 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2; 10130 case ARM_CPU_MODE_MON: 10131 return arm_current_el(env) < 3; 10132 default: 10133 return 1; 10134 } 10135 } 10136 10137 uint32_t cpsr_read(CPUARMState *env) 10138 { 10139 int ZF; 10140 ZF = (env->ZF == 0); 10141 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 10142 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 10143 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 10144 | ((env->condexec_bits & 0xfc) << 8) 10145 | (env->GE << 16) | (env->daif & CPSR_AIF); 10146 } 10147 10148 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 10149 CPSRWriteType write_type) 10150 { 10151 uint32_t changed_daif; 10152 bool rebuild_hflags = (write_type != CPSRWriteRaw) && 10153 (mask & (CPSR_M | CPSR_E | CPSR_IL)); 10154 10155 if (mask & CPSR_NZCV) { 10156 env->ZF = (~val) & CPSR_Z; 10157 env->NF = val; 10158 env->CF = (val >> 29) & 1; 10159 env->VF = (val << 3) & 0x80000000; 10160 } 10161 if (mask & CPSR_Q) { 10162 env->QF = ((val & CPSR_Q) != 0); 10163 } 10164 if (mask & CPSR_T) { 10165 env->thumb = ((val & CPSR_T) != 0); 10166 } 10167 if (mask & CPSR_IT_0_1) { 10168 env->condexec_bits &= ~3; 10169 env->condexec_bits |= (val >> 25) & 3; 10170 } 10171 if (mask & CPSR_IT_2_7) { 10172 env->condexec_bits &= 3; 10173 env->condexec_bits |= (val >> 8) & 0xfc; 10174 } 10175 if (mask & CPSR_GE) { 10176 env->GE = (val >> 16) & 0xf; 10177 } 10178 10179 /* 10180 * In a V7 implementation that includes the security extensions but does 10181 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 10182 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 10183 * bits respectively. 10184 * 10185 * In a V8 implementation, it is permitted for privileged software to 10186 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 10187 */ 10188 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 10189 arm_feature(env, ARM_FEATURE_EL3) && 10190 !arm_feature(env, ARM_FEATURE_EL2) && 10191 !arm_is_secure(env)) { 10192 10193 changed_daif = (env->daif ^ val) & mask; 10194 10195 if (changed_daif & CPSR_A) { 10196 /* 10197 * Check to see if we are allowed to change the masking of async 10198 * abort exceptions from a non-secure state. 10199 */ 10200 if (!(env->cp15.scr_el3 & SCR_AW)) { 10201 qemu_log_mask(LOG_GUEST_ERROR, 10202 "Ignoring attempt to switch CPSR_A flag from " 10203 "non-secure world with SCR.AW bit clear\n"); 10204 mask &= ~CPSR_A; 10205 } 10206 } 10207 10208 if (changed_daif & CPSR_F) { 10209 /* 10210 * Check to see if we are allowed to change the masking of FIQ 10211 * exceptions from a non-secure state. 10212 */ 10213 if (!(env->cp15.scr_el3 & SCR_FW)) { 10214 qemu_log_mask(LOG_GUEST_ERROR, 10215 "Ignoring attempt to switch CPSR_F flag from " 10216 "non-secure world with SCR.FW bit clear\n"); 10217 mask &= ~CPSR_F; 10218 } 10219 10220 /* 10221 * Check whether non-maskable FIQ (NMFI) support is enabled. 10222 * If this bit is set software is not allowed to mask 10223 * FIQs, but is allowed to set CPSR_F to 0. 10224 */ 10225 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 10226 (val & CPSR_F)) { 10227 qemu_log_mask(LOG_GUEST_ERROR, 10228 "Ignoring attempt to enable CPSR_F flag " 10229 "(non-maskable FIQ [NMFI] support enabled)\n"); 10230 mask &= ~CPSR_F; 10231 } 10232 } 10233 } 10234 10235 env->daif &= ~(CPSR_AIF & mask); 10236 env->daif |= val & CPSR_AIF & mask; 10237 10238 if (write_type != CPSRWriteRaw && 10239 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 10240 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 10241 /* 10242 * Note that we can only get here in USR mode if this is a 10243 * gdb stub write; for this case we follow the architectural 10244 * behaviour for guest writes in USR mode of ignoring an attempt 10245 * to switch mode. (Those are caught by translate.c for writes 10246 * triggered by guest instructions.) 10247 */ 10248 mask &= ~CPSR_M; 10249 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 10250 /* 10251 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in 10252 * v7, and has defined behaviour in v8: 10253 * + leave CPSR.M untouched 10254 * + allow changes to the other CPSR fields 10255 * + set PSTATE.IL 10256 * For user changes via the GDB stub, we don't set PSTATE.IL, 10257 * as this would be unnecessarily harsh for a user error. 10258 */ 10259 mask &= ~CPSR_M; 10260 if (write_type != CPSRWriteByGDBStub && 10261 arm_feature(env, ARM_FEATURE_V8)) { 10262 mask |= CPSR_IL; 10263 val |= CPSR_IL; 10264 } 10265 qemu_log_mask(LOG_GUEST_ERROR, 10266 "Illegal AArch32 mode switch attempt from %s to %s\n", 10267 aarch32_mode_name(env->uncached_cpsr), 10268 aarch32_mode_name(val)); 10269 } else { 10270 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n", 10271 write_type == CPSRWriteExceptionReturn ? 10272 "Exception return from AArch32" : 10273 "AArch32 mode switch from", 10274 aarch32_mode_name(env->uncached_cpsr), 10275 aarch32_mode_name(val), env->regs[15]); 10276 switch_mode(env, val & CPSR_M); 10277 } 10278 } 10279 mask &= ~CACHED_CPSR_BITS; 10280 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 10281 if (tcg_enabled() && rebuild_hflags) { 10282 arm_rebuild_hflags(env); 10283 } 10284 } 10285 10286 #ifdef CONFIG_USER_ONLY 10287 10288 static void switch_mode(CPUARMState *env, int mode) 10289 { 10290 ARMCPU *cpu = env_archcpu(env); 10291 10292 if (mode != ARM_CPU_MODE_USR) { 10293 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 10294 } 10295 } 10296 10297 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 10298 uint32_t cur_el, bool secure) 10299 { 10300 return 1; 10301 } 10302 10303 void aarch64_sync_64_to_32(CPUARMState *env) 10304 { 10305 g_assert_not_reached(); 10306 } 10307 10308 #else 10309 10310 static void switch_mode(CPUARMState *env, int mode) 10311 { 10312 int old_mode; 10313 int i; 10314 10315 old_mode = env->uncached_cpsr & CPSR_M; 10316 if (mode == old_mode) { 10317 return; 10318 } 10319 10320 if (old_mode == ARM_CPU_MODE_FIQ) { 10321 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 10322 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 10323 } else if (mode == ARM_CPU_MODE_FIQ) { 10324 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 10325 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 10326 } 10327 10328 i = bank_number(old_mode); 10329 env->banked_r13[i] = env->regs[13]; 10330 env->banked_spsr[i] = env->spsr; 10331 10332 i = bank_number(mode); 10333 env->regs[13] = env->banked_r13[i]; 10334 env->spsr = env->banked_spsr[i]; 10335 10336 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14]; 10337 env->regs[14] = env->banked_r14[r14_bank_number(mode)]; 10338 } 10339 10340 /* 10341 * Physical Interrupt Target EL Lookup Table 10342 * 10343 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 10344 * 10345 * The below multi-dimensional table is used for looking up the target 10346 * exception level given numerous condition criteria. Specifically, the 10347 * target EL is based on SCR and HCR routing controls as well as the 10348 * currently executing EL and secure state. 10349 * 10350 * Dimensions: 10351 * target_el_table[2][2][2][2][2][4] 10352 * | | | | | +--- Current EL 10353 * | | | | +------ Non-secure(0)/Secure(1) 10354 * | | | +--------- HCR mask override 10355 * | | +------------ SCR exec state control 10356 * | +--------------- SCR mask override 10357 * +------------------ 32-bit(0)/64-bit(1) EL3 10358 * 10359 * The table values are as such: 10360 * 0-3 = EL0-EL3 10361 * -1 = Cannot occur 10362 * 10363 * The ARM ARM target EL table includes entries indicating that an "exception 10364 * is not taken". The two cases where this is applicable are: 10365 * 1) An exception is taken from EL3 but the SCR does not have the exception 10366 * routed to EL3. 10367 * 2) An exception is taken from EL2 but the HCR does not have the exception 10368 * routed to EL2. 10369 * In these two cases, the below table contain a target of EL1. This value is 10370 * returned as it is expected that the consumer of the table data will check 10371 * for "target EL >= current EL" to ensure the exception is not taken. 10372 * 10373 * SCR HCR 10374 * 64 EA AMO From 10375 * BIT IRQ IMO Non-secure Secure 10376 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 10377 */ 10378 static const int8_t target_el_table[2][2][2][2][2][4] = { 10379 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 10380 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 10381 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 10382 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 10383 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 10384 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 10385 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 10386 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 10387 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 10388 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},}, 10389 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },}, 10390 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},}, 10391 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 10392 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 10393 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },}, 10394 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},}, 10395 }; 10396 10397 /* 10398 * Determine the target EL for physical exceptions 10399 */ 10400 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 10401 uint32_t cur_el, bool secure) 10402 { 10403 CPUARMState *env = cpu_env(cs); 10404 bool rw; 10405 bool scr; 10406 bool hcr; 10407 int target_el; 10408 /* Is the highest EL AArch64? */ 10409 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64); 10410 uint64_t hcr_el2; 10411 10412 if (arm_feature(env, ARM_FEATURE_EL3)) { 10413 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); 10414 } else { 10415 /* 10416 * Either EL2 is the highest EL (and so the EL2 register width 10417 * is given by is64); or there is no EL2 or EL3, in which case 10418 * the value of 'rw' does not affect the table lookup anyway. 10419 */ 10420 rw = is64; 10421 } 10422 10423 hcr_el2 = arm_hcr_el2_eff(env); 10424 switch (excp_idx) { 10425 case EXCP_IRQ: 10426 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 10427 hcr = hcr_el2 & HCR_IMO; 10428 break; 10429 case EXCP_FIQ: 10430 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 10431 hcr = hcr_el2 & HCR_FMO; 10432 break; 10433 default: 10434 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 10435 hcr = hcr_el2 & HCR_AMO; 10436 break; 10437 }; 10438 10439 /* 10440 * For these purposes, TGE and AMO/IMO/FMO both force the 10441 * interrupt to EL2. Fold TGE into the bit extracted above. 10442 */ 10443 hcr |= (hcr_el2 & HCR_TGE) != 0; 10444 10445 /* Perform a table-lookup for the target EL given the current state */ 10446 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 10447 10448 assert(target_el > 0); 10449 10450 return target_el; 10451 } 10452 10453 void arm_log_exception(CPUState *cs) 10454 { 10455 int idx = cs->exception_index; 10456 10457 if (qemu_loglevel_mask(CPU_LOG_INT)) { 10458 const char *exc = NULL; 10459 static const char * const excnames[] = { 10460 [EXCP_UDEF] = "Undefined Instruction", 10461 [EXCP_SWI] = "SVC", 10462 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 10463 [EXCP_DATA_ABORT] = "Data Abort", 10464 [EXCP_IRQ] = "IRQ", 10465 [EXCP_FIQ] = "FIQ", 10466 [EXCP_BKPT] = "Breakpoint", 10467 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 10468 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 10469 [EXCP_HVC] = "Hypervisor Call", 10470 [EXCP_HYP_TRAP] = "Hypervisor Trap", 10471 [EXCP_SMC] = "Secure Monitor Call", 10472 [EXCP_VIRQ] = "Virtual IRQ", 10473 [EXCP_VFIQ] = "Virtual FIQ", 10474 [EXCP_SEMIHOST] = "Semihosting call", 10475 [EXCP_NOCP] = "v7M NOCP UsageFault", 10476 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 10477 [EXCP_STKOF] = "v8M STKOF UsageFault", 10478 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking", 10479 [EXCP_LSERR] = "v8M LSERR UsageFault", 10480 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault", 10481 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault", 10482 [EXCP_VSERR] = "Virtual SERR", 10483 [EXCP_GPC] = "Granule Protection Check", 10484 }; 10485 10486 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 10487 exc = excnames[idx]; 10488 } 10489 if (!exc) { 10490 exc = "unknown"; 10491 } 10492 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n", 10493 idx, exc, cs->cpu_index); 10494 } 10495 } 10496 10497 /* 10498 * Function used to synchronize QEMU's AArch64 register set with AArch32 10499 * register set. This is necessary when switching between AArch32 and AArch64 10500 * execution state. 10501 */ 10502 void aarch64_sync_32_to_64(CPUARMState *env) 10503 { 10504 int i; 10505 uint32_t mode = env->uncached_cpsr & CPSR_M; 10506 10507 /* We can blanket copy R[0:7] to X[0:7] */ 10508 for (i = 0; i < 8; i++) { 10509 env->xregs[i] = env->regs[i]; 10510 } 10511 10512 /* 10513 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 10514 * Otherwise, they come from the banked user regs. 10515 */ 10516 if (mode == ARM_CPU_MODE_FIQ) { 10517 for (i = 8; i < 13; i++) { 10518 env->xregs[i] = env->usr_regs[i - 8]; 10519 } 10520 } else { 10521 for (i = 8; i < 13; i++) { 10522 env->xregs[i] = env->regs[i]; 10523 } 10524 } 10525 10526 /* 10527 * Registers x13-x23 are the various mode SP and FP registers. Registers 10528 * r13 and r14 are only copied if we are in that mode, otherwise we copy 10529 * from the mode banked register. 10530 */ 10531 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 10532 env->xregs[13] = env->regs[13]; 10533 env->xregs[14] = env->regs[14]; 10534 } else { 10535 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 10536 /* HYP is an exception in that it is copied from r14 */ 10537 if (mode == ARM_CPU_MODE_HYP) { 10538 env->xregs[14] = env->regs[14]; 10539 } else { 10540 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)]; 10541 } 10542 } 10543 10544 if (mode == ARM_CPU_MODE_HYP) { 10545 env->xregs[15] = env->regs[13]; 10546 } else { 10547 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 10548 } 10549 10550 if (mode == ARM_CPU_MODE_IRQ) { 10551 env->xregs[16] = env->regs[14]; 10552 env->xregs[17] = env->regs[13]; 10553 } else { 10554 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)]; 10555 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 10556 } 10557 10558 if (mode == ARM_CPU_MODE_SVC) { 10559 env->xregs[18] = env->regs[14]; 10560 env->xregs[19] = env->regs[13]; 10561 } else { 10562 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)]; 10563 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 10564 } 10565 10566 if (mode == ARM_CPU_MODE_ABT) { 10567 env->xregs[20] = env->regs[14]; 10568 env->xregs[21] = env->regs[13]; 10569 } else { 10570 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)]; 10571 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 10572 } 10573 10574 if (mode == ARM_CPU_MODE_UND) { 10575 env->xregs[22] = env->regs[14]; 10576 env->xregs[23] = env->regs[13]; 10577 } else { 10578 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)]; 10579 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 10580 } 10581 10582 /* 10583 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 10584 * mode, then we can copy from r8-r14. Otherwise, we copy from the 10585 * FIQ bank for r8-r14. 10586 */ 10587 if (mode == ARM_CPU_MODE_FIQ) { 10588 for (i = 24; i < 31; i++) { 10589 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 10590 } 10591 } else { 10592 for (i = 24; i < 29; i++) { 10593 env->xregs[i] = env->fiq_regs[i - 24]; 10594 } 10595 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 10596 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)]; 10597 } 10598 10599 env->pc = env->regs[15]; 10600 } 10601 10602 /* 10603 * Function used to synchronize QEMU's AArch32 register set with AArch64 10604 * register set. This is necessary when switching between AArch32 and AArch64 10605 * execution state. 10606 */ 10607 void aarch64_sync_64_to_32(CPUARMState *env) 10608 { 10609 int i; 10610 uint32_t mode = env->uncached_cpsr & CPSR_M; 10611 10612 /* We can blanket copy X[0:7] to R[0:7] */ 10613 for (i = 0; i < 8; i++) { 10614 env->regs[i] = env->xregs[i]; 10615 } 10616 10617 /* 10618 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 10619 * Otherwise, we copy x8-x12 into the banked user regs. 10620 */ 10621 if (mode == ARM_CPU_MODE_FIQ) { 10622 for (i = 8; i < 13; i++) { 10623 env->usr_regs[i - 8] = env->xregs[i]; 10624 } 10625 } else { 10626 for (i = 8; i < 13; i++) { 10627 env->regs[i] = env->xregs[i]; 10628 } 10629 } 10630 10631 /* 10632 * Registers r13 & r14 depend on the current mode. 10633 * If we are in a given mode, we copy the corresponding x registers to r13 10634 * and r14. Otherwise, we copy the x register to the banked r13 and r14 10635 * for the mode. 10636 */ 10637 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 10638 env->regs[13] = env->xregs[13]; 10639 env->regs[14] = env->xregs[14]; 10640 } else { 10641 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 10642 10643 /* 10644 * HYP is an exception in that it does not have its own banked r14 but 10645 * shares the USR r14 10646 */ 10647 if (mode == ARM_CPU_MODE_HYP) { 10648 env->regs[14] = env->xregs[14]; 10649 } else { 10650 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 10651 } 10652 } 10653 10654 if (mode == ARM_CPU_MODE_HYP) { 10655 env->regs[13] = env->xregs[15]; 10656 } else { 10657 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 10658 } 10659 10660 if (mode == ARM_CPU_MODE_IRQ) { 10661 env->regs[14] = env->xregs[16]; 10662 env->regs[13] = env->xregs[17]; 10663 } else { 10664 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 10665 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 10666 } 10667 10668 if (mode == ARM_CPU_MODE_SVC) { 10669 env->regs[14] = env->xregs[18]; 10670 env->regs[13] = env->xregs[19]; 10671 } else { 10672 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 10673 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 10674 } 10675 10676 if (mode == ARM_CPU_MODE_ABT) { 10677 env->regs[14] = env->xregs[20]; 10678 env->regs[13] = env->xregs[21]; 10679 } else { 10680 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 10681 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 10682 } 10683 10684 if (mode == ARM_CPU_MODE_UND) { 10685 env->regs[14] = env->xregs[22]; 10686 env->regs[13] = env->xregs[23]; 10687 } else { 10688 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 10689 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 10690 } 10691 10692 /* 10693 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 10694 * mode, then we can copy to r8-r14. Otherwise, we copy to the 10695 * FIQ bank for r8-r14. 10696 */ 10697 if (mode == ARM_CPU_MODE_FIQ) { 10698 for (i = 24; i < 31; i++) { 10699 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 10700 } 10701 } else { 10702 for (i = 24; i < 29; i++) { 10703 env->fiq_regs[i - 24] = env->xregs[i]; 10704 } 10705 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 10706 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 10707 } 10708 10709 env->regs[15] = env->pc; 10710 } 10711 10712 static void take_aarch32_exception(CPUARMState *env, int new_mode, 10713 uint32_t mask, uint32_t offset, 10714 uint32_t newpc) 10715 { 10716 int new_el; 10717 10718 /* Change the CPU state so as to actually take the exception. */ 10719 switch_mode(env, new_mode); 10720 10721 /* 10722 * For exceptions taken to AArch32 we must clear the SS bit in both 10723 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 10724 */ 10725 env->pstate &= ~PSTATE_SS; 10726 env->spsr = cpsr_read(env); 10727 /* Clear IT bits. */ 10728 env->condexec_bits = 0; 10729 /* Switch to the new mode, and to the correct instruction set. */ 10730 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 10731 10732 /* This must be after mode switching. */ 10733 new_el = arm_current_el(env); 10734 10735 /* Set new mode endianness */ 10736 env->uncached_cpsr &= ~CPSR_E; 10737 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) { 10738 env->uncached_cpsr |= CPSR_E; 10739 } 10740 /* J and IL must always be cleared for exception entry */ 10741 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J); 10742 env->daif |= mask; 10743 10744 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) { 10745 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) { 10746 env->uncached_cpsr |= CPSR_SSBS; 10747 } else { 10748 env->uncached_cpsr &= ~CPSR_SSBS; 10749 } 10750 } 10751 10752 if (new_mode == ARM_CPU_MODE_HYP) { 10753 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0; 10754 env->elr_el[2] = env->regs[15]; 10755 } else { 10756 /* CPSR.PAN is normally preserved preserved unless... */ 10757 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) { 10758 switch (new_el) { 10759 case 3: 10760 if (!arm_is_secure_below_el3(env)) { 10761 /* ... the target is EL3, from non-secure state. */ 10762 env->uncached_cpsr &= ~CPSR_PAN; 10763 break; 10764 } 10765 /* ... the target is EL3, from secure state ... */ 10766 /* fall through */ 10767 case 1: 10768 /* ... the target is EL1 and SCTLR.SPAN is 0. */ 10769 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) { 10770 env->uncached_cpsr |= CPSR_PAN; 10771 } 10772 break; 10773 } 10774 } 10775 /* 10776 * this is a lie, as there was no c1_sys on V4T/V5, but who cares 10777 * and we should just guard the thumb mode on V4 10778 */ 10779 if (arm_feature(env, ARM_FEATURE_V4T)) { 10780 env->thumb = 10781 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 10782 } 10783 env->regs[14] = env->regs[15] + offset; 10784 } 10785 env->regs[15] = newpc; 10786 10787 if (tcg_enabled()) { 10788 arm_rebuild_hflags(env); 10789 } 10790 } 10791 10792 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs) 10793 { 10794 /* 10795 * Handle exception entry to Hyp mode; this is sufficiently 10796 * different to entry to other AArch32 modes that we handle it 10797 * separately here. 10798 * 10799 * The vector table entry used is always the 0x14 Hyp mode entry point, 10800 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp. 10801 * The offset applied to the preferred return address is always zero 10802 * (see DDI0487C.a section G1.12.3). 10803 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values. 10804 */ 10805 uint32_t addr, mask; 10806 ARMCPU *cpu = ARM_CPU(cs); 10807 CPUARMState *env = &cpu->env; 10808 10809 switch (cs->exception_index) { 10810 case EXCP_UDEF: 10811 addr = 0x04; 10812 break; 10813 case EXCP_SWI: 10814 addr = 0x08; 10815 break; 10816 case EXCP_BKPT: 10817 /* Fall through to prefetch abort. */ 10818 case EXCP_PREFETCH_ABORT: 10819 env->cp15.ifar_s = env->exception.vaddress; 10820 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n", 10821 (uint32_t)env->exception.vaddress); 10822 addr = 0x0c; 10823 break; 10824 case EXCP_DATA_ABORT: 10825 env->cp15.dfar_s = env->exception.vaddress; 10826 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n", 10827 (uint32_t)env->exception.vaddress); 10828 addr = 0x10; 10829 break; 10830 case EXCP_IRQ: 10831 addr = 0x18; 10832 break; 10833 case EXCP_FIQ: 10834 addr = 0x1c; 10835 break; 10836 case EXCP_HVC: 10837 addr = 0x08; 10838 break; 10839 case EXCP_HYP_TRAP: 10840 addr = 0x14; 10841 break; 10842 default: 10843 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10844 } 10845 10846 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) { 10847 if (!arm_feature(env, ARM_FEATURE_V8)) { 10848 /* 10849 * QEMU syndrome values are v8-style. v7 has the IL bit 10850 * UNK/SBZP for "field not valid" cases, where v8 uses RES1. 10851 * If this is a v7 CPU, squash the IL bit in those cases. 10852 */ 10853 if (cs->exception_index == EXCP_PREFETCH_ABORT || 10854 (cs->exception_index == EXCP_DATA_ABORT && 10855 !(env->exception.syndrome & ARM_EL_ISV)) || 10856 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) { 10857 env->exception.syndrome &= ~ARM_EL_IL; 10858 } 10859 } 10860 env->cp15.esr_el[2] = env->exception.syndrome; 10861 } 10862 10863 if (arm_current_el(env) != 2 && addr < 0x14) { 10864 addr = 0x14; 10865 } 10866 10867 mask = 0; 10868 if (!(env->cp15.scr_el3 & SCR_EA)) { 10869 mask |= CPSR_A; 10870 } 10871 if (!(env->cp15.scr_el3 & SCR_IRQ)) { 10872 mask |= CPSR_I; 10873 } 10874 if (!(env->cp15.scr_el3 & SCR_FIQ)) { 10875 mask |= CPSR_F; 10876 } 10877 10878 addr += env->cp15.hvbar; 10879 10880 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr); 10881 } 10882 10883 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 10884 { 10885 ARMCPU *cpu = ARM_CPU(cs); 10886 CPUARMState *env = &cpu->env; 10887 uint32_t addr; 10888 uint32_t mask; 10889 int new_mode; 10890 uint32_t offset; 10891 uint32_t moe; 10892 10893 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 10894 switch (syn_get_ec(env->exception.syndrome)) { 10895 case EC_BREAKPOINT: 10896 case EC_BREAKPOINT_SAME_EL: 10897 moe = 1; 10898 break; 10899 case EC_WATCHPOINT: 10900 case EC_WATCHPOINT_SAME_EL: 10901 moe = 10; 10902 break; 10903 case EC_AA32_BKPT: 10904 moe = 3; 10905 break; 10906 case EC_VECTORCATCH: 10907 moe = 5; 10908 break; 10909 default: 10910 moe = 0; 10911 break; 10912 } 10913 10914 if (moe) { 10915 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 10916 } 10917 10918 if (env->exception.target_el == 2) { 10919 arm_cpu_do_interrupt_aarch32_hyp(cs); 10920 return; 10921 } 10922 10923 switch (cs->exception_index) { 10924 case EXCP_UDEF: 10925 new_mode = ARM_CPU_MODE_UND; 10926 addr = 0x04; 10927 mask = CPSR_I; 10928 if (env->thumb) { 10929 offset = 2; 10930 } else { 10931 offset = 4; 10932 } 10933 break; 10934 case EXCP_SWI: 10935 new_mode = ARM_CPU_MODE_SVC; 10936 addr = 0x08; 10937 mask = CPSR_I; 10938 /* The PC already points to the next instruction. */ 10939 offset = 0; 10940 break; 10941 case EXCP_BKPT: 10942 /* Fall through to prefetch abort. */ 10943 case EXCP_PREFETCH_ABORT: 10944 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 10945 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 10946 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 10947 env->exception.fsr, (uint32_t)env->exception.vaddress); 10948 new_mode = ARM_CPU_MODE_ABT; 10949 addr = 0x0c; 10950 mask = CPSR_A | CPSR_I; 10951 offset = 4; 10952 break; 10953 case EXCP_DATA_ABORT: 10954 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 10955 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 10956 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 10957 env->exception.fsr, 10958 (uint32_t)env->exception.vaddress); 10959 new_mode = ARM_CPU_MODE_ABT; 10960 addr = 0x10; 10961 mask = CPSR_A | CPSR_I; 10962 offset = 8; 10963 break; 10964 case EXCP_IRQ: 10965 new_mode = ARM_CPU_MODE_IRQ; 10966 addr = 0x18; 10967 /* Disable IRQ and imprecise data aborts. */ 10968 mask = CPSR_A | CPSR_I; 10969 offset = 4; 10970 if (env->cp15.scr_el3 & SCR_IRQ) { 10971 /* IRQ routed to monitor mode */ 10972 new_mode = ARM_CPU_MODE_MON; 10973 mask |= CPSR_F; 10974 } 10975 break; 10976 case EXCP_FIQ: 10977 new_mode = ARM_CPU_MODE_FIQ; 10978 addr = 0x1c; 10979 /* Disable FIQ, IRQ and imprecise data aborts. */ 10980 mask = CPSR_A | CPSR_I | CPSR_F; 10981 if (env->cp15.scr_el3 & SCR_FIQ) { 10982 /* FIQ routed to monitor mode */ 10983 new_mode = ARM_CPU_MODE_MON; 10984 } 10985 offset = 4; 10986 break; 10987 case EXCP_VIRQ: 10988 new_mode = ARM_CPU_MODE_IRQ; 10989 addr = 0x18; 10990 /* Disable IRQ and imprecise data aborts. */ 10991 mask = CPSR_A | CPSR_I; 10992 offset = 4; 10993 break; 10994 case EXCP_VFIQ: 10995 new_mode = ARM_CPU_MODE_FIQ; 10996 addr = 0x1c; 10997 /* Disable FIQ, IRQ and imprecise data aborts. */ 10998 mask = CPSR_A | CPSR_I | CPSR_F; 10999 offset = 4; 11000 break; 11001 case EXCP_VSERR: 11002 { 11003 /* 11004 * Note that this is reported as a data abort, but the DFAR 11005 * has an UNKNOWN value. Construct the SError syndrome from 11006 * AET and ExT fields. 11007 */ 11008 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, }; 11009 11010 if (extended_addresses_enabled(env)) { 11011 env->exception.fsr = arm_fi_to_lfsc(&fi); 11012 } else { 11013 env->exception.fsr = arm_fi_to_sfsc(&fi); 11014 } 11015 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000; 11016 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 11017 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n", 11018 env->exception.fsr); 11019 11020 new_mode = ARM_CPU_MODE_ABT; 11021 addr = 0x10; 11022 mask = CPSR_A | CPSR_I; 11023 offset = 8; 11024 } 11025 break; 11026 case EXCP_SMC: 11027 new_mode = ARM_CPU_MODE_MON; 11028 addr = 0x08; 11029 mask = CPSR_A | CPSR_I | CPSR_F; 11030 offset = 0; 11031 break; 11032 default: 11033 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 11034 return; /* Never happens. Keep compiler happy. */ 11035 } 11036 11037 if (new_mode == ARM_CPU_MODE_MON) { 11038 addr += env->cp15.mvbar; 11039 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 11040 /* High vectors. When enabled, base address cannot be remapped. */ 11041 addr += 0xffff0000; 11042 } else { 11043 /* 11044 * ARM v7 architectures provide a vector base address register to remap 11045 * the interrupt vector table. 11046 * This register is only followed in non-monitor mode, and is banked. 11047 * Note: only bits 31:5 are valid. 11048 */ 11049 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 11050 } 11051 11052 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 11053 env->cp15.scr_el3 &= ~SCR_NS; 11054 } 11055 11056 take_aarch32_exception(env, new_mode, mask, offset, addr); 11057 } 11058 11059 static int aarch64_regnum(CPUARMState *env, int aarch32_reg) 11060 { 11061 /* 11062 * Return the register number of the AArch64 view of the AArch32 11063 * register @aarch32_reg. The CPUARMState CPSR is assumed to still 11064 * be that of the AArch32 mode the exception came from. 11065 */ 11066 int mode = env->uncached_cpsr & CPSR_M; 11067 11068 switch (aarch32_reg) { 11069 case 0 ... 7: 11070 return aarch32_reg; 11071 case 8 ... 12: 11072 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg; 11073 case 13: 11074 switch (mode) { 11075 case ARM_CPU_MODE_USR: 11076 case ARM_CPU_MODE_SYS: 11077 return 13; 11078 case ARM_CPU_MODE_HYP: 11079 return 15; 11080 case ARM_CPU_MODE_IRQ: 11081 return 17; 11082 case ARM_CPU_MODE_SVC: 11083 return 19; 11084 case ARM_CPU_MODE_ABT: 11085 return 21; 11086 case ARM_CPU_MODE_UND: 11087 return 23; 11088 case ARM_CPU_MODE_FIQ: 11089 return 29; 11090 default: 11091 g_assert_not_reached(); 11092 } 11093 case 14: 11094 switch (mode) { 11095 case ARM_CPU_MODE_USR: 11096 case ARM_CPU_MODE_SYS: 11097 case ARM_CPU_MODE_HYP: 11098 return 14; 11099 case ARM_CPU_MODE_IRQ: 11100 return 16; 11101 case ARM_CPU_MODE_SVC: 11102 return 18; 11103 case ARM_CPU_MODE_ABT: 11104 return 20; 11105 case ARM_CPU_MODE_UND: 11106 return 22; 11107 case ARM_CPU_MODE_FIQ: 11108 return 30; 11109 default: 11110 g_assert_not_reached(); 11111 } 11112 case 15: 11113 return 31; 11114 default: 11115 g_assert_not_reached(); 11116 } 11117 } 11118 11119 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env) 11120 { 11121 uint32_t ret = cpsr_read(env); 11122 11123 /* Move DIT to the correct location for SPSR_ELx */ 11124 if (ret & CPSR_DIT) { 11125 ret &= ~CPSR_DIT; 11126 ret |= PSTATE_DIT; 11127 } 11128 /* Merge PSTATE.SS into SPSR_ELx */ 11129 ret |= env->pstate & PSTATE_SS; 11130 11131 return ret; 11132 } 11133 11134 static bool syndrome_is_sync_extabt(uint32_t syndrome) 11135 { 11136 /* Return true if this syndrome value is a synchronous external abort */ 11137 switch (syn_get_ec(syndrome)) { 11138 case EC_INSNABORT: 11139 case EC_INSNABORT_SAME_EL: 11140 case EC_DATAABORT: 11141 case EC_DATAABORT_SAME_EL: 11142 /* Look at fault status code for all the synchronous ext abort cases */ 11143 switch (syndrome & 0x3f) { 11144 case 0x10: 11145 case 0x13: 11146 case 0x14: 11147 case 0x15: 11148 case 0x16: 11149 case 0x17: 11150 return true; 11151 default: 11152 return false; 11153 } 11154 default: 11155 return false; 11156 } 11157 } 11158 11159 /* Handle exception entry to a target EL which is using AArch64 */ 11160 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 11161 { 11162 ARMCPU *cpu = ARM_CPU(cs); 11163 CPUARMState *env = &cpu->env; 11164 unsigned int new_el = env->exception.target_el; 11165 target_ulong addr = env->cp15.vbar_el[new_el]; 11166 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 11167 unsigned int old_mode; 11168 unsigned int cur_el = arm_current_el(env); 11169 int rt; 11170 11171 if (tcg_enabled()) { 11172 /* 11173 * Note that new_el can never be 0. If cur_el is 0, then 11174 * el0_a64 is is_a64(), else el0_a64 is ignored. 11175 */ 11176 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env)); 11177 } 11178 11179 if (cur_el < new_el) { 11180 /* 11181 * Entry vector offset depends on whether the implemented EL 11182 * immediately lower than the target level is using AArch32 or AArch64 11183 */ 11184 bool is_aa64; 11185 uint64_t hcr; 11186 11187 switch (new_el) { 11188 case 3: 11189 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; 11190 break; 11191 case 2: 11192 hcr = arm_hcr_el2_eff(env); 11193 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 11194 is_aa64 = (hcr & HCR_RW) != 0; 11195 break; 11196 } 11197 /* fall through */ 11198 case 1: 11199 is_aa64 = is_a64(env); 11200 break; 11201 default: 11202 g_assert_not_reached(); 11203 } 11204 11205 if (is_aa64) { 11206 addr += 0x400; 11207 } else { 11208 addr += 0x600; 11209 } 11210 } else if (pstate_read(env) & PSTATE_SP) { 11211 addr += 0x200; 11212 } 11213 11214 switch (cs->exception_index) { 11215 case EXCP_GPC: 11216 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n", 11217 env->cp15.mfar_el3); 11218 /* fall through */ 11219 case EXCP_PREFETCH_ABORT: 11220 case EXCP_DATA_ABORT: 11221 /* 11222 * FEAT_DoubleFault allows synchronous external aborts taken to EL3 11223 * to be taken to the SError vector entrypoint. 11224 */ 11225 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) && 11226 syndrome_is_sync_extabt(env->exception.syndrome)) { 11227 addr += 0x180; 11228 } 11229 env->cp15.far_el[new_el] = env->exception.vaddress; 11230 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 11231 env->cp15.far_el[new_el]); 11232 /* fall through */ 11233 case EXCP_BKPT: 11234 case EXCP_UDEF: 11235 case EXCP_SWI: 11236 case EXCP_HVC: 11237 case EXCP_HYP_TRAP: 11238 case EXCP_SMC: 11239 switch (syn_get_ec(env->exception.syndrome)) { 11240 case EC_ADVSIMDFPACCESSTRAP: 11241 /* 11242 * QEMU internal FP/SIMD syndromes from AArch32 include the 11243 * TA and coproc fields which are only exposed if the exception 11244 * is taken to AArch32 Hyp mode. Mask them out to get a valid 11245 * AArch64 format syndrome. 11246 */ 11247 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20); 11248 break; 11249 case EC_CP14RTTRAP: 11250 case EC_CP15RTTRAP: 11251 case EC_CP14DTTRAP: 11252 /* 11253 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently 11254 * the raw register field from the insn; when taking this to 11255 * AArch64 we must convert it to the AArch64 view of the register 11256 * number. Notice that we read a 4-bit AArch32 register number and 11257 * write back a 5-bit AArch64 one. 11258 */ 11259 rt = extract32(env->exception.syndrome, 5, 4); 11260 rt = aarch64_regnum(env, rt); 11261 env->exception.syndrome = deposit32(env->exception.syndrome, 11262 5, 5, rt); 11263 break; 11264 case EC_CP15RRTTRAP: 11265 case EC_CP14RRTTRAP: 11266 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */ 11267 rt = extract32(env->exception.syndrome, 5, 4); 11268 rt = aarch64_regnum(env, rt); 11269 env->exception.syndrome = deposit32(env->exception.syndrome, 11270 5, 5, rt); 11271 rt = extract32(env->exception.syndrome, 10, 4); 11272 rt = aarch64_regnum(env, rt); 11273 env->exception.syndrome = deposit32(env->exception.syndrome, 11274 10, 5, rt); 11275 break; 11276 } 11277 env->cp15.esr_el[new_el] = env->exception.syndrome; 11278 break; 11279 case EXCP_IRQ: 11280 case EXCP_VIRQ: 11281 addr += 0x80; 11282 break; 11283 case EXCP_FIQ: 11284 case EXCP_VFIQ: 11285 addr += 0x100; 11286 break; 11287 case EXCP_VSERR: 11288 addr += 0x180; 11289 /* Construct the SError syndrome from IDS and ISS fields. */ 11290 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff); 11291 env->cp15.esr_el[new_el] = env->exception.syndrome; 11292 break; 11293 default: 11294 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 11295 } 11296 11297 if (is_a64(env)) { 11298 old_mode = pstate_read(env); 11299 aarch64_save_sp(env, arm_current_el(env)); 11300 env->elr_el[new_el] = env->pc; 11301 11302 if (cur_el == 1 && new_el == 1 && 11303 ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == HCR_NV)) { 11304 /* I_ZJRNN: report EL2 in the SPSR by setting M[3:2] to 0b10 */ 11305 old_mode = deposit32(old_mode, 2, 2, 2); 11306 } 11307 } else { 11308 old_mode = cpsr_read_for_spsr_elx(env); 11309 env->elr_el[new_el] = env->regs[15]; 11310 11311 aarch64_sync_32_to_64(env); 11312 11313 env->condexec_bits = 0; 11314 } 11315 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode; 11316 11317 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 11318 env->elr_el[new_el]); 11319 11320 if (cpu_isar_feature(aa64_pan, cpu)) { 11321 /* The value of PSTATE.PAN is normally preserved, except when ... */ 11322 new_mode |= old_mode & PSTATE_PAN; 11323 switch (new_el) { 11324 case 2: 11325 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */ 11326 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) 11327 != (HCR_E2H | HCR_TGE)) { 11328 break; 11329 } 11330 /* fall through */ 11331 case 1: 11332 /* ... the target is EL1 ... */ 11333 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */ 11334 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) { 11335 new_mode |= PSTATE_PAN; 11336 } 11337 break; 11338 } 11339 } 11340 if (cpu_isar_feature(aa64_mte, cpu)) { 11341 new_mode |= PSTATE_TCO; 11342 } 11343 11344 if (cpu_isar_feature(aa64_ssbs, cpu)) { 11345 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) { 11346 new_mode |= PSTATE_SSBS; 11347 } else { 11348 new_mode &= ~PSTATE_SSBS; 11349 } 11350 } 11351 11352 pstate_write(env, PSTATE_DAIF | new_mode); 11353 env->aarch64 = true; 11354 aarch64_restore_sp(env, new_el); 11355 11356 if (tcg_enabled()) { 11357 helper_rebuild_hflags_a64(env, new_el); 11358 } 11359 11360 env->pc = addr; 11361 11362 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 11363 new_el, env->pc, pstate_read(env)); 11364 } 11365 11366 /* 11367 * Do semihosting call and set the appropriate return value. All the 11368 * permission and validity checks have been done at translate time. 11369 * 11370 * We only see semihosting exceptions in TCG only as they are not 11371 * trapped to the hypervisor in KVM. 11372 */ 11373 #ifdef CONFIG_TCG 11374 static void tcg_handle_semihosting(CPUState *cs) 11375 { 11376 ARMCPU *cpu = ARM_CPU(cs); 11377 CPUARMState *env = &cpu->env; 11378 11379 if (is_a64(env)) { 11380 qemu_log_mask(CPU_LOG_INT, 11381 "...handling as semihosting call 0x%" PRIx64 "\n", 11382 env->xregs[0]); 11383 do_common_semihosting(cs); 11384 env->pc += 4; 11385 } else { 11386 qemu_log_mask(CPU_LOG_INT, 11387 "...handling as semihosting call 0x%x\n", 11388 env->regs[0]); 11389 do_common_semihosting(cs); 11390 env->regs[15] += env->thumb ? 2 : 4; 11391 } 11392 } 11393 #endif 11394 11395 /* 11396 * Handle a CPU exception for A and R profile CPUs. 11397 * Do any appropriate logging, handle PSCI calls, and then hand off 11398 * to the AArch64-entry or AArch32-entry function depending on the 11399 * target exception level's register width. 11400 * 11401 * Note: this is used for both TCG (as the do_interrupt tcg op), 11402 * and KVM to re-inject guest debug exceptions, and to 11403 * inject a Synchronous-External-Abort. 11404 */ 11405 void arm_cpu_do_interrupt(CPUState *cs) 11406 { 11407 ARMCPU *cpu = ARM_CPU(cs); 11408 CPUARMState *env = &cpu->env; 11409 unsigned int new_el = env->exception.target_el; 11410 11411 assert(!arm_feature(env, ARM_FEATURE_M)); 11412 11413 arm_log_exception(cs); 11414 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 11415 new_el); 11416 if (qemu_loglevel_mask(CPU_LOG_INT) 11417 && !excp_is_internal(cs->exception_index)) { 11418 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 11419 syn_get_ec(env->exception.syndrome), 11420 env->exception.syndrome); 11421 } 11422 11423 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) { 11424 arm_handle_psci_call(cpu); 11425 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 11426 return; 11427 } 11428 11429 /* 11430 * Semihosting semantics depend on the register width of the code 11431 * that caused the exception, not the target exception level, so 11432 * must be handled here. 11433 */ 11434 #ifdef CONFIG_TCG 11435 if (cs->exception_index == EXCP_SEMIHOST) { 11436 tcg_handle_semihosting(cs); 11437 return; 11438 } 11439 #endif 11440 11441 /* 11442 * Hooks may change global state so BQL should be held, also the 11443 * BQL needs to be held for any modification of 11444 * cs->interrupt_request. 11445 */ 11446 g_assert(bql_locked()); 11447 11448 arm_call_pre_el_change_hook(cpu); 11449 11450 assert(!excp_is_internal(cs->exception_index)); 11451 if (arm_el_is_aa64(env, new_el)) { 11452 arm_cpu_do_interrupt_aarch64(cs); 11453 } else { 11454 arm_cpu_do_interrupt_aarch32(cs); 11455 } 11456 11457 arm_call_el_change_hook(cpu); 11458 11459 if (!kvm_enabled()) { 11460 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 11461 } 11462 } 11463 #endif /* !CONFIG_USER_ONLY */ 11464 11465 uint64_t arm_sctlr(CPUARMState *env, int el) 11466 { 11467 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */ 11468 if (el == 0) { 11469 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0); 11470 el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1; 11471 } 11472 return env->cp15.sctlr_el[el]; 11473 } 11474 11475 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx) 11476 { 11477 if (regime_has_2_ranges(mmu_idx)) { 11478 return extract64(tcr, 37, 2); 11479 } else if (regime_is_stage2(mmu_idx)) { 11480 return 0; /* VTCR_EL2 */ 11481 } else { 11482 /* Replicate the single TBI bit so we always have 2 bits. */ 11483 return extract32(tcr, 20, 1) * 3; 11484 } 11485 } 11486 11487 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx) 11488 { 11489 if (regime_has_2_ranges(mmu_idx)) { 11490 return extract64(tcr, 51, 2); 11491 } else if (regime_is_stage2(mmu_idx)) { 11492 return 0; /* VTCR_EL2 */ 11493 } else { 11494 /* Replicate the single TBID bit so we always have 2 bits. */ 11495 return extract32(tcr, 29, 1) * 3; 11496 } 11497 } 11498 11499 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) 11500 { 11501 if (regime_has_2_ranges(mmu_idx)) { 11502 return extract64(tcr, 57, 2); 11503 } else { 11504 /* Replicate the single TCMA bit so we always have 2 bits. */ 11505 return extract32(tcr, 30, 1) * 3; 11506 } 11507 } 11508 11509 static ARMGranuleSize tg0_to_gran_size(int tg) 11510 { 11511 switch (tg) { 11512 case 0: 11513 return Gran4K; 11514 case 1: 11515 return Gran64K; 11516 case 2: 11517 return Gran16K; 11518 default: 11519 return GranInvalid; 11520 } 11521 } 11522 11523 static ARMGranuleSize tg1_to_gran_size(int tg) 11524 { 11525 switch (tg) { 11526 case 1: 11527 return Gran16K; 11528 case 2: 11529 return Gran4K; 11530 case 3: 11531 return Gran64K; 11532 default: 11533 return GranInvalid; 11534 } 11535 } 11536 11537 static inline bool have4k(ARMCPU *cpu, bool stage2) 11538 { 11539 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu) 11540 : cpu_isar_feature(aa64_tgran4, cpu); 11541 } 11542 11543 static inline bool have16k(ARMCPU *cpu, bool stage2) 11544 { 11545 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu) 11546 : cpu_isar_feature(aa64_tgran16, cpu); 11547 } 11548 11549 static inline bool have64k(ARMCPU *cpu, bool stage2) 11550 { 11551 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu) 11552 : cpu_isar_feature(aa64_tgran64, cpu); 11553 } 11554 11555 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran, 11556 bool stage2) 11557 { 11558 switch (gran) { 11559 case Gran4K: 11560 if (have4k(cpu, stage2)) { 11561 return gran; 11562 } 11563 break; 11564 case Gran16K: 11565 if (have16k(cpu, stage2)) { 11566 return gran; 11567 } 11568 break; 11569 case Gran64K: 11570 if (have64k(cpu, stage2)) { 11571 return gran; 11572 } 11573 break; 11574 case GranInvalid: 11575 break; 11576 } 11577 /* 11578 * If the guest selects a granule size that isn't implemented, 11579 * the architecture requires that we behave as if it selected one 11580 * that is (with an IMPDEF choice of which one to pick). We choose 11581 * to implement the smallest supported granule size. 11582 */ 11583 if (have4k(cpu, stage2)) { 11584 return Gran4K; 11585 } 11586 if (have16k(cpu, stage2)) { 11587 return Gran16K; 11588 } 11589 assert(have64k(cpu, stage2)); 11590 return Gran64K; 11591 } 11592 11593 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va, 11594 ARMMMUIdx mmu_idx, bool data, 11595 bool el1_is_aa32) 11596 { 11597 uint64_t tcr = regime_tcr(env, mmu_idx); 11598 bool epd, hpd, tsz_oob, ds, ha, hd; 11599 int select, tsz, tbi, max_tsz, min_tsz, ps, sh; 11600 ARMGranuleSize gran; 11601 ARMCPU *cpu = env_archcpu(env); 11602 bool stage2 = regime_is_stage2(mmu_idx); 11603 11604 if (!regime_has_2_ranges(mmu_idx)) { 11605 select = 0; 11606 tsz = extract32(tcr, 0, 6); 11607 gran = tg0_to_gran_size(extract32(tcr, 14, 2)); 11608 if (stage2) { 11609 /* VTCR_EL2 */ 11610 hpd = false; 11611 } else { 11612 hpd = extract32(tcr, 24, 1); 11613 } 11614 epd = false; 11615 sh = extract32(tcr, 12, 2); 11616 ps = extract32(tcr, 16, 3); 11617 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu); 11618 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu); 11619 ds = extract64(tcr, 32, 1); 11620 } else { 11621 bool e0pd; 11622 11623 /* 11624 * Bit 55 is always between the two regions, and is canonical for 11625 * determining if address tagging is enabled. 11626 */ 11627 select = extract64(va, 55, 1); 11628 if (!select) { 11629 tsz = extract32(tcr, 0, 6); 11630 gran = tg0_to_gran_size(extract32(tcr, 14, 2)); 11631 epd = extract32(tcr, 7, 1); 11632 sh = extract32(tcr, 12, 2); 11633 hpd = extract64(tcr, 41, 1); 11634 e0pd = extract64(tcr, 55, 1); 11635 } else { 11636 tsz = extract32(tcr, 16, 6); 11637 gran = tg1_to_gran_size(extract32(tcr, 30, 2)); 11638 epd = extract32(tcr, 23, 1); 11639 sh = extract32(tcr, 28, 2); 11640 hpd = extract64(tcr, 42, 1); 11641 e0pd = extract64(tcr, 56, 1); 11642 } 11643 ps = extract64(tcr, 32, 3); 11644 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu); 11645 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu); 11646 ds = extract64(tcr, 59, 1); 11647 11648 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) && 11649 regime_is_user(env, mmu_idx)) { 11650 epd = true; 11651 } 11652 } 11653 11654 gran = sanitize_gran_size(cpu, gran, stage2); 11655 11656 if (cpu_isar_feature(aa64_st, cpu)) { 11657 max_tsz = 48 - (gran == Gran64K); 11658 } else { 11659 max_tsz = 39; 11660 } 11661 11662 /* 11663 * DS is RES0 unless FEAT_LPA2 is supported for the given page size; 11664 * adjust the effective value of DS, as documented. 11665 */ 11666 min_tsz = 16; 11667 if (gran == Gran64K) { 11668 if (cpu_isar_feature(aa64_lva, cpu)) { 11669 min_tsz = 12; 11670 } 11671 ds = false; 11672 } else if (ds) { 11673 if (regime_is_stage2(mmu_idx)) { 11674 if (gran == Gran16K) { 11675 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu); 11676 } else { 11677 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu); 11678 } 11679 } else { 11680 if (gran == Gran16K) { 11681 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu); 11682 } else { 11683 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu); 11684 } 11685 } 11686 if (ds) { 11687 min_tsz = 12; 11688 } 11689 } 11690 11691 if (stage2 && el1_is_aa32) { 11692 /* 11693 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements 11694 * are loosened: a configured IPA of 40 bits is permitted even if 11695 * the implemented PA is less than that (and so a 40 bit IPA would 11696 * fault for an AArch64 EL1). See R_DTLMN. 11697 */ 11698 min_tsz = MIN(min_tsz, 24); 11699 } 11700 11701 if (tsz > max_tsz) { 11702 tsz = max_tsz; 11703 tsz_oob = true; 11704 } else if (tsz < min_tsz) { 11705 tsz = min_tsz; 11706 tsz_oob = true; 11707 } else { 11708 tsz_oob = false; 11709 } 11710 11711 /* Present TBI as a composite with TBID. */ 11712 tbi = aa64_va_parameter_tbi(tcr, mmu_idx); 11713 if (!data) { 11714 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx); 11715 } 11716 tbi = (tbi >> select) & 1; 11717 11718 return (ARMVAParameters) { 11719 .tsz = tsz, 11720 .ps = ps, 11721 .sh = sh, 11722 .select = select, 11723 .tbi = tbi, 11724 .epd = epd, 11725 .hpd = hpd, 11726 .tsz_oob = tsz_oob, 11727 .ds = ds, 11728 .ha = ha, 11729 .hd = ha && hd, 11730 .gran = gran, 11731 }; 11732 } 11733 11734 /* 11735 * Note that signed overflow is undefined in C. The following routines are 11736 * careful to use unsigned types where modulo arithmetic is required. 11737 * Failure to do so _will_ break on newer gcc. 11738 */ 11739 11740 /* Signed saturating arithmetic. */ 11741 11742 /* Perform 16-bit signed saturating addition. */ 11743 static inline uint16_t add16_sat(uint16_t a, uint16_t b) 11744 { 11745 uint16_t res; 11746 11747 res = a + b; 11748 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { 11749 if (a & 0x8000) { 11750 res = 0x8000; 11751 } else { 11752 res = 0x7fff; 11753 } 11754 } 11755 return res; 11756 } 11757 11758 /* Perform 8-bit signed saturating addition. */ 11759 static inline uint8_t add8_sat(uint8_t a, uint8_t b) 11760 { 11761 uint8_t res; 11762 11763 res = a + b; 11764 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { 11765 if (a & 0x80) { 11766 res = 0x80; 11767 } else { 11768 res = 0x7f; 11769 } 11770 } 11771 return res; 11772 } 11773 11774 /* Perform 16-bit signed saturating subtraction. */ 11775 static inline uint16_t sub16_sat(uint16_t a, uint16_t b) 11776 { 11777 uint16_t res; 11778 11779 res = a - b; 11780 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { 11781 if (a & 0x8000) { 11782 res = 0x8000; 11783 } else { 11784 res = 0x7fff; 11785 } 11786 } 11787 return res; 11788 } 11789 11790 /* Perform 8-bit signed saturating subtraction. */ 11791 static inline uint8_t sub8_sat(uint8_t a, uint8_t b) 11792 { 11793 uint8_t res; 11794 11795 res = a - b; 11796 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { 11797 if (a & 0x80) { 11798 res = 0x80; 11799 } else { 11800 res = 0x7f; 11801 } 11802 } 11803 return res; 11804 } 11805 11806 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); 11807 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); 11808 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); 11809 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); 11810 #define PFX q 11811 11812 #include "op_addsub.h" 11813 11814 /* Unsigned saturating arithmetic. */ 11815 static inline uint16_t add16_usat(uint16_t a, uint16_t b) 11816 { 11817 uint16_t res; 11818 res = a + b; 11819 if (res < a) { 11820 res = 0xffff; 11821 } 11822 return res; 11823 } 11824 11825 static inline uint16_t sub16_usat(uint16_t a, uint16_t b) 11826 { 11827 if (a > b) { 11828 return a - b; 11829 } else { 11830 return 0; 11831 } 11832 } 11833 11834 static inline uint8_t add8_usat(uint8_t a, uint8_t b) 11835 { 11836 uint8_t res; 11837 res = a + b; 11838 if (res < a) { 11839 res = 0xff; 11840 } 11841 return res; 11842 } 11843 11844 static inline uint8_t sub8_usat(uint8_t a, uint8_t b) 11845 { 11846 if (a > b) { 11847 return a - b; 11848 } else { 11849 return 0; 11850 } 11851 } 11852 11853 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); 11854 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); 11855 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); 11856 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); 11857 #define PFX uq 11858 11859 #include "op_addsub.h" 11860 11861 /* Signed modulo arithmetic. */ 11862 #define SARITH16(a, b, n, op) do { \ 11863 int32_t sum; \ 11864 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ 11865 RESULT(sum, n, 16); \ 11866 if (sum >= 0) \ 11867 ge |= 3 << (n * 2); \ 11868 } while (0) 11869 11870 #define SARITH8(a, b, n, op) do { \ 11871 int32_t sum; \ 11872 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ 11873 RESULT(sum, n, 8); \ 11874 if (sum >= 0) \ 11875 ge |= 1 << n; \ 11876 } while (0) 11877 11878 11879 #define ADD16(a, b, n) SARITH16(a, b, n, +) 11880 #define SUB16(a, b, n) SARITH16(a, b, n, -) 11881 #define ADD8(a, b, n) SARITH8(a, b, n, +) 11882 #define SUB8(a, b, n) SARITH8(a, b, n, -) 11883 #define PFX s 11884 #define ARITH_GE 11885 11886 #include "op_addsub.h" 11887 11888 /* Unsigned modulo arithmetic. */ 11889 #define ADD16(a, b, n) do { \ 11890 uint32_t sum; \ 11891 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ 11892 RESULT(sum, n, 16); \ 11893 if ((sum >> 16) == 1) \ 11894 ge |= 3 << (n * 2); \ 11895 } while (0) 11896 11897 #define ADD8(a, b, n) do { \ 11898 uint32_t sum; \ 11899 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ 11900 RESULT(sum, n, 8); \ 11901 if ((sum >> 8) == 1) \ 11902 ge |= 1 << n; \ 11903 } while (0) 11904 11905 #define SUB16(a, b, n) do { \ 11906 uint32_t sum; \ 11907 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ 11908 RESULT(sum, n, 16); \ 11909 if ((sum >> 16) == 0) \ 11910 ge |= 3 << (n * 2); \ 11911 } while (0) 11912 11913 #define SUB8(a, b, n) do { \ 11914 uint32_t sum; \ 11915 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ 11916 RESULT(sum, n, 8); \ 11917 if ((sum >> 8) == 0) \ 11918 ge |= 1 << n; \ 11919 } while (0) 11920 11921 #define PFX u 11922 #define ARITH_GE 11923 11924 #include "op_addsub.h" 11925 11926 /* Halved signed arithmetic. */ 11927 #define ADD16(a, b, n) \ 11928 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) 11929 #define SUB16(a, b, n) \ 11930 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) 11931 #define ADD8(a, b, n) \ 11932 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) 11933 #define SUB8(a, b, n) \ 11934 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) 11935 #define PFX sh 11936 11937 #include "op_addsub.h" 11938 11939 /* Halved unsigned arithmetic. */ 11940 #define ADD16(a, b, n) \ 11941 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) 11942 #define SUB16(a, b, n) \ 11943 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) 11944 #define ADD8(a, b, n) \ 11945 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) 11946 #define SUB8(a, b, n) \ 11947 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) 11948 #define PFX uh 11949 11950 #include "op_addsub.h" 11951 11952 static inline uint8_t do_usad(uint8_t a, uint8_t b) 11953 { 11954 if (a > b) { 11955 return a - b; 11956 } else { 11957 return b - a; 11958 } 11959 } 11960 11961 /* Unsigned sum of absolute byte differences. */ 11962 uint32_t HELPER(usad8)(uint32_t a, uint32_t b) 11963 { 11964 uint32_t sum; 11965 sum = do_usad(a, b); 11966 sum += do_usad(a >> 8, b >> 8); 11967 sum += do_usad(a >> 16, b >> 16); 11968 sum += do_usad(a >> 24, b >> 24); 11969 return sum; 11970 } 11971 11972 /* For ARMv6 SEL instruction. */ 11973 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) 11974 { 11975 uint32_t mask; 11976 11977 mask = 0; 11978 if (flags & 1) { 11979 mask |= 0xff; 11980 } 11981 if (flags & 2) { 11982 mask |= 0xff00; 11983 } 11984 if (flags & 4) { 11985 mask |= 0xff0000; 11986 } 11987 if (flags & 8) { 11988 mask |= 0xff000000; 11989 } 11990 return (a & mask) | (b & ~mask); 11991 } 11992 11993 /* 11994 * CRC helpers. 11995 * The upper bytes of val (above the number specified by 'bytes') must have 11996 * been zeroed out by the caller. 11997 */ 11998 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) 11999 { 12000 uint8_t buf[4]; 12001 12002 stl_le_p(buf, val); 12003 12004 /* zlib crc32 converts the accumulator and output to one's complement. */ 12005 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; 12006 } 12007 12008 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) 12009 { 12010 uint8_t buf[4]; 12011 12012 stl_le_p(buf, val); 12013 12014 /* Linux crc32c converts the output to one's complement. */ 12015 return crc32c(acc, buf, bytes) ^ 0xffffffff; 12016 } 12017 12018 /* 12019 * Return the exception level to which FP-disabled exceptions should 12020 * be taken, or 0 if FP is enabled. 12021 */ 12022 int fp_exception_el(CPUARMState *env, int cur_el) 12023 { 12024 #ifndef CONFIG_USER_ONLY 12025 uint64_t hcr_el2; 12026 12027 /* 12028 * CPACR and the CPTR registers don't exist before v6, so FP is 12029 * always accessible 12030 */ 12031 if (!arm_feature(env, ARM_FEATURE_V6)) { 12032 return 0; 12033 } 12034 12035 if (arm_feature(env, ARM_FEATURE_M)) { 12036 /* CPACR can cause a NOCP UsageFault taken to current security state */ 12037 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) { 12038 return 1; 12039 } 12040 12041 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) { 12042 if (!extract32(env->v7m.nsacr, 10, 1)) { 12043 /* FP insns cause a NOCP UsageFault taken to Secure */ 12044 return 3; 12045 } 12046 } 12047 12048 return 0; 12049 } 12050 12051 hcr_el2 = arm_hcr_el2_eff(env); 12052 12053 /* 12054 * The CPACR controls traps to EL1, or PL1 if we're 32 bit: 12055 * 0, 2 : trap EL0 and EL1/PL1 accesses 12056 * 1 : trap only EL0 accesses 12057 * 3 : trap no accesses 12058 * This register is ignored if E2H+TGE are both set. 12059 */ 12060 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 12061 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN); 12062 12063 switch (fpen) { 12064 case 1: 12065 if (cur_el != 0) { 12066 break; 12067 } 12068 /* fall through */ 12069 case 0: 12070 case 2: 12071 /* Trap from Secure PL0 or PL1 to Secure PL1. */ 12072 if (!arm_el_is_aa64(env, 3) 12073 && (cur_el == 3 || arm_is_secure_below_el3(env))) { 12074 return 3; 12075 } 12076 if (cur_el <= 1) { 12077 return 1; 12078 } 12079 break; 12080 } 12081 } 12082 12083 /* 12084 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode 12085 * to control non-secure access to the FPU. It doesn't have any 12086 * effect if EL3 is AArch64 or if EL3 doesn't exist at all. 12087 */ 12088 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 12089 cur_el <= 2 && !arm_is_secure_below_el3(env))) { 12090 if (!extract32(env->cp15.nsacr, 10, 1)) { 12091 /* FP insns act as UNDEF */ 12092 return cur_el == 2 ? 2 : 1; 12093 } 12094 } 12095 12096 /* 12097 * CPTR_EL2 is present in v7VE or v8, and changes format 12098 * with HCR_EL2.E2H (regardless of TGE). 12099 */ 12100 if (cur_el <= 2) { 12101 if (hcr_el2 & HCR_E2H) { 12102 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) { 12103 case 1: 12104 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) { 12105 break; 12106 } 12107 /* fall through */ 12108 case 0: 12109 case 2: 12110 return 2; 12111 } 12112 } else if (arm_is_el2_enabled(env)) { 12113 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) { 12114 return 2; 12115 } 12116 } 12117 } 12118 12119 /* CPTR_EL3 : present in v8 */ 12120 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) { 12121 /* Trap all FP ops to EL3 */ 12122 return 3; 12123 } 12124 #endif 12125 return 0; 12126 } 12127 12128 /* Return the exception level we're running at if this is our mmu_idx */ 12129 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx) 12130 { 12131 if (mmu_idx & ARM_MMU_IDX_M) { 12132 return mmu_idx & ARM_MMU_IDX_M_PRIV; 12133 } 12134 12135 switch (mmu_idx) { 12136 case ARMMMUIdx_E10_0: 12137 case ARMMMUIdx_E20_0: 12138 return 0; 12139 case ARMMMUIdx_E10_1: 12140 case ARMMMUIdx_E10_1_PAN: 12141 return 1; 12142 case ARMMMUIdx_E2: 12143 case ARMMMUIdx_E20_2: 12144 case ARMMMUIdx_E20_2_PAN: 12145 return 2; 12146 case ARMMMUIdx_E3: 12147 return 3; 12148 default: 12149 g_assert_not_reached(); 12150 } 12151 } 12152 12153 #ifndef CONFIG_TCG 12154 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate) 12155 { 12156 g_assert_not_reached(); 12157 } 12158 #endif 12159 12160 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el) 12161 { 12162 ARMMMUIdx idx; 12163 uint64_t hcr; 12164 12165 if (arm_feature(env, ARM_FEATURE_M)) { 12166 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure); 12167 } 12168 12169 /* See ARM pseudo-function ELIsInHost. */ 12170 switch (el) { 12171 case 0: 12172 hcr = arm_hcr_el2_eff(env); 12173 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 12174 idx = ARMMMUIdx_E20_0; 12175 } else { 12176 idx = ARMMMUIdx_E10_0; 12177 } 12178 break; 12179 case 1: 12180 if (arm_pan_enabled(env)) { 12181 idx = ARMMMUIdx_E10_1_PAN; 12182 } else { 12183 idx = ARMMMUIdx_E10_1; 12184 } 12185 break; 12186 case 2: 12187 /* Note that TGE does not apply at EL2. */ 12188 if (arm_hcr_el2_eff(env) & HCR_E2H) { 12189 if (arm_pan_enabled(env)) { 12190 idx = ARMMMUIdx_E20_2_PAN; 12191 } else { 12192 idx = ARMMMUIdx_E20_2; 12193 } 12194 } else { 12195 idx = ARMMMUIdx_E2; 12196 } 12197 break; 12198 case 3: 12199 return ARMMMUIdx_E3; 12200 default: 12201 g_assert_not_reached(); 12202 } 12203 12204 return idx; 12205 } 12206 12207 ARMMMUIdx arm_mmu_idx(CPUARMState *env) 12208 { 12209 return arm_mmu_idx_el(env, arm_current_el(env)); 12210 } 12211 12212 static bool mve_no_pred(CPUARMState *env) 12213 { 12214 /* 12215 * Return true if there is definitely no predication of MVE 12216 * instructions by VPR or LTPSIZE. (Returning false even if there 12217 * isn't any predication is OK; generated code will just be 12218 * a little worse.) 12219 * If the CPU does not implement MVE then this TB flag is always 0. 12220 * 12221 * NOTE: if you change this logic, the "recalculate s->mve_no_pred" 12222 * logic in gen_update_fp_context() needs to be updated to match. 12223 * 12224 * We do not include the effect of the ECI bits here -- they are 12225 * tracked in other TB flags. This simplifies the logic for 12226 * "when did we emit code that changes the MVE_NO_PRED TB flag 12227 * and thus need to end the TB?". 12228 */ 12229 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) { 12230 return false; 12231 } 12232 if (env->v7m.vpr) { 12233 return false; 12234 } 12235 if (env->v7m.ltpsize < 4) { 12236 return false; 12237 } 12238 return true; 12239 } 12240 12241 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc, 12242 uint64_t *cs_base, uint32_t *pflags) 12243 { 12244 CPUARMTBFlags flags; 12245 12246 assert_hflags_rebuild_correctly(env); 12247 flags = env->hflags; 12248 12249 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) { 12250 *pc = env->pc; 12251 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) { 12252 DP_TBFLAG_A64(flags, BTYPE, env->btype); 12253 } 12254 } else { 12255 *pc = env->regs[15]; 12256 12257 if (arm_feature(env, ARM_FEATURE_M)) { 12258 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 12259 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) 12260 != env->v7m.secure) { 12261 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1); 12262 } 12263 12264 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) && 12265 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) || 12266 (env->v7m.secure && 12267 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) { 12268 /* 12269 * ASPEN is set, but FPCA/SFPA indicate that there is no 12270 * active FP context; we must create a new FP context before 12271 * executing any FP insn. 12272 */ 12273 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1); 12274 } 12275 12276 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 12277 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) { 12278 DP_TBFLAG_M32(flags, LSPACT, 1); 12279 } 12280 12281 if (mve_no_pred(env)) { 12282 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1); 12283 } 12284 } else { 12285 /* 12286 * Note that XSCALE_CPAR shares bits with VECSTRIDE. 12287 * Note that VECLEN+VECSTRIDE are RES0 for M-profile. 12288 */ 12289 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 12290 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar); 12291 } else { 12292 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len); 12293 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride); 12294 } 12295 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) { 12296 DP_TBFLAG_A32(flags, VFPEN, 1); 12297 } 12298 } 12299 12300 DP_TBFLAG_AM32(flags, THUMB, env->thumb); 12301 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits); 12302 } 12303 12304 /* 12305 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 12306 * states defined in the ARM ARM for software singlestep: 12307 * SS_ACTIVE PSTATE.SS State 12308 * 0 x Inactive (the TB flag for SS is always 0) 12309 * 1 0 Active-pending 12310 * 1 1 Active-not-pending 12311 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB. 12312 */ 12313 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) { 12314 DP_TBFLAG_ANY(flags, PSTATE__SS, 1); 12315 } 12316 12317 *pflags = flags.flags; 12318 *cs_base = flags.flags2; 12319 } 12320 12321 #ifdef TARGET_AARCH64 12322 /* 12323 * The manual says that when SVE is enabled and VQ is widened the 12324 * implementation is allowed to zero the previously inaccessible 12325 * portion of the registers. The corollary to that is that when 12326 * SVE is enabled and VQ is narrowed we are also allowed to zero 12327 * the now inaccessible portion of the registers. 12328 * 12329 * The intent of this is that no predicate bit beyond VQ is ever set. 12330 * Which means that some operations on predicate registers themselves 12331 * may operate on full uint64_t or even unrolled across the maximum 12332 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally 12333 * may well be cheaper than conditionals to restrict the operation 12334 * to the relevant portion of a uint16_t[16]. 12335 */ 12336 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) 12337 { 12338 int i, j; 12339 uint64_t pmask; 12340 12341 assert(vq >= 1 && vq <= ARM_MAX_VQ); 12342 assert(vq <= env_archcpu(env)->sve_max_vq); 12343 12344 /* Zap the high bits of the zregs. */ 12345 for (i = 0; i < 32; i++) { 12346 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq)); 12347 } 12348 12349 /* Zap the high bits of the pregs and ffr. */ 12350 pmask = 0; 12351 if (vq & 3) { 12352 pmask = ~(-1ULL << (16 * (vq & 3))); 12353 } 12354 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) { 12355 for (i = 0; i < 17; ++i) { 12356 env->vfp.pregs[i].p[j] &= pmask; 12357 } 12358 pmask = 0; 12359 } 12360 } 12361 12362 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm) 12363 { 12364 int exc_el; 12365 12366 if (sm) { 12367 exc_el = sme_exception_el(env, el); 12368 } else { 12369 exc_el = sve_exception_el(env, el); 12370 } 12371 if (exc_el) { 12372 return 0; /* disabled */ 12373 } 12374 return sve_vqm1_for_el_sm(env, el, sm); 12375 } 12376 12377 /* 12378 * Notice a change in SVE vector size when changing EL. 12379 */ 12380 void aarch64_sve_change_el(CPUARMState *env, int old_el, 12381 int new_el, bool el0_a64) 12382 { 12383 ARMCPU *cpu = env_archcpu(env); 12384 int old_len, new_len; 12385 bool old_a64, new_a64, sm; 12386 12387 /* Nothing to do if no SVE. */ 12388 if (!cpu_isar_feature(aa64_sve, cpu)) { 12389 return; 12390 } 12391 12392 /* Nothing to do if FP is disabled in either EL. */ 12393 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) { 12394 return; 12395 } 12396 12397 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64; 12398 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64; 12399 12400 /* 12401 * Both AArch64.TakeException and AArch64.ExceptionReturn 12402 * invoke ResetSVEState when taking an exception from, or 12403 * returning to, AArch32 state when PSTATE.SM is enabled. 12404 */ 12405 sm = FIELD_EX64(env->svcr, SVCR, SM); 12406 if (old_a64 != new_a64 && sm) { 12407 arm_reset_sve_state(env); 12408 return; 12409 } 12410 12411 /* 12412 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped 12413 * at ELx, or not available because the EL is in AArch32 state, then 12414 * for all purposes other than a direct read, the ZCR_ELx.LEN field 12415 * has an effective value of 0". 12416 * 12417 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0). 12418 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition 12419 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that 12420 * we already have the correct register contents when encountering the 12421 * vq0->vq0 transition between EL0->EL1. 12422 */ 12423 old_len = new_len = 0; 12424 if (old_a64) { 12425 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm); 12426 } 12427 if (new_a64) { 12428 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm); 12429 } 12430 12431 /* When changing vector length, clear inaccessible state. */ 12432 if (new_len < old_len) { 12433 aarch64_sve_narrow_vq(env, new_len + 1); 12434 } 12435 } 12436 #endif 12437 12438 #ifndef CONFIG_USER_ONLY 12439 ARMSecuritySpace arm_security_space(CPUARMState *env) 12440 { 12441 if (arm_feature(env, ARM_FEATURE_M)) { 12442 return arm_secure_to_space(env->v7m.secure); 12443 } 12444 12445 /* 12446 * If EL3 is not supported then the secure state is implementation 12447 * defined, in which case QEMU defaults to non-secure. 12448 */ 12449 if (!arm_feature(env, ARM_FEATURE_EL3)) { 12450 return ARMSS_NonSecure; 12451 } 12452 12453 /* Check for AArch64 EL3 or AArch32 Mon. */ 12454 if (is_a64(env)) { 12455 if (extract32(env->pstate, 2, 2) == 3) { 12456 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) { 12457 return ARMSS_Root; 12458 } else { 12459 return ARMSS_Secure; 12460 } 12461 } 12462 } else { 12463 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 12464 return ARMSS_Secure; 12465 } 12466 } 12467 12468 return arm_security_space_below_el3(env); 12469 } 12470 12471 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env) 12472 { 12473 assert(!arm_feature(env, ARM_FEATURE_M)); 12474 12475 /* 12476 * If EL3 is not supported then the secure state is implementation 12477 * defined, in which case QEMU defaults to non-secure. 12478 */ 12479 if (!arm_feature(env, ARM_FEATURE_EL3)) { 12480 return ARMSS_NonSecure; 12481 } 12482 12483 /* 12484 * Note NSE cannot be set without RME, and NSE & !NS is Reserved. 12485 * Ignoring NSE when !NS retains consistency without having to 12486 * modify other predicates. 12487 */ 12488 if (!(env->cp15.scr_el3 & SCR_NS)) { 12489 return ARMSS_Secure; 12490 } else if (env->cp15.scr_el3 & SCR_NSE) { 12491 return ARMSS_Realm; 12492 } else { 12493 return ARMSS_NonSecure; 12494 } 12495 } 12496 #endif /* !CONFIG_USER_ONLY */ 12497