1 /* 2 * QEMU ARM CPU -- internal functions and types 3 * 4 * Copyright (c) 2014 Linaro Ltd 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see 18 * <http://www.gnu.org/licenses/gpl-2.0.html> 19 * 20 * This header defines functions, types, etc which need to be shared 21 * between different source files within target/arm/ but which are 22 * private to it and not required by the rest of QEMU. 23 */ 24 25 #ifndef TARGET_ARM_INTERNALS_H 26 #define TARGET_ARM_INTERNALS_H 27 28 #include "hw/registerfields.h" 29 #include "tcg/tcg-gvec-desc.h" 30 #include "syndrome.h" 31 32 /* register banks for CPU modes */ 33 #define BANK_USRSYS 0 34 #define BANK_SVC 1 35 #define BANK_ABT 2 36 #define BANK_UND 3 37 #define BANK_IRQ 4 38 #define BANK_FIQ 5 39 #define BANK_HYP 6 40 #define BANK_MON 7 41 42 static inline bool excp_is_internal(int excp) 43 { 44 /* Return true if this exception number represents a QEMU-internal 45 * exception that will not be passed to the guest. 46 */ 47 return excp == EXCP_INTERRUPT 48 || excp == EXCP_HLT 49 || excp == EXCP_DEBUG 50 || excp == EXCP_HALTED 51 || excp == EXCP_EXCEPTION_EXIT 52 || excp == EXCP_KERNEL_TRAP 53 || excp == EXCP_SEMIHOST; 54 } 55 56 /* Scale factor for generic timers, ie number of ns per tick. 57 * This gives a 62.5MHz timer. 58 */ 59 #define GTIMER_SCALE 16 60 61 /* Bit definitions for the v7M CONTROL register */ 62 FIELD(V7M_CONTROL, NPRIV, 0, 1) 63 FIELD(V7M_CONTROL, SPSEL, 1, 1) 64 FIELD(V7M_CONTROL, FPCA, 2, 1) 65 FIELD(V7M_CONTROL, SFPA, 3, 1) 66 67 /* Bit definitions for v7M exception return payload */ 68 FIELD(V7M_EXCRET, ES, 0, 1) 69 FIELD(V7M_EXCRET, RES0, 1, 1) 70 FIELD(V7M_EXCRET, SPSEL, 2, 1) 71 FIELD(V7M_EXCRET, MODE, 3, 1) 72 FIELD(V7M_EXCRET, FTYPE, 4, 1) 73 FIELD(V7M_EXCRET, DCRS, 5, 1) 74 FIELD(V7M_EXCRET, S, 6, 1) 75 FIELD(V7M_EXCRET, RES1, 7, 25) /* including the must-be-1 prefix */ 76 77 /* Minimum value which is a magic number for exception return */ 78 #define EXC_RETURN_MIN_MAGIC 0xff000000 79 /* Minimum number which is a magic number for function or exception return 80 * when using v8M security extension 81 */ 82 #define FNC_RETURN_MIN_MAGIC 0xfefffffe 83 84 /* We use a few fake FSR values for internal purposes in M profile. 85 * M profile cores don't have A/R format FSRs, but currently our 86 * get_phys_addr() code assumes A/R profile and reports failures via 87 * an A/R format FSR value. We then translate that into the proper 88 * M profile exception and FSR status bit in arm_v7m_cpu_do_interrupt(). 89 * Mostly the FSR values we use for this are those defined for v7PMSA, 90 * since we share some of that codepath. A few kinds of fault are 91 * only for M profile and have no A/R equivalent, though, so we have 92 * to pick a value from the reserved range (which we never otherwise 93 * generate) to use for these. 94 * These values will never be visible to the guest. 95 */ 96 #define M_FAKE_FSR_NSC_EXEC 0xf /* NS executing in S&NSC memory */ 97 #define M_FAKE_FSR_SFAULT 0xe /* SecureFault INVTRAN, INVEP or AUVIOL */ 98 99 /** 100 * raise_exception: Raise the specified exception. 101 * Raise a guest exception with the specified value, syndrome register 102 * and target exception level. This should be called from helper functions, 103 * and never returns because we will longjump back up to the CPU main loop. 104 */ 105 void QEMU_NORETURN raise_exception(CPUARMState *env, uint32_t excp, 106 uint32_t syndrome, uint32_t target_el); 107 108 /* 109 * Similarly, but also use unwinding to restore cpu state. 110 */ 111 void QEMU_NORETURN raise_exception_ra(CPUARMState *env, uint32_t excp, 112 uint32_t syndrome, uint32_t target_el, 113 uintptr_t ra); 114 115 /* 116 * For AArch64, map a given EL to an index in the banked_spsr array. 117 * Note that this mapping and the AArch32 mapping defined in bank_number() 118 * must agree such that the AArch64<->AArch32 SPSRs have the architecturally 119 * mandated mapping between each other. 120 */ 121 static inline unsigned int aarch64_banked_spsr_index(unsigned int el) 122 { 123 static const unsigned int map[4] = { 124 [1] = BANK_SVC, /* EL1. */ 125 [2] = BANK_HYP, /* EL2. */ 126 [3] = BANK_MON, /* EL3. */ 127 }; 128 assert(el >= 1 && el <= 3); 129 return map[el]; 130 } 131 132 /* Map CPU modes onto saved register banks. */ 133 static inline int bank_number(int mode) 134 { 135 switch (mode) { 136 case ARM_CPU_MODE_USR: 137 case ARM_CPU_MODE_SYS: 138 return BANK_USRSYS; 139 case ARM_CPU_MODE_SVC: 140 return BANK_SVC; 141 case ARM_CPU_MODE_ABT: 142 return BANK_ABT; 143 case ARM_CPU_MODE_UND: 144 return BANK_UND; 145 case ARM_CPU_MODE_IRQ: 146 return BANK_IRQ; 147 case ARM_CPU_MODE_FIQ: 148 return BANK_FIQ; 149 case ARM_CPU_MODE_HYP: 150 return BANK_HYP; 151 case ARM_CPU_MODE_MON: 152 return BANK_MON; 153 } 154 g_assert_not_reached(); 155 } 156 157 /** 158 * r14_bank_number: Map CPU mode onto register bank for r14 159 * 160 * Given an AArch32 CPU mode, return the index into the saved register 161 * banks to use for the R14 (LR) in that mode. This is the same as 162 * bank_number(), except for the special case of Hyp mode, where 163 * R14 is shared with USR and SYS, unlike its R13 and SPSR. 164 * This should be used as the index into env->banked_r14[], and 165 * bank_number() used for the index into env->banked_r13[] and 166 * env->banked_spsr[]. 167 */ 168 static inline int r14_bank_number(int mode) 169 { 170 return (mode == ARM_CPU_MODE_HYP) ? BANK_USRSYS : bank_number(mode); 171 } 172 173 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu); 174 void arm_translate_init(void); 175 176 #ifdef CONFIG_TCG 177 void arm_cpu_synchronize_from_tb(CPUState *cs, const TranslationBlock *tb); 178 #endif /* CONFIG_TCG */ 179 180 /** 181 * aarch64_sve_zcr_get_valid_len: 182 * @cpu: cpu context 183 * @start_len: maximum len to consider 184 * 185 * Return the maximum supported sve vector length <= @start_len. 186 * Note that both @start_len and the return value are in units 187 * of ZCR_ELx.LEN, so the vector bit length is (x + 1) * 128. 188 */ 189 uint32_t aarch64_sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len); 190 191 enum arm_fprounding { 192 FPROUNDING_TIEEVEN, 193 FPROUNDING_POSINF, 194 FPROUNDING_NEGINF, 195 FPROUNDING_ZERO, 196 FPROUNDING_TIEAWAY, 197 FPROUNDING_ODD 198 }; 199 200 int arm_rmode_to_sf(int rmode); 201 202 static inline void aarch64_save_sp(CPUARMState *env, int el) 203 { 204 if (env->pstate & PSTATE_SP) { 205 env->sp_el[el] = env->xregs[31]; 206 } else { 207 env->sp_el[0] = env->xregs[31]; 208 } 209 } 210 211 static inline void aarch64_restore_sp(CPUARMState *env, int el) 212 { 213 if (env->pstate & PSTATE_SP) { 214 env->xregs[31] = env->sp_el[el]; 215 } else { 216 env->xregs[31] = env->sp_el[0]; 217 } 218 } 219 220 static inline void update_spsel(CPUARMState *env, uint32_t imm) 221 { 222 unsigned int cur_el = arm_current_el(env); 223 /* Update PSTATE SPSel bit; this requires us to update the 224 * working stack pointer in xregs[31]. 225 */ 226 if (!((imm ^ env->pstate) & PSTATE_SP)) { 227 return; 228 } 229 aarch64_save_sp(env, cur_el); 230 env->pstate = deposit32(env->pstate, 0, 1, imm); 231 232 /* We rely on illegal updates to SPsel from EL0 to get trapped 233 * at translation time. 234 */ 235 assert(cur_el >= 1 && cur_el <= 3); 236 aarch64_restore_sp(env, cur_el); 237 } 238 239 /* 240 * arm_pamax 241 * @cpu: ARMCPU 242 * 243 * Returns the implementation defined bit-width of physical addresses. 244 * The ARMv8 reference manuals refer to this as PAMax(). 245 */ 246 unsigned int arm_pamax(ARMCPU *cpu); 247 248 /* Return true if extended addresses are enabled. 249 * This is always the case if our translation regime is 64 bit, 250 * but depends on TTBCR.EAE for 32 bit. 251 */ 252 static inline bool extended_addresses_enabled(CPUARMState *env) 253 { 254 TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1]; 255 return arm_el_is_aa64(env, 1) || 256 (arm_feature(env, ARM_FEATURE_LPAE) && (tcr->raw_tcr & TTBCR_EAE)); 257 } 258 259 /* Update a QEMU watchpoint based on the information the guest has set in the 260 * DBGWCR<n>_EL1 and DBGWVR<n>_EL1 registers. 261 */ 262 void hw_watchpoint_update(ARMCPU *cpu, int n); 263 /* Update the QEMU watchpoints for every guest watchpoint. This does a 264 * complete delete-and-reinstate of the QEMU watchpoint list and so is 265 * suitable for use after migration or on reset. 266 */ 267 void hw_watchpoint_update_all(ARMCPU *cpu); 268 /* Update a QEMU breakpoint based on the information the guest has set in the 269 * DBGBCR<n>_EL1 and DBGBVR<n>_EL1 registers. 270 */ 271 void hw_breakpoint_update(ARMCPU *cpu, int n); 272 /* Update the QEMU breakpoints for every guest breakpoint. This does a 273 * complete delete-and-reinstate of the QEMU breakpoint list and so is 274 * suitable for use after migration or on reset. 275 */ 276 void hw_breakpoint_update_all(ARMCPU *cpu); 277 278 /* Callback function for checking if a breakpoint should trigger. */ 279 bool arm_debug_check_breakpoint(CPUState *cs); 280 281 /* Callback function for checking if a watchpoint should trigger. */ 282 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp); 283 284 /* Adjust addresses (in BE32 mode) before testing against watchpoint 285 * addresses. 286 */ 287 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len); 288 289 /* Callback function for when a watchpoint or breakpoint triggers. */ 290 void arm_debug_excp_handler(CPUState *cs); 291 292 #if defined(CONFIG_USER_ONLY) || !defined(CONFIG_TCG) 293 static inline bool arm_is_psci_call(ARMCPU *cpu, int excp_type) 294 { 295 return false; 296 } 297 static inline void arm_handle_psci_call(ARMCPU *cpu) 298 { 299 g_assert_not_reached(); 300 } 301 #else 302 /* Return true if the r0/x0 value indicates that this SMC/HVC is a PSCI call. */ 303 bool arm_is_psci_call(ARMCPU *cpu, int excp_type); 304 /* Actually handle a PSCI call */ 305 void arm_handle_psci_call(ARMCPU *cpu); 306 #endif 307 308 /** 309 * arm_clear_exclusive: clear the exclusive monitor 310 * @env: CPU env 311 * Clear the CPU's exclusive monitor, like the guest CLREX instruction. 312 */ 313 static inline void arm_clear_exclusive(CPUARMState *env) 314 { 315 env->exclusive_addr = -1; 316 } 317 318 /** 319 * ARMFaultType: type of an ARM MMU fault 320 * This corresponds to the v8A pseudocode's Fault enumeration, 321 * with extensions for QEMU internal conditions. 322 */ 323 typedef enum ARMFaultType { 324 ARMFault_None, 325 ARMFault_AccessFlag, 326 ARMFault_Alignment, 327 ARMFault_Background, 328 ARMFault_Domain, 329 ARMFault_Permission, 330 ARMFault_Translation, 331 ARMFault_AddressSize, 332 ARMFault_SyncExternal, 333 ARMFault_SyncExternalOnWalk, 334 ARMFault_SyncParity, 335 ARMFault_SyncParityOnWalk, 336 ARMFault_AsyncParity, 337 ARMFault_AsyncExternal, 338 ARMFault_Debug, 339 ARMFault_TLBConflict, 340 ARMFault_Lockdown, 341 ARMFault_Exclusive, 342 ARMFault_ICacheMaint, 343 ARMFault_QEMU_NSCExec, /* v8M: NS executing in S&NSC memory */ 344 ARMFault_QEMU_SFault, /* v8M: SecureFault INVTRAN, INVEP or AUVIOL */ 345 } ARMFaultType; 346 347 /** 348 * ARMMMUFaultInfo: Information describing an ARM MMU Fault 349 * @type: Type of fault 350 * @level: Table walk level (for translation, access flag and permission faults) 351 * @domain: Domain of the fault address (for non-LPAE CPUs only) 352 * @s2addr: Address that caused a fault at stage 2 353 * @stage2: True if we faulted at stage 2 354 * @s1ptw: True if we faulted at stage 2 while doing a stage 1 page-table walk 355 * @s1ns: True if we faulted on a non-secure IPA while in secure state 356 * @ea: True if we should set the EA (external abort type) bit in syndrome 357 */ 358 typedef struct ARMMMUFaultInfo ARMMMUFaultInfo; 359 struct ARMMMUFaultInfo { 360 ARMFaultType type; 361 target_ulong s2addr; 362 int level; 363 int domain; 364 bool stage2; 365 bool s1ptw; 366 bool s1ns; 367 bool ea; 368 }; 369 370 /** 371 * arm_fi_to_sfsc: Convert fault info struct to short-format FSC 372 * Compare pseudocode EncodeSDFSC(), though unlike that function 373 * we set up a whole FSR-format code including domain field and 374 * putting the high bit of the FSC into bit 10. 375 */ 376 static inline uint32_t arm_fi_to_sfsc(ARMMMUFaultInfo *fi) 377 { 378 uint32_t fsc; 379 380 switch (fi->type) { 381 case ARMFault_None: 382 return 0; 383 case ARMFault_AccessFlag: 384 fsc = fi->level == 1 ? 0x3 : 0x6; 385 break; 386 case ARMFault_Alignment: 387 fsc = 0x1; 388 break; 389 case ARMFault_Permission: 390 fsc = fi->level == 1 ? 0xd : 0xf; 391 break; 392 case ARMFault_Domain: 393 fsc = fi->level == 1 ? 0x9 : 0xb; 394 break; 395 case ARMFault_Translation: 396 fsc = fi->level == 1 ? 0x5 : 0x7; 397 break; 398 case ARMFault_SyncExternal: 399 fsc = 0x8 | (fi->ea << 12); 400 break; 401 case ARMFault_SyncExternalOnWalk: 402 fsc = fi->level == 1 ? 0xc : 0xe; 403 fsc |= (fi->ea << 12); 404 break; 405 case ARMFault_SyncParity: 406 fsc = 0x409; 407 break; 408 case ARMFault_SyncParityOnWalk: 409 fsc = fi->level == 1 ? 0x40c : 0x40e; 410 break; 411 case ARMFault_AsyncParity: 412 fsc = 0x408; 413 break; 414 case ARMFault_AsyncExternal: 415 fsc = 0x406 | (fi->ea << 12); 416 break; 417 case ARMFault_Debug: 418 fsc = 0x2; 419 break; 420 case ARMFault_TLBConflict: 421 fsc = 0x400; 422 break; 423 case ARMFault_Lockdown: 424 fsc = 0x404; 425 break; 426 case ARMFault_Exclusive: 427 fsc = 0x405; 428 break; 429 case ARMFault_ICacheMaint: 430 fsc = 0x4; 431 break; 432 case ARMFault_Background: 433 fsc = 0x0; 434 break; 435 case ARMFault_QEMU_NSCExec: 436 fsc = M_FAKE_FSR_NSC_EXEC; 437 break; 438 case ARMFault_QEMU_SFault: 439 fsc = M_FAKE_FSR_SFAULT; 440 break; 441 default: 442 /* Other faults can't occur in a context that requires a 443 * short-format status code. 444 */ 445 g_assert_not_reached(); 446 } 447 448 fsc |= (fi->domain << 4); 449 return fsc; 450 } 451 452 /** 453 * arm_fi_to_lfsc: Convert fault info struct to long-format FSC 454 * Compare pseudocode EncodeLDFSC(), though unlike that function 455 * we fill in also the LPAE bit 9 of a DFSR format. 456 */ 457 static inline uint32_t arm_fi_to_lfsc(ARMMMUFaultInfo *fi) 458 { 459 uint32_t fsc; 460 461 switch (fi->type) { 462 case ARMFault_None: 463 return 0; 464 case ARMFault_AddressSize: 465 assert(fi->level >= -1 && fi->level <= 3); 466 if (fi->level < 0) { 467 fsc = 0b101001; 468 } else { 469 fsc = fi->level; 470 } 471 break; 472 case ARMFault_AccessFlag: 473 assert(fi->level >= 0 && fi->level <= 3); 474 fsc = 0b001000 | fi->level; 475 break; 476 case ARMFault_Permission: 477 assert(fi->level >= 0 && fi->level <= 3); 478 fsc = 0b001100 | fi->level; 479 break; 480 case ARMFault_Translation: 481 assert(fi->level >= -1 && fi->level <= 3); 482 if (fi->level < 0) { 483 fsc = 0b101011; 484 } else { 485 fsc = 0b000100 | fi->level; 486 } 487 break; 488 case ARMFault_SyncExternal: 489 fsc = 0x10 | (fi->ea << 12); 490 break; 491 case ARMFault_SyncExternalOnWalk: 492 assert(fi->level >= -1 && fi->level <= 3); 493 if (fi->level < 0) { 494 fsc = 0b010011; 495 } else { 496 fsc = 0b010100 | fi->level; 497 } 498 fsc |= fi->ea << 12; 499 break; 500 case ARMFault_SyncParity: 501 fsc = 0x18; 502 break; 503 case ARMFault_SyncParityOnWalk: 504 assert(fi->level >= -1 && fi->level <= 3); 505 if (fi->level < 0) { 506 fsc = 0b011011; 507 } else { 508 fsc = 0b011100 | fi->level; 509 } 510 break; 511 case ARMFault_AsyncParity: 512 fsc = 0x19; 513 break; 514 case ARMFault_AsyncExternal: 515 fsc = 0x11 | (fi->ea << 12); 516 break; 517 case ARMFault_Alignment: 518 fsc = 0x21; 519 break; 520 case ARMFault_Debug: 521 fsc = 0x22; 522 break; 523 case ARMFault_TLBConflict: 524 fsc = 0x30; 525 break; 526 case ARMFault_Lockdown: 527 fsc = 0x34; 528 break; 529 case ARMFault_Exclusive: 530 fsc = 0x35; 531 break; 532 default: 533 /* Other faults can't occur in a context that requires a 534 * long-format status code. 535 */ 536 g_assert_not_reached(); 537 } 538 539 fsc |= 1 << 9; 540 return fsc; 541 } 542 543 static inline bool arm_extabort_type(MemTxResult result) 544 { 545 /* The EA bit in syndromes and fault status registers is an 546 * IMPDEF classification of external aborts. ARM implementations 547 * usually use this to indicate AXI bus Decode error (0) or 548 * Slave error (1); in QEMU we follow that. 549 */ 550 return result != MEMTX_DECODE_ERROR; 551 } 552 553 #ifdef CONFIG_USER_ONLY 554 void arm_cpu_record_sigsegv(CPUState *cpu, vaddr addr, 555 MMUAccessType access_type, 556 bool maperr, uintptr_t ra); 557 void arm_cpu_record_sigbus(CPUState *cpu, vaddr addr, 558 MMUAccessType access_type, uintptr_t ra); 559 #else 560 bool arm_cpu_tlb_fill(CPUState *cs, vaddr address, int size, 561 MMUAccessType access_type, int mmu_idx, 562 bool probe, uintptr_t retaddr); 563 #endif 564 565 static inline int arm_to_core_mmu_idx(ARMMMUIdx mmu_idx) 566 { 567 return mmu_idx & ARM_MMU_IDX_COREIDX_MASK; 568 } 569 570 static inline ARMMMUIdx core_to_arm_mmu_idx(CPUARMState *env, int mmu_idx) 571 { 572 if (arm_feature(env, ARM_FEATURE_M)) { 573 return mmu_idx | ARM_MMU_IDX_M; 574 } else { 575 return mmu_idx | ARM_MMU_IDX_A; 576 } 577 } 578 579 static inline ARMMMUIdx core_to_aa64_mmu_idx(int mmu_idx) 580 { 581 /* AArch64 is always a-profile. */ 582 return mmu_idx | ARM_MMU_IDX_A; 583 } 584 585 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx); 586 587 /* 588 * Return the MMU index for a v7M CPU with all relevant information 589 * manually specified. 590 */ 591 ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env, 592 bool secstate, bool priv, bool negpri); 593 594 /* 595 * Return the MMU index for a v7M CPU in the specified security and 596 * privilege state. 597 */ 598 ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env, 599 bool secstate, bool priv); 600 601 /* Return the MMU index for a v7M CPU in the specified security state */ 602 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate); 603 604 /* Return true if the stage 1 translation regime is using LPAE format page 605 * tables */ 606 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx); 607 608 /* Raise a data fault alignment exception for the specified virtual address */ 609 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, 610 MMUAccessType access_type, 611 int mmu_idx, uintptr_t retaddr) QEMU_NORETURN; 612 613 /* arm_cpu_do_transaction_failed: handle a memory system error response 614 * (eg "no device/memory present at address") by raising an external abort 615 * exception 616 */ 617 void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, 618 vaddr addr, unsigned size, 619 MMUAccessType access_type, 620 int mmu_idx, MemTxAttrs attrs, 621 MemTxResult response, uintptr_t retaddr); 622 623 /* Call any registered EL change hooks */ 624 static inline void arm_call_pre_el_change_hook(ARMCPU *cpu) 625 { 626 ARMELChangeHook *hook, *next; 627 QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) { 628 hook->hook(cpu, hook->opaque); 629 } 630 } 631 static inline void arm_call_el_change_hook(ARMCPU *cpu) 632 { 633 ARMELChangeHook *hook, *next; 634 QLIST_FOREACH_SAFE(hook, &cpu->el_change_hooks, node, next) { 635 hook->hook(cpu, hook->opaque); 636 } 637 } 638 639 /* Return true if this address translation regime has two ranges. */ 640 static inline bool regime_has_2_ranges(ARMMMUIdx mmu_idx) 641 { 642 switch (mmu_idx) { 643 case ARMMMUIdx_Stage1_E0: 644 case ARMMMUIdx_Stage1_E1: 645 case ARMMMUIdx_Stage1_E1_PAN: 646 case ARMMMUIdx_Stage1_SE0: 647 case ARMMMUIdx_Stage1_SE1: 648 case ARMMMUIdx_Stage1_SE1_PAN: 649 case ARMMMUIdx_E10_0: 650 case ARMMMUIdx_E10_1: 651 case ARMMMUIdx_E10_1_PAN: 652 case ARMMMUIdx_E20_0: 653 case ARMMMUIdx_E20_2: 654 case ARMMMUIdx_E20_2_PAN: 655 case ARMMMUIdx_SE10_0: 656 case ARMMMUIdx_SE10_1: 657 case ARMMMUIdx_SE10_1_PAN: 658 case ARMMMUIdx_SE20_0: 659 case ARMMMUIdx_SE20_2: 660 case ARMMMUIdx_SE20_2_PAN: 661 return true; 662 default: 663 return false; 664 } 665 } 666 667 /* Return true if this address translation regime is secure */ 668 static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx) 669 { 670 switch (mmu_idx) { 671 case ARMMMUIdx_E10_0: 672 case ARMMMUIdx_E10_1: 673 case ARMMMUIdx_E10_1_PAN: 674 case ARMMMUIdx_E20_0: 675 case ARMMMUIdx_E20_2: 676 case ARMMMUIdx_E20_2_PAN: 677 case ARMMMUIdx_Stage1_E0: 678 case ARMMMUIdx_Stage1_E1: 679 case ARMMMUIdx_Stage1_E1_PAN: 680 case ARMMMUIdx_E2: 681 case ARMMMUIdx_Stage2: 682 case ARMMMUIdx_MPrivNegPri: 683 case ARMMMUIdx_MUserNegPri: 684 case ARMMMUIdx_MPriv: 685 case ARMMMUIdx_MUser: 686 return false; 687 case ARMMMUIdx_SE3: 688 case ARMMMUIdx_SE10_0: 689 case ARMMMUIdx_SE10_1: 690 case ARMMMUIdx_SE10_1_PAN: 691 case ARMMMUIdx_SE20_0: 692 case ARMMMUIdx_SE20_2: 693 case ARMMMUIdx_SE20_2_PAN: 694 case ARMMMUIdx_Stage1_SE0: 695 case ARMMMUIdx_Stage1_SE1: 696 case ARMMMUIdx_Stage1_SE1_PAN: 697 case ARMMMUIdx_SE2: 698 case ARMMMUIdx_Stage2_S: 699 case ARMMMUIdx_MSPrivNegPri: 700 case ARMMMUIdx_MSUserNegPri: 701 case ARMMMUIdx_MSPriv: 702 case ARMMMUIdx_MSUser: 703 return true; 704 default: 705 g_assert_not_reached(); 706 } 707 } 708 709 static inline bool regime_is_pan(CPUARMState *env, ARMMMUIdx mmu_idx) 710 { 711 switch (mmu_idx) { 712 case ARMMMUIdx_Stage1_E1_PAN: 713 case ARMMMUIdx_Stage1_SE1_PAN: 714 case ARMMMUIdx_E10_1_PAN: 715 case ARMMMUIdx_E20_2_PAN: 716 case ARMMMUIdx_SE10_1_PAN: 717 case ARMMMUIdx_SE20_2_PAN: 718 return true; 719 default: 720 return false; 721 } 722 } 723 724 /* Return the exception level which controls this address translation regime */ 725 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx) 726 { 727 switch (mmu_idx) { 728 case ARMMMUIdx_SE20_0: 729 case ARMMMUIdx_SE20_2: 730 case ARMMMUIdx_SE20_2_PAN: 731 case ARMMMUIdx_E20_0: 732 case ARMMMUIdx_E20_2: 733 case ARMMMUIdx_E20_2_PAN: 734 case ARMMMUIdx_Stage2: 735 case ARMMMUIdx_Stage2_S: 736 case ARMMMUIdx_SE2: 737 case ARMMMUIdx_E2: 738 return 2; 739 case ARMMMUIdx_SE3: 740 return 3; 741 case ARMMMUIdx_SE10_0: 742 case ARMMMUIdx_Stage1_SE0: 743 return arm_el_is_aa64(env, 3) ? 1 : 3; 744 case ARMMMUIdx_SE10_1: 745 case ARMMMUIdx_SE10_1_PAN: 746 case ARMMMUIdx_Stage1_E0: 747 case ARMMMUIdx_Stage1_E1: 748 case ARMMMUIdx_Stage1_E1_PAN: 749 case ARMMMUIdx_Stage1_SE1: 750 case ARMMMUIdx_Stage1_SE1_PAN: 751 case ARMMMUIdx_E10_0: 752 case ARMMMUIdx_E10_1: 753 case ARMMMUIdx_E10_1_PAN: 754 case ARMMMUIdx_MPrivNegPri: 755 case ARMMMUIdx_MUserNegPri: 756 case ARMMMUIdx_MPriv: 757 case ARMMMUIdx_MUser: 758 case ARMMMUIdx_MSPrivNegPri: 759 case ARMMMUIdx_MSUserNegPri: 760 case ARMMMUIdx_MSPriv: 761 case ARMMMUIdx_MSUser: 762 return 1; 763 default: 764 g_assert_not_reached(); 765 } 766 } 767 768 /* Return the TCR controlling this translation regime */ 769 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx) 770 { 771 if (mmu_idx == ARMMMUIdx_Stage2) { 772 return &env->cp15.vtcr_el2; 773 } 774 if (mmu_idx == ARMMMUIdx_Stage2_S) { 775 /* 776 * Note: Secure stage 2 nominally shares fields from VTCR_EL2, but 777 * those are not currently used by QEMU, so just return VSTCR_EL2. 778 */ 779 return &env->cp15.vstcr_el2; 780 } 781 return &env->cp15.tcr_el[regime_el(env, mmu_idx)]; 782 } 783 784 /* Return the FSR value for a debug exception (watchpoint, hardware 785 * breakpoint or BKPT insn) targeting the specified exception level. 786 */ 787 static inline uint32_t arm_debug_exception_fsr(CPUARMState *env) 788 { 789 ARMMMUFaultInfo fi = { .type = ARMFault_Debug }; 790 int target_el = arm_debug_target_el(env); 791 bool using_lpae = false; 792 793 if (target_el == 2 || arm_el_is_aa64(env, target_el)) { 794 using_lpae = true; 795 } else { 796 if (arm_feature(env, ARM_FEATURE_LPAE) && 797 (env->cp15.tcr_el[target_el].raw_tcr & TTBCR_EAE)) { 798 using_lpae = true; 799 } 800 } 801 802 if (using_lpae) { 803 return arm_fi_to_lfsc(&fi); 804 } else { 805 return arm_fi_to_sfsc(&fi); 806 } 807 } 808 809 /** 810 * arm_num_brps: Return number of implemented breakpoints. 811 * Note that the ID register BRPS field is "number of bps - 1", 812 * and we return the actual number of breakpoints. 813 */ 814 static inline int arm_num_brps(ARMCPU *cpu) 815 { 816 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 817 return FIELD_EX64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, BRPS) + 1; 818 } else { 819 return FIELD_EX32(cpu->isar.dbgdidr, DBGDIDR, BRPS) + 1; 820 } 821 } 822 823 /** 824 * arm_num_wrps: Return number of implemented watchpoints. 825 * Note that the ID register WRPS field is "number of wps - 1", 826 * and we return the actual number of watchpoints. 827 */ 828 static inline int arm_num_wrps(ARMCPU *cpu) 829 { 830 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 831 return FIELD_EX64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, WRPS) + 1; 832 } else { 833 return FIELD_EX32(cpu->isar.dbgdidr, DBGDIDR, WRPS) + 1; 834 } 835 } 836 837 /** 838 * arm_num_ctx_cmps: Return number of implemented context comparators. 839 * Note that the ID register CTX_CMPS field is "number of cmps - 1", 840 * and we return the actual number of comparators. 841 */ 842 static inline int arm_num_ctx_cmps(ARMCPU *cpu) 843 { 844 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 845 return FIELD_EX64(cpu->isar.id_aa64dfr0, ID_AA64DFR0, CTX_CMPS) + 1; 846 } else { 847 return FIELD_EX32(cpu->isar.dbgdidr, DBGDIDR, CTX_CMPS) + 1; 848 } 849 } 850 851 /** 852 * v7m_using_psp: Return true if using process stack pointer 853 * Return true if the CPU is currently using the process stack 854 * pointer, or false if it is using the main stack pointer. 855 */ 856 static inline bool v7m_using_psp(CPUARMState *env) 857 { 858 /* Handler mode always uses the main stack; for thread mode 859 * the CONTROL.SPSEL bit determines the answer. 860 * Note that in v7M it is not possible to be in Handler mode with 861 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both. 862 */ 863 return !arm_v7m_is_handler_mode(env) && 864 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK; 865 } 866 867 /** 868 * v7m_sp_limit: Return SP limit for current CPU state 869 * Return the SP limit value for the current CPU security state 870 * and stack pointer. 871 */ 872 static inline uint32_t v7m_sp_limit(CPUARMState *env) 873 { 874 if (v7m_using_psp(env)) { 875 return env->v7m.psplim[env->v7m.secure]; 876 } else { 877 return env->v7m.msplim[env->v7m.secure]; 878 } 879 } 880 881 /** 882 * v7m_cpacr_pass: 883 * Return true if the v7M CPACR permits access to the FPU for the specified 884 * security state and privilege level. 885 */ 886 static inline bool v7m_cpacr_pass(CPUARMState *env, 887 bool is_secure, bool is_priv) 888 { 889 switch (extract32(env->v7m.cpacr[is_secure], 20, 2)) { 890 case 0: 891 case 2: /* UNPREDICTABLE: we treat like 0 */ 892 return false; 893 case 1: 894 return is_priv; 895 case 3: 896 return true; 897 default: 898 g_assert_not_reached(); 899 } 900 } 901 902 /** 903 * aarch32_mode_name(): Return name of the AArch32 CPU mode 904 * @psr: Program Status Register indicating CPU mode 905 * 906 * Returns, for debug logging purposes, a printable representation 907 * of the AArch32 CPU mode ("svc", "usr", etc) as indicated by 908 * the low bits of the specified PSR. 909 */ 910 static inline const char *aarch32_mode_name(uint32_t psr) 911 { 912 static const char cpu_mode_names[16][4] = { 913 "usr", "fiq", "irq", "svc", "???", "???", "mon", "abt", 914 "???", "???", "hyp", "und", "???", "???", "???", "sys" 915 }; 916 917 return cpu_mode_names[psr & 0xf]; 918 } 919 920 /** 921 * arm_cpu_update_virq: Update CPU_INTERRUPT_VIRQ bit in cs->interrupt_request 922 * 923 * Update the CPU_INTERRUPT_VIRQ bit in cs->interrupt_request, following 924 * a change to either the input VIRQ line from the GIC or the HCR_EL2.VI bit. 925 * Must be called with the iothread lock held. 926 */ 927 void arm_cpu_update_virq(ARMCPU *cpu); 928 929 /** 930 * arm_cpu_update_vfiq: Update CPU_INTERRUPT_VFIQ bit in cs->interrupt_request 931 * 932 * Update the CPU_INTERRUPT_VFIQ bit in cs->interrupt_request, following 933 * a change to either the input VFIQ line from the GIC or the HCR_EL2.VF bit. 934 * Must be called with the iothread lock held. 935 */ 936 void arm_cpu_update_vfiq(ARMCPU *cpu); 937 938 /** 939 * arm_mmu_idx_el: 940 * @env: The cpu environment 941 * @el: The EL to use. 942 * 943 * Return the full ARMMMUIdx for the translation regime for EL. 944 */ 945 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el); 946 947 /** 948 * arm_mmu_idx: 949 * @env: The cpu environment 950 * 951 * Return the full ARMMMUIdx for the current translation regime. 952 */ 953 ARMMMUIdx arm_mmu_idx(CPUARMState *env); 954 955 /** 956 * arm_stage1_mmu_idx: 957 * @env: The cpu environment 958 * 959 * Return the ARMMMUIdx for the stage1 traversal for the current regime. 960 */ 961 #ifdef CONFIG_USER_ONLY 962 static inline ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env) 963 { 964 return ARMMMUIdx_Stage1_E0; 965 } 966 #else 967 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env); 968 #endif 969 970 /** 971 * arm_mmu_idx_is_stage1_of_2: 972 * @mmu_idx: The ARMMMUIdx to test 973 * 974 * Return true if @mmu_idx is a NOTLB mmu_idx that is the 975 * first stage of a two stage regime. 976 */ 977 static inline bool arm_mmu_idx_is_stage1_of_2(ARMMMUIdx mmu_idx) 978 { 979 switch (mmu_idx) { 980 case ARMMMUIdx_Stage1_E0: 981 case ARMMMUIdx_Stage1_E1: 982 case ARMMMUIdx_Stage1_E1_PAN: 983 case ARMMMUIdx_Stage1_SE0: 984 case ARMMMUIdx_Stage1_SE1: 985 case ARMMMUIdx_Stage1_SE1_PAN: 986 return true; 987 default: 988 return false; 989 } 990 } 991 992 static inline uint32_t aarch32_cpsr_valid_mask(uint64_t features, 993 const ARMISARegisters *id) 994 { 995 uint32_t valid = CPSR_M | CPSR_AIF | CPSR_IL | CPSR_NZCV; 996 997 if ((features >> ARM_FEATURE_V4T) & 1) { 998 valid |= CPSR_T; 999 } 1000 if ((features >> ARM_FEATURE_V5) & 1) { 1001 valid |= CPSR_Q; /* V5TE in reality*/ 1002 } 1003 if ((features >> ARM_FEATURE_V6) & 1) { 1004 valid |= CPSR_E | CPSR_GE; 1005 } 1006 if ((features >> ARM_FEATURE_THUMB2) & 1) { 1007 valid |= CPSR_IT; 1008 } 1009 if (isar_feature_aa32_jazelle(id)) { 1010 valid |= CPSR_J; 1011 } 1012 if (isar_feature_aa32_pan(id)) { 1013 valid |= CPSR_PAN; 1014 } 1015 if (isar_feature_aa32_dit(id)) { 1016 valid |= CPSR_DIT; 1017 } 1018 if (isar_feature_aa32_ssbs(id)) { 1019 valid |= CPSR_SSBS; 1020 } 1021 1022 return valid; 1023 } 1024 1025 static inline uint32_t aarch64_pstate_valid_mask(const ARMISARegisters *id) 1026 { 1027 uint32_t valid; 1028 1029 valid = PSTATE_M | PSTATE_DAIF | PSTATE_IL | PSTATE_SS | PSTATE_NZCV; 1030 if (isar_feature_aa64_bti(id)) { 1031 valid |= PSTATE_BTYPE; 1032 } 1033 if (isar_feature_aa64_pan(id)) { 1034 valid |= PSTATE_PAN; 1035 } 1036 if (isar_feature_aa64_uao(id)) { 1037 valid |= PSTATE_UAO; 1038 } 1039 if (isar_feature_aa64_dit(id)) { 1040 valid |= PSTATE_DIT; 1041 } 1042 if (isar_feature_aa64_ssbs(id)) { 1043 valid |= PSTATE_SSBS; 1044 } 1045 if (isar_feature_aa64_mte(id)) { 1046 valid |= PSTATE_TCO; 1047 } 1048 1049 return valid; 1050 } 1051 1052 /* 1053 * Parameters of a given virtual address, as extracted from the 1054 * translation control register (TCR) for a given regime. 1055 */ 1056 typedef struct ARMVAParameters { 1057 unsigned tsz : 8; 1058 unsigned ps : 3; 1059 unsigned sh : 2; 1060 unsigned select : 1; 1061 bool tbi : 1; 1062 bool epd : 1; 1063 bool hpd : 1; 1064 bool using16k : 1; 1065 bool using64k : 1; 1066 bool tsz_oob : 1; /* tsz has been clamped to legal range */ 1067 bool ds : 1; 1068 } ARMVAParameters; 1069 1070 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va, 1071 ARMMMUIdx mmu_idx, bool data); 1072 1073 static inline int exception_target_el(CPUARMState *env) 1074 { 1075 int target_el = MAX(1, arm_current_el(env)); 1076 1077 /* 1078 * No such thing as secure EL1 if EL3 is aarch32, 1079 * so update the target EL to EL3 in this case. 1080 */ 1081 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) { 1082 target_el = 3; 1083 } 1084 1085 return target_el; 1086 } 1087 1088 /* Determine if allocation tags are available. */ 1089 static inline bool allocation_tag_access_enabled(CPUARMState *env, int el, 1090 uint64_t sctlr) 1091 { 1092 if (el < 3 1093 && arm_feature(env, ARM_FEATURE_EL3) 1094 && !(env->cp15.scr_el3 & SCR_ATA)) { 1095 return false; 1096 } 1097 if (el < 2 && arm_is_el2_enabled(env)) { 1098 uint64_t hcr = arm_hcr_el2_eff(env); 1099 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) { 1100 return false; 1101 } 1102 } 1103 sctlr &= (el == 0 ? SCTLR_ATA0 : SCTLR_ATA); 1104 return sctlr != 0; 1105 } 1106 1107 #ifndef CONFIG_USER_ONLY 1108 1109 /* Security attributes for an address, as returned by v8m_security_lookup. */ 1110 typedef struct V8M_SAttributes { 1111 bool subpage; /* true if these attrs don't cover the whole TARGET_PAGE */ 1112 bool ns; 1113 bool nsc; 1114 uint8_t sregion; 1115 bool srvalid; 1116 uint8_t iregion; 1117 bool irvalid; 1118 } V8M_SAttributes; 1119 1120 void v8m_security_lookup(CPUARMState *env, uint32_t address, 1121 MMUAccessType access_type, ARMMMUIdx mmu_idx, 1122 V8M_SAttributes *sattrs); 1123 1124 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address, 1125 MMUAccessType access_type, ARMMMUIdx mmu_idx, 1126 hwaddr *phys_ptr, MemTxAttrs *txattrs, 1127 int *prot, bool *is_subpage, 1128 ARMMMUFaultInfo *fi, uint32_t *mregion); 1129 1130 /* Cacheability and shareability attributes for a memory access */ 1131 typedef struct ARMCacheAttrs { 1132 unsigned int attrs:8; /* as in the MAIR register encoding */ 1133 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */ 1134 } ARMCacheAttrs; 1135 1136 bool get_phys_addr(CPUARMState *env, target_ulong address, 1137 MMUAccessType access_type, ARMMMUIdx mmu_idx, 1138 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 1139 target_ulong *page_size, 1140 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 1141 __attribute__((nonnull)); 1142 1143 void arm_log_exception(CPUState *cs); 1144 1145 #endif /* !CONFIG_USER_ONLY */ 1146 1147 /* 1148 * The log2 of the words in the tag block, for GMID_EL1.BS. 1149 * The is the maximum, 256 bytes, which manipulates 64-bits of tags. 1150 */ 1151 #define GMID_EL1_BS 6 1152 1153 /* We associate one allocation tag per 16 bytes, the minimum. */ 1154 #define LOG2_TAG_GRANULE 4 1155 #define TAG_GRANULE (1 << LOG2_TAG_GRANULE) 1156 1157 /* 1158 * SVE predicates are 1/8 the size of SVE vectors, and cannot use 1159 * the same simd_desc() encoding due to restrictions on size. 1160 * Use these instead. 1161 */ 1162 FIELD(PREDDESC, OPRSZ, 0, 6) 1163 FIELD(PREDDESC, ESZ, 6, 2) 1164 FIELD(PREDDESC, DATA, 8, 24) 1165 1166 /* 1167 * The SVE simd_data field, for memory ops, contains either 1168 * rd (5 bits) or a shift count (2 bits). 1169 */ 1170 #define SVE_MTEDESC_SHIFT 5 1171 1172 /* Bits within a descriptor passed to the helper_mte_check* functions. */ 1173 FIELD(MTEDESC, MIDX, 0, 4) 1174 FIELD(MTEDESC, TBI, 4, 2) 1175 FIELD(MTEDESC, TCMA, 6, 2) 1176 FIELD(MTEDESC, WRITE, 8, 1) 1177 FIELD(MTEDESC, SIZEM1, 9, SIMD_DATA_BITS - 9) /* size - 1 */ 1178 1179 bool mte_probe(CPUARMState *env, uint32_t desc, uint64_t ptr); 1180 uint64_t mte_check(CPUARMState *env, uint32_t desc, uint64_t ptr, uintptr_t ra); 1181 1182 static inline int allocation_tag_from_addr(uint64_t ptr) 1183 { 1184 return extract64(ptr, 56, 4); 1185 } 1186 1187 static inline uint64_t address_with_allocation_tag(uint64_t ptr, int rtag) 1188 { 1189 return deposit64(ptr, 56, 4, rtag); 1190 } 1191 1192 /* Return true if tbi bits mean that the access is checked. */ 1193 static inline bool tbi_check(uint32_t desc, int bit55) 1194 { 1195 return (desc >> (R_MTEDESC_TBI_SHIFT + bit55)) & 1; 1196 } 1197 1198 /* Return true if tcma bits mean that the access is unchecked. */ 1199 static inline bool tcma_check(uint32_t desc, int bit55, int ptr_tag) 1200 { 1201 /* 1202 * We had extracted bit55 and ptr_tag for other reasons, so fold 1203 * (ptr<59:55> == 00000 || ptr<59:55> == 11111) into a single test. 1204 */ 1205 bool match = ((ptr_tag + bit55) & 0xf) == 0; 1206 bool tcma = (desc >> (R_MTEDESC_TCMA_SHIFT + bit55)) & 1; 1207 return tcma && match; 1208 } 1209 1210 /* 1211 * For TBI, ideally, we would do nothing. Proper behaviour on fault is 1212 * for the tag to be present in the FAR_ELx register. But for user-only 1213 * mode, we do not have a TLB with which to implement this, so we must 1214 * remove the top byte. 1215 */ 1216 static inline uint64_t useronly_clean_ptr(uint64_t ptr) 1217 { 1218 #ifdef CONFIG_USER_ONLY 1219 /* TBI0 is known to be enabled, while TBI1 is disabled. */ 1220 ptr &= sextract64(ptr, 0, 56); 1221 #endif 1222 return ptr; 1223 } 1224 1225 static inline uint64_t useronly_maybe_clean_ptr(uint32_t desc, uint64_t ptr) 1226 { 1227 #ifdef CONFIG_USER_ONLY 1228 int64_t clean_ptr = sextract64(ptr, 0, 56); 1229 if (tbi_check(desc, clean_ptr < 0)) { 1230 ptr = clean_ptr; 1231 } 1232 #endif 1233 return ptr; 1234 } 1235 1236 /* Values for M-profile PSR.ECI for MVE insns */ 1237 enum MVEECIState { 1238 ECI_NONE = 0, /* No completed beats */ 1239 ECI_A0 = 1, /* Completed: A0 */ 1240 ECI_A0A1 = 2, /* Completed: A0, A1 */ 1241 /* 3 is reserved */ 1242 ECI_A0A1A2 = 4, /* Completed: A0, A1, A2 */ 1243 ECI_A0A1A2B0 = 5, /* Completed: A0, A1, A2, B0 */ 1244 /* All other values reserved */ 1245 }; 1246 1247 /* Definitions for the PMU registers */ 1248 #define PMCRN_MASK 0xf800 1249 #define PMCRN_SHIFT 11 1250 #define PMCRLC 0x40 1251 #define PMCRDP 0x20 1252 #define PMCRX 0x10 1253 #define PMCRD 0x8 1254 #define PMCRC 0x4 1255 #define PMCRP 0x2 1256 #define PMCRE 0x1 1257 /* 1258 * Mask of PMCR bits writeable by guest (not including WO bits like C, P, 1259 * which can be written as 1 to trigger behaviour but which stay RAZ). 1260 */ 1261 #define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE) 1262 1263 #define PMXEVTYPER_P 0x80000000 1264 #define PMXEVTYPER_U 0x40000000 1265 #define PMXEVTYPER_NSK 0x20000000 1266 #define PMXEVTYPER_NSU 0x10000000 1267 #define PMXEVTYPER_NSH 0x08000000 1268 #define PMXEVTYPER_M 0x04000000 1269 #define PMXEVTYPER_MT 0x02000000 1270 #define PMXEVTYPER_EVTCOUNT 0x0000ffff 1271 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \ 1272 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \ 1273 PMXEVTYPER_M | PMXEVTYPER_MT | \ 1274 PMXEVTYPER_EVTCOUNT) 1275 1276 #define PMCCFILTR 0xf8000000 1277 #define PMCCFILTR_M PMXEVTYPER_M 1278 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M) 1279 1280 static inline uint32_t pmu_num_counters(CPUARMState *env) 1281 { 1282 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT; 1283 } 1284 1285 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */ 1286 static inline uint64_t pmu_counter_mask(CPUARMState *env) 1287 { 1288 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1); 1289 } 1290 1291 #ifdef TARGET_AARCH64 1292 int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg); 1293 int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg); 1294 int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg); 1295 int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg); 1296 #endif 1297 1298 #endif 1299