2 * Copyright (c) 2010, 2012-2021 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;
102 /** SVE vector length in quadwords */
106 * If true, accesses to IMPLEMENTATION DEFINED registers are treated
107 * as NOP hence not causing UNDEFINED INSTRUCTION.
113 SelfDebug * selfDebug;
115 /** MiscReg metadata **/
116 struct MiscRegLUTEntry {
117 uint32_t lower; // Lower half mapped to this register
118 uint32_t upper; // Upper half mapped to this register
119 uint64_t _reset; // value taken on reset (i.e. initialization)
120 uint64_t _res0; // reserved
121 uint64_t _res1; // reserved
122 uint64_t _raz; // read as zero (fixed at 0)
123 uint64_t _rao; // read as one (fixed at 1)
127 _reset(0), _res0(0), _res1(0), _raz(0), _rao(0) {}
128 uint64_t reset() const { return _reset; }
129 uint64_t res0() const { return _res0; }
130 uint64_t res1() const { return _res1; }
131 uint64_t raz() const { return _raz; }
132 uint64_t rao() const { return _rao; }
133 // raz/rao implies writes ignored
134 uint64_t wi() const { return _raz | _rao; }
137 /** Metadata table accessible via the value of the register */
138 static std::vector<struct MiscRegLUTEntry> lookUpMiscReg;
140 class MiscRegLUTEntryInitializer {
141 struct MiscRegLUTEntry &entry;
142 std::bitset<NUM_MISCREG_INFOS> &info;
143 typedef const MiscRegLUTEntryInitializer& chain;
145 chain mapsTo(uint32_t l, uint32_t u = 0) const {
150 chain res0(uint64_t mask) const {
154 chain res1(uint64_t mask) const {
158 chain raz(uint64_t mask) const {
162 chain rao(uint64_t mask) const {
166 chain implemented(bool v = true) const {
167 info[MISCREG_IMPLEMENTED] = v;
170 chain unimplemented() const {
171 return implemented(false);
173 chain unverifiable(bool v = true) const {
174 info[MISCREG_UNVERIFIABLE] = v;
177 chain warnNotFail(bool v = true) const {
178 info[MISCREG_WARN_NOT_FAIL] = v;
181 chain mutex(bool v = true) const {
182 info[MISCREG_MUTEX] = v;
185 chain banked(bool v = true) const {
186 info[MISCREG_BANKED] = v;
189 chain banked64(bool v = true) const {
190 info[MISCREG_BANKED64] = v;
193 chain bankedChild(bool v = true) const {
194 info[MISCREG_BANKED_CHILD] = v;
197 chain userNonSecureRead(bool v = true) const {
198 info[MISCREG_USR_NS_RD] = v;
201 chain userNonSecureWrite(bool v = true) const {
202 info[MISCREG_USR_NS_WR] = v;
205 chain userSecureRead(bool v = true) const {
206 info[MISCREG_USR_S_RD] = v;
209 chain userSecureWrite(bool v = true) const {
210 info[MISCREG_USR_S_WR] = v;
213 chain user(bool v = true) const {
214 userNonSecureRead(v);
215 userNonSecureWrite(v);
220 chain privNonSecureRead(bool v = true) const {
221 info[MISCREG_PRI_NS_RD] = v;
224 chain privNonSecureWrite(bool v = true) const {
225 info[MISCREG_PRI_NS_WR] = v;
228 chain privNonSecure(bool v = true) const {
229 privNonSecureRead(v);
230 privNonSecureWrite(v);
233 chain privSecureRead(bool v = true) const {
234 info[MISCREG_PRI_S_RD] = v;
237 chain privSecureWrite(bool v = true) const {
238 info[MISCREG_PRI_S_WR] = v;
241 chain privSecure(bool v = true) const {
246 chain priv(bool v = true) const {
251 chain privRead(bool v = true) const {
253 privNonSecureRead(v);
256 chain hypE2HSecureRead(bool v = true) const {
257 info[MISCREG_HYP_E2H_S_RD] = v;
260 chain hypE2HNonSecureRead(bool v = true) const {
261 info[MISCREG_HYP_E2H_NS_RD] = v;
264 chain hypE2HRead(bool v = true) const {
266 hypE2HNonSecureRead(v);
269 chain hypE2HSecureWrite(bool v = true) const {
270 info[MISCREG_HYP_E2H_S_WR] = v;
273 chain hypE2HNonSecureWrite(bool v = true) const {
274 info[MISCREG_HYP_E2H_NS_WR] = v;
277 chain hypE2HWrite(bool v = true) const {
278 hypE2HSecureWrite(v);
279 hypE2HNonSecureWrite(v);
282 chain hypE2H(bool v = true) const {
287 chain hypSecureRead(bool v = true) const {
288 info[MISCREG_HYP_S_RD] = v;
291 chain hypNonSecureRead(bool v = true) const {
292 info[MISCREG_HYP_NS_RD] = v;
295 chain hypRead(bool v = true) const {
301 chain hypSecureWrite(bool v = true) const {
302 info[MISCREG_HYP_S_WR] = v;
305 chain hypNonSecureWrite(bool v = true) const {
306 info[MISCREG_HYP_NS_WR] = v;
309 chain hypWrite(bool v = true) const {
312 hypNonSecureWrite(v);
315 chain hypSecure(bool v = true) const {
317 hypE2HSecureWrite(v);
322 chain hyp(bool v = true) const {
327 chain monE2HRead(bool v = true) const {
328 info[MISCREG_MON_E2H_RD] = v;
331 chain monE2HWrite(bool v = true) const {
332 info[MISCREG_MON_E2H_WR] = v;
335 chain monE2H(bool v = true) const {
340 chain monSecureRead(bool v = true) const {
342 info[MISCREG_MON_NS0_RD] = v;
345 chain monSecureWrite(bool v = true) const {
347 info[MISCREG_MON_NS0_WR] = v;
350 chain monNonSecureRead(bool v = true) const {
352 info[MISCREG_MON_NS1_RD] = v;
355 chain monNonSecureWrite(bool v = true) const {
357 info[MISCREG_MON_NS1_WR] = v;
360 chain mon(bool v = true) const {
364 monNonSecureWrite(v);
367 chain monSecure(bool v = true) const {
372 chain monNonSecure(bool v = true) const {
374 monNonSecureWrite(v);
377 chain allPrivileges(bool v = true) const {
378 userNonSecureRead(v);
379 userNonSecureWrite(v);
382 privNonSecureRead(v);
383 privNonSecureWrite(v);
391 monNonSecureWrite(v);
394 chain nonSecure(bool v = true) const {
395 userNonSecureRead(v);
396 userNonSecureWrite(v);
397 privNonSecureRead(v);
398 privNonSecureWrite(v);
402 monNonSecureWrite(v);
405 chain secure(bool v = true) const {
414 chain reads(bool v) const {
415 userNonSecureRead(v);
417 privNonSecureRead(v);
424 chain writes(bool v) const {
425 userNonSecureWrite(v);
427 privNonSecureWrite(v);
431 monNonSecureWrite(v);
434 chain exceptUserMode() const {
438 chain highest(ArmSystem *const sys) const;
439 MiscRegLUTEntryInitializer(struct MiscRegLUTEntry &e,
440 std::bitset<NUM_MISCREG_INFOS> &i)
444 // force unimplemented registers to be thusly declared
449 const MiscRegLUTEntryInitializer InitReg(uint32_t reg) {
450 return MiscRegLUTEntryInitializer(lookUpMiscReg[reg],
454 void initializeMiscRegMetadata();
456 RegVal miscRegs[NumMiscRegs];
457 const IntRegIndex *intRegMap;
460 updateRegMap(CPSR cpsr)
462 if (cpsr.width == 0) {
463 intRegMap = IntReg64Map;
468 intRegMap = IntRegUsrMap;
471 intRegMap = IntRegFiqMap;
474 intRegMap = IntRegIrqMap;
477 intRegMap = IntRegSvcMap;
480 intRegMap = IntRegMonMap;
483 intRegMap = IntRegAbtMap;
486 intRegMap = IntRegHypMap;
489 intRegMap = IntRegUndMap;
492 panic("Unrecognized mode setting in CPSR.\n");
497 BaseISADevice &getGenericTimer();
498 BaseISADevice &getGICv3CPUInterface();
501 void assert32() { assert(((CPSR)readMiscReg(MISCREG_CPSR)).width); }
502 void assert64() { assert(!((CPSR)readMiscReg(MISCREG_CPSR)).width); }
508 void clear32(const ArmISAParams &p, const SCTLR &sctlr_rst);
509 void clear64(const ArmISAParams &p);
510 void initID32(const ArmISAParams &p);
511 void initID64(const ArmISAParams &p);
513 void addressTranslation(TLB::ArmTranslationType tran_type,
514 BaseTLB::Mode mode, Request::Flags flags, RegVal val);
515 void addressTranslation64(TLB::ArmTranslationType tran_type,
516 BaseTLB::Mode mode, Request::Flags flags, RegVal val);
526 getSelfDebug(ThreadContext *tc)
528 auto *arm_isa = static_cast<ArmISA::ISA *>(tc->getIsaPtr());
529 return arm_isa->getSelfDebug();
532 RegVal readMiscRegNoEffect(int misc_reg) const;
533 RegVal readMiscReg(int misc_reg);
534 void setMiscRegNoEffect(int misc_reg, RegVal val);
535 void setMiscReg(int misc_reg, RegVal val);
538 flattenRegId(const RegId& regId) const
540 switch (regId.classValue()) {
542 return RegId(IntRegClass, flattenIntIndex(regId.index()));
544 return RegId(FloatRegClass, flattenFloatIndex(regId.index()));
546 return RegId(VecRegClass, flattenVecIndex(regId.index()));
548 return RegId(VecElemClass, flattenVecElemIndex(regId.index()),
550 case VecPredRegClass:
551 return RegId(VecPredRegClass,
552 flattenVecPredIndex(regId.index()));
554 return RegId(CCRegClass, flattenCCIndex(regId.index()));
556 return RegId(MiscRegClass, flattenMiscIndex(regId.index()));
562 flattenIntIndex(int reg) const
565 if (reg < NUM_ARCH_INTREGS) {
566 return intRegMap[reg];
567 } else if (reg < NUM_INTREGS) {
569 } else if (reg == INTREG_SPX) {
570 CPSR cpsr = miscRegs[MISCREG_CPSR];
571 ExceptionLevel el = opModeToEL(
572 (OperatingMode) (uint8_t) cpsr.mode);
573 if (!cpsr.sp && el != EL0)
585 panic("Invalid exception level");
586 return 0; // Never happens.
589 return flattenIntRegModeIndex(reg);
594 flattenFloatIndex(int reg) const
601 flattenVecIndex(int reg) const
608 flattenVecElemIndex(int reg) const
615 flattenVecPredIndex(int reg) const
622 flattenCCIndex(int reg) const
629 flattenMiscIndex(int reg) const
634 if (reg == MISCREG_SPSR) {
635 CPSR cpsr = miscRegs[MISCREG_CPSR];
638 warn("User mode does not have SPSR\n");
639 flat_idx = MISCREG_SPSR;
643 flat_idx = MISCREG_SPSR_EL1;
647 flat_idx = MISCREG_SPSR_EL2;
651 flat_idx = MISCREG_SPSR_EL3;
654 warn("User mode does not have SPSR\n");
655 flat_idx = MISCREG_SPSR;
658 flat_idx = MISCREG_SPSR_FIQ;
661 flat_idx = MISCREG_SPSR_IRQ;
664 flat_idx = MISCREG_SPSR_SVC;
667 flat_idx = MISCREG_SPSR_MON;
670 flat_idx = MISCREG_SPSR_ABT;
673 flat_idx = MISCREG_SPSR_HYP;
676 flat_idx = MISCREG_SPSR_UND;
679 warn("Trying to access SPSR in an invalid mode: %d\n",
681 flat_idx = MISCREG_SPSR;
684 } else if (miscRegInfo[reg][MISCREG_MUTEX]) {
685 // Mutually exclusive CP15 register
687 case MISCREG_PRRR_MAIR0:
688 case MISCREG_PRRR_MAIR0_NS:
689 case MISCREG_PRRR_MAIR0_S:
691 TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
692 // If the muxed reg has been flattened, work out the
693 // offset and apply it to the unmuxed reg
694 int idxOffset = reg - MISCREG_PRRR_MAIR0;
696 flat_idx = flattenMiscIndex(MISCREG_MAIR0 +
699 flat_idx = flattenMiscIndex(MISCREG_PRRR +
703 case MISCREG_NMRR_MAIR1:
704 case MISCREG_NMRR_MAIR1_NS:
705 case MISCREG_NMRR_MAIR1_S:
707 TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
708 // If the muxed reg has been flattened, work out the
709 // offset and apply it to the unmuxed reg
710 int idxOffset = reg - MISCREG_NMRR_MAIR1;
712 flat_idx = flattenMiscIndex(MISCREG_MAIR1 +
715 flat_idx = flattenMiscIndex(MISCREG_NMRR +
719 case MISCREG_PMXEVTYPER_PMCCFILTR:
721 PMSELR pmselr = miscRegs[MISCREG_PMSELR];
722 if (pmselr.sel == 31)
723 flat_idx = flattenMiscIndex(MISCREG_PMCCFILTR);
725 flat_idx = flattenMiscIndex(MISCREG_PMXEVTYPER);
729 panic("Unrecognized misc. register.\n");
733 if (miscRegInfo[reg][MISCREG_BANKED]) {
734 bool secureReg = haveSecurity && !highestELIs64 &&
735 inSecureState(miscRegs[MISCREG_SCR],
736 miscRegs[MISCREG_CPSR]);
737 flat_idx += secureReg ? 2 : 1;
739 flat_idx = snsBankedIndex64((MiscRegIndex)reg,
740 !inSecureState(miscRegs[MISCREG_SCR],
741 miscRegs[MISCREG_CPSR]));
748 * Returns the enconcing equivalent when VHE is implemented and
749 * HCR_EL2.E2H is enabled and executing at EL2
752 redirectRegVHE(ThreadContext * tc, int misc_reg)
754 const HCR hcr = readMiscRegNoEffect(MISCREG_HCR_EL2);
755 if (hcr.e2h == 0x0 || currEL(tc) != EL2)
757 SCR scr = readMiscRegNoEffect(MISCREG_SCR_EL3);
758 bool sec_el2 = scr.eel2 && haveSecEL2;
760 case MISCREG_SPSR_EL1:
761 return MISCREG_SPSR_EL2;
762 case MISCREG_ELR_EL1:
763 return MISCREG_ELR_EL2;
764 case MISCREG_SCTLR_EL1:
765 return MISCREG_SCTLR_EL2;
766 case MISCREG_CPACR_EL1:
767 return MISCREG_CPTR_EL2;
769 // return MISCREG_TRFCR_EL2;
770 case MISCREG_TTBR0_EL1:
771 return MISCREG_TTBR0_EL2;
772 case MISCREG_TTBR1_EL1:
773 return MISCREG_TTBR1_EL2;
774 case MISCREG_TCR_EL1:
775 return MISCREG_TCR_EL2;
776 case MISCREG_AFSR0_EL1:
777 return MISCREG_AFSR0_EL2;
778 case MISCREG_AFSR1_EL1:
779 return MISCREG_AFSR1_EL2;
780 case MISCREG_ESR_EL1:
781 return MISCREG_ESR_EL2;
782 case MISCREG_FAR_EL1:
783 return MISCREG_FAR_EL2;
784 case MISCREG_MAIR_EL1:
785 return MISCREG_MAIR_EL2;
786 case MISCREG_AMAIR_EL1:
787 return MISCREG_AMAIR_EL2;
788 case MISCREG_VBAR_EL1:
789 return MISCREG_VBAR_EL2;
790 case MISCREG_CONTEXTIDR_EL1:
791 return MISCREG_CONTEXTIDR_EL2;
792 case MISCREG_CNTKCTL_EL1:
793 return MISCREG_CNTHCTL_EL2;
794 case MISCREG_CNTP_TVAL_EL0:
795 return sec_el2? MISCREG_CNTHPS_TVAL_EL2:
796 MISCREG_CNTHP_TVAL_EL2;
797 case MISCREG_CNTP_CTL_EL0:
798 return sec_el2? MISCREG_CNTHPS_CTL_EL2:
799 MISCREG_CNTHP_CTL_EL2;
800 case MISCREG_CNTP_CVAL_EL0:
801 return sec_el2? MISCREG_CNTHPS_CVAL_EL2:
802 MISCREG_CNTHP_CVAL_EL2;
803 case MISCREG_CNTV_TVAL_EL0:
804 return sec_el2? MISCREG_CNTHVS_TVAL_EL2:
805 MISCREG_CNTHV_TVAL_EL2;
806 case MISCREG_CNTV_CTL_EL0:
807 return sec_el2? MISCREG_CNTHVS_CTL_EL2:
808 MISCREG_CNTHV_CTL_EL2;
809 case MISCREG_CNTV_CVAL_EL0:
810 return sec_el2? MISCREG_CNTHVS_CVAL_EL2:
811 MISCREG_CNTHV_CVAL_EL2;
815 /*should not be accessible */
820 snsBankedIndex64(MiscRegIndex reg, bool ns) const
822 int reg_as_int = static_cast<int>(reg);
823 if (miscRegInfo[reg][MISCREG_BANKED64]) {
824 reg_as_int += (haveSecurity && !ns) ? 2 : 1;
829 std::pair<int,int> getMiscIndices(int misc_reg) const
831 // Note: indexes of AArch64 registers are left unchanged
832 int flat_idx = flattenMiscIndex(misc_reg);
834 if (lookUpMiscReg[flat_idx].lower == 0) {
835 return std::make_pair(flat_idx, 0);
838 // do additional S/NS flattenings if mapped to NS while in S
839 bool S = haveSecurity && !highestELIs64 &&
840 inSecureState(miscRegs[MISCREG_SCR],
841 miscRegs[MISCREG_CPSR]);
842 int lower = lookUpMiscReg[flat_idx].lower;
843 int upper = lookUpMiscReg[flat_idx].upper;
844 // upper == 0, which is CPSR, is not MISCREG_BANKED_CHILD (no-op)
845 lower += S && miscRegInfo[lower][MISCREG_BANKED_CHILD];
846 upper += S && miscRegInfo[upper][MISCREG_BANKED_CHILD];
847 return std::make_pair(lower, upper);
850 unsigned getCurSveVecLenInBits() const;
852 unsigned getCurSveVecLenInBitsAtReset() const { return sveVL * 128; }
854 static void zeroSveVecRegUpperPart(VecRegContainer &vc,
857 void serialize(CheckpointOut &cp) const override;
858 void unserialize(CheckpointIn &cp) override;
860 void startup() override;
862 void setupThreadContext();
864 void takeOverFrom(ThreadContext *new_tc,
865 ThreadContext *old_tc) override;
867 Enums::DecoderFlavor decoderFlavor() const { return _decoderFlavor; }
869 /** Returns true if the ISA has a GICv3 cpu interface */
870 bool haveGICv3CpuIfc() const
872 // gicv3CpuInterface is initialized at startup time, hence
873 // trying to read its value before the startup stage will lead
875 assert(afterStartup);
876 return gicv3CpuInterface != nullptr;
879 Enums::VecRegRenameMode
880 vecRegRenameMode() const
882 return _vecRegRenameMode;
885 typedef ArmISAParams Params;
887 const Params ¶ms() const;
889 ISA(const Params &p);
894 struct RenameMode<ArmISA::ISA>
896 static Enums::VecRegRenameMode
897 init(const BaseISA* isa)
899 auto arm_isa = dynamic_cast<const ArmISA::ISA *>(isa);
901 return arm_isa->vecRegRenameMode();
904 static Enums::VecRegRenameMode
905 mode(const ArmISA::PCState& pc)
915 equalsInit(const BaseISA* isa1, const BaseISA* isa2)
917 return init(isa1) == init(isa2);