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;
100 /** SVE vector length in quadwords */
104 * If true, accesses to IMPLEMENTATION DEFINED registers are treated
105 * as NOP hence not causing UNDEFINED INSTRUCTION.
111 SelfDebug * selfDebug;
113 /** MiscReg metadata **/
114 struct MiscRegLUTEntry {
115 uint32_t lower; // Lower half mapped to this register
116 uint32_t upper; // Upper half mapped to this register
117 uint64_t _reset; // value taken on reset (i.e. initialization)
118 uint64_t _res0; // reserved
119 uint64_t _res1; // reserved
120 uint64_t _raz; // read as zero (fixed at 0)
121 uint64_t _rao; // read as one (fixed at 1)
125 _reset(0), _res0(0), _res1(0), _raz(0), _rao(0) {}
126 uint64_t reset() const { return _reset; }
127 uint64_t res0() const { return _res0; }
128 uint64_t res1() const { return _res1; }
129 uint64_t raz() const { return _raz; }
130 uint64_t rao() const { return _rao; }
131 // raz/rao implies writes ignored
132 uint64_t wi() const { return _raz | _rao; }
135 /** Metadata table accessible via the value of the register */
136 static std::vector<struct MiscRegLUTEntry> lookUpMiscReg;
138 class MiscRegLUTEntryInitializer {
139 struct MiscRegLUTEntry &entry;
140 std::bitset<NUM_MISCREG_INFOS> &info;
141 typedef const MiscRegLUTEntryInitializer& chain;
143 chain mapsTo(uint32_t l, uint32_t u = 0) const {
148 chain res0(uint64_t mask) const {
152 chain res1(uint64_t mask) const {
156 chain raz(uint64_t mask) const {
160 chain rao(uint64_t mask) const {
164 chain implemented(bool v = true) const {
165 info[MISCREG_IMPLEMENTED] = v;
168 chain unimplemented() const {
169 return implemented(false);
171 chain unverifiable(bool v = true) const {
172 info[MISCREG_UNVERIFIABLE] = v;
175 chain warnNotFail(bool v = true) const {
176 info[MISCREG_WARN_NOT_FAIL] = v;
179 chain mutex(bool v = true) const {
180 info[MISCREG_MUTEX] = v;
183 chain banked(bool v = true) const {
184 info[MISCREG_BANKED] = v;
187 chain banked64(bool v = true) const {
188 info[MISCREG_BANKED64] = v;
191 chain bankedChild(bool v = true) const {
192 info[MISCREG_BANKED_CHILD] = v;
195 chain userNonSecureRead(bool v = true) const {
196 info[MISCREG_USR_NS_RD] = v;
199 chain userNonSecureWrite(bool v = true) const {
200 info[MISCREG_USR_NS_WR] = v;
203 chain userSecureRead(bool v = true) const {
204 info[MISCREG_USR_S_RD] = v;
207 chain userSecureWrite(bool v = true) const {
208 info[MISCREG_USR_S_WR] = v;
211 chain user(bool v = true) const {
212 userNonSecureRead(v);
213 userNonSecureWrite(v);
218 chain privNonSecureRead(bool v = true) const {
219 info[MISCREG_PRI_NS_RD] = v;
222 chain privNonSecureWrite(bool v = true) const {
223 info[MISCREG_PRI_NS_WR] = v;
226 chain privNonSecure(bool v = true) const {
227 privNonSecureRead(v);
228 privNonSecureWrite(v);
231 chain privSecureRead(bool v = true) const {
232 info[MISCREG_PRI_S_RD] = v;
235 chain privSecureWrite(bool v = true) const {
236 info[MISCREG_PRI_S_WR] = v;
239 chain privSecure(bool v = true) const {
244 chain priv(bool v = true) const {
249 chain privRead(bool v = true) const {
251 privNonSecureRead(v);
254 chain hypE2HRead(bool v = true) const {
255 info[MISCREG_HYP_E2H_RD] = v;
258 chain hypE2HWrite(bool v = true) const {
259 info[MISCREG_HYP_E2H_WR] = v;
262 chain hypE2H(bool v = true) const {
267 chain hypRead(bool v = true) const {
269 info[MISCREG_HYP_RD] = v;
272 chain hypWrite(bool v = true) const {
274 info[MISCREG_HYP_WR] = v;
277 chain hyp(bool v = true) const {
282 chain monE2HRead(bool v = true) const {
283 info[MISCREG_MON_E2H_RD] = v;
286 chain monE2HWrite(bool v = true) const {
287 info[MISCREG_MON_E2H_WR] = v;
290 chain monE2H(bool v = true) const {
295 chain monSecureRead(bool v = true) const {
297 info[MISCREG_MON_NS0_RD] = v;
300 chain monSecureWrite(bool v = true) const {
302 info[MISCREG_MON_NS0_WR] = v;
305 chain monNonSecureRead(bool v = true) const {
307 info[MISCREG_MON_NS1_RD] = v;
310 chain monNonSecureWrite(bool v = true) const {
312 info[MISCREG_MON_NS1_WR] = v;
315 chain mon(bool v = true) const {
319 monNonSecureWrite(v);
322 chain monSecure(bool v = true) const {
327 chain monNonSecure(bool v = true) const {
329 monNonSecureWrite(v);
332 chain allPrivileges(bool v = true) const {
333 userNonSecureRead(v);
334 userNonSecureWrite(v);
337 privNonSecureRead(v);
338 privNonSecureWrite(v);
346 monNonSecureWrite(v);
349 chain nonSecure(bool v = true) const {
350 userNonSecureRead(v);
351 userNonSecureWrite(v);
352 privNonSecureRead(v);
353 privNonSecureWrite(v);
357 monNonSecureWrite(v);
360 chain secure(bool v = true) const {
369 chain reads(bool v) const {
370 userNonSecureRead(v);
372 privNonSecureRead(v);
379 chain writes(bool v) const {
380 userNonSecureWrite(v);
382 privNonSecureWrite(v);
386 monNonSecureWrite(v);
389 chain exceptUserMode() const {
393 chain highest(ArmSystem *const sys) const;
394 MiscRegLUTEntryInitializer(struct MiscRegLUTEntry &e,
395 std::bitset<NUM_MISCREG_INFOS> &i)
399 // force unimplemented registers to be thusly declared
404 const MiscRegLUTEntryInitializer InitReg(uint32_t reg) {
405 return MiscRegLUTEntryInitializer(lookUpMiscReg[reg],
409 void initializeMiscRegMetadata();
411 RegVal miscRegs[NumMiscRegs];
412 const IntRegIndex *intRegMap;
415 updateRegMap(CPSR cpsr)
417 if (cpsr.width == 0) {
418 intRegMap = IntReg64Map;
423 intRegMap = IntRegUsrMap;
426 intRegMap = IntRegFiqMap;
429 intRegMap = IntRegIrqMap;
432 intRegMap = IntRegSvcMap;
435 intRegMap = IntRegMonMap;
438 intRegMap = IntRegAbtMap;
441 intRegMap = IntRegHypMap;
444 intRegMap = IntRegUndMap;
447 panic("Unrecognized mode setting in CPSR.\n");
452 BaseISADevice &getGenericTimer();
453 BaseISADevice &getGICv3CPUInterface();
456 void assert32() { assert(((CPSR)readMiscReg(MISCREG_CPSR)).width); }
457 void assert64() { assert(!((CPSR)readMiscReg(MISCREG_CPSR)).width); }
463 void clear32(const ArmISAParams *p, const SCTLR &sctlr_rst);
464 void clear64(const ArmISAParams *p);
465 void initID32(const ArmISAParams *p);
466 void initID64(const ArmISAParams *p);
469 SelfDebug * getSelfDebug()
473 RegVal readMiscRegNoEffect(int misc_reg) const;
474 RegVal readMiscReg(int misc_reg);
475 void setMiscRegNoEffect(int misc_reg, RegVal val);
476 void setMiscReg(int misc_reg, RegVal val);
479 flattenRegId(const RegId& regId) const
481 switch (regId.classValue()) {
483 return RegId(IntRegClass, flattenIntIndex(regId.index()));
485 return RegId(FloatRegClass, flattenFloatIndex(regId.index()));
487 return RegId(VecRegClass, flattenVecIndex(regId.index()));
489 return RegId(VecElemClass, flattenVecElemIndex(regId.index()),
491 case VecPredRegClass:
492 return RegId(VecPredRegClass,
493 flattenVecPredIndex(regId.index()));
495 return RegId(CCRegClass, flattenCCIndex(regId.index()));
497 return RegId(MiscRegClass, flattenMiscIndex(regId.index()));
503 flattenIntIndex(int reg) const
506 if (reg < NUM_ARCH_INTREGS) {
507 return intRegMap[reg];
508 } else if (reg < NUM_INTREGS) {
510 } else if (reg == INTREG_SPX) {
511 CPSR cpsr = miscRegs[MISCREG_CPSR];
512 ExceptionLevel el = opModeToEL(
513 (OperatingMode) (uint8_t) cpsr.mode);
514 if (!cpsr.sp && el != EL0)
526 panic("Invalid exception level");
527 return 0; // Never happens.
530 return flattenIntRegModeIndex(reg);
535 flattenFloatIndex(int reg) const
542 flattenVecIndex(int reg) const
549 flattenVecElemIndex(int reg) const
556 flattenVecPredIndex(int reg) const
563 flattenCCIndex(int reg) const
570 flattenMiscIndex(int reg) const
575 if (reg == MISCREG_SPSR) {
576 CPSR cpsr = miscRegs[MISCREG_CPSR];
579 warn("User mode does not have SPSR\n");
580 flat_idx = MISCREG_SPSR;
584 flat_idx = MISCREG_SPSR_EL1;
588 flat_idx = MISCREG_SPSR_EL2;
592 flat_idx = MISCREG_SPSR_EL3;
595 warn("User mode does not have SPSR\n");
596 flat_idx = MISCREG_SPSR;
599 flat_idx = MISCREG_SPSR_FIQ;
602 flat_idx = MISCREG_SPSR_IRQ;
605 flat_idx = MISCREG_SPSR_SVC;
608 flat_idx = MISCREG_SPSR_MON;
611 flat_idx = MISCREG_SPSR_ABT;
614 flat_idx = MISCREG_SPSR_HYP;
617 flat_idx = MISCREG_SPSR_UND;
620 warn("Trying to access SPSR in an invalid mode: %d\n",
622 flat_idx = MISCREG_SPSR;
625 } else if (miscRegInfo[reg][MISCREG_MUTEX]) {
626 // Mutually exclusive CP15 register
628 case MISCREG_PRRR_MAIR0:
629 case MISCREG_PRRR_MAIR0_NS:
630 case MISCREG_PRRR_MAIR0_S:
632 TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
633 // If the muxed reg has been flattened, work out the
634 // offset and apply it to the unmuxed reg
635 int idxOffset = reg - MISCREG_PRRR_MAIR0;
637 flat_idx = flattenMiscIndex(MISCREG_MAIR0 +
640 flat_idx = flattenMiscIndex(MISCREG_PRRR +
644 case MISCREG_NMRR_MAIR1:
645 case MISCREG_NMRR_MAIR1_NS:
646 case MISCREG_NMRR_MAIR1_S:
648 TTBCR ttbcr = readMiscRegNoEffect(MISCREG_TTBCR);
649 // If the muxed reg has been flattened, work out the
650 // offset and apply it to the unmuxed reg
651 int idxOffset = reg - MISCREG_NMRR_MAIR1;
653 flat_idx = flattenMiscIndex(MISCREG_MAIR1 +
656 flat_idx = flattenMiscIndex(MISCREG_NMRR +
660 case MISCREG_PMXEVTYPER_PMCCFILTR:
662 PMSELR pmselr = miscRegs[MISCREG_PMSELR];
663 if (pmselr.sel == 31)
664 flat_idx = flattenMiscIndex(MISCREG_PMCCFILTR);
666 flat_idx = flattenMiscIndex(MISCREG_PMXEVTYPER);
670 panic("Unrecognized misc. register.\n");
674 if (miscRegInfo[reg][MISCREG_BANKED]) {
675 bool secureReg = haveSecurity && !highestELIs64 &&
676 inSecureState(miscRegs[MISCREG_SCR],
677 miscRegs[MISCREG_CPSR]);
678 flat_idx += secureReg ? 2 : 1;
680 flat_idx = snsBankedIndex64((MiscRegIndex)reg,
681 !inSecureState(miscRegs[MISCREG_SCR],
682 miscRegs[MISCREG_CPSR]));
689 * Returns the enconcing equivalent when VHE is implemented and
690 * HCR_EL2.E2H is enabled and executing at EL2
693 redirectRegVHE(ThreadContext * tc, int misc_reg)
695 const HCR hcr = readMiscRegNoEffect(MISCREG_HCR_EL2);
696 if (hcr.e2h == 0x0 || currEL(tc) != EL2)
698 SCR scr = readMiscRegNoEffect(MISCREG_SCR_EL3);
699 bool sec_el2 = scr.eel2 && haveSecEL2;
701 case MISCREG_SPSR_EL1:
702 return MISCREG_SPSR_EL2;
703 case MISCREG_ELR_EL1:
704 return MISCREG_ELR_EL2;
705 case MISCREG_SCTLR_EL1:
706 return MISCREG_SCTLR_EL2;
707 case MISCREG_CPACR_EL1:
708 return MISCREG_CPTR_EL2;
710 // return MISCREG_TRFCR_EL2;
711 case MISCREG_TTBR0_EL1:
712 return MISCREG_TTBR0_EL2;
713 case MISCREG_TTBR1_EL1:
714 return MISCREG_TTBR1_EL2;
715 case MISCREG_TCR_EL1:
716 return MISCREG_TCR_EL2;
717 case MISCREG_AFSR0_EL1:
718 return MISCREG_AFSR0_EL2;
719 case MISCREG_AFSR1_EL1:
720 return MISCREG_AFSR1_EL2;
721 case MISCREG_ESR_EL1:
722 return MISCREG_ESR_EL2;
723 case MISCREG_FAR_EL1:
724 return MISCREG_FAR_EL2;
725 case MISCREG_MAIR_EL1:
726 return MISCREG_MAIR_EL2;
727 case MISCREG_AMAIR_EL1:
728 return MISCREG_AMAIR_EL2;
729 case MISCREG_VBAR_EL1:
730 return MISCREG_VBAR_EL2;
731 case MISCREG_CONTEXTIDR_EL1:
732 return MISCREG_CONTEXTIDR_EL2;
733 case MISCREG_CNTKCTL_EL1:
734 return MISCREG_CNTHCTL_EL2;
735 case MISCREG_CNTP_TVAL_EL0:
736 return sec_el2? MISCREG_CNTHPS_TVAL_EL2:
737 MISCREG_CNTHP_TVAL_EL2;
738 case MISCREG_CNTP_CTL_EL0:
739 return sec_el2? MISCREG_CNTHPS_CTL_EL2:
740 MISCREG_CNTHP_CTL_EL2;
741 case MISCREG_CNTP_CVAL_EL0:
742 return sec_el2? MISCREG_CNTHPS_CVAL_EL2:
743 MISCREG_CNTHP_CVAL_EL2;
744 case MISCREG_CNTV_TVAL_EL0:
745 return sec_el2? MISCREG_CNTHVS_TVAL_EL2:
746 MISCREG_CNTHV_TVAL_EL2;
747 case MISCREG_CNTV_CTL_EL0:
748 return sec_el2? MISCREG_CNTHVS_CTL_EL2:
749 MISCREG_CNTHV_CTL_EL2;
750 case MISCREG_CNTV_CVAL_EL0:
751 return sec_el2? MISCREG_CNTHVS_CVAL_EL2:
752 MISCREG_CNTHV_CVAL_EL2;
756 /*should not be accessible */
761 snsBankedIndex64(MiscRegIndex reg, bool ns) const
763 int reg_as_int = static_cast<int>(reg);
764 if (miscRegInfo[reg][MISCREG_BANKED64]) {
765 reg_as_int += (haveSecurity && !ns) ? 2 : 1;
770 std::pair<int,int> getMiscIndices(int misc_reg) const
772 // Note: indexes of AArch64 registers are left unchanged
773 int flat_idx = flattenMiscIndex(misc_reg);
775 if (lookUpMiscReg[flat_idx].lower == 0) {
776 return std::make_pair(flat_idx, 0);
779 // do additional S/NS flattenings if mapped to NS while in S
780 bool S = haveSecurity && !highestELIs64 &&
781 inSecureState(miscRegs[MISCREG_SCR],
782 miscRegs[MISCREG_CPSR]);
783 int lower = lookUpMiscReg[flat_idx].lower;
784 int upper = lookUpMiscReg[flat_idx].upper;
785 // upper == 0, which is CPSR, is not MISCREG_BANKED_CHILD (no-op)
786 lower += S && miscRegInfo[lower][MISCREG_BANKED_CHILD];
787 upper += S && miscRegInfo[upper][MISCREG_BANKED_CHILD];
788 return std::make_pair(lower, upper);
791 unsigned getCurSveVecLenInBits() const;
793 unsigned getCurSveVecLenInBitsAtReset() const { return sveVL * 128; }
795 static void zeroSveVecRegUpperPart(VecRegContainer &vc,
799 serialize(CheckpointOut &cp) const override
801 DPRINTF(Checkpoint, "Serializing Arm Misc Registers\n");
802 SERIALIZE_ARRAY(miscRegs, NUM_PHYS_MISCREGS);
806 unserialize(CheckpointIn &cp) override
808 DPRINTF(Checkpoint, "Unserializing Arm Misc Registers\n");
809 UNSERIALIZE_ARRAY(miscRegs, NUM_PHYS_MISCREGS);
810 CPSR tmp_cpsr = miscRegs[MISCREG_CPSR];
811 updateRegMap(tmp_cpsr);
814 void startup() override;
816 void setupThreadContext();
818 void takeOverFrom(ThreadContext *new_tc,
819 ThreadContext *old_tc) override;
821 Enums::DecoderFlavor decoderFlavor() const { return _decoderFlavor; }
823 /** Returns true if the ISA has a GICv3 cpu interface */
824 bool haveGICv3CpuIfc() const
826 // gicv3CpuInterface is initialized at startup time, hence
827 // trying to read its value before the startup stage will lead
829 assert(afterStartup);
830 return gicv3CpuInterface != nullptr;
833 Enums::VecRegRenameMode
834 vecRegRenameMode() const
836 return _vecRegRenameMode;
839 typedef ArmISAParams Params;
841 const Params *params() const;
848 struct RenameMode<ArmISA::ISA>
850 static Enums::VecRegRenameMode
851 init(const BaseISA* isa)
853 auto arm_isa = dynamic_cast<const ArmISA::ISA *>(isa);
855 return arm_isa->vecRegRenameMode();
858 static Enums::VecRegRenameMode
859 mode(const ArmISA::PCState& pc)
869 equalsInit(const BaseISA* isa1, const BaseISA* isa2)
871 return init(isa1) == init(isa2);