tlb.hh revision 12406
11689SN/A/*
22326SN/A * Copyright (c) 2010-2013, 2016 ARM Limited
31689SN/A * All rights reserved
41689SN/A *
51689SN/A * The license below extends only to copyright in the software and shall
61689SN/A * not be construed as granting a license to any other intellectual
71689SN/A * property including but not limited to intellectual property relating
81689SN/A * to a hardware implementation of the functionality of the software
91689SN/A * licensed hereunder.  You may use the software subject to the license
101689SN/A * terms below provided that you ensure that this notice is replicated
111689SN/A * unmodified and in its entirety in all distributions of the software,
121689SN/A * modified or unmodified, in source code or in binary form.
131689SN/A *
141689SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
151689SN/A * All rights reserved.
161689SN/A *
171689SN/A * Redistribution and use in source and binary forms, with or without
181689SN/A * modification, are permitted provided that the following conditions are
191689SN/A * met: redistributions of source code must retain the above copyright
201689SN/A * notice, this list of conditions and the following disclaimer;
211689SN/A * redistributions in binary form must reproduce the above copyright
221689SN/A * notice, this list of conditions and the following disclaimer in the
231689SN/A * documentation and/or other materials provided with the distribution;
241689SN/A * neither the name of the copyright holders nor the names of its
251689SN/A * contributors may be used to endorse or promote products derived from
261689SN/A * this software without specific prior written permission.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311060SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321060SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341060SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351060SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361060SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
371060SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
386658Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392292SN/A *
401717SN/A * Authors: Ali Saidi
415529Snate@binkert.org */
421060SN/A
436221Snate@binkert.org#ifndef __ARCH_ARM_TLB_HH__
446221Snate@binkert.org#define __ARCH_ARM_TLB_HH__
451681SN/A
465529Snate@binkert.org
472873Sktlim@umich.edu#include "arch/arm/isa_traits.hh"
484329Sktlim@umich.edu#include "arch/arm/pagetable.hh"
494329Sktlim@umich.edu#include "arch/arm/utility.hh"
504329Sktlim@umich.edu#include "arch/arm/vtophys.hh"
512292SN/A#include "arch/generic/tlb.hh"
522292SN/A#include "base/statistics.hh"
532292SN/A#include "mem/request.hh"
542292SN/A#include "params/ArmTLB.hh"
552820Sktlim@umich.edu#include "sim/probe/pmu.hh"
562292SN/A
572820Sktlim@umich.educlass ThreadContext;
582820Sktlim@umich.edu
595529Snate@binkert.orgnamespace ArmISA {
602307SN/A
611060SN/Aclass TableWalker;
622292SN/Aclass Stage2LookUp;
632292SN/Aclass Stage2MMU;
642292SN/Aclass TLB;
651060SN/A
661060SN/Aclass TlbTestInterface
671060SN/A{
681060SN/A  public:
691060SN/A    TlbTestInterface() {}
701060SN/A    virtual ~TlbTestInterface() {}
711681SN/A
726221Snate@binkert.org    /**
736221Snate@binkert.org     * Check if a TLB translation should be forced to fail.
746221Snate@binkert.org     *
756221Snate@binkert.org     * @param req Request requiring a translation.
762292SN/A     * @param is_priv Access from a privileged mode (i.e., not EL0)
772292SN/A     * @param mode Access type
782820Sktlim@umich.edu     * @param domain Domain type
792820Sktlim@umich.edu     */
802292SN/A    virtual Fault translationCheck(RequestPtr req, bool is_priv,
812292SN/A                                   BaseTLB::Mode mode,
822820Sktlim@umich.edu                                   TlbEntry::DomainType domain) = 0;
832820Sktlim@umich.edu
842292SN/A    /**
852292SN/A     * Check if a page table walker access should be forced to fail.
862292SN/A     *
872292SN/A     * @param pa Physical address the walker is accessing
882292SN/A     * @param size Walker access size
892292SN/A     * @param va Virtual address that initiated the walk
902292SN/A     * @param is_secure Access from secure state
912292SN/A     * @param is_priv Access from a privileged mode (i.e., not EL0)
921060SN/A     * @param mode Access type
931060SN/A     * @param domain Domain type
941681SN/A     * @param lookup_level Page table walker level
951062SN/A     */
962292SN/A    virtual Fault walkCheck(Addr pa, Addr size, Addr va, bool is_secure,
971062SN/A                            Addr is_priv, BaseTLB::Mode mode,
982301SN/A                            TlbEntry::DomainType domain,
992301SN/A                            LookupLevel lookup_level) = 0;
1001062SN/A};
1012727Sktlim@umich.edu
1021062SN/Aclass TLB : public BaseTLB
1031062SN/A{
1041062SN/A  public:
1051062SN/A    enum ArmFlags {
1061062SN/A        AlignmentMask = 0x7,
1071062SN/A
1081062SN/A        AlignByte = 0x0,
1091062SN/A        AlignHalfWord = 0x1,
1101062SN/A        AlignWord = 0x2,
1111062SN/A        AlignDoubleWord = 0x3,
1121062SN/A        AlignQuadWord = 0x4,
1131062SN/A        AlignOctWord = 0x5,
1141062SN/A
1151062SN/A        AllowUnaligned = 0x8,
1161062SN/A        // Priv code operating as if it wasn't
1171062SN/A        UserMode = 0x10,
1181062SN/A        // Because zero otherwise looks like a valid setting and may be used
1191062SN/A        // accidentally, this bit must be non-zero to show it was used on
1201062SN/A        // purpose.
1211062SN/A        MustBeOne = 0x40
1221062SN/A    };
1231062SN/A
1241062SN/A    enum ArmTranslationType {
1251062SN/A        NormalTran = 0,
1261062SN/A        S1CTran = 0x1,
1271062SN/A        HypMode = 0x2,
1281062SN/A        // Secure code operating as if it wasn't (required by some Address
1291062SN/A        // Translate operations)
1301062SN/A        S1S2NsTran = 0x4,
1311062SN/A        // Address translation instructions (eg AT S1E0R_Xt) need to be handled
1321062SN/A        // in special ways during translation because they could need to act
1331062SN/A        // like a different EL than the current EL. The following flags are
1341062SN/A        // for these instructions
1351062SN/A        S1E0Tran = 0x8,
1361062SN/A        S1E1Tran = 0x10,
1371062SN/A        S1E2Tran = 0x20,
1381062SN/A        S1E3Tran = 0x40,
1391062SN/A        S12E0Tran = 0x80,
1401062SN/A        S12E1Tran = 0x100
1411062SN/A    };
1421062SN/A  protected:
1432292SN/A    TlbEntry* table;     // the Page Table
1442292SN/A    int size;            // TLB Size
1452292SN/A    bool isStage2;       // Indicates this TLB is part of the second stage MMU
1462292SN/A    bool stage2Req;      // Indicates whether a stage 2 lookup is also required
1471062SN/A    uint64_t _attr;      // Memory attributes for last accessed TLB entry
1481062SN/A    bool directToStage2; // Indicates whether all translation requests should
1491062SN/A                         // be routed directly to the stage 2 TLB
1501062SN/A
1511062SN/A    TableWalker *tableWalker;
1521062SN/A    TLB *stage2Tlb;
1531062SN/A    Stage2MMU *stage2Mmu;
1542292SN/A
1552292SN/A    TlbTestInterface *test;
1562292SN/A
1572292SN/A    // Access Stats
1582292SN/A    mutable Stats::Scalar instHits;
1592292SN/A    mutable Stats::Scalar instMisses;
1602292SN/A    mutable Stats::Scalar readHits;
1612292SN/A    mutable Stats::Scalar readMisses;
1622292SN/A    mutable Stats::Scalar writeHits;
1632292SN/A    mutable Stats::Scalar writeMisses;
1642301SN/A    mutable Stats::Scalar inserts;
1652727Sktlim@umich.edu    mutable Stats::Scalar flushTlb;
1662353SN/A    mutable Stats::Scalar flushTlbMva;
1672727Sktlim@umich.edu    mutable Stats::Scalar flushTlbMvaAsid;
1682727Sktlim@umich.edu    mutable Stats::Scalar flushTlbAsid;
1692727Sktlim@umich.edu    mutable Stats::Scalar flushedEntries;
1706221Snate@binkert.org    mutable Stats::Scalar alignFaults;
1712353SN/A    mutable Stats::Scalar prefetchFaults;
1722727Sktlim@umich.edu    mutable Stats::Scalar domainFaults;
1732727Sktlim@umich.edu    mutable Stats::Scalar permsFaults;
1742727Sktlim@umich.edu
1752727Sktlim@umich.edu    Stats::Formula readAccesses;
1762353SN/A    Stats::Formula writeAccesses;
1772727Sktlim@umich.edu    Stats::Formula instAccesses;
1782727Sktlim@umich.edu    Stats::Formula hits;
1792727Sktlim@umich.edu    Stats::Formula misses;
1806221Snate@binkert.org    Stats::Formula accesses;
1812301SN/A
1822301SN/A    /** PMU probe for TLB refills */
1832727Sktlim@umich.edu    ProbePoints::PMUUPtr ppRefills;
1842301SN/A
1852727Sktlim@umich.edu    int rangeMRU; //On lookup, only move entries ahead when outside rangeMRU
1866221Snate@binkert.org
1872301SN/A  public:
1882301SN/A    TLB(const ArmTLBParams *p);
1892727Sktlim@umich.edu    TLB(const Params *p, int _size, TableWalker *_walker);
1902301SN/A
1912727Sktlim@umich.edu    /** Lookup an entry in the TLB
1926221Snate@binkert.org     * @param vpn virtual address
1932301SN/A     * @param asn context id/address space id to use
1942301SN/A     * @param vmid The virtual machine ID used for stage 2 translation
1952727Sktlim@umich.edu     * @param secure if the lookup is secure
1962301SN/A     * @param hyp if the lookup is done from hyp mode
1972727Sktlim@umich.edu     * @param functional if the lookup should modify state
1986221Snate@binkert.org     * @param ignore_asn if on lookup asn should be ignored
1992301SN/A     * @return pointer to TLB entry if it exists
2002301SN/A     */
2012727Sktlim@umich.edu    TlbEntry *lookup(Addr vpn, uint16_t asn, uint8_t vmid, bool hyp,
2022301SN/A                     bool secure, bool functional,
2032301SN/A                     bool ignore_asn, uint8_t target_el);
2042301SN/A
2052301SN/A    virtual ~TLB();
2062727Sktlim@umich.edu
2072727Sktlim@umich.edu    void takeOverFrom(BaseTLB *otlb) override;
2082727Sktlim@umich.edu
2092727Sktlim@umich.edu    /// setup all the back pointers
2102727Sktlim@umich.edu    void init() override;
2112727Sktlim@umich.edu
2122727Sktlim@umich.edu    void setTestInterface(SimObject *ti);
2132727Sktlim@umich.edu
2142727Sktlim@umich.edu    TableWalker *getTableWalker() { return tableWalker; }
2152301SN/A
2162301SN/A    void setMMU(Stage2MMU *m, MasterID master_id);
2176221Snate@binkert.org
2182301SN/A    int getsize() const { return size; }
2192301SN/A
2202727Sktlim@umich.edu    void insert(Addr vaddr, TlbEntry &pte);
2212301SN/A
2222326SN/A    Fault getTE(TlbEntry **te, RequestPtr req, ThreadContext *tc, Mode mode,
2236221Snate@binkert.org                Translation *translation, bool timing, bool functional,
2242301SN/A                bool is_secure, ArmTranslationType tranType);
2252301SN/A
2262727Sktlim@umich.edu    Fault getResultTe(TlbEntry **te, RequestPtr req, ThreadContext *tc,
2272301SN/A                      Mode mode, Translation *translation, bool timing,
2282326SN/A                      bool functional, TlbEntry *mergeTe);
2296221Snate@binkert.org
2302301SN/A    Fault checkPermissions(TlbEntry *te, RequestPtr req, Mode mode);
2312301SN/A    Fault checkPermissions64(TlbEntry *te, RequestPtr req, Mode mode,
2322727Sktlim@umich.edu                             ThreadContext *tc);
2332301SN/A
2342326SN/A
2356221Snate@binkert.org    /** Reset the entire TLB
2362301SN/A     * @param secure_lookup if the operation affects the secure world
2372301SN/A     */
2382727Sktlim@umich.edu    void flushAllSecurity(bool secure_lookup, uint8_t target_el,
2392301SN/A                          bool ignore_el = false);
2402326SN/A
2416221Snate@binkert.org    /** Remove all entries in the non secure world, depending on whether they
2422301SN/A     *  were allocated in hyp mode or not
2432301SN/A     * @param hyp if the opperation affects hyp mode
2442727Sktlim@umich.edu     */
2452301SN/A    void flushAllNs(bool hyp, uint8_t target_el, bool ignore_el = false);
2462326SN/A
2472301SN/A
2482301SN/A    /** Reset the entire TLB. Used for CPU switching to prevent stale
2492727Sktlim@umich.edu     * translations after multiple switches
2502301SN/A     */
2512326SN/A    void flushAll() override
2522301SN/A    {
2532326SN/A        flushAllSecurity(false, 0, true);
2542301SN/A        flushAllSecurity(true, 0, true);
2552301SN/A    }
2562727Sktlim@umich.edu
2572301SN/A    /** Remove any entries that match both a va and asn
2582326SN/A     * @param mva virtual address to flush
2592301SN/A     * @param asn contextid/asn to flush on match
2602326SN/A     * @param secure_lookup if the operation affects the secure world
2612301SN/A     */
2622301SN/A    void flushMvaAsid(Addr mva, uint64_t asn, bool secure_lookup,
2632727Sktlim@umich.edu                      uint8_t target_el);
2642326SN/A
2651062SN/A    /** Remove any entries that match the asn
2661062SN/A     * @param asn contextid/asn to flush on match
2671681SN/A     * @param secure_lookup if the operation affects the secure world
2681060SN/A     */
2692292SN/A    void flushAsid(uint64_t asn, bool secure_lookup, uint8_t target_el);
2701060SN/A
2716221Snate@binkert.org    /** Remove all entries that match the va regardless of asn
2722292SN/A     * @param mva address to flush from cache
2732292SN/A     * @param secure_lookup if the operation affects the secure world
2742292SN/A     * @param hyp if the operation affects hyp mode
2752292SN/A     */
2762292SN/A    void flushMva(Addr mva, bool secure_lookup, bool hyp, uint8_t target_el);
2772292SN/A
2782292SN/A    /**
2792292SN/A     * Invalidate all entries in the stage 2 TLB that match the given ipa
2802292SN/A     * and the current VMID
2812733Sktlim@umich.edu     * @param ipa the address to invalidate
2821060SN/A     * @param secure_lookup if the operation affects the secure world
2831060SN/A     * @param hyp if the operation affects hyp mode
2841681SN/A     */
2851060SN/A    void flushIpaVmid(Addr ipa, bool secure_lookup, bool hyp, uint8_t target_el);
2862292SN/A
2871060SN/A    Fault trickBoxCheck(RequestPtr req, Mode mode, TlbEntry::DomainType domain);
2881060SN/A    Fault walkTrickBoxCheck(Addr pa, bool is_secure, Addr va, Addr sz, bool is_exec,
2891060SN/A            bool is_write, TlbEntry::DomainType domain, LookupLevel lookup_level);
2901060SN/A
2911060SN/A    void printTlb() const;
2921060SN/A
2931060SN/A    void demapPage(Addr vaddr, uint64_t asn) override
2941060SN/A    {
2951060SN/A        // needed for x86 only
2962292SN/A        panic("demapPage() is not implemented.\n");
2972292SN/A    }
2981060SN/A
2991060SN/A    /**
3001060SN/A     * Do a functional lookup on the TLB (for debugging)
3011060SN/A     * and don't modify any internal state
3021681SN/A     * @param tc thread context to get the context id from
3031060SN/A     * @param vaddr virtual address to translate
3042292SN/A     * @param pa returned physical address
3051060SN/A     * @return if the translation was successful
3061060SN/A     */
3071060SN/A    bool translateFunctional(ThreadContext *tc, Addr vaddr, Addr &paddr);
3081060SN/A
3091060SN/A    /**
3101060SN/A     * Do a functional lookup on the TLB (for checker cpu) that
3111060SN/A     * behaves like a normal lookup without modifying any page table state.
3121681SN/A     */
3131060SN/A    Fault translateFunctional(RequestPtr req, ThreadContext *tc, Mode mode,
3142292SN/A            ArmTranslationType tranType = NormalTran);
3151060SN/A
3161060SN/A    /** Accessor functions for memory attributes for last accessed TLB entry
3171060SN/A     */
3181060SN/A    void
3191060SN/A    setAttr(uint64_t attr)
3201060SN/A    {
3211060SN/A        _attr = attr;
3221681SN/A    }
3231060SN/A
3246221Snate@binkert.org    uint64_t
3251060SN/A    getAttr() const
3262292SN/A    {
3272292SN/A        return _attr;
3282292SN/A    }
3292292SN/A
3301060SN/A    Fault translateFs(RequestPtr req, ThreadContext *tc, Mode mode,
3311060SN/A            Translation *translation, bool &delay,
3321681SN/A            bool timing, ArmTranslationType tranType, bool functional = false);
3331060SN/A    Fault translateSe(RequestPtr req, ThreadContext *tc, Mode mode,
3342292SN/A            Translation *translation, bool &delay, bool timing);
3351060SN/A    Fault translateAtomic(RequestPtr req, ThreadContext *tc, Mode mode,
3362292SN/A            ArmTranslationType tranType);
3371060SN/A    Fault
3381060SN/A    translateAtomic(RequestPtr req, ThreadContext *tc, Mode mode) override
3392307SN/A    {
3402863Sktlim@umich.edu        return translateAtomic(req, tc, mode, NormalTran);
3412843Sktlim@umich.edu    }
3422307SN/A    void translateTiming(
3432843Sktlim@umich.edu            RequestPtr req, ThreadContext *tc,
3442843Sktlim@umich.edu            Translation *translation, Mode mode,
3452863Sktlim@umich.edu            ArmTranslationType tranType);
3461681SN/A    void
3471681SN/A    translateTiming(RequestPtr req, ThreadContext *tc,
3482316SN/A                    Translation *translation, Mode mode) override
3491681SN/A    {
3502843Sktlim@umich.edu        translateTiming(req, tc, translation, mode, NormalTran);
3512843Sktlim@umich.edu    }
3522843Sktlim@umich.edu    Fault translateComplete(RequestPtr req, ThreadContext *tc,
3532843Sktlim@umich.edu            Translation *translation, Mode mode, ArmTranslationType tranType,
3542843Sktlim@umich.edu            bool callFromS2);
3552843Sktlim@umich.edu    Fault finalizePhysical(
3562843Sktlim@umich.edu            RequestPtr req, ThreadContext *tc, Mode mode) const override;
3571681SN/A
3582348SN/A    void drainResume() override;
3592307SN/A
3602367SN/A    // Checkpointing
3612367SN/A    void serialize(CheckpointOut &cp) const override;
3621681SN/A    void unserialize(CheckpointIn &cp) override;
3632307SN/A
3642307SN/A    void regStats() override;
3652307SN/A
3662307SN/A    void regProbePoints() override;
3676221Snate@binkert.org
3686221Snate@binkert.org    /**
3696221Snate@binkert.org     * Get the table walker master port. This is used for migrating
3706221Snate@binkert.org     * port connections during a CPU takeOverFrom() call. For
3716221Snate@binkert.org     * architectures that do not have a table walker, NULL is
3722307SN/A     * returned, hence the use of a pointer rather than a
3731681SN/A     * reference. For ARM this method will always return a valid port
3741681SN/A     * pointer.
3752307SN/A     *
3761681SN/A     * @return A pointer to the walker master port
3772307SN/A     */
3781060SN/A    BaseMasterPort* getMasterPort() override;
3792348SN/A
3802307SN/A    // Caching misc register values here.
3812307SN/A    // Writing to misc registers needs to invalidate them.
3822307SN/A    // translateFunctional/translateSe/translateFs checks if they are
3832307SN/A    // invalid and call updateMiscReg if necessary.
3841060SN/Aprotected:
3852307SN/A    CPSR cpsr;
3862307SN/A    bool aarch64;
3872307SN/A    ExceptionLevel aarch64EL;
3881060SN/A    SCTLR sctlr;
3892307SN/A    SCR scr;
3902307SN/A    bool isPriv;
3911060SN/A    bool isSecure;
3926221Snate@binkert.org    bool isHyp;
3936221Snate@binkert.org    TTBCR ttbcr;
3946221Snate@binkert.org    uint16_t asid;
3956221Snate@binkert.org    uint8_t vmid;
3962307SN/A    PRRR prrr;
3971060SN/A    NMRR nmrr;
3982307SN/A    HCR hcr;
3992307SN/A    uint32_t dacr;
4002873Sktlim@umich.edu    bool miscRegValid;
4012307SN/A    ContextID miscRegContext;
4021060SN/A    ArmTranslationType curTranType;
4031060SN/A
4041060SN/A    // Cached copies of system-level properties
4051681SN/A    bool haveLPAE;
4061060SN/A    bool haveVirtualization;
4076221Snate@binkert.org    bool haveLargeAsid64;
4082107SN/A
4096221Snate@binkert.org    AddrRange m5opRange;
4102107SN/A
4112292SN/A    void updateMiscReg(ThreadContext *tc,
4122292SN/A                       ArmTranslationType tranType = NormalTran);
4132107SN/A
4142292SN/Apublic:
4152326SN/A    const Params *
4162292SN/A    params() const
4172107SN/A    {
4182292SN/A        return dynamic_cast<const Params *>(_params);
4192935Sksewell@umich.edu    }
4204632Sgblack@eecs.umich.edu    inline void invalidateMiscReg() { miscRegValid = false; }
4212935Sksewell@umich.edu
4222292SN/Aprivate:
4232292SN/A    /** Remove any entries that match both a va and asn
4242292SN/A     * @param mva virtual address to flush
4252292SN/A     * @param asn contextid/asn to flush on match
4262292SN/A     * @param secure_lookup if the operation affects the secure world
4272107SN/A     * @param hyp if the operation affects hyp mode
4282292SN/A     * @param ignore_asn if the flush should ignore the asn
4292107SN/A     */
4302292SN/A    void _flushMva(Addr mva, uint64_t asn, bool secure_lookup,
4312292SN/A                   bool hyp, bool ignore_asn, uint8_t target_el);
4322107SN/A
4332702Sktlim@umich.edu    bool checkELMatch(uint8_t target_el, uint8_t tentry_el, bool ignore_el);
4342107SN/A
4352107SN/A  public: /* Testing */
4362107SN/A    Fault testTranslation(RequestPtr req, Mode mode,
4372107SN/A                          TlbEntry::DomainType domain);
4386221Snate@binkert.org    Fault testWalk(Addr pa, Addr size, Addr va, bool is_secure, Mode mode,
4392292SN/A                   TlbEntry::DomainType domain,
4402292SN/A                   LookupLevel lookup_level);
4412292SN/A};
4422292SN/A
4432292SN/A} // namespace ArmISA
4442292SN/A
4452292SN/A#endif // __ARCH_ARM_TLB_HH__
4462292SN/A