tlb.cc revision 9898
14997Sgblack@eecs.umich.edu/*
25417Sgblack@eecs.umich.edu * Copyright (c) 2007-2008 The Hewlett-Packard Development Company
34997Sgblack@eecs.umich.edu * All rights reserved.
44997Sgblack@eecs.umich.edu *
57087Snate@binkert.org * The license below extends only to copyright in the software and shall
67087Snate@binkert.org * not be construed as granting a license to any other intellectual
77087Snate@binkert.org * property including but not limited to intellectual property relating
87087Snate@binkert.org * to a hardware implementation of the functionality of the software
97087Snate@binkert.org * licensed hereunder.  You may use the software subject to the license
107087Snate@binkert.org * terms below provided that you ensure that this notice is replicated
117087Snate@binkert.org * unmodified and in its entirety in all distributions of the software,
127087Snate@binkert.org * modified or unmodified, in source code or in binary form.
134997Sgblack@eecs.umich.edu *
147087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
157087Snate@binkert.org * modification, are permitted provided that the following conditions are
167087Snate@binkert.org * met: redistributions of source code must retain the above copyright
177087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
187087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
197087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
207087Snate@binkert.org * documentation and/or other materials provided with the distribution;
217087Snate@binkert.org * neither the name of the copyright holders nor the names of its
224997Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
237087Snate@binkert.org * this software without specific prior written permission.
244997Sgblack@eecs.umich.edu *
254997Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
264997Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
274997Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
284997Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
294997Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
304997Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
314997Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
324997Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
334997Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
344997Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
354997Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
364997Sgblack@eecs.umich.edu *
374997Sgblack@eecs.umich.edu * Authors: Gabe Black
384997Sgblack@eecs.umich.edu */
394997Sgblack@eecs.umich.edu
404997Sgblack@eecs.umich.edu#include <cstring>
414997Sgblack@eecs.umich.edu
429898Sandreas@sandberg.pp.se#include "arch/generic/mmapped_ipr.hh"
438229Snate@binkert.org#include "arch/x86/insts/microldstop.hh"
448229Snate@binkert.org#include "arch/x86/regs/misc.hh"
458582Sgblack@eecs.umich.edu#include "arch/x86/regs/msr.hh"
466315Sgblack@eecs.umich.edu#include "arch/x86/faults.hh"
475124Sgblack@eecs.umich.edu#include "arch/x86/pagetable.hh"
488752Sgblack@eecs.umich.edu#include "arch/x86/pagetable_walker.hh"
495086Sgblack@eecs.umich.edu#include "arch/x86/tlb.hh"
505149Sgblack@eecs.umich.edu#include "arch/x86/x86_traits.hh"
515086Sgblack@eecs.umich.edu#include "base/bitfield.hh"
525086Sgblack@eecs.umich.edu#include "base/trace.hh"
538229Snate@binkert.org#include "cpu/base.hh"
545086Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
558232Snate@binkert.org#include "debug/TLB.hh"
565086Sgblack@eecs.umich.edu#include "mem/packet_access.hh"
578767Sgblack@eecs.umich.edu#include "mem/page_table.hh"
585086Sgblack@eecs.umich.edu#include "mem/request.hh"
598767Sgblack@eecs.umich.edu#include "sim/full_system.hh"
605895Sgblack@eecs.umich.edu#include "sim/process.hh"
615086Sgblack@eecs.umich.edu
625086Sgblack@eecs.umich.edunamespace X86ISA {
635086Sgblack@eecs.umich.edu
648953Sgblack@eecs.umich.eduTLB::TLB(const Params *p) : BaseTLB(p), configAddress(0), size(p->size),
658953Sgblack@eecs.umich.edu    lruSeq(0)
665124Sgblack@eecs.umich.edu{
678953Sgblack@eecs.umich.edu    if (!size)
688953Sgblack@eecs.umich.edu        fatal("TLBs must have a non-zero size.\n");
695124Sgblack@eecs.umich.edu    tlb = new TlbEntry[size];
705124Sgblack@eecs.umich.edu    std::memset(tlb, 0, sizeof(TlbEntry) * size);
715124Sgblack@eecs.umich.edu
728953Sgblack@eecs.umich.edu    for (int x = 0; x < size; x++) {
738953Sgblack@eecs.umich.edu        tlb[x].trieHandle = NULL;
745124Sgblack@eecs.umich.edu        freeList.push_back(&tlb[x]);
758953Sgblack@eecs.umich.edu    }
765124Sgblack@eecs.umich.edu
775245Sgblack@eecs.umich.edu    walker = p->walker;
785245Sgblack@eecs.umich.edu    walker->setTLB(this);
795236Sgblack@eecs.umich.edu}
805236Sgblack@eecs.umich.edu
818953Sgblack@eecs.umich.eduvoid
828953Sgblack@eecs.umich.eduTLB::evictLRU()
838953Sgblack@eecs.umich.edu{
848953Sgblack@eecs.umich.edu    // Find the entry with the lowest (and hence least recently updated)
858953Sgblack@eecs.umich.edu    // sequence number.
868953Sgblack@eecs.umich.edu
878953Sgblack@eecs.umich.edu    unsigned lru = 0;
888953Sgblack@eecs.umich.edu    for (unsigned i = 1; i < size; i++) {
898953Sgblack@eecs.umich.edu        if (tlb[i].lruSeq < tlb[lru].lruSeq)
908953Sgblack@eecs.umich.edu            lru = i;
918953Sgblack@eecs.umich.edu    }
928953Sgblack@eecs.umich.edu
938953Sgblack@eecs.umich.edu    assert(tlb[lru].trieHandle);
948953Sgblack@eecs.umich.edu    trie.remove(tlb[lru].trieHandle);
958953Sgblack@eecs.umich.edu    tlb[lru].trieHandle = NULL;
968953Sgblack@eecs.umich.edu    freeList.push_back(&tlb[lru]);
978953Sgblack@eecs.umich.edu}
988953Sgblack@eecs.umich.edu
995895Sgblack@eecs.umich.eduTlbEntry *
1005124Sgblack@eecs.umich.eduTLB::insert(Addr vpn, TlbEntry &entry)
1015124Sgblack@eecs.umich.edu{
1028962Sgblack@eecs.umich.edu    // If somebody beat us to it, just use that existing entry.
1038962Sgblack@eecs.umich.edu    TlbEntry *newEntry = trie.lookup(vpn);
1048962Sgblack@eecs.umich.edu    if (newEntry) {
1059064Snilay@cs.wisc.edu        assert(newEntry->vaddr == vpn);
1068962Sgblack@eecs.umich.edu        return newEntry;
1078962Sgblack@eecs.umich.edu    }
1085124Sgblack@eecs.umich.edu
1098953Sgblack@eecs.umich.edu    if (freeList.empty())
1108953Sgblack@eecs.umich.edu        evictLRU();
1118962Sgblack@eecs.umich.edu
1128953Sgblack@eecs.umich.edu    newEntry = freeList.front();
1138953Sgblack@eecs.umich.edu    freeList.pop_front();
1148953Sgblack@eecs.umich.edu
1155124Sgblack@eecs.umich.edu    *newEntry = entry;
1168953Sgblack@eecs.umich.edu    newEntry->lruSeq = nextSeq();
1175124Sgblack@eecs.umich.edu    newEntry->vaddr = vpn;
1188953Sgblack@eecs.umich.edu    newEntry->trieHandle =
1198962Sgblack@eecs.umich.edu    trie.insert(vpn, TlbEntryTrie::MaxBits - entry.logBytes, newEntry);
1205895Sgblack@eecs.umich.edu    return newEntry;
1215124Sgblack@eecs.umich.edu}
1225124Sgblack@eecs.umich.edu
1235360Sgblack@eecs.umich.eduTlbEntry *
1245360Sgblack@eecs.umich.eduTLB::lookup(Addr va, bool update_lru)
1255360Sgblack@eecs.umich.edu{
1268953Sgblack@eecs.umich.edu    TlbEntry *entry = trie.lookup(va);
1278953Sgblack@eecs.umich.edu    if (entry && update_lru)
1288953Sgblack@eecs.umich.edu        entry->lruSeq = nextSeq();
1298953Sgblack@eecs.umich.edu    return entry;
1305124Sgblack@eecs.umich.edu}
1315124Sgblack@eecs.umich.edu
1325124Sgblack@eecs.umich.eduvoid
1339423SAndreas.Sandberg@arm.comTLB::flushAll()
1345124Sgblack@eecs.umich.edu{
1355242Sgblack@eecs.umich.edu    DPRINTF(TLB, "Invalidating all entries.\n");
1368953Sgblack@eecs.umich.edu    for (unsigned i = 0; i < size; i++) {
1378953Sgblack@eecs.umich.edu        if (tlb[i].trieHandle) {
1388953Sgblack@eecs.umich.edu            trie.remove(tlb[i].trieHandle);
1398953Sgblack@eecs.umich.edu            tlb[i].trieHandle = NULL;
1408953Sgblack@eecs.umich.edu            freeList.push_back(&tlb[i]);
1418953Sgblack@eecs.umich.edu        }
1425242Sgblack@eecs.umich.edu    }
1435124Sgblack@eecs.umich.edu}
1445124Sgblack@eecs.umich.edu
1455124Sgblack@eecs.umich.eduvoid
1465357Sgblack@eecs.umich.eduTLB::setConfigAddress(uint32_t addr)
1475357Sgblack@eecs.umich.edu{
1485357Sgblack@eecs.umich.edu    configAddress = addr;
1495357Sgblack@eecs.umich.edu}
1505357Sgblack@eecs.umich.edu
1515357Sgblack@eecs.umich.eduvoid
1529423SAndreas.Sandberg@arm.comTLB::flushNonGlobal()
1535124Sgblack@eecs.umich.edu{
1545242Sgblack@eecs.umich.edu    DPRINTF(TLB, "Invalidating all non global entries.\n");
1558953Sgblack@eecs.umich.edu    for (unsigned i = 0; i < size; i++) {
1568953Sgblack@eecs.umich.edu        if (tlb[i].trieHandle && !tlb[i].global) {
1578953Sgblack@eecs.umich.edu            trie.remove(tlb[i].trieHandle);
1588953Sgblack@eecs.umich.edu            tlb[i].trieHandle = NULL;
1598953Sgblack@eecs.umich.edu            freeList.push_back(&tlb[i]);
1605242Sgblack@eecs.umich.edu        }
1615242Sgblack@eecs.umich.edu    }
1625124Sgblack@eecs.umich.edu}
1635124Sgblack@eecs.umich.edu
1645124Sgblack@eecs.umich.eduvoid
1655358Sgblack@eecs.umich.eduTLB::demapPage(Addr va, uint64_t asn)
1665086Sgblack@eecs.umich.edu{
1678953Sgblack@eecs.umich.edu    TlbEntry *entry = trie.lookup(va);
1688953Sgblack@eecs.umich.edu    if (entry) {
1698953Sgblack@eecs.umich.edu        trie.remove(entry->trieHandle);
1708953Sgblack@eecs.umich.edu        entry->trieHandle = NULL;
1718953Sgblack@eecs.umich.edu        freeList.push_back(entry);
1725359Sgblack@eecs.umich.edu    }
1735086Sgblack@eecs.umich.edu}
1745086Sgblack@eecs.umich.edu
1755086Sgblack@eecs.umich.eduFault
1766141Sgblack@eecs.umich.eduTLB::translateInt(RequestPtr req, ThreadContext *tc)
1776141Sgblack@eecs.umich.edu{
1786141Sgblack@eecs.umich.edu    DPRINTF(TLB, "Addresses references internal memory.\n");
1796141Sgblack@eecs.umich.edu    Addr vaddr = req->getVaddr();
1806141Sgblack@eecs.umich.edu    Addr prefix = (vaddr >> 3) & IntAddrPrefixMask;
1816141Sgblack@eecs.umich.edu    if (prefix == IntAddrPrefixCPUID) {
1826141Sgblack@eecs.umich.edu        panic("CPUID memory space not yet implemented!\n");
1836141Sgblack@eecs.umich.edu    } else if (prefix == IntAddrPrefixMSR) {
1848582Sgblack@eecs.umich.edu        vaddr = (vaddr >> 3) & ~IntAddrPrefixMask;
1858105Sgblack@eecs.umich.edu        req->setFlags(Request::MMAPPED_IPR);
1868582Sgblack@eecs.umich.edu
1878582Sgblack@eecs.umich.edu        MiscRegIndex regNum;
1888582Sgblack@eecs.umich.edu        if (!msrAddrToIndex(regNum, vaddr))
1896141Sgblack@eecs.umich.edu            return new GeneralProtection(0);
1908582Sgblack@eecs.umich.edu
1916141Sgblack@eecs.umich.edu        //The index is multiplied by the size of a MiscReg so that
1926141Sgblack@eecs.umich.edu        //any memory dependence calculations will not see these as
1936141Sgblack@eecs.umich.edu        //overlapping.
1948582Sgblack@eecs.umich.edu        req->setPaddr((Addr)regNum * sizeof(MiscReg));
1956141Sgblack@eecs.umich.edu        return NoFault;
1966141Sgblack@eecs.umich.edu    } else if (prefix == IntAddrPrefixIO) {
1976141Sgblack@eecs.umich.edu        // TODO If CPL > IOPL or in virtual mode, check the I/O permission
1986141Sgblack@eecs.umich.edu        // bitmap in the TSS.
1996141Sgblack@eecs.umich.edu
2006141Sgblack@eecs.umich.edu        Addr IOPort = vaddr & ~IntAddrPrefixMask;
2016141Sgblack@eecs.umich.edu        // Make sure the address fits in the expected 16 bit IO address
2026141Sgblack@eecs.umich.edu        // space.
2036141Sgblack@eecs.umich.edu        assert(!(IOPort & ~0xFFFF));
2046141Sgblack@eecs.umich.edu        if (IOPort == 0xCF8 && req->getSize() == 4) {
2058105Sgblack@eecs.umich.edu            req->setFlags(Request::MMAPPED_IPR);
2066141Sgblack@eecs.umich.edu            req->setPaddr(MISCREG_PCI_CONFIG_ADDRESS * sizeof(MiscReg));
2076141Sgblack@eecs.umich.edu        } else if ((IOPort & ~mask(2)) == 0xCFC) {
2087774Sgblack@eecs.umich.edu            req->setFlags(Request::UNCACHEABLE);
2096141Sgblack@eecs.umich.edu            Addr configAddress =
2106141Sgblack@eecs.umich.edu                tc->readMiscRegNoEffect(MISCREG_PCI_CONFIG_ADDRESS);
2116141Sgblack@eecs.umich.edu            if (bits(configAddress, 31, 31)) {
2126141Sgblack@eecs.umich.edu                req->setPaddr(PhysAddrPrefixPciConfig |
2136141Sgblack@eecs.umich.edu                        mbits(configAddress, 30, 2) |
2146141Sgblack@eecs.umich.edu                        (IOPort & mask(2)));
2158098Sgblack@eecs.umich.edu            } else {
2168098Sgblack@eecs.umich.edu                req->setPaddr(PhysAddrPrefixIO | IOPort);
2176141Sgblack@eecs.umich.edu            }
2186141Sgblack@eecs.umich.edu        } else {
2197774Sgblack@eecs.umich.edu            req->setFlags(Request::UNCACHEABLE);
2206141Sgblack@eecs.umich.edu            req->setPaddr(PhysAddrPrefixIO | IOPort);
2216141Sgblack@eecs.umich.edu        }
2226141Sgblack@eecs.umich.edu        return NoFault;
2236141Sgblack@eecs.umich.edu    } else {
2246141Sgblack@eecs.umich.edu        panic("Access to unrecognized internal address space %#x.\n",
2256141Sgblack@eecs.umich.edu                prefix);
2266141Sgblack@eecs.umich.edu    }
2276141Sgblack@eecs.umich.edu}
2286141Sgblack@eecs.umich.edu
2296141Sgblack@eecs.umich.eduFault
2309738Sandreas@sandberg.pp.seTLB::finalizePhysical(RequestPtr req, ThreadContext *tc, Mode mode) const
2319738Sandreas@sandberg.pp.se{
2329738Sandreas@sandberg.pp.se    Addr paddr = req->getPaddr();
2339738Sandreas@sandberg.pp.se
2349738Sandreas@sandberg.pp.se    // Check for an access to the local APIC
2359738Sandreas@sandberg.pp.se    if (FullSystem) {
2369738Sandreas@sandberg.pp.se        LocalApicBase localApicBase =
2379738Sandreas@sandberg.pp.se            tc->readMiscRegNoEffect(MISCREG_APIC_BASE);
2389738Sandreas@sandberg.pp.se        AddrRange apicRange(localApicBase.base * PageBytes,
2399738Sandreas@sandberg.pp.se                            (localApicBase.base + 1) * PageBytes - 1);
2409738Sandreas@sandberg.pp.se
2419898Sandreas@sandberg.pp.se        AddrRange m5opRange(0xFFFF0000, 0xFFFFFFFF);
2429898Sandreas@sandberg.pp.se
2439738Sandreas@sandberg.pp.se        if (apicRange.contains(paddr)) {
2449738Sandreas@sandberg.pp.se            // The Intel developer's manuals say the below restrictions apply,
2459738Sandreas@sandberg.pp.se            // but the linux kernel, because of a compiler optimization, breaks
2469738Sandreas@sandberg.pp.se            // them.
2479738Sandreas@sandberg.pp.se            /*
2489738Sandreas@sandberg.pp.se            // Check alignment
2499738Sandreas@sandberg.pp.se            if (paddr & ((32/8) - 1))
2509738Sandreas@sandberg.pp.se                return new GeneralProtection(0);
2519738Sandreas@sandberg.pp.se            // Check access size
2529738Sandreas@sandberg.pp.se            if (req->getSize() != (32/8))
2539738Sandreas@sandberg.pp.se                return new GeneralProtection(0);
2549738Sandreas@sandberg.pp.se            */
2559738Sandreas@sandberg.pp.se            // Force the access to be uncacheable.
2569738Sandreas@sandberg.pp.se            req->setFlags(Request::UNCACHEABLE);
2579738Sandreas@sandberg.pp.se            req->setPaddr(x86LocalAPICAddress(tc->contextId(),
2589738Sandreas@sandberg.pp.se                                              paddr - apicRange.start()));
2599898Sandreas@sandberg.pp.se        } else if (m5opRange.contains(paddr)) {
2609898Sandreas@sandberg.pp.se            req->setFlags(Request::MMAPPED_IPR);
2619898Sandreas@sandberg.pp.se            req->setPaddr(GenericISA::iprAddressPseudoInst(
2629898Sandreas@sandberg.pp.se                              (paddr >> 8) & 0xFF,
2639898Sandreas@sandberg.pp.se                              paddr & 0xFF));
2649738Sandreas@sandberg.pp.se        }
2659738Sandreas@sandberg.pp.se    }
2669738Sandreas@sandberg.pp.se
2679738Sandreas@sandberg.pp.se    return NoFault;
2689738Sandreas@sandberg.pp.se}
2699738Sandreas@sandberg.pp.se
2709738Sandreas@sandberg.pp.seFault
2716023Snate@binkert.orgTLB::translate(RequestPtr req, ThreadContext *tc, Translation *translation,
2726023Snate@binkert.org        Mode mode, bool &delayedResponse, bool timing)
2735086Sgblack@eecs.umich.edu{
2746141Sgblack@eecs.umich.edu    uint32_t flags = req->getFlags();
2756141Sgblack@eecs.umich.edu    int seg = flags & SegmentFlagMask;
2766141Sgblack@eecs.umich.edu    bool storeCheck = flags & (StoreCheck << FlagShift);
2776141Sgblack@eecs.umich.edu
2788535Sgblack@eecs.umich.edu    delayedResponse = false;
2798535Sgblack@eecs.umich.edu
2806141Sgblack@eecs.umich.edu    // If this is true, we're dealing with a request to a non-memory address
2816141Sgblack@eecs.umich.edu    // space.
2826141Sgblack@eecs.umich.edu    if (seg == SEGMENT_REG_MS) {
2836141Sgblack@eecs.umich.edu        return translateInt(req, tc);
2846141Sgblack@eecs.umich.edu    }
2856141Sgblack@eecs.umich.edu
2865124Sgblack@eecs.umich.edu    Addr vaddr = req->getVaddr();
2875140Sgblack@eecs.umich.edu    DPRINTF(TLB, "Translating vaddr %#x.\n", vaddr);
2885140Sgblack@eecs.umich.edu
2896141Sgblack@eecs.umich.edu    HandyM5Reg m5Reg = tc->readMiscRegNoEffect(MISCREG_M5_REG);
2905140Sgblack@eecs.umich.edu
2915140Sgblack@eecs.umich.edu    // If protected mode has been enabled...
2926141Sgblack@eecs.umich.edu    if (m5Reg.prot) {
2935237Sgblack@eecs.umich.edu        DPRINTF(TLB, "In protected mode.\n");
2945140Sgblack@eecs.umich.edu        // If we're not in 64-bit mode, do protection/limit checks
2956141Sgblack@eecs.umich.edu        if (m5Reg.mode != LongMode) {
2965237Sgblack@eecs.umich.edu            DPRINTF(TLB, "Not in long mode. Checking segment protection.\n");
2975431Sgblack@eecs.umich.edu            // Check for a NULL segment selector.
2986059Sgblack@eecs.umich.edu            if (!(seg == SEGMENT_REG_TSG || seg == SYS_SEGMENT_REG_IDTR ||
2996141Sgblack@eecs.umich.edu                        seg == SEGMENT_REG_HS || seg == SEGMENT_REG_LS)
3006059Sgblack@eecs.umich.edu                    && !tc->readMiscRegNoEffect(MISCREG_SEG_SEL(seg)))
3015431Sgblack@eecs.umich.edu                return new GeneralProtection(0);
3025433Sgblack@eecs.umich.edu            bool expandDown = false;
3035965Sgblack@eecs.umich.edu            SegAttr attr = tc->readMiscRegNoEffect(MISCREG_SEG_ATTR(seg));
3045433Sgblack@eecs.umich.edu            if (seg >= SEGMENT_REG_ES && seg <= SEGMENT_REG_HS) {
3056099Sgblack@eecs.umich.edu                if (!attr.writable && (mode == Write || storeCheck))
3065433Sgblack@eecs.umich.edu                    return new GeneralProtection(0);
3076023Snate@binkert.org                if (!attr.readable && mode == Read)
3085433Sgblack@eecs.umich.edu                    return new GeneralProtection(0);
3095433Sgblack@eecs.umich.edu                expandDown = attr.expandDown;
3105965Sgblack@eecs.umich.edu
3115433Sgblack@eecs.umich.edu            }
3125140Sgblack@eecs.umich.edu            Addr base = tc->readMiscRegNoEffect(MISCREG_SEG_BASE(seg));
3135140Sgblack@eecs.umich.edu            Addr limit = tc->readMiscRegNoEffect(MISCREG_SEG_LIMIT(seg));
3145965Sgblack@eecs.umich.edu            bool sizeOverride = (flags & (AddrSizeFlagBit << FlagShift));
3159062Sjayneel@cs.wisc.edu            unsigned logSize = sizeOverride ? (unsigned)m5Reg.altAddr
3169062Sjayneel@cs.wisc.edu                                            : (unsigned)m5Reg.defAddr;
3179028Sgblack@eecs.umich.edu            int size = (1 << logSize) * 8;
3189028Sgblack@eecs.umich.edu            Addr offset = bits(vaddr - base, size - 1, 0);
3195965Sgblack@eecs.umich.edu            Addr endOffset = offset + req->getSize() - 1;
3205433Sgblack@eecs.umich.edu            if (expandDown) {
3215237Sgblack@eecs.umich.edu                DPRINTF(TLB, "Checking an expand down segment.\n");
3225965Sgblack@eecs.umich.edu                warn_once("Expand down segments are untested.\n");
3235965Sgblack@eecs.umich.edu                if (offset <= limit || endOffset <= limit)
3245965Sgblack@eecs.umich.edu                    return new GeneralProtection(0);
3255140Sgblack@eecs.umich.edu            } else {
3265965Sgblack@eecs.umich.edu                if (offset > limit || endOffset > limit)
3275965Sgblack@eecs.umich.edu                    return new GeneralProtection(0);
3285140Sgblack@eecs.umich.edu            }
3295140Sgblack@eecs.umich.edu        }
3309025Sgblack@eecs.umich.edu        if (m5Reg.submode != SixtyFourBitMode ||
3318925Sgblack@eecs.umich.edu                (flags & (AddrSizeFlagBit << FlagShift)))
3328925Sgblack@eecs.umich.edu            vaddr &= mask(32);
3335140Sgblack@eecs.umich.edu        // If paging is enabled, do the translation.
3346141Sgblack@eecs.umich.edu        if (m5Reg.paging) {
3355237Sgblack@eecs.umich.edu            DPRINTF(TLB, "Paging enabled.\n");
3365140Sgblack@eecs.umich.edu            // The vaddr already has the segment base applied.
3375140Sgblack@eecs.umich.edu            TlbEntry *entry = lookup(vaddr);
3385140Sgblack@eecs.umich.edu            if (!entry) {
3398752Sgblack@eecs.umich.edu                if (FullSystem) {
3408752Sgblack@eecs.umich.edu                    Fault fault = walker->start(tc, translation, req, mode);
3418752Sgblack@eecs.umich.edu                    if (timing || fault != NoFault) {
3428752Sgblack@eecs.umich.edu                        // This gets ignored in atomic mode.
3438752Sgblack@eecs.umich.edu                        delayedResponse = true;
3448752Sgblack@eecs.umich.edu                        return fault;
3458752Sgblack@eecs.umich.edu                    }
3468752Sgblack@eecs.umich.edu                    entry = lookup(vaddr);
3478752Sgblack@eecs.umich.edu                    assert(entry);
3488752Sgblack@eecs.umich.edu                } else {
3498752Sgblack@eecs.umich.edu                    DPRINTF(TLB, "Handling a TLB miss for "
3508752Sgblack@eecs.umich.edu                            "address %#x at pc %#x.\n",
3518752Sgblack@eecs.umich.edu                            vaddr, tc->instAddr());
3528752Sgblack@eecs.umich.edu
3538752Sgblack@eecs.umich.edu                    Process *p = tc->getProcessPtr();
3548752Sgblack@eecs.umich.edu                    TlbEntry newEntry;
3558752Sgblack@eecs.umich.edu                    bool success = p->pTable->lookup(vaddr, newEntry);
3568752Sgblack@eecs.umich.edu                    if (!success && mode != Execute) {
3578752Sgblack@eecs.umich.edu                        // Check if we just need to grow the stack.
3588752Sgblack@eecs.umich.edu                        if (p->fixupStackFault(vaddr)) {
3598752Sgblack@eecs.umich.edu                            // If we did, lookup the entry for the new page.
3608752Sgblack@eecs.umich.edu                            success = p->pTable->lookup(vaddr, newEntry);
3618752Sgblack@eecs.umich.edu                        }
3628752Sgblack@eecs.umich.edu                    }
3638752Sgblack@eecs.umich.edu                    if (!success) {
3648752Sgblack@eecs.umich.edu                        return new PageFault(vaddr, true, mode, true, false);
3658752Sgblack@eecs.umich.edu                    } else {
3668752Sgblack@eecs.umich.edu                        Addr alignedVaddr = p->pTable->pageAlign(vaddr);
3678752Sgblack@eecs.umich.edu                        DPRINTF(TLB, "Mapping %#x to %#x\n", alignedVaddr,
3688752Sgblack@eecs.umich.edu                                newEntry.pageStart());
3698752Sgblack@eecs.umich.edu                        entry = insert(alignedVaddr, newEntry);
3708752Sgblack@eecs.umich.edu                    }
3718752Sgblack@eecs.umich.edu                    DPRINTF(TLB, "Miss was serviced.\n");
3725895Sgblack@eecs.umich.edu                }
3735140Sgblack@eecs.umich.edu            }
3748646Snilay@cs.wisc.edu
3758646Snilay@cs.wisc.edu            DPRINTF(TLB, "Entry found with paddr %#x, "
3768646Snilay@cs.wisc.edu                    "doing protection checks.\n", entry->paddr);
3775895Sgblack@eecs.umich.edu            // Do paging protection checks.
3786141Sgblack@eecs.umich.edu            bool inUser = (m5Reg.cpl == 3 &&
3795917Sgblack@eecs.umich.edu                    !(flags & (CPL0FlagBit << FlagShift)));
3807933Stharris@microsoft.com            CR0 cr0 = tc->readMiscRegNoEffect(MISCREG_CR0);
3817933Stharris@microsoft.com            bool badWrite = (!entry->writable && (inUser || cr0.wp));
3827933Stharris@microsoft.com            if ((inUser && !entry->user) || (mode == Write && badWrite)) {
3835917Sgblack@eecs.umich.edu                // The page must have been present to get into the TLB in
3845917Sgblack@eecs.umich.edu                // the first place. We'll assume the reserved bits are
3855917Sgblack@eecs.umich.edu                // fine even though we're not checking them.
3866023Snate@binkert.org                return new PageFault(vaddr, true, mode, inUser, false);
3875917Sgblack@eecs.umich.edu            }
3887933Stharris@microsoft.com            if (storeCheck && badWrite) {
3896099Sgblack@eecs.umich.edu                // This would fault if this were a write, so return a page
3906099Sgblack@eecs.umich.edu                // fault that reflects that happening.
3916099Sgblack@eecs.umich.edu                return new PageFault(vaddr, true, Write, inUser, false);
3926099Sgblack@eecs.umich.edu            }
3935917Sgblack@eecs.umich.edu
3948953Sgblack@eecs.umich.edu            Addr paddr = entry->paddr | (vaddr & mask(entry->logBytes));
3955895Sgblack@eecs.umich.edu            DPRINTF(TLB, "Translated %#x -> %#x.\n", vaddr, paddr);
3965895Sgblack@eecs.umich.edu            req->setPaddr(paddr);
3977775Sgblack@eecs.umich.edu            if (entry->uncacheable)
3987775Sgblack@eecs.umich.edu                req->setFlags(Request::UNCACHEABLE);
3995140Sgblack@eecs.umich.edu        } else {
4005140Sgblack@eecs.umich.edu            //Use the address which already has segmentation applied.
4015237Sgblack@eecs.umich.edu            DPRINTF(TLB, "Paging disabled.\n");
4025237Sgblack@eecs.umich.edu            DPRINTF(TLB, "Translated %#x -> %#x.\n", vaddr, vaddr);
4035140Sgblack@eecs.umich.edu            req->setPaddr(vaddr);
4045140Sgblack@eecs.umich.edu        }
4055124Sgblack@eecs.umich.edu    } else {
4065140Sgblack@eecs.umich.edu        // Real mode
4075237Sgblack@eecs.umich.edu        DPRINTF(TLB, "In real mode.\n");
4085237Sgblack@eecs.umich.edu        DPRINTF(TLB, "Translated %#x -> %#x.\n", vaddr, vaddr);
4095140Sgblack@eecs.umich.edu        req->setPaddr(vaddr);
4105124Sgblack@eecs.umich.edu    }
4119738Sandreas@sandberg.pp.se
4129738Sandreas@sandberg.pp.se    return finalizePhysical(req, tc, mode);
4138902Sandreas.hansson@arm.com}
4145086Sgblack@eecs.umich.edu
4155140Sgblack@eecs.umich.eduFault
4166023Snate@binkert.orgTLB::translateAtomic(RequestPtr req, ThreadContext *tc, Mode mode)
4175140Sgblack@eecs.umich.edu{
4185895Sgblack@eecs.umich.edu    bool delayedResponse;
4196023Snate@binkert.org    return TLB::translate(req, tc, NULL, mode, delayedResponse, false);
4205140Sgblack@eecs.umich.edu}
4215140Sgblack@eecs.umich.edu
4225894Sgblack@eecs.umich.eduvoid
4236022Sgblack@eecs.umich.eduTLB::translateTiming(RequestPtr req, ThreadContext *tc,
4246023Snate@binkert.org        Translation *translation, Mode mode)
4255894Sgblack@eecs.umich.edu{
4265895Sgblack@eecs.umich.edu    bool delayedResponse;
4275894Sgblack@eecs.umich.edu    assert(translation);
4286023Snate@binkert.org    Fault fault =
4296023Snate@binkert.org        TLB::translate(req, tc, translation, mode, delayedResponse, true);
4305895Sgblack@eecs.umich.edu    if (!delayedResponse)
4316023Snate@binkert.org        translation->finish(fault, req, tc, mode);
4325894Sgblack@eecs.umich.edu}
4335894Sgblack@eecs.umich.edu
4348888Sgeoffrey.blake@arm.comFault
4358888Sgeoffrey.blake@arm.comTLB::translateFunctional(RequestPtr req, ThreadContext *tc, Mode mode)
4368888Sgeoffrey.blake@arm.com{
4378888Sgeoffrey.blake@arm.com    panic("Not implemented\n");
4388888Sgeoffrey.blake@arm.com    return NoFault;
4398888Sgeoffrey.blake@arm.com}
4408888Sgeoffrey.blake@arm.com
4417912Shestness@cs.utexas.eduWalker *
4427912Shestness@cs.utexas.eduTLB::getWalker()
4437912Shestness@cs.utexas.edu{
4447912Shestness@cs.utexas.edu    return walker;
4457912Shestness@cs.utexas.edu}
4467912Shestness@cs.utexas.edu
4475086Sgblack@eecs.umich.eduvoid
4485086Sgblack@eecs.umich.eduTLB::serialize(std::ostream &os)
4495086Sgblack@eecs.umich.edu{
4509818Snilay@cs.wisc.edu    // Only store the entries in use.
4519818Snilay@cs.wisc.edu    uint32_t _size = size - freeList.size();
4529818Snilay@cs.wisc.edu    SERIALIZE_SCALAR(_size);
4539818Snilay@cs.wisc.edu    SERIALIZE_SCALAR(lruSeq);
4549818Snilay@cs.wisc.edu
4559818Snilay@cs.wisc.edu    uint32_t _count = 0;
4569818Snilay@cs.wisc.edu
4579818Snilay@cs.wisc.edu    for (uint32_t x = 0; x < size; x++) {
4589818Snilay@cs.wisc.edu        if (tlb[x].trieHandle != NULL) {
4599818Snilay@cs.wisc.edu            os << "\n[" << csprintf("%s.Entry%d", name(), _count) << "]\n";
4609818Snilay@cs.wisc.edu            tlb[x].serialize(os);
4619818Snilay@cs.wisc.edu            _count++;
4629818Snilay@cs.wisc.edu        }
4639818Snilay@cs.wisc.edu    }
4645086Sgblack@eecs.umich.edu}
4655086Sgblack@eecs.umich.edu
4665086Sgblack@eecs.umich.eduvoid
4675086Sgblack@eecs.umich.eduTLB::unserialize(Checkpoint *cp, const std::string &section)
4685086Sgblack@eecs.umich.edu{
4699818Snilay@cs.wisc.edu    // Do not allow to restore with a smaller tlb.
4709818Snilay@cs.wisc.edu    uint32_t _size;
4719818Snilay@cs.wisc.edu    UNSERIALIZE_SCALAR(_size);
4729818Snilay@cs.wisc.edu    if (_size > size) {
4739818Snilay@cs.wisc.edu        fatal("TLB size less than the one in checkpoint!");
4749818Snilay@cs.wisc.edu    }
4759818Snilay@cs.wisc.edu
4769818Snilay@cs.wisc.edu    UNSERIALIZE_SCALAR(lruSeq);
4779818Snilay@cs.wisc.edu
4789818Snilay@cs.wisc.edu    for (uint32_t x = 0; x < _size; x++) {
4799818Snilay@cs.wisc.edu        TlbEntry *newEntry = freeList.front();
4809818Snilay@cs.wisc.edu        freeList.pop_front();
4819818Snilay@cs.wisc.edu
4829818Snilay@cs.wisc.edu        newEntry->unserialize(cp, csprintf("%s.Entry%d", name(), x));
4839818Snilay@cs.wisc.edu        newEntry->trieHandle = trie.insert(newEntry->vaddr,
4849818Snilay@cs.wisc.edu            TlbEntryTrie::MaxBits - newEntry->logBytes, newEntry);
4859818Snilay@cs.wisc.edu    }
4865086Sgblack@eecs.umich.edu}
4875086Sgblack@eecs.umich.edu
4889294Sandreas.hansson@arm.comBaseMasterPort *
4898922Swilliam.wang@arm.comTLB::getMasterPort()
4908864Snilay@cs.wisc.edu{
4918922Swilliam.wang@arm.com    return &walker->getMasterPort("port");
4928864Snilay@cs.wisc.edu}
4938864Snilay@cs.wisc.edu
4947811Ssteve.reinhardt@amd.com} // namespace X86ISA
4955086Sgblack@eecs.umich.edu
4966022Sgblack@eecs.umich.eduX86ISA::TLB *
4976022Sgblack@eecs.umich.eduX86TLBParams::create()
4984997Sgblack@eecs.umich.edu{
4996022Sgblack@eecs.umich.edu    return new X86ISA::TLB(this);
5004997Sgblack@eecs.umich.edu}
501