2 * Copyright (c) 2010, 2012-2020 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2009 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __ARCH_ARM_ISA_HH__
42 #define __ARCH_ARM_ISA_HH__
44 #include "arch/arm/isa_device.hh"
45 #include "arch/arm/miscregs.hh"
46 #include "arch/arm/registers.hh"
47 #include "arch/arm/self_debug.hh"
48 #include "arch/arm/system.hh"
49 #include "arch/arm/tlb.hh"
50 #include "arch/arm/types.hh"
51 #include "arch/generic/isa.hh"
52 #include "arch/generic/traits.hh"
53 #include "debug/Checkpoint.hh"
54 #include "enums/DecoderFlavor.hh"
55 #include "enums/VecRegRenameMode.hh"
56 #include "sim/sim_object.hh"
59 struct DummyArmISADeviceParams;
65 class ISA : public BaseISA
72 const Enums::DecoderFlavor _decoderFlavor;
73 const Enums::VecRegRenameMode _vecRegRenameMode;
75 /** Dummy device for to handle non-existing ISA devices */
76 DummyISADevice dummyDevice;
78 // PMU belonging to this ISA
81 // Generic timer interface belonging to this ISA
82 std::unique_ptr<BaseISADevice> timer;
84 // GICv3 CPU interface belonging to this ISA
85 std::unique_ptr<BaseISADevice> gicv3CpuInterface;
87 // Cached copies of system-level properties
91 bool haveVirtualization;
94 uint8_t physAddrRange;
99 /** SVE vector length in quadwords */
103 * If true, accesses to IMPLEMENTATION DEFINED registers are treated
104 * as NOP hence not causing UNDEFINED INSTRUCTION.
110 SelfDebug * selfDebug;
112 /** MiscReg metadata **/
113 struct MiscRegLUTEntry {
114 uint32_t lower; // Lower half mapped to this register
115 uint32_t upper; // Upper half mapped to this register
116 uint64_t _reset; // value taken on reset (i.e. initialization)
117 uint64_t _res0; // reserved
118 uint64_t _res1; // reserved
119 uint64_t _raz; // read as zero (fixed at 0)
120 uint64_t _rao; // read as one (fixed at 1)
124 _reset(0), _res0(0), _res1(0), _raz(0), _rao(0) {}
125 uint64_t reset() const { return _reset; }
126 uint64_t res0() const { return _res0; }
127 uint64_t res1() const { return _res1; }
128 uint64_t raz() const { return _raz; }
129 uint64_t rao() const { return _rao; }
130 // raz/rao implies writes ignored
131 uint64_t wi() const { return _raz | _rao; }
134 /** Metadata table accessible via the value of the register */
135 static std::vector<struct MiscRegLUTEntry> lookUpMiscReg;
137 class MiscRegLUTEntryInitializer {
138 struct MiscRegLUTEntry &entry;
139 std::bitset<NUM_MISCREG_INFOS> &info;
140 typedef const MiscRegLUTEntryInitializer& chain;
142 chain mapsTo(uint32_t l, uint32_t u = 0) const {
147 chain res0(uint64_t mask) const {
151 chain res1(uint64_t mask) const {
155 chain raz(uint64_t mask) const {
159 chain rao(uint64_t mask) const {
163 chain implemented(bool v = true) const {
164 info[MISCREG_IMPLEMENTED] = v;
167 chain unimplemented() const {
168 return implemented(false);
170 chain unverifiable(bool v = true) const {
171 info[MISCREG_UNVERIFIABLE] = v;
174 chain warnNotFail(bool v = true) const {
175 info[MISCREG_WARN_NOT_FAIL] = v;
178 chain mutex(bool v = true) const {
179 info[MISCREG_MUTEX] = v;
182 chain banked(bool v = true) const {
183 info[MISCREG_BANKED] = v;
186 chain banked64(bool v = true) const {
187 info[MISCREG_BANKED64] = v;
190 chain bankedChild(bool v = true) const {
191 info[MISCREG_BANKED_CHILD] = v;
194 chain userNonSecureRead(bool v = true) const {
195 info[MISCREG_USR_NS_RD] = v;
198 chain userNonSecureWrite(bool v = true) const {
199 info[MISCREG_USR_NS_WR] = v;
202 chain userSecureRead(bool v = true) const {
203 info[MISCREG_USR_S_RD] = v;
206 chain userSecureWrite(bool v = true) const {
207 info[MISCREG_USR_S_WR] = v;
210 chain user(bool v = true) const {
211 userNonSecureRead(v);
212 userNonSecureWrite(v);
217 chain privNonSecureRead(bool v = true) const {
218 info[MISCREG_PRI_NS_RD] = v;
221 chain privNonSecureWrite(bool v = true) const {
222 info[MISCREG_PRI_NS_WR] = v;
225 chain privNonSecure(bool v = true) const {
226 privNonSecureRead(v);
227 privNonSecureWrite(v);
230 chain privSecureRead(bool v = true) const {
231 info[MISCREG_PRI_S_RD] = v;
234 chain privSecureWrite(bool v = true) const {
235 info[MISCREG_PRI_S_WR] = v;
238 chain privSecure(bool v = true) const {
243 chain priv(bool v = true) const {
248 chain privRead(bool v = true) const {
250 privNonSecureRead(v);
253 chain hypE2HRead(bool v = true) const {
254 info[MISCREG_HYP_E2H_RD] = v;
257 chain hypE2HWrite(bool v = true) const {
258 info[MISCREG_HYP_E2H_WR] = v;
261 chain hypE2H(bool v = true) const {
266 chain hypRead(bool v = true) const {
268 info[MISCREG_HYP_RD] = v;
271 chain hypWrite(bool v = true) const {
273 info[MISCREG_HYP_WR] = v;
276 chain hyp(bool v = true) const {
281 chain monE2HRead(bool v = true) const {
282 info[MISCREG_MON_E2H_RD] = v;
285 chain monE2HWrite(bool v = true) const {
286 info[MISCREG_MON_E2H_WR] = v;
289 chain monE2H(bool v = true) const {
294 chain monSecureRead(bool v = true) const {
296 info[MISCREG_MON_NS0_RD] = v;
299 chain monSecureWrite(bool v = true) const {
301 info[MISCREG_MON_NS0_WR] = v;
304 chain monNonSecureRead(bool v = true) const {
306 info[MISCREG_MON_NS1_RD] = v;
309 chain monNonSecureWrite(bool v = true) const {
311 info[MISCREG_MON_NS1_WR] = v;
314 chain mon(bool v = true) const {
318 monNonSecureWrite(v);
321 chain monSecure(bool v = true) const {
326 chain monNonSecure(bool v = true) const {
328 monNonSecureWrite(v);
331 chain allPrivileges(bool v = true) const {
332 userNonSecureRead(v);
333 userNonSecureWrite(v);
336 privNonSecureRead(v);
337 privNonSecureWrite(v);
345 monNonSecureWrite(v);
348 chain nonSecure(bool v = true) const {
349 userNonSecureRead(v);
350 userNonSecureWrite(v);
351 privNonSecureRead(v);
352 privNonSecureWrite(v);
356 monNonSecureWrite(v);
359 chain secure(bool v = true) const {
368 chain reads(bool v) const {
369 userNonSecureRead(v);
371 privNonSecureRead(v);
378 chain writes(bool v) const {
379 userNonSecureWrite(v);
381 privNonSecureWrite(v);
385 monNonSecureWrite(v);
388 chain exceptUserMode() const {
392 chain highest(ArmSystem *const sys) const;
393 MiscRegLUTEntryInitializer(struct MiscRegLUTEntry &e,
394 std::bitset<NUM_MISCREG_INFOS> &i)
398 // force unimplemented registers to be thusly declared
403 const MiscRegLUTEntryInitializer InitReg(uint32_t reg) {
404 return MiscRegLUTEntryInitializer(lookUpMiscReg[reg],
408 void initializeMiscRegMetadata();
410 RegVal miscRegs[NumMiscRegs];
411 const IntRegIndex *intRegMap;
414 updateRegMap(CPSR cpsr)
416 if (cpsr.width == 0) {
417 intRegMap = IntReg64Map;
422 intRegMap = IntRegUsrMap;
425 intRegMap = IntRegFiqMap;
428 intRegMap = IntRegIrqMap;
431 intRegMap = IntRegSvcMap;
434 intRegMap = IntRegMonMap;
437 intRegMap = IntRegAbtMap;
440 intRegMap = IntRegHypMap;
443 intRegMap = IntRegUndMap;
446 panic("Unrecognized mode setting in CPSR.\n");
451 BaseISADevice &getGenericTimer();
452 BaseISADevice &getGICv3CPUInterface();
455 void assert32() { assert(((CPSR)readMiscReg(MISCREG_CPSR)).width); }
456 void assert64() { assert(!((CPSR)readMiscReg(MISCREG_CPSR)).width); }
462 void clear32(const ArmISAParams *p, const SCTLR &sctlr_rst);
463 void clear64(const ArmISAParams *p);
464 void initID32(const ArmISAParams *p);
465 void initID64(const ArmISAParams *p);
468 SelfDebug * getSelfDebug()
472 RegVal readMiscRegNoEffect(int misc_reg) const;
473 RegVal readMiscReg(int misc_reg);
474 void setMiscRegNoEffect(int misc_reg, RegVal val);
475 void setMiscReg(int misc_reg, RegVal val);
478 flattenRegId(const RegId& regId) const
480 switch (regId.classValue()) {
482 return RegId(IntRegClass, flattenIntIndex(regId.index()));
484 return RegId(FloatRegClass, flattenFloatIndex(regId.index()));
486 return RegId(VecRegClass, flattenVecIndex(regId.index()));
488 return RegId(VecElemClass, flattenVecElemIndex(regId.index()),
490 case VecPredRegClass:
491 return RegId(VecPredRegClass,
492 flattenVecPredIndex(regId.index()));
494 return RegId(CCRegClass, flattenCCIndex(regId.index()));
496 return RegId(MiscRegClass, flattenMiscIndex(regId.index()));
502 flattenIntIndex(int reg) const
505 if (reg < NUM_ARCH_INTREGS) {
506 return intRegMap[reg];
507 } else if (reg < NUM_INTREGS) {
509 } else if (reg == INTREG_SPX) {
510 CPSR cpsr = miscRegs[MISCREG_CPSR];
511 ExceptionLevel el = opModeToEL(
512 (OperatingMode) (uint8_t) cpsr.mode);
513 if (!cpsr.sp && el != EL0)
525 panic("Invalid exception level");
526 return 0; // Never happens.
529 return flattenIntRegModeIndex(reg);
534 flattenFloatIndex(int reg) const
541 flattenVecIndex(int reg) const
548 flattenVecElemIndex(int reg) const
555 flattenVecPredIndex(int reg) const
562 flattenCCIndex(int reg) const
569 flattenMiscIndex(int reg) const
574 if (reg == MISCREG_SPSR) {
575 CPSR cpsr = miscRegs[MISCREG_CPSR];
578 warn("User mode does not have SPSR\n");
579 flat_idx = MISCREG_SPSR;
583 flat_idx = MISCREG_SPSR_EL1;
587 flat_idx = MISCREG_SPSR_EL2;
591 flat_idx = MISCREG_SPSR_EL3;
594 warn("User mode does not have SPSR\n");
595 flat_idx = MISCREG_SPSR;
598 flat_idx = MISCREG_SPSR_FIQ;
601 flat_idx = MISCREG_SPSR_IRQ;
604 flat_idx = MISCREG_SPSR_SVC;
607 flat_idx = MISCREG_SPSR_MON;
610 flat_idx = MISCREG_SPSR_ABT;
613 flat_idx = MISCREG_SPSR_HYP;
616 flat_idx = MISCREG_SPSR_UND;
619 warn("Trying to access SPSR in an invalid mode: %d\n",
621 flat_idx = MISCREG_SPSR;
624 } else if (miscRegInfo[reg][MISCREG_MUTEX]) {
625 // Mutually exclusive CP15 register
627 case MISCREG_PRRR_MAIR0:
628 case MISCREG_PRRR_MAIR0_NS:
629 case MISCREG_PRRR_MAIR0_S:
631 TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
632 // If the muxed reg has been flattened, work out the
633 // offset and apply it to the unmuxed reg
634 int idxOffset = reg - MISCREG_PRRR_MAIR0;
636 flat_idx = flattenMiscIndex(MISCREG_MAIR0 +
639 flat_idx = flattenMiscIndex(MISCREG_PRRR +
643 case MISCREG_NMRR_MAIR1:
644 case MISCREG_NMRR_MAIR1_NS:
645 case MISCREG_NMRR_MAIR1_S:
647 TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
648 // If the muxed reg has been flattened, work out the
649 // offset and apply it to the unmuxed reg
650 int idxOffset = reg - MISCREG_NMRR_MAIR1;
652 flat_idx = flattenMiscIndex(MISCREG_MAIR1 +
655 flat_idx = flattenMiscIndex(MISCREG_NMRR +
659 case MISCREG_PMXEVTYPER_PMCCFILTR:
661 PMSELR pmselr = miscRegs[MISCREG_PMSELR];
662 if (pmselr.sel == 31)
663 flat_idx = flattenMiscIndex(MISCREG_PMCCFILTR);
665 flat_idx = flattenMiscIndex(MISCREG_PMXEVTYPER);
669 panic("Unrecognized misc. register.\n");
673 if (miscRegInfo[reg][MISCREG_BANKED]) {
674 bool secureReg = haveSecurity && !highestELIs64 &&
675 inSecureState(miscRegs[MISCREG_SCR],
676 miscRegs[MISCREG_CPSR]);
677 flat_idx += secureReg ? 2 : 1;
679 flat_idx = snsBankedIndex64((MiscRegIndex)reg,
680 !inSecureState(miscRegs[MISCREG_SCR],
681 miscRegs[MISCREG_CPSR]));
688 * Returns the enconcing equivalent when VHE is implemented and
689 * HCR_EL2.E2H is enabled and executing at EL2
692 redirectRegVHE(ThreadContext * tc, int misc_reg)
694 const HCR hcr = readMiscRegNoEffect(MISCREG_HCR_EL2);
695 if (hcr.e2h == 0x0 || currEL(tc) != EL2)
697 SCR scr = readMiscRegNoEffect(MISCREG_SCR_EL3);
698 bool sec_el2 = scr.eel2 && false;
700 case MISCREG_SPSR_EL1:
701 return MISCREG_SPSR_EL2;
702 case MISCREG_ELR_EL1:
703 return MISCREG_ELR_EL2;
704 case MISCREG_SCTLR_EL1:
705 return MISCREG_SCTLR_EL2;
706 case MISCREG_CPACR_EL1:
707 return MISCREG_CPTR_EL2;
709 // return MISCREG_TRFCR_EL2;
710 case MISCREG_TTBR0_EL1:
711 return MISCREG_TTBR0_EL2;
712 case MISCREG_TTBR1_EL1:
713 return MISCREG_TTBR1_EL2;
714 case MISCREG_TCR_EL1:
715 return MISCREG_TCR_EL2;
716 case MISCREG_AFSR0_EL1:
717 return MISCREG_AFSR0_EL2;
718 case MISCREG_AFSR1_EL1:
719 return MISCREG_AFSR1_EL2;
720 case MISCREG_ESR_EL1:
721 return MISCREG_ESR_EL2;
722 case MISCREG_FAR_EL1:
723 return MISCREG_FAR_EL2;
724 case MISCREG_MAIR_EL1:
725 return MISCREG_MAIR_EL2;
726 case MISCREG_AMAIR_EL1:
727 return MISCREG_AMAIR_EL2;
728 case MISCREG_VBAR_EL1:
729 return MISCREG_VBAR_EL2;
730 case MISCREG_CONTEXTIDR_EL1:
731 return MISCREG_CONTEXTIDR_EL2;
732 case MISCREG_CNTKCTL_EL1:
733 return MISCREG_CNTHCTL_EL2;
734 case MISCREG_CNTP_TVAL_EL0:
735 return sec_el2? MISCREG_CNTHPS_TVAL_EL2:
736 MISCREG_CNTHP_TVAL_EL2;
737 case MISCREG_CNTP_CTL_EL0:
738 return sec_el2? MISCREG_CNTHPS_CTL_EL2:
739 MISCREG_CNTHP_CTL_EL2;
740 case MISCREG_CNTP_CVAL_EL0:
741 return sec_el2? MISCREG_CNTHPS_CVAL_EL2:
742 MISCREG_CNTHP_CVAL_EL2;
743 case MISCREG_CNTV_TVAL_EL0:
744 return sec_el2? MISCREG_CNTHVS_TVAL_EL2:
745 MISCREG_CNTHV_TVAL_EL2;
746 case MISCREG_CNTV_CTL_EL0:
747 return sec_el2? MISCREG_CNTHVS_CTL_EL2:
748 MISCREG_CNTHV_CTL_EL2;
749 case MISCREG_CNTV_CVAL_EL0:
750 return sec_el2? MISCREG_CNTHVS_CVAL_EL2:
751 MISCREG_CNTHV_CVAL_EL2;
755 /*should not be accessible */
760 snsBankedIndex64(MiscRegIndex reg, bool ns) const
762 int reg_as_int = static_cast<int>(reg);
763 if (miscRegInfo[reg][MISCREG_BANKED64]) {
764 reg_as_int += (haveSecurity && !ns) ? 2 : 1;
769 std::pair<int,int> getMiscIndices(int misc_reg) const
771 // Note: indexes of AArch64 registers are left unchanged
772 int flat_idx = flattenMiscIndex(misc_reg);
774 if (lookUpMiscReg[flat_idx].lower == 0) {
775 return std::make_pair(flat_idx, 0);
778 // do additional S/NS flattenings if mapped to NS while in S
779 bool S = haveSecurity && !highestELIs64 &&
780 inSecureState(miscRegs[MISCREG_SCR],
781 miscRegs[MISCREG_CPSR]);
782 int lower = lookUpMiscReg[flat_idx].lower;
783 int upper = lookUpMiscReg[flat_idx].upper;
784 // upper == 0, which is CPSR, is not MISCREG_BANKED_CHILD (no-op)
785 lower += S && miscRegInfo[lower][MISCREG_BANKED_CHILD];
786 upper += S && miscRegInfo[upper][MISCREG_BANKED_CHILD];
787 return std::make_pair(lower, upper);
790 unsigned getCurSveVecLenInBits() const;
792 unsigned getCurSveVecLenInBitsAtReset() const { return sveVL * 128; }
794 static void zeroSveVecRegUpperPart(VecRegContainer &vc,
798 serialize(CheckpointOut &cp) const override
800 DPRINTF(Checkpoint, "Serializing Arm Misc Registers\n");
801 SERIALIZE_ARRAY(miscRegs, NUM_PHYS_MISCREGS);
805 unserialize(CheckpointIn &cp) override
807 DPRINTF(Checkpoint, "Unserializing Arm Misc Registers\n");
808 UNSERIALIZE_ARRAY(miscRegs, NUM_PHYS_MISCREGS);
809 CPSR tmp_cpsr = miscRegs[MISCREG_CPSR];
810 updateRegMap(tmp_cpsr);
813 void startup() override;
815 void setupThreadContext();
817 void takeOverFrom(ThreadContext *new_tc,
818 ThreadContext *old_tc) override;
820 Enums::DecoderFlavor decoderFlavor() const { return _decoderFlavor; }
822 /** Returns true if the ISA has a GICv3 cpu interface */
823 bool haveGICv3CpuIfc() const
825 // gicv3CpuInterface is initialized at startup time, hence
826 // trying to read its value before the startup stage will lead
828 assert(afterStartup);
829 return gicv3CpuInterface != nullptr;
832 Enums::VecRegRenameMode
833 vecRegRenameMode() const
835 return _vecRegRenameMode;
838 typedef ArmISAParams Params;
840 const Params *params() const;
847 struct RenameMode<ArmISA::ISA>
849 static Enums::VecRegRenameMode
850 init(const BaseISA* isa)
852 auto arm_isa = dynamic_cast<const ArmISA::ISA *>(isa);
854 return arm_isa->vecRegRenameMode();
857 static Enums::VecRegRenameMode
858 mode(const ArmISA::PCState& pc)
868 equalsInit(const BaseISA* isa1, const BaseISA* isa2)
870 return init(isa1) == init(isa2);