2 * Copyright (c) 2010-2016, 2019 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 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #ifndef __ARCH_ARM_TABLE_WALKER_HH__
39 #define __ARCH_ARM_TABLE_WALKER_HH__
43 #include "arch/arm/faults.hh"
44 #include "arch/arm/miscregs.hh"
45 #include "arch/arm/system.hh"
46 #include "arch/arm/tlb.hh"
47 #include "mem/request.hh"
48 #include "params/ArmTableWalker.hh"
49 #include "sim/clocked_object.hh"
50 #include "sim/eventq.hh"
61 class TableWalker : public ClockedObject
66 class DescriptorBase {
68 DescriptorBase() : lookupLevel(L0) {}
70 /** Current lookup level for this descriptor */
71 LookupLevel lookupLevel;
73 virtual Addr pfn() const = 0;
74 virtual TlbEntry::DomainType domain() const = 0;
75 virtual bool xn() const = 0;
76 virtual uint8_t ap() const = 0;
77 virtual bool global(WalkerState *currState) const = 0;
78 virtual uint8_t offsetBits() const = 0;
79 virtual bool secure(bool have_security, WalkerState *currState) const = 0;
80 virtual std::string dbgHeader() const = 0;
81 virtual uint64_t getRawData() const = 0;
82 virtual uint8_t texcb() const
84 panic("texcb() not implemented for this class\n");
86 virtual bool shareable() const
88 panic("shareable() not implemented for this class\n");
92 class L1Descriptor : public DescriptorBase {
94 /** Type of page table entry ARM DDI 0406B: B3-8*/
102 /** The raw bits of the entry */
105 /** This entry has been modified (access flag set) and needs to be
106 * written back to memory */
110 L1Descriptor() : data(0), _dirty(false)
115 virtual uint64_t getRawData() const
120 virtual std::string dbgHeader() const
122 return "Inserting Section Descriptor into TLB\n";
125 virtual uint8_t offsetBits() const
130 EntryType type() const
132 return (EntryType)(data & 0x3);
135 /** Is the page a Supersection (16MB)?*/
136 bool supersection() const
138 return bits(data, 18);
141 /** Return the physcal address of the entry, bits in position*/
145 panic("Super sections not implemented\n");
146 return mbits(data, 31, 20);
148 /** Return the physcal address of the entry, bits in position*/
149 Addr paddr(Addr va) const
152 panic("Super sections not implemented\n");
153 return mbits(data, 31, 20) | mbits(va, 19, 0);
157 /** Return the physical frame, bits shifted right */
161 panic("Super sections not implemented\n");
162 return bits(data, 31, 20);
165 /** Is the translation global (no asid used)? */
166 bool global(WalkerState *currState) const
168 return !bits(data, 17);
171 /** Is the translation not allow execution? */
174 return bits(data, 4);
177 /** Three bit access protection flags */
180 return (bits(data, 15) << 2) | bits(data, 11, 10);
183 /** Domain Client/Manager: ARM DDI 0406B: B3-31 */
184 TlbEntry::DomainType domain() const
186 return static_cast<TlbEntry::DomainType>(bits(data, 8, 5));
189 /** Address of L2 descriptor if it exists */
192 return mbits(data, 31, 10);
195 /** Memory region attributes: ARM DDI 0406B: B3-32.
196 * These bits are largly ignored by M5 and only used to
197 * provide the illusion that the memory system cares about
198 * anything but cachable vs. uncachable.
200 uint8_t texcb() const
202 return bits(data, 2) | bits(data, 3) << 1 | bits(data, 14, 12) << 2;
205 /** If the section is shareable. See texcb() comment. */
206 bool shareable() const
208 return bits(data, 16);
211 /** Set access flag that this entry has been touched. Mark
212 * the entry as requiring a writeback, in the future.
220 /** This entry needs to be written back to memory */
227 * Returns true if this entry targets the secure physical address
230 bool secure(bool have_security, WalkerState *currState) const
233 if (type() == PageTable)
234 return !bits(data, 3);
236 return !bits(data, 19);
242 /** Level 2 page table descriptor */
243 class L2Descriptor : public DescriptorBase {
245 /** The raw bits of the entry. */
247 L1Descriptor *l1Parent;
249 /** This entry has been modified (access flag set) and needs to be
250 * written back to memory */
254 L2Descriptor() : data(0), l1Parent(nullptr), _dirty(false)
259 L2Descriptor(L1Descriptor &parent) : data(0), l1Parent(&parent),
265 virtual uint64_t getRawData() const
270 virtual std::string dbgHeader() const
272 return "Inserting L2 Descriptor into TLB\n";
275 virtual TlbEntry::DomainType domain() const
277 return l1Parent->domain();
280 bool secure(bool have_security, WalkerState *currState) const
282 return l1Parent->secure(have_security, currState);
285 virtual uint8_t offsetBits() const
287 return large() ? 16 : 12;
290 /** Is the entry invalid */
293 return bits(data, 1, 0) == 0;
296 /** What is the size of the mapping? */
299 return bits(data, 1) == 0;
302 /** Is execution allowed on this mapping? */
305 return large() ? bits(data, 15) : bits(data, 0);
308 /** Is the translation global (no asid used)? */
309 bool global(WalkerState *currState) const
311 return !bits(data, 11);
314 /** Three bit access protection flags */
317 return bits(data, 5, 4) | (bits(data, 9) << 2);
320 /** Memory region attributes: ARM DDI 0406B: B3-32 */
321 uint8_t texcb() const
324 (bits(data, 2) | (bits(data, 3) << 1) | (bits(data, 14, 12) << 2)) :
325 (bits(data, 2) | (bits(data, 3) << 1) | (bits(data, 8, 6) << 2));
328 /** Return the physical frame, bits shifted right */
331 return large() ? bits(data, 31, 16) : bits(data, 31, 12);
334 /** Return complete physical address given a VA */
335 Addr paddr(Addr va) const
338 return mbits(data, 31, 16) | mbits(va, 15, 0);
340 return mbits(data, 31, 12) | mbits(va, 11, 0);
343 /** If the section is shareable. See texcb() comment. */
344 bool shareable() const
346 return bits(data, 10);
349 /** Set access flag that this entry has been touched. Mark
350 * the entry as requiring a writeback, in the future.
358 /** This entry needs to be written back to memory */
366 // Granule sizes for AArch64 long descriptors
374 /** Long-descriptor format (LPAE) */
375 class LongDescriptor : public DescriptorBase {
377 /** Descriptor type */
385 LongDescriptor() : data(0), _dirty(false) {}
387 /** The raw bits of the entry */
390 /** This entry has been modified (access flag set) and needs to be
391 * written back to memory */
394 virtual uint64_t getRawData() const
399 virtual std::string dbgHeader() const
401 if (type() == LongDescriptor::Page) {
402 assert(lookupLevel == L3);
403 return "Inserting Page descriptor into TLB\n";
405 assert(lookupLevel < L3);
406 return "Inserting Block descriptor into TLB\n";
411 * Returns true if this entry targets the secure physical address
414 bool secure(bool have_security, WalkerState *currState) const
416 assert(type() == Block || type() == Page);
417 return have_security && (currState->secureLookup && !bits(data, 5));
420 /** True if the current lookup is performed in AArch64 state */
423 /** Width of the granule size in bits */
426 /** Return the descriptor type */
427 EntryType type() const
429 switch (bits(data, 1, 0)) {
431 // In AArch64 blocks are not allowed at L0 for the 4 KB granule
432 // and at L1 for 16/64 KB granules
433 if (grainSize > Grain4KB)
434 return lookupLevel == L2 ? Block : Invalid;
435 return lookupLevel == L0 || lookupLevel == L3 ? Invalid : Block;
437 return lookupLevel == L3 ? Page : Table;
443 /** Return the bit width of the page/block offset */
444 uint8_t offsetBits() const
446 if (type() == Block) {
449 return lookupLevel == L1 ? 30 /* 1 GB */
452 return 25 /* 32 MB */;
454 return 29 /* 512 MB */;
456 panic("Invalid AArch64 VM granule size\n");
458 } else if (type() == Page) {
463 return grainSize; /* enum -> uint okay */
465 panic("Invalid AArch64 VM granule size\n");
468 panic("AArch64 page table entry must be block or page\n");
472 /** Return the physical frame, bits shifted right */
476 return bits(data, 47, offsetBits());
477 return bits(data, 39, offsetBits());
480 /** Return the complete physical address given a VA */
481 Addr paddr(Addr va) const
483 int n = offsetBits();
485 return mbits(data, 47, n) | mbits(va, n - 1, 0);
486 return mbits(data, 39, n) | mbits(va, n - 1, 0);
489 /** Return the physical address of the entry */
493 return mbits(data, 47, offsetBits());
494 return mbits(data, 39, offsetBits());
497 /** Return the address of the next page table */
498 Addr nextTableAddr() const
500 assert(type() == Table);
502 return mbits(data, 47, grainSize);
504 return mbits(data, 39, 12);
507 /** Return the address of the next descriptor */
508 Addr nextDescAddr(Addr va) const
510 assert(type() == Table);
513 int stride = grainSize - 3;
514 int va_lo = stride * (3 - (lookupLevel + 1)) + grainSize;
515 int va_hi = va_lo + stride - 1;
516 pa = nextTableAddr() | (bits(va, va_hi, va_lo) << 3);
518 if (lookupLevel == L1)
519 pa = nextTableAddr() | (bits(va, 29, 21) << 3);
520 else // lookupLevel == L2
521 pa = nextTableAddr() | (bits(va, 20, 12) << 3);
526 /** Is execution allowed on this mapping? */
529 assert(type() == Block || type() == Page);
530 return bits(data, 54);
533 /** Is privileged execution allowed on this mapping? (LPAE only) */
536 assert(type() == Block || type() == Page);
537 return bits(data, 53);
540 /** Contiguous hint bit. */
541 bool contiguousHint() const
543 assert(type() == Block || type() == Page);
544 return bits(data, 52);
547 /** Is the translation global (no asid used)? */
548 bool global(WalkerState *currState) const
550 assert(currState && (type() == Block || type() == Page));
551 if (!currState->aarch64 && (currState->isSecure &&
552 !currState->secureLookup)) {
553 return false; // ARM ARM issue C B3.6.3
554 } else if (currState->aarch64) {
555 if (currState->el == EL2 || currState->el == EL3) {
556 return true; // By default translations are treated as global
557 // in AArch64 EL2 and EL3
558 } else if (currState->isSecure && !currState->secureLookup) {
562 return !bits(data, 11);
565 /** Returns true if the access flag (AF) is set. */
568 assert(type() == Block || type() == Page);
569 return bits(data, 10);
572 /** 2-bit shareability field */
575 assert(type() == Block || type() == Page);
576 return bits(data, 9, 8);
579 /** 2-bit access protection flags */
582 assert(type() == Block || type() == Page);
583 // Long descriptors only support the AP[2:1] scheme
584 return bits(data, 7, 6);
587 /** Read/write access protection flag */
590 assert(type() == Block || type() == Page);
591 return !bits(data, 7);
594 /** User/privileged level access protection flag */
597 assert(type() == Block || type() == Page);
598 return bits(data, 6);
601 /** Return the AP bits as compatible with the AP[2:0] format. Utility
602 * function used to simplify the code in the TLB for performing
603 * permission checks. */
604 static uint8_t ap(bool rw, bool user)
606 return ((!rw) << 2) | (user << 1);
609 TlbEntry::DomainType domain() const
611 // Long-desc. format only supports Client domain
612 assert(type() == Block || type() == Page);
613 return TlbEntry::DomainType::Client;
616 /** Attribute index */
617 uint8_t attrIndx() const
619 assert(type() == Block || type() == Page);
620 return bits(data, 4, 2);
623 /** Memory attributes, only used by stage 2 translations */
624 uint8_t memAttr() const
626 assert(type() == Block || type() == Page);
627 return bits(data, 5, 2);
630 /** Set access flag that this entry has been touched. Mark the entry as
631 * requiring a writeback, in the future. */
638 /** This entry needs to be written back to memory */
644 /** Whether the subsequent levels of lookup are secure */
645 bool secureTable() const
647 assert(type() == Table);
648 return !bits(data, 63);
651 /** Two bit access protection flags for subsequent levels of lookup */
652 uint8_t apTable() const
654 assert(type() == Table);
655 return bits(data, 62, 61);
658 /** R/W protection flag for subsequent levels of lookup */
659 uint8_t rwTable() const
661 assert(type() == Table);
662 return !bits(data, 62);
665 /** User/privileged mode protection flag for subsequent levels of
667 uint8_t userTable() const
669 assert(type() == Table);
670 return !bits(data, 61);
673 /** Is execution allowed on subsequent lookup levels? */
676 assert(type() == Table);
677 return bits(data, 60);
680 /** Is privileged execution allowed on subsequent lookup levels? */
681 bool pxnTable() const
683 assert(type() == Table);
684 return bits(data, 59);
691 /** Thread context that we're doing the walk for */
694 /** If the access is performed in AArch64 state */
697 /** Current exception level */
700 /** Current physical address range in bits */
703 /** Request that is currently being serviced */
706 /** ASID that we're servicing the request under */
711 /** Translation state for delayed requests */
712 TLB::Translation *transState;
714 /** The fault that we are going to return */
717 /** The virtual address that is being translated with tagging removed.*/
720 /** The virtual address that is being translated */
723 /** Cached copy of the sctlr as it existed when translation began */
726 /** Cached copy of the scr as it existed when translation began */
729 /** Cached copy of the cpsr as it existed when translation began */
732 /** Cached copy of ttbcr/tcr as it existed when translation began */
734 TTBCR ttbcr; // AArch32 translations
735 TCR tcr; // AArch64 translations
738 /** Cached copy of the htcr as it existed when translation began. */
741 /** Cached copy of the htcr as it existed when translation began. */
744 /** Cached copy of the vtcr as it existed when translation began. */
747 /** If the access is a write */
750 /** If the access is a fetch (for execution, and no-exec) must be checked?*/
753 /** If the access comes from the secure state. */
756 /** True if table walks are uncacheable (for table descriptors) */
759 /** Helper variables used to implement hierarchical access permissions
760 * when the long-desc. format is used (LPAE only) */
767 /** Hierarchical access permission disable */
770 /** Flag indicating if a second stage of lookup is required */
773 /** A pointer to the stage 2 translation that's in progress */
774 TLB::Translation *stage2Tran;
776 /** If the mode is timing or atomic */
779 /** If the atomic mode should be functional */
782 /** Save mode for use in delayed response */
785 /** The translation type that has been requested */
786 TLB::ArmTranslationType tranType;
788 /** Short-format descriptors */
792 /** Long-format descriptor (LPAE and AArch64) */
793 LongDescriptor longDesc;
795 /** Whether the response is delayed in timing mode due to additional
799 TableWalker *tableWalker;
801 /** Timestamp for calculating elapsed time in service (for stats) */
804 /** Page entries walked during service (for stats) */
807 void doL1Descriptor();
808 void doL2Descriptor();
810 void doLongDescriptor();
814 std::string name() const { return tableWalker->name(); }
819 /** Queues of requests for all the different lookup levels */
820 std::list<WalkerState *> stateQueues[MAX_LOOKUP_LEVELS];
822 /** Queue of requests that have passed are waiting because the walker is
824 std::list<WalkerState *> pendingQueue;
826 /** The MMU to forward second stage look upts to */
827 Stage2MMU *stage2Mmu;
829 /** Port shared by the two table walkers. */
832 /** Master id assigned by the MMU. */
835 /** Indicates whether this table walker is part of the stage 2 mmu */
838 /** TLB that is initiating these table walks */
841 /** Cached copy of the sctlr as it existed when translation began */
844 WalkerState *currState;
846 /** If a timing translation is currently in progress */
849 /** The number of walks belonging to squashed instructions that can be
850 * removed from the pendingQueue per cycle. */
851 unsigned numSquashable;
853 /** Cached copies of system-level properties */
856 bool _haveVirtualization;
857 uint8_t physAddrRange;
858 bool _haveLargeAsid64;
861 Stats::Scalar statWalks;
862 Stats::Scalar statWalksShortDescriptor;
863 Stats::Scalar statWalksLongDescriptor;
864 Stats::Vector statWalksShortTerminatedAtLevel;
865 Stats::Vector statWalksLongTerminatedAtLevel;
866 Stats::Scalar statSquashedBefore;
867 Stats::Scalar statSquashedAfter;
868 Stats::Histogram statWalkWaitTime;
869 Stats::Histogram statWalkServiceTime;
870 Stats::Histogram statPendingWalks; // essentially "L" of queueing theory
871 Stats::Vector statPageSizes;
872 Stats::Vector2d statRequestOrigin;
874 mutable unsigned pendingReqs;
875 mutable Tick pendingChangeTick;
877 static const unsigned REQUESTED = 0;
878 static const unsigned COMPLETED = 1;
881 typedef ArmTableWalkerParams Params;
882 TableWalker(const Params *p);
883 virtual ~TableWalker();
888 return dynamic_cast<const Params *>(_params);
891 void init() override;
893 bool haveLPAE() const { return _haveLPAE; }
894 bool haveVirtualization() const { return _haveVirtualization; }
895 bool haveLargeAsid64() const { return _haveLargeAsid64; }
896 /** Checks if all state is cleared and if so, completes drain */
897 void completeDrain();
898 DrainState drain() override;
899 void drainResume() override;
901 Port &getPort(const std::string &if_name,
902 PortID idx=InvalidPortID) override;
904 void regStats() override;
906 Fault walk(const RequestPtr &req, ThreadContext *tc,
907 uint16_t asid, uint8_t _vmid,
908 bool _isHyp, TLB::Mode mode, TLB::Translation *_trans,
909 bool timing, bool functional, bool secure,
910 TLB::ArmTranslationType tranType, bool _stage2Req);
912 void setTlb(TLB *_tlb) { tlb = _tlb; }
913 TLB* getTlb() { return tlb; }
914 void setMMU(Stage2MMU *m, MasterID master_id);
915 void memAttrs(ThreadContext *tc, TlbEntry &te, SCTLR sctlr,
916 uint8_t texcb, bool s);
917 void memAttrsLPAE(ThreadContext *tc, TlbEntry &te,
918 LongDescriptor &lDescriptor);
919 void memAttrsAArch64(ThreadContext *tc, TlbEntry &te,
920 LongDescriptor &lDescriptor);
922 static LookupLevel toLookupLevel(uint8_t lookup_level_as_int);
926 void doL1Descriptor();
927 void doL1DescriptorWrapper();
928 EventFunctionWrapper doL1DescEvent;
930 void doL2Descriptor();
931 void doL2DescriptorWrapper();
932 EventFunctionWrapper doL2DescEvent;
934 void doLongDescriptor();
936 void doL0LongDescriptorWrapper();
937 EventFunctionWrapper doL0LongDescEvent;
938 void doL1LongDescriptorWrapper();
939 EventFunctionWrapper doL1LongDescEvent;
940 void doL2LongDescriptorWrapper();
941 EventFunctionWrapper doL2LongDescEvent;
942 void doL3LongDescriptorWrapper();
943 EventFunctionWrapper doL3LongDescEvent;
945 void doLongDescriptorWrapper(LookupLevel curr_lookup_level);
946 Event* LongDescEventByLevel[4];
948 bool fetchDescriptor(Addr descAddr, uint8_t *data, int numBytes,
949 Request::Flags flags, int queueIndex, Event *event,
950 void (TableWalker::*doDescriptor)());
952 Fault generateLongDescFault(ArmFault::FaultSource src);
954 void insertTableEntry(DescriptorBase &descriptor, bool longDescriptor);
957 Fault processWalkLPAE();
958 static unsigned adjustTableSizeAArch64(unsigned tsz);
959 /// Returns true if the address exceeds the range permitted by the
960 /// system-wide setting or by the TCR_ELx IPS/PS setting
961 static bool checkAddrSizeFaultAArch64(Addr addr, int currPhysAddrRange);
962 Fault processWalkAArch64();
963 void processWalkWrapper();
964 EventFunctionWrapper doProcessEvent;
966 void nextWalk(ThreadContext *tc);
968 void pendingChange();
970 static uint8_t pageSizeNtoStatBin(uint8_t N);
972 Fault testWalk(Addr pa, Addr size, TlbEntry::DomainType domain,
973 LookupLevel lookup_level);
976 } // namespace ArmISA
978 #endif //__ARCH_ARM_TABLE_WALKER_HH__