isa.cc revision 13417
1360SN/A/*
21458SN/A * Copyright (c) 2010-2018 ARM Limited
3360SN/A * All rights reserved
4360SN/A *
5360SN/A * The license below extends only to copyright in the software and shall
6360SN/A * not be construed as granting a license to any other intellectual
7360SN/A * property including but not limited to intellectual property relating
8360SN/A * to a hardware implementation of the functionality of the software
9360SN/A * licensed hereunder.  You may use the software subject to the license
10360SN/A * terms below provided that you ensure that this notice is replicated
11360SN/A * unmodified and in its entirety in all distributions of the software,
12360SN/A * modified or unmodified, in source code or in binary form.
13360SN/A *
14360SN/A * Redistribution and use in source and binary forms, with or without
15360SN/A * modification, are permitted provided that the following conditions are
16360SN/A * met: redistributions of source code must retain the above copyright
17360SN/A * notice, this list of conditions and the following disclaimer;
18360SN/A * redistributions in binary form must reproduce the above copyright
19360SN/A * notice, this list of conditions and the following disclaimer in the
20360SN/A * documentation and/or other materials provided with the distribution;
21360SN/A * neither the name of the copyright holders nor the names of its
22360SN/A * contributors may be used to endorse or promote products derived from
23360SN/A * this software without specific prior written permission.
24360SN/A *
25360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
272665Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
282665Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
292665Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
302707Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
331354SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
341354SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362764Sstever@eecs.umich.edu *
372764Sstever@eecs.umich.edu * Authors: Gabe Black
382064SN/A *          Ali Saidi
39360SN/A */
40360SN/A
41360SN/A#include "arch/arm/isa.hh"
42360SN/A#include "arch/arm/pmu.hh"
43360SN/A#include "arch/arm/system.hh"
44360SN/A#include "arch/arm/tlb.hh"
451354SN/A#include "arch/arm/tlbi_op.hh"
46360SN/A#include "cpu/base.hh"
471809SN/A#include "cpu/checker/cpu.hh"
481809SN/A#include "debug/Arm.hh"
491809SN/A#include "debug/MiscRegs.hh"
501999SN/A#include "dev/arm/generic_timer.hh"
51360SN/A#include "params/ArmISA.hh"
522474SN/A#include "sim/faults.hh"
532474SN/A#include "sim/stat_control.hh"
54360SN/A#include "sim/system.hh"
552462SN/A
561354SN/Anamespace ArmISA
572474SN/A{
582680Sktlim@umich.edu
592474SN/AISA::ISA(Params *p)
602474SN/A    : SimObject(p),
611354SN/A      system(NULL),
62360SN/A      _decoderFlavour(p->decoderFlavour),
63360SN/A      _vecRegRenameMode(p->vecRegRenameMode),
64360SN/A      pmu(p->pmu),
65360SN/A      impdefAsNop(p->impdef_nop)
66360SN/A{
67360SN/A    miscRegs[MISCREG_SCTLR_RST] = 0;
68360SN/A
69360SN/A    // Hook up a dummy device if we haven't been configured with a
70378SN/A    // real PMU. By using a dummy device, we don't need to check that
711450SN/A    // the PMU exist every time we try to access a PMU register.
722680Sktlim@umich.edu    if (!pmu)
73360SN/A        pmu = &dummyDevice;
74360SN/A
75360SN/A    // Give all ISA devices a pointer to this ISA
76360SN/A    pmu->setISA(this);
77360SN/A
78360SN/A    system = dynamic_cast<ArmSystem *>(p->system);
79360SN/A
80360SN/A    // Cache system-level properties
81360SN/A    if (FullSystem && system) {
822680Sktlim@umich.edu        highestELIs64 = system->highestELIs64();
83360SN/A        haveSecurity = system->haveSecurity();
84360SN/A        haveLPAE = system->haveLPAE();
85360SN/A        haveCrypto = system->haveCrypto();
86360SN/A        haveVirtualization = system->haveVirtualization();
87360SN/A        haveLargeAsid64 = system->haveLargeAsid64();
88360SN/A        physAddrRange = system->physAddrRange();
89360SN/A    } else {
90360SN/A        highestELIs64 = true; // ArmSystem::highestELIs64 does the same
91360SN/A        haveSecurity = haveLPAE = haveVirtualization = false;
92360SN/A        haveCrypto = false;
93360SN/A        haveLargeAsid64 = false;
942680Sktlim@umich.edu        physAddrRange = 32;  // dummy value
95360SN/A    }
96360SN/A
97360SN/A    initializeMiscRegMetadata();
98360SN/A    preUnflattenMiscReg();
99360SN/A
100360SN/A    clear();
101360SN/A}
102360SN/A
103360SN/Astd::vector<struct ISA::MiscRegLUTEntry> ISA::lookUpMiscReg(NUM_MISCREGS);
104360SN/A
105360SN/Aconst ArmISAParams *
106360SN/AISA::params() const
107360SN/A{
108360SN/A    return dynamic_cast<const Params *>(_params);
109360SN/A}
110360SN/A
111360SN/Avoid
112360SN/AISA::clear()
113360SN/A{
114360SN/A    const Params *p(params());
115360SN/A
1162400SN/A    SCTLR sctlr_rst = miscRegs[MISCREG_SCTLR_RST];
117360SN/A    memset(miscRegs, 0, sizeof(miscRegs));
1182461SN/A
119360SN/A    initID32(p);
120360SN/A
121360SN/A    // We always initialize AArch64 ID registers even
122360SN/A    // if we are in AArch32. This is done since if we
123360SN/A    // are in SE mode we don't know if our ArmProcess is
124360SN/A    // AArch32 or AArch64
1252400SN/A    initID64(p);
126360SN/A
1272461SN/A    // Start with an event in the mailbox
128360SN/A    miscRegs[MISCREG_SEV_MAILBOX] = 1;
129360SN/A
130360SN/A    // Separate Instruction and Data TLBs
131360SN/A    miscRegs[MISCREG_TLBTR] = 1;
132360SN/A
133360SN/A    MVFR0 mvfr0 = 0;
134360SN/A    mvfr0.advSimdRegisters = 2;
135360SN/A    mvfr0.singlePrecision = 2;
136360SN/A    mvfr0.doublePrecision = 2;
137360SN/A    mvfr0.vfpExceptionTrapping = 0;
138360SN/A    mvfr0.divide = 1;
139360SN/A    mvfr0.squareRoot = 1;
140360SN/A    mvfr0.shortVectors = 1;
141360SN/A    mvfr0.roundingModes = 1;
142360SN/A    miscRegs[MISCREG_MVFR0] = mvfr0;
143360SN/A
144360SN/A    MVFR1 mvfr1 = 0;
145360SN/A    mvfr1.flushToZero = 1;
146360SN/A    mvfr1.defaultNaN = 1;
147360SN/A    mvfr1.advSimdLoadStore = 1;
148360SN/A    mvfr1.advSimdInteger = 1;
149360SN/A    mvfr1.advSimdSinglePrecision = 1;
150360SN/A    mvfr1.advSimdHalfPrecision = 1;
151360SN/A    mvfr1.vfpHalfPrecision = 1;
152360SN/A    miscRegs[MISCREG_MVFR1] = mvfr1;
153360SN/A
154360SN/A    // Reset values of PRRR and NMRR are implementation dependent
155360SN/A
156360SN/A    // @todo: PRRR and NMRR in secure state?
157360SN/A    miscRegs[MISCREG_PRRR_NS] =
158360SN/A        (1 << 19) | // 19
159360SN/A        (0 << 18) | // 18
160502SN/A        (0 << 17) | // 17
161360SN/A        (1 << 16) | // 16
162502SN/A        (2 << 14) | // 15:14
163360SN/A        (0 << 12) | // 13:12
164360SN/A        (2 << 10) | // 11:10
165360SN/A        (2 << 8)  | // 9:8
166360SN/A        (2 << 6)  | // 7:6
167360SN/A        (2 << 4)  | // 5:4
168360SN/A        (1 << 2)  | // 3:2
169360SN/A        0;          // 1:0
170360SN/A
171360SN/A    miscRegs[MISCREG_NMRR_NS] =
172360SN/A        (1 << 30) | // 31:30
173360SN/A        (0 << 26) | // 27:26
174378SN/A        (0 << 24) | // 25:24
1751706SN/A        (3 << 22) | // 23:22
1762680Sktlim@umich.edu        (2 << 20) | // 21:20
177378SN/A        (0 << 18) | // 19:18
178378SN/A        (0 << 16) | // 17:16
179378SN/A        (1 << 14) | // 15:14
180378SN/A        (0 << 12) | // 13:12
181378SN/A        (2 << 10) | // 11:10
1821706SN/A        (0 << 8)  | // 9:8
1832680Sktlim@umich.edu        (3 << 6)  | // 7:6
184360SN/A        (2 << 4)  | // 5:4
185378SN/A        (0 << 2)  | // 3:2
1861706SN/A        0;          // 1:0
1872680Sktlim@umich.edu
188378SN/A    if (FullSystem && system->highestELIs64()) {
189378SN/A        // Initialize AArch64 state
1901706SN/A        clear64(p);
1912680Sktlim@umich.edu        return;
192378SN/A    }
193378SN/A
1941706SN/A    // Initialize AArch32 state...
1952680Sktlim@umich.edu    clear32(p, sctlr_rst);
196378SN/A}
197378SN/A
1981706SN/Avoid
1992680Sktlim@umich.eduISA::clear32(const ArmISAParams *p, const SCTLR &sctlr_rst)
200378SN/A{
201378SN/A    CPSR cpsr = 0;
2021706SN/A    cpsr.mode = MODE_USER;
2032680Sktlim@umich.edu
204378SN/A    if (FullSystem) {
205378SN/A        miscRegs[MISCREG_MVBAR] = system->resetAddr();
2061706SN/A    }
2072680Sktlim@umich.edu
208378SN/A    miscRegs[MISCREG_CPSR] = cpsr;
209378SN/A    updateRegMap(cpsr);
2101706SN/A
2112680Sktlim@umich.edu    SCTLR sctlr = 0;
212378SN/A    sctlr.te = (bool) sctlr_rst.te;
213378SN/A    sctlr.nmfi = (bool) sctlr_rst.nmfi;
2141706SN/A    sctlr.v = (bool) sctlr_rst.v;
2152680Sktlim@umich.edu    sctlr.u = 1;
216378SN/A    sctlr.xp = 1;
217378SN/A    sctlr.rao2 = 1;
2181706SN/A    sctlr.rao3 = 1;
2192680Sktlim@umich.edu    sctlr.rao4 = 0xf;  // SCTLR[6:3]
220360SN/A    sctlr.uci = 1;
221511SN/A    sctlr.dze = 1;
2221706SN/A    miscRegs[MISCREG_SCTLR_NS] = sctlr;
2232680Sktlim@umich.edu    miscRegs[MISCREG_SCTLR_RST] = sctlr_rst;
224511SN/A    miscRegs[MISCREG_HCPTR] = 0;
225511SN/A
2261706SN/A    miscRegs[MISCREG_CPACR] = 0;
2272680Sktlim@umich.edu
2281706SN/A    miscRegs[MISCREG_FPSID] = p->fpsid;
2291706SN/A
2301706SN/A    if (haveLPAE) {
2311706SN/A        TTBCR ttbcr = miscRegs[MISCREG_TTBCR_NS];
2322680Sktlim@umich.edu        ttbcr.eae = 0;
2331706SN/A        miscRegs[MISCREG_TTBCR_NS] = ttbcr;
2341706SN/A        // Enforce consistency with system-level settings
2351706SN/A        miscRegs[MISCREG_ID_MMFR0] = (miscRegs[MISCREG_ID_MMFR0] & ~0xf) | 0x5;
2361706SN/A    }
2372680Sktlim@umich.edu
2381706SN/A    if (haveSecurity) {
239511SN/A        miscRegs[MISCREG_SCTLR_S] = sctlr;
2401999SN/A        miscRegs[MISCREG_SCR] = 0;
2411999SN/A        miscRegs[MISCREG_VBAR_S] = 0;
2422680Sktlim@umich.edu    } else {
2431999SN/A        // we're always non-secure
2441999SN/A        miscRegs[MISCREG_SCR] = 1;
2451999SN/A    }
2461999SN/A
2472680Sktlim@umich.edu    //XXX We need to initialize the rest of the state.
2481999SN/A}
2492093SN/A
2502093SN/Avoid
2512680Sktlim@umich.eduISA::clear64(const ArmISAParams *p)
2522093SN/A{
2532687Sksewell@umich.edu    CPSR cpsr = 0;
2542687Sksewell@umich.edu    Addr rvbar = system->resetAddr();
2552687Sksewell@umich.edu    switch (system->highestEL()) {
2562687Sksewell@umich.edu        // Set initial EL to highest implemented EL using associated stack
2572238SN/A        // pointer (SP_ELx); set RVBAR_ELx to implementation defined reset
2582238SN/A        // value
2592680Sktlim@umich.edu      case EL3:
2602238SN/A        cpsr.mode = MODE_EL3H;
2612238SN/A        miscRegs[MISCREG_RVBAR_EL3] = rvbar;
2622238SN/A        break;
2632680Sktlim@umich.edu      case EL2:
2642238SN/A        cpsr.mode = MODE_EL2H;
2652238SN/A        miscRegs[MISCREG_RVBAR_EL2] = rvbar;
2662238SN/A        break;
2672680Sktlim@umich.edu      case EL1:
2682238SN/A        cpsr.mode = MODE_EL1H;
2692238SN/A        miscRegs[MISCREG_RVBAR_EL1] = rvbar;
2702238SN/A        break;
2712680Sktlim@umich.edu      default:
2722238SN/A        panic("Invalid highest implemented exception level");
2732238SN/A        break;
2742238SN/A    }
2752680Sktlim@umich.edu
2762238SN/A    // Initialize rest of CPSR
2772238SN/A    cpsr.daif = 0xf;  // Mask all interrupts
2782238SN/A    cpsr.ss = 0;
2792680Sktlim@umich.edu    cpsr.il = 0;
2802238SN/A    miscRegs[MISCREG_CPSR] = cpsr;
2812238SN/A    updateRegMap(cpsr);
2822238SN/A
2832680Sktlim@umich.edu    // Initialize other control registers
2842238SN/A    miscRegs[MISCREG_MPIDR_EL1] = 0x80000000;
2852238SN/A    if (haveSecurity) {
2862238SN/A        miscRegs[MISCREG_SCTLR_EL3] = 0x30c50830;
2872238SN/A        miscRegs[MISCREG_SCR_EL3]   = 0x00000030;  // RES1 fields
2882238SN/A    } else if (haveVirtualization) {
2892238SN/A        // also  MISCREG_SCTLR_EL2 (by mapping)
2902680Sktlim@umich.edu        miscRegs[MISCREG_HSCTLR] = 0x30c50830;
2912238SN/A    } else {
2922238SN/A        // also  MISCREG_SCTLR_EL1 (by mapping)
2932238SN/A        miscRegs[MISCREG_SCTLR_NS] = 0x30d00800 | 0x00050030; // RES1 | init
2942680Sktlim@umich.edu        // Always non-secure
2952238SN/A        miscRegs[MISCREG_SCR_EL3] = 1;
2962238SN/A    }
2972238SN/A}
2982680Sktlim@umich.edu
2992238SN/Avoid
3002238SN/AISA::initID32(const ArmISAParams *p)
3012238SN/A{
3022680Sktlim@umich.edu    // Initialize configurable default values
3032238SN/A    miscRegs[MISCREG_MIDR] = p->midr;
3042238SN/A    miscRegs[MISCREG_MIDR_EL1] = p->midr;
3051354SN/A    miscRegs[MISCREG_VPIDR] = p->midr;
3061354SN/A
3071354SN/A    miscRegs[MISCREG_ID_ISAR0] = p->id_isar0;
3081354SN/A    miscRegs[MISCREG_ID_ISAR1] = p->id_isar1;
3091354SN/A    miscRegs[MISCREG_ID_ISAR2] = p->id_isar2;
3101354SN/A    miscRegs[MISCREG_ID_ISAR3] = p->id_isar3;
3111354SN/A    miscRegs[MISCREG_ID_ISAR4] = p->id_isar4;
3121354SN/A    miscRegs[MISCREG_ID_ISAR5] = p->id_isar5;
3131354SN/A
3141354SN/A    miscRegs[MISCREG_ID_MMFR0] = p->id_mmfr0;
3151354SN/A    miscRegs[MISCREG_ID_MMFR1] = p->id_mmfr1;
3161354SN/A    miscRegs[MISCREG_ID_MMFR2] = p->id_mmfr2;
3171354SN/A    miscRegs[MISCREG_ID_MMFR3] = p->id_mmfr3;
3181354SN/A}
3191354SN/A
3201354SN/Avoid
3211354SN/AISA::initID64(const ArmISAParams *p)
3221354SN/A{
3231354SN/A    // Initialize configurable id registers
3241354SN/A    miscRegs[MISCREG_ID_AA64AFR0_EL1] = p->id_aa64afr0_el1;
3251354SN/A    miscRegs[MISCREG_ID_AA64AFR1_EL1] = p->id_aa64afr1_el1;
3261354SN/A    miscRegs[MISCREG_ID_AA64DFR0_EL1] =
3271354SN/A        (p->id_aa64dfr0_el1 & 0xfffffffffffff0ffULL) |
3281609SN/A        (p->pmu ?             0x0000000000000100ULL : 0); // Enable PMUv3
3291354SN/A
3301354SN/A    miscRegs[MISCREG_ID_AA64DFR1_EL1] = p->id_aa64dfr1_el1;
3311354SN/A    miscRegs[MISCREG_ID_AA64ISAR0_EL1] = p->id_aa64isar0_el1;
3321354SN/A    miscRegs[MISCREG_ID_AA64ISAR1_EL1] = p->id_aa64isar1_el1;
333360SN/A    miscRegs[MISCREG_ID_AA64MMFR0_EL1] = p->id_aa64mmfr0_el1;
334360SN/A    miscRegs[MISCREG_ID_AA64MMFR1_EL1] = p->id_aa64mmfr1_el1;
335360SN/A    miscRegs[MISCREG_ID_AA64MMFR2_EL1] = p->id_aa64mmfr2_el1;
336360SN/A
337360SN/A    miscRegs[MISCREG_ID_DFR0_EL1] =
338360SN/A        (p->pmu ? 0x03000000ULL : 0); // Enable PMUv3
339360SN/A
340378SN/A    miscRegs[MISCREG_ID_DFR0] = miscRegs[MISCREG_ID_DFR0_EL1];
341378SN/A
342378SN/A    // Enforce consistency with system-level settings...
343360SN/A
3441450SN/A    // EL3
345360SN/A    miscRegs[MISCREG_ID_AA64PFR0_EL1] = insertBits(
3462680Sktlim@umich.edu        miscRegs[MISCREG_ID_AA64PFR0_EL1], 15, 12,
347360SN/A        haveSecurity ? 0x2 : 0x0);
3482680Sktlim@umich.edu    // EL2
3492680Sktlim@umich.edu    miscRegs[MISCREG_ID_AA64PFR0_EL1] = insertBits(
350360SN/A        miscRegs[MISCREG_ID_AA64PFR0_EL1], 11, 8,
3511969SN/A        haveVirtualization ? 0x2 : 0x0);
352360SN/A    // Large ASID support
353360SN/A    miscRegs[MISCREG_ID_AA64MMFR0_EL1] = insertBits(
354360SN/A        miscRegs[MISCREG_ID_AA64MMFR0_EL1], 7, 4,
3551458SN/A        haveLargeAsid64 ? 0x2 : 0x0);
356360SN/A    // Physical address size
357360SN/A    miscRegs[MISCREG_ID_AA64MMFR0_EL1] = insertBits(
358360SN/A        miscRegs[MISCREG_ID_AA64MMFR0_EL1], 3, 0,
3592553SN/A        encodePhysAddrRange64(physAddrRange));
3602553SN/A    // Crypto
3612553SN/A    miscRegs[MISCREG_ID_AA64ISAR0_EL1] = insertBits(
3622553SN/A        miscRegs[MISCREG_ID_AA64ISAR0_EL1], 19, 4,
3632553SN/A        haveCrypto ? 0x1112 : 0x0);
3642553SN/A}
3652553SN/A
3662553SN/Avoid
3671458SN/AISA::startup(ThreadContext *tc)
368360SN/A{
369360SN/A    pmu->setThreadContext(tc);
3701706SN/A
3712680Sktlim@umich.edu}
372360SN/A
373360SN/A
374360SN/AMiscReg
375378SN/AISA::readMiscRegNoEffect(int misc_reg) const
376360SN/A{
3771450SN/A    assert(misc_reg < NumMiscRegs);
378360SN/A
3792680Sktlim@umich.edu    const auto &reg = lookUpMiscReg[misc_reg]; // bit masks
380360SN/A    const auto &map = getMiscIndices(misc_reg);
381360SN/A    int lower = map.first, upper = map.second;
382360SN/A    // NB!: apply architectural masks according to desired register,
3832680Sktlim@umich.edu    // despite possibly getting value from different (mapped) register.
3841458SN/A    auto val = !upper ? miscRegs[lower] : ((miscRegs[lower] & mask(32))
385360SN/A                                          |(miscRegs[upper] << 32));
386360SN/A    if (val & reg.res0()) {
387360SN/A        DPRINTF(MiscRegs, "Reading MiscReg %s with set res0 bits: %#x\n",
388360SN/A                miscRegName[misc_reg], val & reg.res0());
3891706SN/A    }
3901458SN/A    if ((val & reg.res1()) != reg.res1()) {
391360SN/A        DPRINTF(MiscRegs, "Reading MiscReg %s with clear res1 bits: %#x\n",
392360SN/A                miscRegName[misc_reg], (val & reg.res1()) ^ reg.res1());
3932680Sktlim@umich.edu    }
3942680Sktlim@umich.edu    return (val & ~reg.raz()) | reg.rao(); // enforce raz/rao
395360SN/A}
396360SN/A
397360SN/A
398360SN/AMiscReg
399360SN/AISA::readMiscReg(int misc_reg, ThreadContext *tc)
400360SN/A{
401360SN/A    CPSR cpsr = 0;
402360SN/A    PCState pc = 0;
403360SN/A    SCR scr = 0;
404360SN/A
405360SN/A    if (misc_reg == MISCREG_CPSR) {
406360SN/A        cpsr = miscRegs[misc_reg];
4071706SN/A        pc = tc->pcState();
408360SN/A        cpsr.j = pc.jazelle() ? 1 : 0;
409360SN/A        cpsr.t = pc.thumb() ? 1 : 0;
410360SN/A        return cpsr;
411360SN/A    }
412360SN/A
4131706SN/A#ifndef NDEBUG
4141706SN/A    if (!miscRegInfo[misc_reg][MISCREG_IMPLEMENTED]) {
415360SN/A        if (miscRegInfo[misc_reg][MISCREG_WARN_NOT_FAIL])
416360SN/A            warn("Unimplemented system register %s read.\n",
417360SN/A                 miscRegName[misc_reg]);
4181970SN/A        else
419360SN/A            panic("Unimplemented system register %s read.\n",
420360SN/A                  miscRegName[misc_reg]);
421360SN/A    }
4221999SN/A#endif
4231999SN/A
4241999SN/A    switch (unflattenMiscReg(misc_reg)) {
4251999SN/A      case MISCREG_HCR:
4262680Sktlim@umich.edu        {
4271999SN/A            if (!haveVirtualization)
4281999SN/A                return 0;
4291999SN/A            else
4302680Sktlim@umich.edu                return readMiscRegNoEffect(MISCREG_HCR);
4311999SN/A        }
4321999SN/A      case MISCREG_CPACR:
4332680Sktlim@umich.edu        {
4341999SN/A            const uint32_t ones = (uint32_t)(-1);
4351999SN/A            CPACR cpacrMask = 0;
4361999SN/A            // Only cp10, cp11, and ase are implemented, nothing else should
4371999SN/A            // be readable? (straight copy from the write code)
4381999SN/A            cpacrMask.cp10 = ones;
4391999SN/A            cpacrMask.cp11 = ones;
4401999SN/A            cpacrMask.asedis = ones;
4411999SN/A
4422218SN/A            // Security Extensions may limit the readability of CPACR
4431999SN/A            if (haveSecurity) {
4441999SN/A                scr = readMiscRegNoEffect(MISCREG_SCR);
4451999SN/A                cpsr = readMiscRegNoEffect(MISCREG_CPSR);
4461999SN/A                if (scr.ns && (cpsr.mode != MODE_MON) && ELIs32(tc, EL3)) {
4471999SN/A                    NSACR nsacr = readMiscRegNoEffect(MISCREG_NSACR);
4481999SN/A                    // NB: Skipping the full loop, here
4491999SN/A                    if (!nsacr.cp10) cpacrMask.cp10 = 0;
4501999SN/A                    if (!nsacr.cp11) cpacrMask.cp11 = 0;
4511999SN/A                }
4522680Sktlim@umich.edu            }
4531999SN/A            MiscReg val = readMiscRegNoEffect(MISCREG_CPACR);
4542680Sktlim@umich.edu            val &= cpacrMask;
4551999SN/A            DPRINTF(MiscRegs, "Reading misc reg %s: %#x\n",
4561999SN/A                    miscRegName[misc_reg], val);
4571999SN/A            return val;
4581999SN/A        }
4591999SN/A      case MISCREG_MPIDR:
4602680Sktlim@umich.edu        cpsr = readMiscRegNoEffect(MISCREG_CPSR);
4611999SN/A        scr  = readMiscRegNoEffect(MISCREG_SCR);
4621999SN/A        if ((cpsr.mode == MODE_HYP) || inSecureState(scr, cpsr)) {
4631999SN/A            return getMPIDR(system, tc);
4641999SN/A        } else {
4651999SN/A            return readMiscReg(MISCREG_VMPIDR, tc);
4661999SN/A        }
4671999SN/A            break;
4681999SN/A      case MISCREG_MPIDR_EL1:
4692218SN/A        // @todo in the absence of v8 virtualization support just return MPIDR_EL1
4701999SN/A        return getMPIDR(system, tc) & 0xffffffff;
4711999SN/A      case MISCREG_VMPIDR:
4721999SN/A        // top bit defined as RES1
4731999SN/A        return readMiscRegNoEffect(misc_reg) | 0x80000000;
4741999SN/A      case MISCREG_ID_AFR0: // not implemented, so alias MIDR
475378SN/A      case MISCREG_REVIDR:  // not implemented, so alias MIDR
476360SN/A      case MISCREG_MIDR:
4771450SN/A        cpsr = readMiscRegNoEffect(MISCREG_CPSR);
478360SN/A        scr  = readMiscRegNoEffect(MISCREG_SCR);
4792680Sktlim@umich.edu        if ((cpsr.mode == MODE_HYP) || inSecureState(scr, cpsr)) {
480360SN/A            return readMiscRegNoEffect(misc_reg);
481360SN/A        } else {
482360SN/A            return readMiscRegNoEffect(MISCREG_VPIDR);
4832680Sktlim@umich.edu        }
4842400SN/A        break;
485360SN/A      case MISCREG_JOSCR: // Jazelle trivial implementation, RAZ/WI
486360SN/A      case MISCREG_JMCR:  // Jazelle trivial implementation, RAZ/WI
487360SN/A      case MISCREG_JIDR:  // Jazelle trivial implementation, RAZ/WI
488360SN/A      case MISCREG_AIDR:  // AUX ID set to 0
489360SN/A      case MISCREG_TCMTR: // No TCM's
4902218SN/A        return 0;
491360SN/A
4922680Sktlim@umich.edu      case MISCREG_CLIDR:
493360SN/A        warn_once("The clidr register always reports 0 caches.\n");
4941458SN/A        warn_once("clidr LoUIS field of 0b001 to match current "
495360SN/A                  "ARM implementations.\n");
496360SN/A        return 0x00200000;
497360SN/A      case MISCREG_CCSIDR:
4981999SN/A        warn_once("The ccsidr register isn't implemented and "
4991999SN/A                "always reads as 0.\n");
5001999SN/A        break;
5011999SN/A      case MISCREG_CTR:                 // AArch32, ARMv7, top bit set
5022680Sktlim@umich.edu      case MISCREG_CTR_EL0:             // AArch64
5031999SN/A        {
5042680Sktlim@umich.edu            //all caches have the same line size in gem5
5051999SN/A            //4 byte words in ARM
5061999SN/A            unsigned lineSizeWords =
5071999SN/A                tc->getSystemPtr()->cacheLineSize() / 4;
5081999SN/A            unsigned log2LineSizeWords = 0;
5091999SN/A
5102764Sstever@eecs.umich.edu            while (lineSizeWords >>= 1) {
5112064SN/A                ++log2LineSizeWords;
5122064SN/A            }
5132064SN/A
5142064SN/A            CTR ctr = 0;
5151999SN/A            //log2 of minimun i-cache line size (words)
5162064SN/A            ctr.iCacheLineSize = log2LineSizeWords;
5171999SN/A            //b11 - gem5 uses pipt
5181999SN/A            ctr.l1IndexPolicy = 0x3;
5192218SN/A            //log2 of minimum d-cache line size (words)
5201999SN/A            ctr.dCacheLineSize = log2LineSizeWords;
5212680Sktlim@umich.edu            //log2 of max reservation size (words)
5221999SN/A            ctr.erg = log2LineSizeWords;
5231999SN/A            //log2 of max writeback size (words)
5241999SN/A            ctr.cwg = log2LineSizeWords;
5251999SN/A            //b100 - gem5 format is ARMv7
5261999SN/A            ctr.format = 0x4;
527378SN/A
528360SN/A            return ctr;
5291450SN/A        }
530360SN/A      case MISCREG_ACTLR:
5312680Sktlim@umich.edu        warn("Not doing anything for miscreg ACTLR\n");
532360SN/A        break;
533360SN/A
534360SN/A      case MISCREG_PMXEVTYPER_PMCCFILTR:
5352680Sktlim@umich.edu      case MISCREG_PMINTENSET_EL1 ... MISCREG_PMOVSSET_EL0:
5362400SN/A      case MISCREG_PMEVCNTR0_EL0 ... MISCREG_PMEVTYPER5_EL0:
537360SN/A      case MISCREG_PMCR ... MISCREG_PMOVSSET:
538360SN/A        return pmu->readMiscReg(misc_reg);
539360SN/A
540360SN/A      case MISCREG_CPSR_Q:
541360SN/A        panic("shouldn't be reading this register seperately\n");
5421458SN/A      case MISCREG_FPSCR_QC:
543360SN/A        return readMiscRegNoEffect(MISCREG_FPSCR) & ~FpscrQcMask;
5442680Sktlim@umich.edu      case MISCREG_FPSCR_EXC:
545360SN/A        return readMiscRegNoEffect(MISCREG_FPSCR) & ~FpscrExcMask;
5461458SN/A      case MISCREG_FPSR:
547360SN/A        {
548360SN/A            const uint32_t ones = (uint32_t)(-1);
5491999SN/A            FPSCR fpscrMask = 0;
5501999SN/A            fpscrMask.ioc = ones;
5511999SN/A            fpscrMask.dzc = ones;
5521999SN/A            fpscrMask.ofc = ones;
5532680Sktlim@umich.edu            fpscrMask.ufc = ones;
5541999SN/A            fpscrMask.ixc = ones;
5551999SN/A            fpscrMask.idc = ones;
5561999SN/A            fpscrMask.qc = ones;
5572680Sktlim@umich.edu            fpscrMask.v = ones;
5582400SN/A            fpscrMask.c = ones;
5591999SN/A            fpscrMask.z = ones;
5602764Sstever@eecs.umich.edu            fpscrMask.n = ones;
5612064SN/A            return readMiscRegNoEffect(MISCREG_FPSCR) & (uint32_t)fpscrMask;
5622064SN/A        }
5632064SN/A      case MISCREG_FPCR:
5641999SN/A        {
5651999SN/A            const uint32_t ones = (uint32_t)(-1);
5662064SN/A            FPSCR fpscrMask  = 0;
5671999SN/A            fpscrMask.len    = ones;
5681999SN/A            fpscrMask.stride = ones;
5691999SN/A            fpscrMask.rMode  = ones;
5701999SN/A            fpscrMask.fz     = ones;
5712680Sktlim@umich.edu            fpscrMask.dn     = ones;
5721999SN/A            fpscrMask.ahp    = ones;
5731999SN/A            return readMiscRegNoEffect(MISCREG_FPSCR) & (uint32_t)fpscrMask;
5741999SN/A        }
5751999SN/A      case MISCREG_NZCV:
576378SN/A        {
577360SN/A            CPSR cpsr = 0;
5781450SN/A            cpsr.nz   = tc->readCCReg(CCREG_NZ);
579360SN/A            cpsr.c    = tc->readCCReg(CCREG_C);
5802680Sktlim@umich.edu            cpsr.v    = tc->readCCReg(CCREG_V);
581360SN/A            return cpsr;
5822680Sktlim@umich.edu        }
583360SN/A      case MISCREG_DAIF:
5841969SN/A        {
585360SN/A            CPSR cpsr = 0;
586360SN/A            cpsr.daif = (uint8_t) ((CPSR) miscRegs[MISCREG_CPSR]).daif;
5871458SN/A            return cpsr;
588360SN/A        }
589360SN/A      case MISCREG_SP_EL0:
590360SN/A        {
591360SN/A            return tc->readIntReg(INTREG_SP0);
592360SN/A        }
5931458SN/A      case MISCREG_SP_EL1:
594360SN/A        {
5952680Sktlim@umich.edu            return tc->readIntReg(INTREG_SP1);
5962021SN/A        }
5971458SN/A      case MISCREG_SP_EL2:
598360SN/A        {
599360SN/A            return tc->readIntReg(INTREG_SP2);
600360SN/A        }
6011706SN/A      case MISCREG_SPSEL:
6021706SN/A        {
6031706SN/A            return miscRegs[MISCREG_CPSR] & 0x1;
6041706SN/A        }
6052680Sktlim@umich.edu      case MISCREG_CURRENTEL:
6061706SN/A        {
6071706SN/A            return miscRegs[MISCREG_CPSR] & 0xc;
6081706SN/A        }
6092680Sktlim@umich.edu      case MISCREG_L2CTLR:
6102400SN/A        {
6111706SN/A            // mostly unimplemented, just set NumCPUs field from sim and return
6121706SN/A            L2CTLR l2ctlr = 0;
6131706SN/A            // b00:1CPU to b11:4CPUs
6141706SN/A            l2ctlr.numCPUs = tc->getSystemPtr()->numContexts() - 1;
6151706SN/A            return l2ctlr;
6162218SN/A        }
6171706SN/A      case MISCREG_DBGDIDR:
6182680Sktlim@umich.edu        /* For now just implement the version number.
6191706SN/A         * ARMv7, v7.1 Debug architecture (0b0101 --> 0x5)
6201706SN/A         */
6211706SN/A        return 0x5 << 16;
6221706SN/A      case MISCREG_DBGDSCRint:
6231706SN/A        return 0;
6241706SN/A      case MISCREG_ISR:
6251706SN/A        return tc->getCpuPtr()->getInterruptController(tc->threadId())->getISR(
6261706SN/A            readMiscRegNoEffect(MISCREG_HCR),
6271706SN/A            readMiscRegNoEffect(MISCREG_CPSR),
6282680Sktlim@umich.edu            readMiscRegNoEffect(MISCREG_SCR));
6291706SN/A      case MISCREG_ISR_EL1:
6302680Sktlim@umich.edu        return tc->getCpuPtr()->getInterruptController(tc->threadId())->getISR(
6311706SN/A            readMiscRegNoEffect(MISCREG_HCR_EL2),
6321706SN/A            readMiscRegNoEffect(MISCREG_CPSR),
6331706SN/A            readMiscRegNoEffect(MISCREG_SCR_EL3));
6341706SN/A      case MISCREG_DCZID_EL0:
6351706SN/A        return 0x04;  // DC ZVA clear 64-byte chunks
6361706SN/A      case MISCREG_HCPTR:
6371706SN/A        {
6381706SN/A            MiscReg val = readMiscRegNoEffect(misc_reg);
6392218SN/A            // The trap bit associated with CP14 is defined as RAZ
6401706SN/A            val &= ~(1 << 14);
6412680Sktlim@umich.edu            // If a CP bit in NSACR is 0 then the corresponding bit in
6421706SN/A            // HCPTR is RAO/WI
6431706SN/A            bool secure_lookup = haveSecurity &&
6441706SN/A                inSecureState(readMiscRegNoEffect(MISCREG_SCR),
6451706SN/A                              readMiscRegNoEffect(MISCREG_CPSR));
6461706SN/A            if (!secure_lookup) {
6471999SN/A                MiscReg mask = readMiscRegNoEffect(MISCREG_NSACR);
6481999SN/A                val |= (mask ^ 0x7FFF) & 0xBFFF;
6491999SN/A            }
6501999SN/A            // Set the bits for unimplemented coprocessors to RAO/WI
6512680Sktlim@umich.edu            val |= 0x33FF;
6521999SN/A            return (val);
6532680Sktlim@umich.edu        }
6541999SN/A      case MISCREG_HDFAR: // alias for secure DFAR
6551999SN/A        return readMiscRegNoEffect(MISCREG_DFAR_S);
6561999SN/A      case MISCREG_HIFAR: // alias for secure IFAR
6571999SN/A        return readMiscRegNoEffect(MISCREG_IFAR_S);
6581999SN/A      case MISCREG_HVBAR: // bottom bits reserved
6592680Sktlim@umich.edu        return readMiscRegNoEffect(MISCREG_HVBAR) & 0xFFFFFFE0;
6602680Sktlim@umich.edu      case MISCREG_SCTLR:
6612680Sktlim@umich.edu        return (readMiscRegNoEffect(misc_reg) & 0x72DD39FF) | 0x00C00818;
6621999SN/A      case MISCREG_SCTLR_EL1:
6631999SN/A        return (readMiscRegNoEffect(misc_reg) & 0x37DDDBBF) | 0x30D00800;
6641999SN/A      case MISCREG_SCTLR_EL2:
6651999SN/A      case MISCREG_SCTLR_EL3:
6662461SN/A      case MISCREG_HSCTLR:
6672461SN/A        return (readMiscRegNoEffect(misc_reg) & 0x32CD183F) | 0x30C50830;
6682461SN/A
6692091SN/A      case MISCREG_ID_PFR0:
6701999SN/A        // !ThumbEE | !Jazelle | Thumb | ARM
6712461SN/A        return 0x00000031;
6722461SN/A      case MISCREG_ID_PFR1:
6731999SN/A        {   // Timer | Virti | !M Profile | TrustZone | ARMv4
6741999SN/A            bool haveTimer = (system->getGenericTimer() != NULL);
6751999SN/A            return 0x00000001
6761999SN/A                 | (haveSecurity       ? 0x00000010 : 0x0)
6771999SN/A                 | (haveVirtualization ? 0x00001000 : 0x0)
6781999SN/A                 | (haveTimer          ? 0x00010000 : 0x0);
6791999SN/A        }
6801999SN/A      case MISCREG_ID_AA64PFR0_EL1:
6811999SN/A        return 0x0000000000000002   // AArch{64,32} supported at EL0
6821999SN/A             | 0x0000000000000020                             // EL1
6832218SN/A             | (haveVirtualization ? 0x0000000000000200 : 0)  // EL2
6841999SN/A             | (haveSecurity       ? 0x0000000000002000 : 0); // EL3
6851999SN/A      case MISCREG_ID_AA64PFR1_EL1:
6861999SN/A        return 0; // bits [63:0] RES0 (reserved for future use)
6871999SN/A
6881999SN/A      // Generic Timer registers
689378SN/A      case MISCREG_CNTHV_CTL_EL2:
690378SN/A      case MISCREG_CNTHV_CVAL_EL2:
691378SN/A      case MISCREG_CNTHV_TVAL_EL2:
692378SN/A      case MISCREG_CNTFRQ ... MISCREG_CNTHP_CTL:
693378SN/A      case MISCREG_CNTPCT ... MISCREG_CNTHP_CVAL:
694378SN/A      case MISCREG_CNTKCTL_EL1 ... MISCREG_CNTV_CVAL_EL0:
695378SN/A      case MISCREG_CNTVOFF_EL2 ... MISCREG_CNTPS_CVAL_EL1:
696378SN/A        return getGenericTimer(tc).readMiscReg(misc_reg);
697360SN/A
698378SN/A      default:
699378SN/A        break;
700378SN/A
701360SN/A    }
7021450SN/A    return readMiscRegNoEffect(misc_reg);
7032680Sktlim@umich.edu}
704360SN/A
7052680Sktlim@umich.eduvoid
7062680Sktlim@umich.eduISA::setMiscRegNoEffect(int misc_reg, const MiscReg &val)
7072680Sktlim@umich.edu{
7082680Sktlim@umich.edu    assert(misc_reg < NumMiscRegs);
7092680Sktlim@umich.edu
7102680Sktlim@umich.edu    const auto &reg = lookUpMiscReg[misc_reg]; // bit masks
711360SN/A    const auto &map = getMiscIndices(misc_reg);
7122544SN/A    int lower = map.first, upper = map.second;
7132544SN/A
7142544SN/A    auto v = (val & ~reg.wi()) | reg.rao();
7152544SN/A    if (upper > 0) {
7162544SN/A        miscRegs[lower] = bits(v, 31, 0);
7172544SN/A        miscRegs[upper] = bits(v, 63, 32);
718360SN/A        DPRINTF(MiscRegs, "Writing to misc reg %d (%d:%d) : %#x\n",
719360SN/A                misc_reg, lower, upper, v);
7202544SN/A    } else {
7212544SN/A        miscRegs[lower] = v;
7222544SN/A        DPRINTF(MiscRegs, "Writing to misc reg %d (%d) : %#x\n",
7232544SN/A                misc_reg, lower, v);
7242544SN/A    }
7252544SN/A}
7262544SN/A
7272544SN/Avoid
7282544SN/AISA::setMiscReg(int misc_reg, const MiscReg &val, ThreadContext *tc)
7292544SN/A{
7302553SN/A
7311969SN/A    MiscReg newVal = val;
7322680Sktlim@umich.edu    bool secure_lookup;
733360SN/A    SCR scr;
734360SN/A
7351458SN/A    if (misc_reg == MISCREG_CPSR) {
736360SN/A        updateRegMap(val);
737360SN/A
738378SN/A
739360SN/A        CPSR old_cpsr = miscRegs[MISCREG_CPSR];
7401450SN/A        int old_mode = old_cpsr.mode;
741360SN/A        CPSR cpsr = val;
7422680Sktlim@umich.edu        if (old_mode != cpsr.mode || cpsr.il != old_cpsr.il) {
743360SN/A            getITBPtr(tc)->invalidateMiscReg();
7442680Sktlim@umich.edu            getDTBPtr(tc)->invalidateMiscReg();
7452680Sktlim@umich.edu        }
746360SN/A
747360SN/A        DPRINTF(Arm, "Updating CPSR from %#x to %#x f:%d i:%d a:%d mode:%#x\n",
7482064SN/A                miscRegs[misc_reg], cpsr, cpsr.f, cpsr.i, cpsr.a, cpsr.mode);
7492064SN/A        PCState pc = tc->pcState();
7502064SN/A        pc.nextThumb(cpsr.t);
7512091SN/A        pc.nextJazelle(cpsr.j);
7522091SN/A        pc.illegalExec(cpsr.il == 1);
7532064SN/A
754360SN/A        // Follow slightly different semantics if a CheckerCPU object
7552064SN/A        // is connected
7562064SN/A        CheckerCPU *checker = tc->getCheckerCpuPtr();
7572064SN/A        if (checker) {
7582064SN/A            tc->pcStateNoRecord(pc);
7592064SN/A        } else {
760360SN/A            tc->pcState(pc);
761360SN/A        }
7622680Sktlim@umich.edu    } else {
7631458SN/A#ifndef NDEBUG
764360SN/A        if (!miscRegInfo[misc_reg][MISCREG_IMPLEMENTED]) {
765360SN/A            if (miscRegInfo[misc_reg][MISCREG_WARN_NOT_FAIL])
766378SN/A                warn("Unimplemented system register %s write with %#x.\n",
767360SN/A                    miscRegName[misc_reg], val);
7681450SN/A            else
769360SN/A                panic("Unimplemented system register %s write with %#x.\n",
7702680Sktlim@umich.edu                    miscRegName[misc_reg], val);
771360SN/A        }
7722680Sktlim@umich.edu#endif
773360SN/A        switch (unflattenMiscReg(misc_reg)) {
774360SN/A          case MISCREG_CPACR:
775360SN/A            {
7762091SN/A
7772091SN/A                const uint32_t ones = (uint32_t)(-1);
778360SN/A                CPACR cpacrMask = 0;
7792680Sktlim@umich.edu                // Only cp10, cp11, and ase are implemented, nothing else should
780360SN/A                // be writable
7811458SN/A                cpacrMask.cp10 = ones;
782360SN/A                cpacrMask.cp11 = ones;
783360SN/A                cpacrMask.asedis = ones;
784360SN/A
7851999SN/A                // Security Extensions may limit the writability of CPACR
7861999SN/A                if (haveSecurity) {
7871999SN/A                    scr = readMiscRegNoEffect(MISCREG_SCR);
7881999SN/A                    CPSR cpsr = readMiscRegNoEffect(MISCREG_CPSR);
7892680Sktlim@umich.edu                    if (scr.ns && (cpsr.mode != MODE_MON) && ELIs32(tc, EL3)) {
7901999SN/A                        NSACR nsacr = readMiscRegNoEffect(MISCREG_NSACR);
7911999SN/A                        // NB: Skipping the full loop, here
7921999SN/A                        if (!nsacr.cp10) cpacrMask.cp10 = 0;
7932680Sktlim@umich.edu                        if (!nsacr.cp11) cpacrMask.cp11 = 0;
7942400SN/A                    }
7951999SN/A                }
7962680Sktlim@umich.edu
7972680Sktlim@umich.edu                MiscReg old_val = readMiscRegNoEffect(MISCREG_CPACR);
7981999SN/A                newVal &= cpacrMask;
7991999SN/A                newVal |= old_val & ~cpacrMask;
8001999SN/A                DPRINTF(MiscRegs, "Writing misc reg %s: %#x\n",
8011999SN/A                        miscRegName[misc_reg], newVal);
8022091SN/A            }
8032091SN/A            break;
8041999SN/A          case MISCREG_CPTR_EL2:
8051999SN/A            {
8061999SN/A                const uint32_t ones = (uint32_t)(-1);
8071999SN/A                CPTR cptrMask = 0;
8081999SN/A                cptrMask.tcpac = ones;
8091999SN/A                cptrMask.tta = ones;
8101999SN/A                cptrMask.tfp = ones;
8111999SN/A                newVal &= cptrMask;
812378SN/A                cptrMask = 0;
813360SN/A                cptrMask.res1_13_12_el2 = ones;
8141450SN/A                cptrMask.res1_9_0_el2 = ones;
815360SN/A                newVal |= cptrMask;
8162680Sktlim@umich.edu                DPRINTF(MiscRegs, "Writing misc reg %s: %#x\n",
817360SN/A                        miscRegName[misc_reg], newVal);
8182680Sktlim@umich.edu            }
8192680Sktlim@umich.edu            break;
820360SN/A          case MISCREG_CPTR_EL3:
8212553SN/A            {
822360SN/A                const uint32_t ones = (uint32_t)(-1);
823360SN/A                CPTR cptrMask = 0;
8241969SN/A                cptrMask.tcpac = ones;
8251969SN/A                cptrMask.tta = ones;
826360SN/A                cptrMask.tfp = ones;
827360SN/A                newVal &= cptrMask;
828360SN/A                DPRINTF(MiscRegs, "Writing misc reg %s: %#x\n",
8292091SN/A                        miscRegName[misc_reg], newVal);
8302091SN/A            }
8312091SN/A            break;
832360SN/A          case MISCREG_CSSELR:
833360SN/A            warn_once("The csselr register isn't implemented.\n");
834360SN/A            return;
835360SN/A
836360SN/A          case MISCREG_DC_ZVA_Xt:
837360SN/A            warn("Calling DC ZVA! Not Implemeted! Expect WEIRD results\n");
838360SN/A            return;
839360SN/A
840360SN/A          case MISCREG_FPSCR:
841360SN/A            {
842360SN/A                const uint32_t ones = (uint32_t)(-1);
843360SN/A                FPSCR fpscrMask = 0;
844360SN/A                fpscrMask.ioc = ones;
845360SN/A                fpscrMask.dzc = ones;
846360SN/A                fpscrMask.ofc = ones;
847360SN/A                fpscrMask.ufc = ones;
848360SN/A                fpscrMask.ixc = ones;
8492680Sktlim@umich.edu                fpscrMask.idc = ones;
850360SN/A                fpscrMask.ioe = ones;
8511458SN/A                fpscrMask.dze = ones;
852360SN/A                fpscrMask.ofe = ones;
853360SN/A                fpscrMask.ufe = ones;
8542553SN/A                fpscrMask.ixe = ones;
8552553SN/A                fpscrMask.ide = ones;
8562553SN/A                fpscrMask.len = ones;
8571354SN/A                fpscrMask.stride = ones;
858                fpscrMask.rMode = ones;
859                fpscrMask.fz = ones;
860                fpscrMask.dn = ones;
861                fpscrMask.ahp = ones;
862                fpscrMask.qc = ones;
863                fpscrMask.v = ones;
864                fpscrMask.c = ones;
865                fpscrMask.z = ones;
866                fpscrMask.n = ones;
867                newVal = (newVal & (uint32_t)fpscrMask) |
868                         (readMiscRegNoEffect(MISCREG_FPSCR) &
869                          ~(uint32_t)fpscrMask);
870                tc->getDecoderPtr()->setContext(newVal);
871            }
872            break;
873          case MISCREG_FPSR:
874            {
875                const uint32_t ones = (uint32_t)(-1);
876                FPSCR fpscrMask = 0;
877                fpscrMask.ioc = ones;
878                fpscrMask.dzc = ones;
879                fpscrMask.ofc = ones;
880                fpscrMask.ufc = ones;
881                fpscrMask.ixc = ones;
882                fpscrMask.idc = ones;
883                fpscrMask.qc = ones;
884                fpscrMask.v = ones;
885                fpscrMask.c = ones;
886                fpscrMask.z = ones;
887                fpscrMask.n = ones;
888                newVal = (newVal & (uint32_t)fpscrMask) |
889                         (readMiscRegNoEffect(MISCREG_FPSCR) &
890                          ~(uint32_t)fpscrMask);
891                misc_reg = MISCREG_FPSCR;
892            }
893            break;
894          case MISCREG_FPCR:
895            {
896                const uint32_t ones = (uint32_t)(-1);
897                FPSCR fpscrMask  = 0;
898                fpscrMask.len    = ones;
899                fpscrMask.stride = ones;
900                fpscrMask.rMode  = ones;
901                fpscrMask.fz     = ones;
902                fpscrMask.dn     = ones;
903                fpscrMask.ahp    = ones;
904                newVal = (newVal & (uint32_t)fpscrMask) |
905                         (readMiscRegNoEffect(MISCREG_FPSCR) &
906                          ~(uint32_t)fpscrMask);
907                misc_reg = MISCREG_FPSCR;
908            }
909            break;
910          case MISCREG_CPSR_Q:
911            {
912                assert(!(newVal & ~CpsrMaskQ));
913                newVal = readMiscRegNoEffect(MISCREG_CPSR) | newVal;
914                misc_reg = MISCREG_CPSR;
915            }
916            break;
917          case MISCREG_FPSCR_QC:
918            {
919                newVal = readMiscRegNoEffect(MISCREG_FPSCR) |
920                         (newVal & FpscrQcMask);
921                misc_reg = MISCREG_FPSCR;
922            }
923            break;
924          case MISCREG_FPSCR_EXC:
925            {
926                newVal = readMiscRegNoEffect(MISCREG_FPSCR) |
927                         (newVal & FpscrExcMask);
928                misc_reg = MISCREG_FPSCR;
929            }
930            break;
931          case MISCREG_FPEXC:
932            {
933                // vfpv3 architecture, section B.6.1 of DDI04068
934                // bit 29 - valid only if fpexc[31] is 0
935                const uint32_t fpexcMask = 0x60000000;
936                newVal = (newVal & fpexcMask) |
937                         (readMiscRegNoEffect(MISCREG_FPEXC) & ~fpexcMask);
938            }
939            break;
940          case MISCREG_HCR:
941            {
942                if (!haveVirtualization)
943                    return;
944            }
945            break;
946          case MISCREG_IFSR:
947            {
948                // ARM ARM (ARM DDI 0406C.b) B4.1.96
949                const uint32_t ifsrMask =
950                    mask(31, 13) | mask(11, 11) | mask(8, 6);
951                newVal = newVal & ~ifsrMask;
952            }
953            break;
954          case MISCREG_DFSR:
955            {
956                // ARM ARM (ARM DDI 0406C.b) B4.1.52
957                const uint32_t dfsrMask = mask(31, 14) | mask(8, 8);
958                newVal = newVal & ~dfsrMask;
959            }
960            break;
961          case MISCREG_AMAIR0:
962          case MISCREG_AMAIR1:
963            {
964                // ARM ARM (ARM DDI 0406C.b) B4.1.5
965                // Valid only with LPAE
966                if (!haveLPAE)
967                    return;
968                DPRINTF(MiscRegs, "Writing AMAIR: %#x\n", newVal);
969            }
970            break;
971          case MISCREG_SCR:
972            getITBPtr(tc)->invalidateMiscReg();
973            getDTBPtr(tc)->invalidateMiscReg();
974            break;
975          case MISCREG_SCTLR:
976            {
977                DPRINTF(MiscRegs, "Writing SCTLR: %#x\n", newVal);
978                scr = readMiscRegNoEffect(MISCREG_SCR);
979
980                MiscRegIndex sctlr_idx;
981                if (haveSecurity && !highestELIs64 && !scr.ns) {
982                    sctlr_idx = MISCREG_SCTLR_S;
983                } else {
984                    sctlr_idx =  MISCREG_SCTLR_NS;
985                }
986
987                SCTLR sctlr = miscRegs[sctlr_idx];
988                SCTLR new_sctlr = newVal;
989                new_sctlr.nmfi =  ((bool)sctlr.nmfi) && !haveVirtualization;
990                miscRegs[sctlr_idx] = (MiscReg)new_sctlr;
991                getITBPtr(tc)->invalidateMiscReg();
992                getDTBPtr(tc)->invalidateMiscReg();
993            }
994          case MISCREG_MIDR:
995          case MISCREG_ID_PFR0:
996          case MISCREG_ID_PFR1:
997          case MISCREG_ID_DFR0:
998          case MISCREG_ID_MMFR0:
999          case MISCREG_ID_MMFR1:
1000          case MISCREG_ID_MMFR2:
1001          case MISCREG_ID_MMFR3:
1002          case MISCREG_ID_ISAR0:
1003          case MISCREG_ID_ISAR1:
1004          case MISCREG_ID_ISAR2:
1005          case MISCREG_ID_ISAR3:
1006          case MISCREG_ID_ISAR4:
1007          case MISCREG_ID_ISAR5:
1008
1009          case MISCREG_MPIDR:
1010          case MISCREG_FPSID:
1011          case MISCREG_TLBTR:
1012          case MISCREG_MVFR0:
1013          case MISCREG_MVFR1:
1014
1015          case MISCREG_ID_AA64AFR0_EL1:
1016          case MISCREG_ID_AA64AFR1_EL1:
1017          case MISCREG_ID_AA64DFR0_EL1:
1018          case MISCREG_ID_AA64DFR1_EL1:
1019          case MISCREG_ID_AA64ISAR0_EL1:
1020          case MISCREG_ID_AA64ISAR1_EL1:
1021          case MISCREG_ID_AA64MMFR0_EL1:
1022          case MISCREG_ID_AA64MMFR1_EL1:
1023          case MISCREG_ID_AA64MMFR2_EL1:
1024          case MISCREG_ID_AA64PFR0_EL1:
1025          case MISCREG_ID_AA64PFR1_EL1:
1026            // ID registers are constants.
1027            return;
1028
1029          // TLB Invalidate All
1030          case MISCREG_TLBIALL: // TLBI all entries, EL0&1,
1031            {
1032                assert32(tc);
1033                scr = readMiscReg(MISCREG_SCR, tc);
1034
1035                TLBIALL tlbiOp(EL1, haveSecurity && !scr.ns);
1036                tlbiOp(tc);
1037                return;
1038            }
1039          // TLB Invalidate All, Inner Shareable
1040          case MISCREG_TLBIALLIS:
1041            {
1042                assert32(tc);
1043                scr = readMiscReg(MISCREG_SCR, tc);
1044
1045                TLBIALL tlbiOp(EL1, haveSecurity && !scr.ns);
1046                tlbiOp.broadcast(tc);
1047                return;
1048            }
1049          // Instruction TLB Invalidate All
1050          case MISCREG_ITLBIALL:
1051            {
1052                assert32(tc);
1053                scr = readMiscReg(MISCREG_SCR, tc);
1054
1055                ITLBIALL tlbiOp(EL1, haveSecurity && !scr.ns);
1056                tlbiOp(tc);
1057                return;
1058            }
1059          // Data TLB Invalidate All
1060          case MISCREG_DTLBIALL:
1061            {
1062                assert32(tc);
1063                scr = readMiscReg(MISCREG_SCR, tc);
1064
1065                DTLBIALL tlbiOp(EL1, haveSecurity && !scr.ns);
1066                tlbiOp(tc);
1067                return;
1068            }
1069          // TLB Invalidate by VA
1070          // mcr tlbimval(is) is invalidating all matching entries
1071          // regardless of the level of lookup, since in gem5 we cache
1072          // in the tlb the last level of lookup only.
1073          case MISCREG_TLBIMVA:
1074          case MISCREG_TLBIMVAL:
1075            {
1076                assert32(tc);
1077                scr = readMiscReg(MISCREG_SCR, tc);
1078
1079                TLBIMVA tlbiOp(EL1,
1080                               haveSecurity && !scr.ns,
1081                               mbits(newVal, 31, 12),
1082                               bits(newVal, 7,0));
1083
1084                tlbiOp(tc);
1085                return;
1086            }
1087          // TLB Invalidate by VA, Inner Shareable
1088          case MISCREG_TLBIMVAIS:
1089          case MISCREG_TLBIMVALIS:
1090            {
1091                assert32(tc);
1092                scr = readMiscReg(MISCREG_SCR, tc);
1093
1094                TLBIMVA tlbiOp(EL1,
1095                               haveSecurity && !scr.ns,
1096                               mbits(newVal, 31, 12),
1097                               bits(newVal, 7,0));
1098
1099                tlbiOp.broadcast(tc);
1100                return;
1101            }
1102          // TLB Invalidate by ASID match
1103          case MISCREG_TLBIASID:
1104            {
1105                assert32(tc);
1106                scr = readMiscReg(MISCREG_SCR, tc);
1107
1108                TLBIASID tlbiOp(EL1,
1109                                haveSecurity && !scr.ns,
1110                                bits(newVal, 7,0));
1111
1112                tlbiOp(tc);
1113                return;
1114            }
1115          // TLB Invalidate by ASID match, Inner Shareable
1116          case MISCREG_TLBIASIDIS:
1117            {
1118                assert32(tc);
1119                scr = readMiscReg(MISCREG_SCR, tc);
1120
1121                TLBIASID tlbiOp(EL1,
1122                                haveSecurity && !scr.ns,
1123                                bits(newVal, 7,0));
1124
1125                tlbiOp.broadcast(tc);
1126                return;
1127            }
1128          // mcr tlbimvaal(is) is invalidating all matching entries
1129          // regardless of the level of lookup, since in gem5 we cache
1130          // in the tlb the last level of lookup only.
1131          // TLB Invalidate by VA, All ASID
1132          case MISCREG_TLBIMVAA:
1133          case MISCREG_TLBIMVAAL:
1134            {
1135                assert32(tc);
1136                scr = readMiscReg(MISCREG_SCR, tc);
1137
1138                TLBIMVAA tlbiOp(EL1, haveSecurity && !scr.ns,
1139                                mbits(newVal, 31,12), false);
1140
1141                tlbiOp(tc);
1142                return;
1143            }
1144          // TLB Invalidate by VA, All ASID, Inner Shareable
1145          case MISCREG_TLBIMVAAIS:
1146          case MISCREG_TLBIMVAALIS:
1147            {
1148                assert32(tc);
1149                scr = readMiscReg(MISCREG_SCR, tc);
1150
1151                TLBIMVAA tlbiOp(EL1, haveSecurity && !scr.ns,
1152                                mbits(newVal, 31,12), false);
1153
1154                tlbiOp.broadcast(tc);
1155                return;
1156            }
1157          // mcr tlbimvalh(is) is invalidating all matching entries
1158          // regardless of the level of lookup, since in gem5 we cache
1159          // in the tlb the last level of lookup only.
1160          // TLB Invalidate by VA, Hyp mode
1161          case MISCREG_TLBIMVAH:
1162          case MISCREG_TLBIMVALH:
1163            {
1164                assert32(tc);
1165                scr = readMiscReg(MISCREG_SCR, tc);
1166
1167                TLBIMVAA tlbiOp(EL1, haveSecurity && !scr.ns,
1168                                mbits(newVal, 31,12), true);
1169
1170                tlbiOp(tc);
1171                return;
1172            }
1173          // TLB Invalidate by VA, Hyp mode, Inner Shareable
1174          case MISCREG_TLBIMVAHIS:
1175          case MISCREG_TLBIMVALHIS:
1176            {
1177                assert32(tc);
1178                scr = readMiscReg(MISCREG_SCR, tc);
1179
1180                TLBIMVAA tlbiOp(EL1, haveSecurity && !scr.ns,
1181                                mbits(newVal, 31,12), true);
1182
1183                tlbiOp.broadcast(tc);
1184                return;
1185            }
1186          // mcr tlbiipas2l(is) is invalidating all matching entries
1187          // regardless of the level of lookup, since in gem5 we cache
1188          // in the tlb the last level of lookup only.
1189          // TLB Invalidate by Intermediate Physical Address, Stage 2
1190          case MISCREG_TLBIIPAS2:
1191          case MISCREG_TLBIIPAS2L:
1192            {
1193                assert32(tc);
1194                scr = readMiscReg(MISCREG_SCR, tc);
1195
1196                TLBIIPA tlbiOp(EL1,
1197                               haveSecurity && !scr.ns,
1198                               static_cast<Addr>(bits(newVal, 35, 0)) << 12);
1199
1200                tlbiOp(tc);
1201                return;
1202            }
1203          // TLB Invalidate by Intermediate Physical Address, Stage 2,
1204          // Inner Shareable
1205          case MISCREG_TLBIIPAS2IS:
1206          case MISCREG_TLBIIPAS2LIS:
1207            {
1208                assert32(tc);
1209                scr = readMiscReg(MISCREG_SCR, tc);
1210
1211                TLBIIPA tlbiOp(EL1,
1212                               haveSecurity && !scr.ns,
1213                               static_cast<Addr>(bits(newVal, 35, 0)) << 12);
1214
1215                tlbiOp.broadcast(tc);
1216                return;
1217            }
1218          // Instruction TLB Invalidate by VA
1219          case MISCREG_ITLBIMVA:
1220            {
1221                assert32(tc);
1222                scr = readMiscReg(MISCREG_SCR, tc);
1223
1224                ITLBIMVA tlbiOp(EL1,
1225                                haveSecurity && !scr.ns,
1226                                mbits(newVal, 31, 12),
1227                                bits(newVal, 7,0));
1228
1229                tlbiOp(tc);
1230                return;
1231            }
1232          // Data TLB Invalidate by VA
1233          case MISCREG_DTLBIMVA:
1234            {
1235                assert32(tc);
1236                scr = readMiscReg(MISCREG_SCR, tc);
1237
1238                DTLBIMVA tlbiOp(EL1,
1239                                haveSecurity && !scr.ns,
1240                                mbits(newVal, 31, 12),
1241                                bits(newVal, 7,0));
1242
1243                tlbiOp(tc);
1244                return;
1245            }
1246          // Instruction TLB Invalidate by ASID match
1247          case MISCREG_ITLBIASID:
1248            {
1249                assert32(tc);
1250                scr = readMiscReg(MISCREG_SCR, tc);
1251
1252                ITLBIASID tlbiOp(EL1,
1253                                 haveSecurity && !scr.ns,
1254                                 bits(newVal, 7,0));
1255
1256                tlbiOp(tc);
1257                return;
1258            }
1259          // Data TLB Invalidate by ASID match
1260          case MISCREG_DTLBIASID:
1261            {
1262                assert32(tc);
1263                scr = readMiscReg(MISCREG_SCR, tc);
1264
1265                DTLBIASID tlbiOp(EL1,
1266                                 haveSecurity && !scr.ns,
1267                                 bits(newVal, 7,0));
1268
1269                tlbiOp(tc);
1270                return;
1271            }
1272          // TLB Invalidate All, Non-Secure Non-Hyp
1273          case MISCREG_TLBIALLNSNH:
1274            {
1275                assert32(tc);
1276
1277                TLBIALLN tlbiOp(EL1, false);
1278                tlbiOp(tc);
1279                return;
1280            }
1281          // TLB Invalidate All, Non-Secure Non-Hyp, Inner Shareable
1282          case MISCREG_TLBIALLNSNHIS:
1283            {
1284                assert32(tc);
1285
1286                TLBIALLN tlbiOp(EL1, false);
1287                tlbiOp.broadcast(tc);
1288                return;
1289            }
1290          // TLB Invalidate All, Hyp mode
1291          case MISCREG_TLBIALLH:
1292            {
1293                assert32(tc);
1294
1295                TLBIALLN tlbiOp(EL1, true);
1296                tlbiOp(tc);
1297                return;
1298            }
1299          // TLB Invalidate All, Hyp mode, Inner Shareable
1300          case MISCREG_TLBIALLHIS:
1301            {
1302                assert32(tc);
1303
1304                TLBIALLN tlbiOp(EL1, true);
1305                tlbiOp.broadcast(tc);
1306                return;
1307            }
1308          // AArch64 TLB Invalidate All, EL3
1309          case MISCREG_TLBI_ALLE3:
1310            {
1311                assert64(tc);
1312
1313                TLBIALL tlbiOp(EL3, true);
1314                tlbiOp(tc);
1315                return;
1316            }
1317          // AArch64 TLB Invalidate All, EL3, Inner Shareable
1318          case MISCREG_TLBI_ALLE3IS:
1319            {
1320                assert64(tc);
1321
1322                TLBIALL tlbiOp(EL3, true);
1323                tlbiOp.broadcast(tc);
1324                return;
1325            }
1326          // @todo: uncomment this to enable Virtualization
1327          // case MISCREG_TLBI_ALLE2IS:
1328          // case MISCREG_TLBI_ALLE2:
1329          // AArch64 TLB Invalidate All, EL1
1330          case MISCREG_TLBI_ALLE1:
1331          case MISCREG_TLBI_VMALLE1:
1332          case MISCREG_TLBI_VMALLS12E1:
1333            // @todo: handle VMID and stage 2 to enable Virtualization
1334            {
1335                assert64(tc);
1336                scr = readMiscReg(MISCREG_SCR, tc);
1337
1338                TLBIALL tlbiOp(EL1, haveSecurity && !scr.ns);
1339                tlbiOp(tc);
1340                return;
1341            }
1342          // AArch64 TLB Invalidate All, EL1, Inner Shareable
1343          case MISCREG_TLBI_ALLE1IS:
1344          case MISCREG_TLBI_VMALLE1IS:
1345          case MISCREG_TLBI_VMALLS12E1IS:
1346            // @todo: handle VMID and stage 2 to enable Virtualization
1347            {
1348                assert64(tc);
1349                scr = readMiscReg(MISCREG_SCR, tc);
1350
1351                TLBIALL tlbiOp(EL1, haveSecurity && !scr.ns);
1352                tlbiOp.broadcast(tc);
1353                return;
1354            }
1355          // VAEx(IS) and VALEx(IS) are the same because TLBs
1356          // only store entries
1357          // from the last level of translation table walks
1358          // @todo: handle VMID to enable Virtualization
1359          // AArch64 TLB Invalidate by VA, EL3
1360          case MISCREG_TLBI_VAE3_Xt:
1361          case MISCREG_TLBI_VALE3_Xt:
1362            {
1363                assert64(tc);
1364
1365                TLBIMVA tlbiOp(EL3, true,
1366                               static_cast<Addr>(bits(newVal, 43, 0)) << 12,
1367                               0xbeef);
1368                tlbiOp(tc);
1369                return;
1370            }
1371          // AArch64 TLB Invalidate by VA, EL3, Inner Shareable
1372          case MISCREG_TLBI_VAE3IS_Xt:
1373          case MISCREG_TLBI_VALE3IS_Xt:
1374            {
1375                assert64(tc);
1376
1377                TLBIMVA tlbiOp(EL3, true,
1378                               static_cast<Addr>(bits(newVal, 43, 0)) << 12,
1379                               0xbeef);
1380
1381                tlbiOp.broadcast(tc);
1382                return;
1383            }
1384          // AArch64 TLB Invalidate by VA, EL2
1385          case MISCREG_TLBI_VAE2_Xt:
1386          case MISCREG_TLBI_VALE2_Xt:
1387            {
1388                assert64(tc);
1389                scr = readMiscReg(MISCREG_SCR, tc);
1390
1391                TLBIMVA tlbiOp(EL2, haveSecurity && !scr.ns,
1392                               static_cast<Addr>(bits(newVal, 43, 0)) << 12,
1393                               0xbeef);
1394                tlbiOp(tc);
1395                return;
1396            }
1397          // AArch64 TLB Invalidate by VA, EL2, Inner Shareable
1398          case MISCREG_TLBI_VAE2IS_Xt:
1399          case MISCREG_TLBI_VALE2IS_Xt:
1400            {
1401                assert64(tc);
1402                scr = readMiscReg(MISCREG_SCR, tc);
1403
1404                TLBIMVA tlbiOp(EL2, haveSecurity && !scr.ns,
1405                               static_cast<Addr>(bits(newVal, 43, 0)) << 12,
1406                               0xbeef);
1407
1408                tlbiOp.broadcast(tc);
1409                return;
1410            }
1411          // AArch64 TLB Invalidate by VA, EL1
1412          case MISCREG_TLBI_VAE1_Xt:
1413          case MISCREG_TLBI_VALE1_Xt:
1414            {
1415                assert64(tc);
1416                scr = readMiscReg(MISCREG_SCR, tc);
1417                auto asid = haveLargeAsid64 ? bits(newVal, 63, 48) :
1418                                              bits(newVal, 55, 48);
1419
1420                TLBIMVA tlbiOp(EL1, haveSecurity && !scr.ns,
1421                               static_cast<Addr>(bits(newVal, 43, 0)) << 12,
1422                               asid);
1423
1424                tlbiOp(tc);
1425                return;
1426            }
1427          // AArch64 TLB Invalidate by VA, EL1, Inner Shareable
1428          case MISCREG_TLBI_VAE1IS_Xt:
1429          case MISCREG_TLBI_VALE1IS_Xt:
1430            {
1431                assert64(tc);
1432                scr = readMiscReg(MISCREG_SCR, tc);
1433                auto asid = haveLargeAsid64 ? bits(newVal, 63, 48) :
1434                                              bits(newVal, 55, 48);
1435
1436                TLBIMVA tlbiOp(EL1, haveSecurity && !scr.ns,
1437                               static_cast<Addr>(bits(newVal, 43, 0)) << 12,
1438                               asid);
1439
1440                tlbiOp.broadcast(tc);
1441                return;
1442            }
1443          // AArch64 TLB Invalidate by ASID, EL1
1444          // @todo: handle VMID to enable Virtualization
1445          case MISCREG_TLBI_ASIDE1_Xt:
1446            {
1447                assert64(tc);
1448                scr = readMiscReg(MISCREG_SCR, tc);
1449                auto asid = haveLargeAsid64 ? bits(newVal, 63, 48) :
1450                                              bits(newVal, 55, 48);
1451
1452                TLBIASID tlbiOp(EL1, haveSecurity && !scr.ns, asid);
1453                tlbiOp(tc);
1454                return;
1455            }
1456          // AArch64 TLB Invalidate by ASID, EL1, Inner Shareable
1457          case MISCREG_TLBI_ASIDE1IS_Xt:
1458            {
1459                assert64(tc);
1460                scr = readMiscReg(MISCREG_SCR, tc);
1461                auto asid = haveLargeAsid64 ? bits(newVal, 63, 48) :
1462                                              bits(newVal, 55, 48);
1463
1464                TLBIASID tlbiOp(EL1, haveSecurity && !scr.ns, asid);
1465                tlbiOp.broadcast(tc);
1466                return;
1467            }
1468          // VAAE1(IS) and VAALE1(IS) are the same because TLBs only store
1469          // entries from the last level of translation table walks
1470          // AArch64 TLB Invalidate by VA, All ASID, EL1
1471          case MISCREG_TLBI_VAAE1_Xt:
1472          case MISCREG_TLBI_VAALE1_Xt:
1473            {
1474                assert64(tc);
1475                scr = readMiscReg(MISCREG_SCR, tc);
1476
1477                TLBIMVAA tlbiOp(EL1, haveSecurity && !scr.ns,
1478                    static_cast<Addr>(bits(newVal, 43, 0)) << 12, false);
1479
1480                tlbiOp(tc);
1481                return;
1482            }
1483          // AArch64 TLB Invalidate by VA, All ASID, EL1, Inner Shareable
1484          case MISCREG_TLBI_VAAE1IS_Xt:
1485          case MISCREG_TLBI_VAALE1IS_Xt:
1486            {
1487                assert64(tc);
1488                scr = readMiscReg(MISCREG_SCR, tc);
1489
1490                TLBIMVAA tlbiOp(EL1, haveSecurity && !scr.ns,
1491                    static_cast<Addr>(bits(newVal, 43, 0)) << 12, false);
1492
1493                tlbiOp.broadcast(tc);
1494                return;
1495            }
1496          // AArch64 TLB Invalidate by Intermediate Physical Address,
1497          // Stage 2, EL1
1498          case MISCREG_TLBI_IPAS2E1_Xt:
1499          case MISCREG_TLBI_IPAS2LE1_Xt:
1500            {
1501                assert64(tc);
1502                scr = readMiscReg(MISCREG_SCR, tc);
1503
1504                TLBIIPA tlbiOp(EL1, haveSecurity && !scr.ns,
1505                               static_cast<Addr>(bits(newVal, 35, 0)) << 12);
1506
1507                tlbiOp(tc);
1508                return;
1509            }
1510          // AArch64 TLB Invalidate by Intermediate Physical Address,
1511          // Stage 2, EL1, Inner Shareable
1512          case MISCREG_TLBI_IPAS2E1IS_Xt:
1513          case MISCREG_TLBI_IPAS2LE1IS_Xt:
1514            {
1515                assert64(tc);
1516                scr = readMiscReg(MISCREG_SCR, tc);
1517
1518                TLBIIPA tlbiOp(EL1, haveSecurity && !scr.ns,
1519                               static_cast<Addr>(bits(newVal, 35, 0)) << 12);
1520
1521                tlbiOp.broadcast(tc);
1522                return;
1523            }
1524          case MISCREG_ACTLR:
1525            warn("Not doing anything for write of miscreg ACTLR\n");
1526            break;
1527
1528          case MISCREG_PMXEVTYPER_PMCCFILTR:
1529          case MISCREG_PMINTENSET_EL1 ... MISCREG_PMOVSSET_EL0:
1530          case MISCREG_PMEVCNTR0_EL0 ... MISCREG_PMEVTYPER5_EL0:
1531          case MISCREG_PMCR ... MISCREG_PMOVSSET:
1532            pmu->setMiscReg(misc_reg, newVal);
1533            break;
1534
1535
1536          case MISCREG_HSTR: // TJDBX, now redifined to be RES0
1537            {
1538                HSTR hstrMask = 0;
1539                hstrMask.tjdbx = 1;
1540                newVal &= ~((uint32_t) hstrMask);
1541                break;
1542            }
1543          case MISCREG_HCPTR:
1544            {
1545                // If a CP bit in NSACR is 0 then the corresponding bit in
1546                // HCPTR is RAO/WI. Same applies to NSASEDIS
1547                secure_lookup = haveSecurity &&
1548                    inSecureState(readMiscRegNoEffect(MISCREG_SCR),
1549                                  readMiscRegNoEffect(MISCREG_CPSR));
1550                if (!secure_lookup) {
1551                    MiscReg oldValue = readMiscRegNoEffect(MISCREG_HCPTR);
1552                    MiscReg mask = (readMiscRegNoEffect(MISCREG_NSACR) ^ 0x7FFF) & 0xBFFF;
1553                    newVal = (newVal & ~mask) | (oldValue & mask);
1554                }
1555                break;
1556            }
1557          case MISCREG_HDFAR: // alias for secure DFAR
1558            misc_reg = MISCREG_DFAR_S;
1559            break;
1560          case MISCREG_HIFAR: // alias for secure IFAR
1561            misc_reg = MISCREG_IFAR_S;
1562            break;
1563          case MISCREG_ATS1CPR:
1564          case MISCREG_ATS1CPW:
1565          case MISCREG_ATS1CUR:
1566          case MISCREG_ATS1CUW:
1567          case MISCREG_ATS12NSOPR:
1568          case MISCREG_ATS12NSOPW:
1569          case MISCREG_ATS12NSOUR:
1570          case MISCREG_ATS12NSOUW:
1571          case MISCREG_ATS1HR:
1572          case MISCREG_ATS1HW:
1573            {
1574              Request::Flags flags = 0;
1575              BaseTLB::Mode mode = BaseTLB::Read;
1576              TLB::ArmTranslationType tranType = TLB::NormalTran;
1577              Fault fault;
1578              switch(misc_reg) {
1579                case MISCREG_ATS1CPR:
1580                  flags    = TLB::MustBeOne;
1581                  tranType = TLB::S1CTran;
1582                  mode     = BaseTLB::Read;
1583                  break;
1584                case MISCREG_ATS1CPW:
1585                  flags    = TLB::MustBeOne;
1586                  tranType = TLB::S1CTran;
1587                  mode     = BaseTLB::Write;
1588                  break;
1589                case MISCREG_ATS1CUR:
1590                  flags    = TLB::MustBeOne | TLB::UserMode;
1591                  tranType = TLB::S1CTran;
1592                  mode     = BaseTLB::Read;
1593                  break;
1594                case MISCREG_ATS1CUW:
1595                  flags    = TLB::MustBeOne | TLB::UserMode;
1596                  tranType = TLB::S1CTran;
1597                  mode     = BaseTLB::Write;
1598                  break;
1599                case MISCREG_ATS12NSOPR:
1600                  if (!haveSecurity)
1601                      panic("Security Extensions required for ATS12NSOPR");
1602                  flags    = TLB::MustBeOne;
1603                  tranType = TLB::S1S2NsTran;
1604                  mode     = BaseTLB::Read;
1605                  break;
1606                case MISCREG_ATS12NSOPW:
1607                  if (!haveSecurity)
1608                      panic("Security Extensions required for ATS12NSOPW");
1609                  flags    = TLB::MustBeOne;
1610                  tranType = TLB::S1S2NsTran;
1611                  mode     = BaseTLB::Write;
1612                  break;
1613                case MISCREG_ATS12NSOUR:
1614                  if (!haveSecurity)
1615                      panic("Security Extensions required for ATS12NSOUR");
1616                  flags    = TLB::MustBeOne | TLB::UserMode;
1617                  tranType = TLB::S1S2NsTran;
1618                  mode     = BaseTLB::Read;
1619                  break;
1620                case MISCREG_ATS12NSOUW:
1621                  if (!haveSecurity)
1622                      panic("Security Extensions required for ATS12NSOUW");
1623                  flags    = TLB::MustBeOne | TLB::UserMode;
1624                  tranType = TLB::S1S2NsTran;
1625                  mode     = BaseTLB::Write;
1626                  break;
1627                case MISCREG_ATS1HR: // only really useful from secure mode.
1628                  flags    = TLB::MustBeOne;
1629                  tranType = TLB::HypMode;
1630                  mode     = BaseTLB::Read;
1631                  break;
1632                case MISCREG_ATS1HW:
1633                  flags    = TLB::MustBeOne;
1634                  tranType = TLB::HypMode;
1635                  mode     = BaseTLB::Write;
1636                  break;
1637              }
1638              // If we're in timing mode then doing the translation in
1639              // functional mode then we're slightly distorting performance
1640              // results obtained from simulations. The translation should be
1641              // done in the same mode the core is running in. NOTE: This
1642              // can't be an atomic translation because that causes problems
1643              // with unexpected atomic snoop requests.
1644              warn("Translating via %s in functional mode! Fix Me!\n",
1645                   miscRegName[misc_reg]);
1646
1647              auto req = std::make_shared<Request>(
1648                  0, val, 0, flags,  Request::funcMasterId,
1649                  tc->pcState().pc(), tc->contextId());
1650
1651              fault = getDTBPtr(tc)->translateFunctional(
1652                      req, tc, mode, tranType);
1653
1654              TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
1655              HCR   hcr   = readMiscRegNoEffect(MISCREG_HCR);
1656
1657              MiscReg newVal;
1658              if (fault == NoFault) {
1659                  Addr paddr = req->getPaddr();
1660                  if (haveLPAE && (ttbcr.eae || tranType & TLB::HypMode ||
1661                     ((tranType & TLB::S1S2NsTran) && hcr.vm) )) {
1662                      newVal = (paddr & mask(39, 12)) |
1663                               (getDTBPtr(tc)->getAttr());
1664                  } else {
1665                      newVal = (paddr & 0xfffff000) |
1666                               (getDTBPtr(tc)->getAttr());
1667                  }
1668                  DPRINTF(MiscRegs,
1669                          "MISCREG: Translated addr 0x%08x: PAR: 0x%08x\n",
1670                          val, newVal);
1671              } else {
1672                  ArmFault *armFault = static_cast<ArmFault *>(fault.get());
1673                  armFault->update(tc);
1674                  // Set fault bit and FSR
1675                  FSR fsr = armFault->getFsr(tc);
1676
1677                  newVal = ((fsr >> 9) & 1) << 11;
1678                  if (newVal) {
1679                    // LPAE - rearange fault status
1680                    newVal |= ((fsr >>  0) & 0x3f) << 1;
1681                  } else {
1682                    // VMSA - rearange fault status
1683                    newVal |= ((fsr >>  0) & 0xf) << 1;
1684                    newVal |= ((fsr >> 10) & 0x1) << 5;
1685                    newVal |= ((fsr >> 12) & 0x1) << 6;
1686                  }
1687                  newVal |= 0x1; // F bit
1688                  newVal |= ((armFault->iss() >> 7) & 0x1) << 8;
1689                  newVal |= armFault->isStage2() ? 0x200 : 0;
1690                  DPRINTF(MiscRegs,
1691                          "MISCREG: Translated addr 0x%08x fault fsr %#x: PAR: 0x%08x\n",
1692                          val, fsr, newVal);
1693              }
1694              setMiscRegNoEffect(MISCREG_PAR, newVal);
1695              return;
1696            }
1697          case MISCREG_TTBCR:
1698            {
1699                TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
1700                const uint32_t ones = (uint32_t)(-1);
1701                TTBCR ttbcrMask = 0;
1702                TTBCR ttbcrNew = newVal;
1703
1704                // ARM DDI 0406C.b, ARMv7-32
1705                ttbcrMask.n = ones; // T0SZ
1706                if (haveSecurity) {
1707                    ttbcrMask.pd0 = ones;
1708                    ttbcrMask.pd1 = ones;
1709                }
1710                ttbcrMask.epd0 = ones;
1711                ttbcrMask.irgn0 = ones;
1712                ttbcrMask.orgn0 = ones;
1713                ttbcrMask.sh0 = ones;
1714                ttbcrMask.ps = ones; // T1SZ
1715                ttbcrMask.a1 = ones;
1716                ttbcrMask.epd1 = ones;
1717                ttbcrMask.irgn1 = ones;
1718                ttbcrMask.orgn1 = ones;
1719                ttbcrMask.sh1 = ones;
1720                if (haveLPAE)
1721                    ttbcrMask.eae = ones;
1722
1723                if (haveLPAE && ttbcrNew.eae) {
1724                    newVal = newVal & ttbcrMask;
1725                } else {
1726                    newVal = (newVal & ttbcrMask) | (ttbcr & (~ttbcrMask));
1727                }
1728                // Invalidate TLB MiscReg
1729                getITBPtr(tc)->invalidateMiscReg();
1730                getDTBPtr(tc)->invalidateMiscReg();
1731                break;
1732            }
1733          case MISCREG_TTBR0:
1734          case MISCREG_TTBR1:
1735            {
1736                TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
1737                if (haveLPAE) {
1738                    if (ttbcr.eae) {
1739                        // ARMv7 bit 63-56, 47-40 reserved, UNK/SBZP
1740                        // ARMv8 AArch32 bit 63-56 only
1741                        uint64_t ttbrMask = mask(63,56) | mask(47,40);
1742                        newVal = (newVal & (~ttbrMask));
1743                    }
1744                }
1745                // Invalidate TLB MiscReg
1746                getITBPtr(tc)->invalidateMiscReg();
1747                getDTBPtr(tc)->invalidateMiscReg();
1748                break;
1749            }
1750          case MISCREG_SCTLR_EL1:
1751          case MISCREG_CONTEXTIDR:
1752          case MISCREG_PRRR:
1753          case MISCREG_NMRR:
1754          case MISCREG_MAIR0:
1755          case MISCREG_MAIR1:
1756          case MISCREG_DACR:
1757          case MISCREG_VTTBR:
1758          case MISCREG_SCR_EL3:
1759          case MISCREG_HCR_EL2:
1760          case MISCREG_TCR_EL1:
1761          case MISCREG_TCR_EL2:
1762          case MISCREG_TCR_EL3:
1763          case MISCREG_SCTLR_EL2:
1764          case MISCREG_SCTLR_EL3:
1765          case MISCREG_HSCTLR:
1766          case MISCREG_TTBR0_EL1:
1767          case MISCREG_TTBR1_EL1:
1768          case MISCREG_TTBR0_EL2:
1769          case MISCREG_TTBR1_EL2:
1770          case MISCREG_TTBR0_EL3:
1771            getITBPtr(tc)->invalidateMiscReg();
1772            getDTBPtr(tc)->invalidateMiscReg();
1773            break;
1774          case MISCREG_NZCV:
1775            {
1776                CPSR cpsr = val;
1777
1778                tc->setCCReg(CCREG_NZ, cpsr.nz);
1779                tc->setCCReg(CCREG_C,  cpsr.c);
1780                tc->setCCReg(CCREG_V,  cpsr.v);
1781            }
1782            break;
1783          case MISCREG_DAIF:
1784            {
1785                CPSR cpsr = miscRegs[MISCREG_CPSR];
1786                cpsr.daif = (uint8_t) ((CPSR) newVal).daif;
1787                newVal = cpsr;
1788                misc_reg = MISCREG_CPSR;
1789            }
1790            break;
1791          case MISCREG_SP_EL0:
1792            tc->setIntReg(INTREG_SP0, newVal);
1793            break;
1794          case MISCREG_SP_EL1:
1795            tc->setIntReg(INTREG_SP1, newVal);
1796            break;
1797          case MISCREG_SP_EL2:
1798            tc->setIntReg(INTREG_SP2, newVal);
1799            break;
1800          case MISCREG_SPSEL:
1801            {
1802                CPSR cpsr = miscRegs[MISCREG_CPSR];
1803                cpsr.sp = (uint8_t) ((CPSR) newVal).sp;
1804                newVal = cpsr;
1805                misc_reg = MISCREG_CPSR;
1806            }
1807            break;
1808          case MISCREG_CURRENTEL:
1809            {
1810                CPSR cpsr = miscRegs[MISCREG_CPSR];
1811                cpsr.el = (uint8_t) ((CPSR) newVal).el;
1812                newVal = cpsr;
1813                misc_reg = MISCREG_CPSR;
1814            }
1815            break;
1816          case MISCREG_AT_S1E1R_Xt:
1817          case MISCREG_AT_S1E1W_Xt:
1818          case MISCREG_AT_S1E0R_Xt:
1819          case MISCREG_AT_S1E0W_Xt:
1820          case MISCREG_AT_S1E2R_Xt:
1821          case MISCREG_AT_S1E2W_Xt:
1822          case MISCREG_AT_S12E1R_Xt:
1823          case MISCREG_AT_S12E1W_Xt:
1824          case MISCREG_AT_S12E0R_Xt:
1825          case MISCREG_AT_S12E0W_Xt:
1826          case MISCREG_AT_S1E3R_Xt:
1827          case MISCREG_AT_S1E3W_Xt:
1828            {
1829                RequestPtr req = std::make_shared<Request>();
1830                Request::Flags flags = 0;
1831                BaseTLB::Mode mode = BaseTLB::Read;
1832                TLB::ArmTranslationType tranType = TLB::NormalTran;
1833                Fault fault;
1834                switch(misc_reg) {
1835                  case MISCREG_AT_S1E1R_Xt:
1836                    flags    = TLB::MustBeOne;
1837                    tranType = TLB::S1E1Tran;
1838                    mode     = BaseTLB::Read;
1839                    break;
1840                  case MISCREG_AT_S1E1W_Xt:
1841                    flags    = TLB::MustBeOne;
1842                    tranType = TLB::S1E1Tran;
1843                    mode     = BaseTLB::Write;
1844                    break;
1845                  case MISCREG_AT_S1E0R_Xt:
1846                    flags    = TLB::MustBeOne | TLB::UserMode;
1847                    tranType = TLB::S1E0Tran;
1848                    mode     = BaseTLB::Read;
1849                    break;
1850                  case MISCREG_AT_S1E0W_Xt:
1851                    flags    = TLB::MustBeOne | TLB::UserMode;
1852                    tranType = TLB::S1E0Tran;
1853                    mode     = BaseTLB::Write;
1854                    break;
1855                  case MISCREG_AT_S1E2R_Xt:
1856                    flags    = TLB::MustBeOne;
1857                    tranType = TLB::S1E2Tran;
1858                    mode     = BaseTLB::Read;
1859                    break;
1860                  case MISCREG_AT_S1E2W_Xt:
1861                    flags    = TLB::MustBeOne;
1862                    tranType = TLB::S1E2Tran;
1863                    mode     = BaseTLB::Write;
1864                    break;
1865                  case MISCREG_AT_S12E0R_Xt:
1866                    flags    = TLB::MustBeOne | TLB::UserMode;
1867                    tranType = TLB::S12E0Tran;
1868                    mode     = BaseTLB::Read;
1869                    break;
1870                  case MISCREG_AT_S12E0W_Xt:
1871                    flags    = TLB::MustBeOne | TLB::UserMode;
1872                    tranType = TLB::S12E0Tran;
1873                    mode     = BaseTLB::Write;
1874                    break;
1875                  case MISCREG_AT_S12E1R_Xt:
1876                    flags    = TLB::MustBeOne;
1877                    tranType = TLB::S12E1Tran;
1878                    mode     = BaseTLB::Read;
1879                    break;
1880                  case MISCREG_AT_S12E1W_Xt:
1881                    flags    = TLB::MustBeOne;
1882                    tranType = TLB::S12E1Tran;
1883                    mode     = BaseTLB::Write;
1884                    break;
1885                  case MISCREG_AT_S1E3R_Xt:
1886                    flags    = TLB::MustBeOne;
1887                    tranType = TLB::S1E3Tran;
1888                    mode     = BaseTLB::Read;
1889                    break;
1890                  case MISCREG_AT_S1E3W_Xt:
1891                    flags    = TLB::MustBeOne;
1892                    tranType = TLB::S1E3Tran;
1893                    mode     = BaseTLB::Write;
1894                    break;
1895                }
1896                // If we're in timing mode then doing the translation in
1897                // functional mode then we're slightly distorting performance
1898                // results obtained from simulations. The translation should be
1899                // done in the same mode the core is running in. NOTE: This
1900                // can't be an atomic translation because that causes problems
1901                // with unexpected atomic snoop requests.
1902                warn("Translating via %s in functional mode! Fix Me!\n",
1903                     miscRegName[misc_reg]);
1904
1905                req->setVirt(0, val, 0, flags,  Request::funcMasterId,
1906                               tc->pcState().pc());
1907                req->setContext(tc->contextId());
1908                fault = getDTBPtr(tc)->translateFunctional(req, tc, mode,
1909                                                           tranType);
1910
1911                MiscReg newVal;
1912                if (fault == NoFault) {
1913                    Addr paddr = req->getPaddr();
1914                    uint64_t attr = getDTBPtr(tc)->getAttr();
1915                    uint64_t attr1 = attr >> 56;
1916                    if (!attr1 || attr1 ==0x44) {
1917                        attr |= 0x100;
1918                        attr &= ~ uint64_t(0x80);
1919                    }
1920                    newVal = (paddr & mask(47, 12)) | attr;
1921                    DPRINTF(MiscRegs,
1922                          "MISCREG: Translated addr %#x: PAR_EL1: %#xx\n",
1923                          val, newVal);
1924                } else {
1925                    ArmFault *armFault = static_cast<ArmFault *>(fault.get());
1926                    armFault->update(tc);
1927                    // Set fault bit and FSR
1928                    FSR fsr = armFault->getFsr(tc);
1929
1930                    CPSR cpsr = tc->readMiscReg(MISCREG_CPSR);
1931                    if (cpsr.width) { // AArch32
1932                        newVal = ((fsr >> 9) & 1) << 11;
1933                        // rearrange fault status
1934                        newVal |= ((fsr >>  0) & 0x3f) << 1;
1935                        newVal |= 0x1; // F bit
1936                        newVal |= ((armFault->iss() >> 7) & 0x1) << 8;
1937                        newVal |= armFault->isStage2() ? 0x200 : 0;
1938                    } else { // AArch64
1939                        newVal = 1; // F bit
1940                        newVal |= fsr << 1; // FST
1941                        // TODO: DDI 0487A.f D7-2083, AbortFault's s1ptw bit.
1942                        newVal |= armFault->isStage2() ? 1 << 8 : 0; // PTW
1943                        newVal |= armFault->isStage2() ? 1 << 9 : 0; // S
1944                        newVal |= 1 << 11; // RES1
1945                    }
1946                    DPRINTF(MiscRegs,
1947                            "MISCREG: Translated addr %#x fault fsr %#x: PAR: %#x\n",
1948                            val, fsr, newVal);
1949                }
1950                setMiscRegNoEffect(MISCREG_PAR_EL1, newVal);
1951                return;
1952            }
1953          case MISCREG_SPSR_EL3:
1954          case MISCREG_SPSR_EL2:
1955          case MISCREG_SPSR_EL1:
1956            // Force bits 23:21 to 0
1957            newVal = val & ~(0x7 << 21);
1958            break;
1959          case MISCREG_L2CTLR:
1960            warn("miscreg L2CTLR (%s) written with %#x. ignored...\n",
1961                 miscRegName[misc_reg], uint32_t(val));
1962            break;
1963
1964          // Generic Timer registers
1965          case MISCREG_CNTHV_CTL_EL2:
1966          case MISCREG_CNTHV_CVAL_EL2:
1967          case MISCREG_CNTHV_TVAL_EL2:
1968          case MISCREG_CNTFRQ ... MISCREG_CNTHP_CTL:
1969          case MISCREG_CNTPCT ... MISCREG_CNTHP_CVAL:
1970          case MISCREG_CNTKCTL_EL1 ... MISCREG_CNTV_CVAL_EL0:
1971          case MISCREG_CNTVOFF_EL2 ... MISCREG_CNTPS_CVAL_EL1:
1972            getGenericTimer(tc).setMiscReg(misc_reg, newVal);
1973            break;
1974        }
1975    }
1976    setMiscRegNoEffect(misc_reg, newVal);
1977}
1978
1979BaseISADevice &
1980ISA::getGenericTimer(ThreadContext *tc)
1981{
1982    // We only need to create an ISA interface the first time we try
1983    // to access the timer.
1984    if (timer)
1985        return *timer.get();
1986
1987    assert(system);
1988    GenericTimer *generic_timer(system->getGenericTimer());
1989    if (!generic_timer) {
1990        panic("Trying to get a generic timer from a system that hasn't "
1991              "been configured to use a generic timer.\n");
1992    }
1993
1994    timer.reset(new GenericTimerISA(*generic_timer, tc->contextId()));
1995    timer->setThreadContext(tc);
1996
1997    return *timer.get();
1998}
1999
2000}
2001
2002ArmISA::ISA *
2003ArmISAParams::create()
2004{
2005    return new ArmISA::ISA(this);
2006}
2007