qemu/target-arm/internals.h
<<
>>
Prefs
   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/* register banks for CPU modes */
  29#define BANK_USRSYS 0
  30#define BANK_SVC    1
  31#define BANK_ABT    2
  32#define BANK_UND    3
  33#define BANK_IRQ    4
  34#define BANK_FIQ    5
  35#define BANK_HYP    6
  36#define BANK_MON    7
  37
  38static inline bool excp_is_internal(int excp)
  39{
  40    /* Return true if this exception number represents a QEMU-internal
  41     * exception that will not be passed to the guest.
  42     */
  43    return excp == EXCP_INTERRUPT
  44        || excp == EXCP_HLT
  45        || excp == EXCP_DEBUG
  46        || excp == EXCP_HALTED
  47        || excp == EXCP_EXCEPTION_EXIT
  48        || excp == EXCP_KERNEL_TRAP
  49        || excp == EXCP_SEMIHOST
  50        || excp == EXCP_STREX;
  51}
  52
  53/* Exception names for debug logging; note that not all of these
  54 * precisely correspond to architectural exceptions.
  55 */
  56static const char * const excnames[] = {
  57    [EXCP_UDEF] = "Undefined Instruction",
  58    [EXCP_SWI] = "SVC",
  59    [EXCP_PREFETCH_ABORT] = "Prefetch Abort",
  60    [EXCP_DATA_ABORT] = "Data Abort",
  61    [EXCP_IRQ] = "IRQ",
  62    [EXCP_FIQ] = "FIQ",
  63    [EXCP_BKPT] = "Breakpoint",
  64    [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit",
  65    [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage",
  66    [EXCP_STREX] = "QEMU intercept of STREX",
  67    [EXCP_HVC] = "Hypervisor Call",
  68    [EXCP_HYP_TRAP] = "Hypervisor Trap",
  69    [EXCP_SMC] = "Secure Monitor Call",
  70    [EXCP_VIRQ] = "Virtual IRQ",
  71    [EXCP_VFIQ] = "Virtual FIQ",
  72    [EXCP_SEMIHOST] = "Semihosting call",
  73    [EXCP_WFI] = "WFI",
  74};
  75
  76static inline void arm_log_exception(int idx)
  77{
  78    if (qemu_loglevel_mask(CPU_LOG_INT)) {
  79        const char *exc = NULL;
  80
  81        if (idx >= 0 && idx < ARRAY_SIZE(excnames)) {
  82            exc = excnames[idx];
  83        }
  84        if (!exc) {
  85            exc = "unknown";
  86        }
  87        qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc);
  88    }
  89}
  90
  91/* Scale factor for generic timers, ie number of ns per tick.
  92 * This gives a 62.5MHz timer.
  93 */
  94#define GTIMER_SCALE 16
  95
  96/*
  97 * For AArch64, map a given EL to an index in the banked_spsr array.
  98 * Note that this mapping and the AArch32 mapping defined in bank_number()
  99 * must agree such that the AArch64<->AArch32 SPSRs have the architecturally
 100 * mandated mapping between each other.
 101 */
 102static inline unsigned int aarch64_banked_spsr_index(unsigned int el)
 103{
 104    static const unsigned int map[4] = {
 105        [1] = BANK_SVC, /* EL1.  */
 106        [2] = BANK_HYP, /* EL2.  */
 107        [3] = BANK_MON, /* EL3.  */
 108    };
 109    assert(el >= 1 && el <= 3);
 110    return map[el];
 111}
 112
 113/* Map CPU modes onto saved register banks.  */
 114static inline int bank_number(int mode)
 115{
 116    switch (mode) {
 117    case ARM_CPU_MODE_USR:
 118    case ARM_CPU_MODE_SYS:
 119        return BANK_USRSYS;
 120    case ARM_CPU_MODE_SVC:
 121        return BANK_SVC;
 122    case ARM_CPU_MODE_ABT:
 123        return BANK_ABT;
 124    case ARM_CPU_MODE_UND:
 125        return BANK_UND;
 126    case ARM_CPU_MODE_IRQ:
 127        return BANK_IRQ;
 128    case ARM_CPU_MODE_FIQ:
 129        return BANK_FIQ;
 130    case ARM_CPU_MODE_HYP:
 131        return BANK_HYP;
 132    case ARM_CPU_MODE_MON:
 133        return BANK_MON;
 134    }
 135    g_assert_not_reached();
 136}
 137
 138void switch_mode(CPUARMState *, int);
 139void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu);
 140void arm_translate_init(void);
 141
 142enum arm_fprounding {
 143    FPROUNDING_TIEEVEN,
 144    FPROUNDING_POSINF,
 145    FPROUNDING_NEGINF,
 146    FPROUNDING_ZERO,
 147    FPROUNDING_TIEAWAY,
 148    FPROUNDING_ODD
 149};
 150
 151int arm_rmode_to_sf(int rmode);
 152
 153static inline void aarch64_save_sp(CPUARMState *env, int el)
 154{
 155    if (env->pstate & PSTATE_SP) {
 156        env->sp_el[el] = env->xregs[31];
 157    } else {
 158        env->sp_el[0] = env->xregs[31];
 159    }
 160}
 161
 162static inline void aarch64_restore_sp(CPUARMState *env, int el)
 163{
 164    if (env->pstate & PSTATE_SP) {
 165        env->xregs[31] = env->sp_el[el];
 166    } else {
 167        env->xregs[31] = env->sp_el[0];
 168    }
 169}
 170
 171static inline void update_spsel(CPUARMState *env, uint32_t imm)
 172{
 173    unsigned int cur_el = arm_current_el(env);
 174    /* Update PSTATE SPSel bit; this requires us to update the
 175     * working stack pointer in xregs[31].
 176     */
 177    if (!((imm ^ env->pstate) & PSTATE_SP)) {
 178        return;
 179    }
 180    aarch64_save_sp(env, cur_el);
 181    env->pstate = deposit32(env->pstate, 0, 1, imm);
 182
 183    /* We rely on illegal updates to SPsel from EL0 to get trapped
 184     * at translation time.
 185     */
 186    assert(cur_el >= 1 && cur_el <= 3);
 187    aarch64_restore_sp(env, cur_el);
 188}
 189
 190/*
 191 * arm_pamax
 192 * @cpu: ARMCPU
 193 *
 194 * Returns the implementation defined bit-width of physical addresses.
 195 * The ARMv8 reference manuals refer to this as PAMax().
 196 */
 197static inline unsigned int arm_pamax(ARMCPU *cpu)
 198{
 199    static const unsigned int pamax_map[] = {
 200        [0] = 32,
 201        [1] = 36,
 202        [2] = 40,
 203        [3] = 42,
 204        [4] = 44,
 205        [5] = 48,
 206    };
 207    unsigned int parange = extract32(cpu->id_aa64mmfr0, 0, 4);
 208
 209    /* id_aa64mmfr0 is a read-only register so values outside of the
 210     * supported mappings can be considered an implementation error.  */
 211    assert(parange < ARRAY_SIZE(pamax_map));
 212    return pamax_map[parange];
 213}
 214
 215/* Return true if extended addresses are enabled.
 216 * This is always the case if our translation regime is 64 bit,
 217 * but depends on TTBCR.EAE for 32 bit.
 218 */
 219static inline bool extended_addresses_enabled_el(CPUARMState *env,
 220                                                 unsigned int el,
 221                                                 TCR *tcr)
 222{
 223    return arm_el_is_aa64(env, el) ||
 224           (arm_feature(env, ARM_FEATURE_LPAE) && (tcr->raw_tcr & TTBCR_EAE));
 225}
 226
 227static inline bool extended_addresses_enabled(CPUARMState *env)
 228{
 229    unsigned int cur_el = arm_current_el(env);
 230    TCR *tcr;
 231
 232    if (cur_el == 0) {
 233        /* EL0 is under EL1's TCR control.  */
 234        cur_el = 1;
 235    }
 236
 237    tcr = &env->cp15.tcr_el[cur_el];
 238    return extended_addresses_enabled_el(env, cur_el, tcr);
 239}
 240
 241/* Valid Syndrome Register EC field values */
 242enum arm_exception_class {
 243    EC_UNCATEGORIZED          = 0x00,
 244    EC_WFX_TRAP               = 0x01,
 245    EC_CP15RTTRAP             = 0x03,
 246    EC_CP15RRTTRAP            = 0x04,
 247    EC_CP14RTTRAP             = 0x05,
 248    EC_CP14DTTRAP             = 0x06,
 249    EC_ADVSIMDFPACCESSTRAP    = 0x07,
 250    EC_FPIDTRAP               = 0x08,
 251    EC_CP14RRTTRAP            = 0x0c,
 252    EC_ILLEGALSTATE           = 0x0e,
 253    EC_AA32_SVC               = 0x11,
 254    EC_AA32_HVC               = 0x12,
 255    EC_AA32_SMC               = 0x13,
 256    EC_AA64_SVC               = 0x15,
 257    EC_AA64_HVC               = 0x16,
 258    EC_AA64_SMC               = 0x17,
 259    EC_SYSTEMREGISTERTRAP     = 0x18,
 260    EC_INSNABORT              = 0x20,
 261    EC_INSNABORT_SAME_EL      = 0x21,
 262    EC_PCALIGNMENT            = 0x22,
 263    EC_DATAABORT              = 0x24,
 264    EC_DATAABORT_SAME_EL      = 0x25,
 265    EC_SPALIGNMENT            = 0x26,
 266    EC_AA32_FPTRAP            = 0x28,
 267    EC_AA64_FPTRAP            = 0x2c,
 268    EC_SERROR                 = 0x2f,
 269    EC_BREAKPOINT             = 0x30,
 270    EC_BREAKPOINT_SAME_EL     = 0x31,
 271    EC_SOFTWARESTEP           = 0x32,
 272    EC_SOFTWARESTEP_SAME_EL   = 0x33,
 273    EC_WATCHPOINT             = 0x34,
 274    EC_WATCHPOINT_SAME_EL     = 0x35,
 275    EC_AA32_BKPT              = 0x38,
 276    EC_VECTORCATCH            = 0x3a,
 277    EC_AA64_BKPT              = 0x3c,
 278};
 279
 280#define ARM_EL_EC_SHIFT 26
 281#define ARM_EL_IL_SHIFT 25
 282#define ARM_EL_ISV_SHIFT 24
 283#define ARM_EL_IL (1 << ARM_EL_IL_SHIFT)
 284#define ARM_EL_ISV (1 << ARM_EL_ISV_SHIFT)
 285
 286/* Utility functions for constructing various kinds of syndrome value.
 287 * Note that in general we follow the AArch64 syndrome values; in a
 288 * few cases the value in HSR for exceptions taken to AArch32 Hyp
 289 * mode differs slightly, so if we ever implemented Hyp mode then the
 290 * syndrome value would need some massaging on exception entry.
 291 * (One example of this is that AArch64 defaults to IL bit set for
 292 * exceptions which don't specifically indicate information about the
 293 * trapping instruction, whereas AArch32 defaults to IL bit clear.)
 294 */
 295static inline uint32_t syn_uncategorized(void)
 296{
 297    return (EC_UNCATEGORIZED << ARM_EL_EC_SHIFT) | ARM_EL_IL;
 298}
 299
 300static inline uint32_t syn_aa64_svc(uint32_t imm16)
 301{
 302    return (EC_AA64_SVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
 303}
 304
 305static inline uint32_t syn_aa64_hvc(uint32_t imm16)
 306{
 307    return (EC_AA64_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
 308}
 309
 310static inline uint32_t syn_aa64_smc(uint32_t imm16)
 311{
 312    return (EC_AA64_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
 313}
 314
 315static inline uint32_t syn_aa32_svc(uint32_t imm16, bool is_16bit)
 316{
 317    return (EC_AA32_SVC << ARM_EL_EC_SHIFT) | (imm16 & 0xffff)
 318        | (is_16bit ? 0 : ARM_EL_IL);
 319}
 320
 321static inline uint32_t syn_aa32_hvc(uint32_t imm16)
 322{
 323    return (EC_AA32_HVC << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
 324}
 325
 326static inline uint32_t syn_aa32_smc(void)
 327{
 328    return (EC_AA32_SMC << ARM_EL_EC_SHIFT) | ARM_EL_IL;
 329}
 330
 331static inline uint32_t syn_aa64_bkpt(uint32_t imm16)
 332{
 333    return (EC_AA64_BKPT << ARM_EL_EC_SHIFT) | ARM_EL_IL | (imm16 & 0xffff);
 334}
 335
 336static inline uint32_t syn_aa32_bkpt(uint32_t imm16, bool is_16bit)
 337{
 338    return (EC_AA32_BKPT << ARM_EL_EC_SHIFT) | (imm16 & 0xffff)
 339        | (is_16bit ? 0 : ARM_EL_IL);
 340}
 341
 342static inline uint32_t syn_aa64_sysregtrap(int op0, int op1, int op2,
 343                                           int crn, int crm, int rt,
 344                                           int isread)
 345{
 346    return (EC_SYSTEMREGISTERTRAP << ARM_EL_EC_SHIFT) | ARM_EL_IL
 347        | (op0 << 20) | (op2 << 17) | (op1 << 14) | (crn << 10) | (rt << 5)
 348        | (crm << 1) | isread;
 349}
 350
 351static inline uint32_t syn_cp14_rt_trap(int cv, int cond, int opc1, int opc2,
 352                                        int crn, int crm, int rt, int isread,
 353                                        bool is_16bit)
 354{
 355    return (EC_CP14RTTRAP << ARM_EL_EC_SHIFT)
 356        | (is_16bit ? 0 : ARM_EL_IL)
 357        | (cv << 24) | (cond << 20) | (opc2 << 17) | (opc1 << 14)
 358        | (crn << 10) | (rt << 5) | (crm << 1) | isread;
 359}
 360
 361static inline uint32_t syn_cp15_rt_trap(int cv, int cond, int opc1, int opc2,
 362                                        int crn, int crm, int rt, int isread,
 363                                        bool is_16bit)
 364{
 365    return (EC_CP15RTTRAP << ARM_EL_EC_SHIFT)
 366        | (is_16bit ? 0 : ARM_EL_IL)
 367        | (cv << 24) | (cond << 20) | (opc2 << 17) | (opc1 << 14)
 368        | (crn << 10) | (rt << 5) | (crm << 1) | isread;
 369}
 370
 371static inline uint32_t syn_cp14_rrt_trap(int cv, int cond, int opc1, int crm,
 372                                         int rt, int rt2, int isread,
 373                                         bool is_16bit)
 374{
 375    return (EC_CP14RRTTRAP << ARM_EL_EC_SHIFT)
 376        | (is_16bit ? 0 : ARM_EL_IL)
 377        | (cv << 24) | (cond << 20) | (opc1 << 16)
 378        | (rt2 << 10) | (rt << 5) | (crm << 1) | isread;
 379}
 380
 381static inline uint32_t syn_cp15_rrt_trap(int cv, int cond, int opc1, int crm,
 382                                         int rt, int rt2, int isread,
 383                                         bool is_16bit)
 384{
 385    return (EC_CP15RRTTRAP << ARM_EL_EC_SHIFT)
 386        | (is_16bit ? 0 : ARM_EL_IL)
 387        | (cv << 24) | (cond << 20) | (opc1 << 16)
 388        | (rt2 << 10) | (rt << 5) | (crm << 1) | isread;
 389}
 390
 391static inline uint32_t syn_fp_access_trap(int cv, int cond, bool is_16bit)
 392{
 393    return (EC_ADVSIMDFPACCESSTRAP << ARM_EL_EC_SHIFT)
 394        | (is_16bit ? 0 : ARM_EL_IL)
 395        | (cv << 24) | (cond << 20);
 396}
 397
 398static inline uint32_t syn_insn_abort(int same_el, int ea, int s1ptw, int fsc)
 399{
 400    return (EC_INSNABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
 401        | (ea << 9) | (s1ptw << 7) | fsc;
 402}
 403
 404static inline uint32_t syn_data_abort_no_iss(int same_el,
 405                                             int ea, int cm, int s1ptw,
 406                                             int wnr, int fsc)
 407{
 408    return (EC_DATAABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
 409           | ARM_EL_IL
 410           | (ea << 9) | (cm << 8) | (s1ptw << 7) | (wnr << 6) | fsc;
 411}
 412
 413static inline uint32_t syn_data_abort_with_iss(int same_el,
 414                                               int sas, int sse, int srt,
 415                                               int sf, int ar,
 416                                               int ea, int cm, int s1ptw,
 417                                               int wnr, int fsc,
 418                                               bool is_16bit)
 419{
 420    return (EC_DATAABORT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
 421           | (is_16bit ? 0 : ARM_EL_IL)
 422           | ARM_EL_ISV | (sas << 22) | (sse << 21) | (srt << 16)
 423           | (sf << 15) | (ar << 14)
 424           | (ea << 9) | (cm << 8) | (s1ptw << 7) | (wnr << 6) | fsc;
 425}
 426
 427static inline uint32_t syn_swstep(int same_el, int isv, int ex)
 428{
 429    return (EC_SOFTWARESTEP << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
 430        | (isv << 24) | (ex << 6) | 0x22;
 431}
 432
 433static inline uint32_t syn_watchpoint(int same_el, int cm, int wnr)
 434{
 435    return (EC_WATCHPOINT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
 436        | (cm << 8) | (wnr << 6) | 0x22;
 437}
 438
 439static inline uint32_t syn_breakpoint(int same_el)
 440{
 441    return (EC_BREAKPOINT << ARM_EL_EC_SHIFT) | (same_el << ARM_EL_EC_SHIFT)
 442        | ARM_EL_IL | 0x22;
 443}
 444
 445static inline uint32_t syn_wfx(int cv, int cond, int ti)
 446{
 447    return (EC_WFX_TRAP << ARM_EL_EC_SHIFT) | ARM_EL_IL |
 448           (cv << 24) | (cond << 20) | ti;
 449}
 450
 451/* Update a QEMU watchpoint based on the information the guest has set in the
 452 * DBGWCR<n>_EL1 and DBGWVR<n>_EL1 registers.
 453 */
 454void hw_watchpoint_update(ARMCPU *cpu, int n);
 455/* Update the QEMU watchpoints for every guest watchpoint. This does a
 456 * complete delete-and-reinstate of the QEMU watchpoint list and so is
 457 * suitable for use after migration or on reset.
 458 */
 459void hw_watchpoint_update_all(ARMCPU *cpu);
 460/* Update a QEMU breakpoint based on the information the guest has set in the
 461 * DBGBCR<n>_EL1 and DBGBVR<n>_EL1 registers.
 462 */
 463void hw_breakpoint_update(ARMCPU *cpu, int n);
 464/* Update the QEMU breakpoints for every guest breakpoint. This does a
 465 * complete delete-and-reinstate of the QEMU breakpoint list and so is
 466 * suitable for use after migration or on reset.
 467 */
 468void hw_breakpoint_update_all(ARMCPU *cpu);
 469
 470/* Callback function for checking if a watchpoint should trigger. */
 471bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp);
 472
 473/* Callback function for when a watchpoint or breakpoint triggers. */
 474void arm_debug_excp_handler(CPUState *cs);
 475
 476#ifdef CONFIG_USER_ONLY
 477static inline bool arm_is_psci_call(ARMCPU *cpu, int excp_type)
 478{
 479    return false;
 480}
 481#else
 482/* Return true if the r0/x0 value indicates that this SMC/HVC is a PSCI call. */
 483bool arm_is_psci_call(ARMCPU *cpu, int excp_type);
 484/* Actually handle a PSCI call */
 485void arm_handle_psci_call(ARMCPU *cpu);
 486#endif
 487
 488/**
 489 * ARMMMUFaultInfo: Information describing an ARM MMU Fault
 490 * @s2addr: Address that caused a fault at stage 2
 491 * @stage2: True if we faulted at stage 2
 492 * @s1ptw: True if we faulted at stage 2 while doing a stage 1 page-table walk
 493 */
 494typedef struct ARMMMUFaultInfo ARMMMUFaultInfo;
 495struct ARMMMUFaultInfo {
 496    target_ulong s2addr;
 497    bool stage2;
 498    bool s1ptw;
 499};
 500
 501/* Do a page table walk and add page to TLB if possible */
 502bool arm_tlb_fill(CPUState *cpu, vaddr address, int rw, int mmu_idx,
 503                  uint32_t *fsr, ARMMMUFaultInfo *fi);
 504
 505/* Return true if the stage 1 translation regime is using LPAE format page
 506 * tables */
 507bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx);
 508
 509/* Raise a data fault alignment exception for the specified virtual address */
 510void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, int is_write,
 511                                 int is_user, uintptr_t retaddr);
 512
 513#endif
 514