tlb.cc revision 5140
14997Sgblack@eecs.umich.edu/* 24997Sgblack@eecs.umich.edu * Copyright (c) 2007 The Hewlett-Packard Development Company 34997Sgblack@eecs.umich.edu * All rights reserved. 44997Sgblack@eecs.umich.edu * 54997Sgblack@eecs.umich.edu * Redistribution and use of this software in source and binary forms, 64997Sgblack@eecs.umich.edu * with or without modification, are permitted provided that the 74997Sgblack@eecs.umich.edu * following conditions are met: 84997Sgblack@eecs.umich.edu * 94997Sgblack@eecs.umich.edu * The software must be used only for Non-Commercial Use which means any 104997Sgblack@eecs.umich.edu * use which is NOT directed to receiving any direct monetary 114997Sgblack@eecs.umich.edu * compensation for, or commercial advantage from such use. Illustrative 124997Sgblack@eecs.umich.edu * examples of non-commercial use are academic research, personal study, 134997Sgblack@eecs.umich.edu * teaching, education and corporate research & development. 144997Sgblack@eecs.umich.edu * Illustrative examples of commercial use are distributing products for 154997Sgblack@eecs.umich.edu * commercial advantage and providing services using the software for 164997Sgblack@eecs.umich.edu * commercial advantage. 174997Sgblack@eecs.umich.edu * 184997Sgblack@eecs.umich.edu * If you wish to use this software or functionality therein that may be 194997Sgblack@eecs.umich.edu * covered by patents for commercial use, please contact: 204997Sgblack@eecs.umich.edu * Director of Intellectual Property Licensing 214997Sgblack@eecs.umich.edu * Office of Strategy and Technology 224997Sgblack@eecs.umich.edu * Hewlett-Packard Company 234997Sgblack@eecs.umich.edu * 1501 Page Mill Road 244997Sgblack@eecs.umich.edu * Palo Alto, California 94304 254997Sgblack@eecs.umich.edu * 264997Sgblack@eecs.umich.edu * Redistributions of source code must retain the above copyright notice, 274997Sgblack@eecs.umich.edu * this list of conditions and the following disclaimer. Redistributions 284997Sgblack@eecs.umich.edu * in binary form must reproduce the above copyright notice, this list of 294997Sgblack@eecs.umich.edu * conditions and the following disclaimer in the documentation and/or 304997Sgblack@eecs.umich.edu * other materials provided with the distribution. Neither the name of 314997Sgblack@eecs.umich.edu * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 324997Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 334997Sgblack@eecs.umich.edu * this software without specific prior written permission. No right of 344997Sgblack@eecs.umich.edu * sublicense is granted herewith. Derivatives of the software and 354997Sgblack@eecs.umich.edu * output created using the software may be prepared, but only for 364997Sgblack@eecs.umich.edu * Non-Commercial Uses. Derivatives of the software may be shared with 374997Sgblack@eecs.umich.edu * others provided: (i) the others agree to abide by the list of 384997Sgblack@eecs.umich.edu * conditions herein which includes the Non-Commercial Use restrictions; 394997Sgblack@eecs.umich.edu * and (ii) such Derivatives of the software include the above copyright 404997Sgblack@eecs.umich.edu * notice to acknowledge the contribution from this software where 414997Sgblack@eecs.umich.edu * applicable, this list of conditions and the disclaimer below. 424997Sgblack@eecs.umich.edu * 434997Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 444997Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 454997Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 464997Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 474997Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 484997Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 494997Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 504997Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 514997Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 524997Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 534997Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 544997Sgblack@eecs.umich.edu * 554997Sgblack@eecs.umich.edu * Authors: Gabe Black 564997Sgblack@eecs.umich.edu */ 574997Sgblack@eecs.umich.edu 584997Sgblack@eecs.umich.edu#include <cstring> 594997Sgblack@eecs.umich.edu 605086Sgblack@eecs.umich.edu#include "config/full_system.hh" 615086Sgblack@eecs.umich.edu 625124Sgblack@eecs.umich.edu#include "arch/x86/pagetable.hh" 635086Sgblack@eecs.umich.edu#include "arch/x86/tlb.hh" 645086Sgblack@eecs.umich.edu#include "base/bitfield.hh" 655086Sgblack@eecs.umich.edu#include "base/trace.hh" 665086Sgblack@eecs.umich.edu#include "cpu/thread_context.hh" 675086Sgblack@eecs.umich.edu#include "cpu/base.hh" 685086Sgblack@eecs.umich.edu#include "mem/packet_access.hh" 695086Sgblack@eecs.umich.edu#include "mem/request.hh" 705086Sgblack@eecs.umich.edu#include "sim/system.hh" 715086Sgblack@eecs.umich.edu 725086Sgblack@eecs.umich.edunamespace X86ISA { 735086Sgblack@eecs.umich.edu 745124Sgblack@eecs.umich.eduTLB::TLB(const Params *p) : SimObject(p), size(p->size) 755124Sgblack@eecs.umich.edu{ 765124Sgblack@eecs.umich.edu tlb = new TlbEntry[size]; 775124Sgblack@eecs.umich.edu std::memset(tlb, 0, sizeof(TlbEntry) * size); 785124Sgblack@eecs.umich.edu 795124Sgblack@eecs.umich.edu for (int x = 0; x < size; x++) 805124Sgblack@eecs.umich.edu freeList.push_back(&tlb[x]); 815124Sgblack@eecs.umich.edu} 825124Sgblack@eecs.umich.edu 835124Sgblack@eecs.umich.eduvoid 845124Sgblack@eecs.umich.eduTLB::insert(Addr vpn, TlbEntry &entry) 855124Sgblack@eecs.umich.edu{ 865124Sgblack@eecs.umich.edu //TODO Deal with conflicting entries 875124Sgblack@eecs.umich.edu 885124Sgblack@eecs.umich.edu TlbEntry *newEntry = NULL; 895124Sgblack@eecs.umich.edu if (!freeList.empty()) { 905124Sgblack@eecs.umich.edu newEntry = freeList.front(); 915124Sgblack@eecs.umich.edu freeList.pop_front(); 925124Sgblack@eecs.umich.edu } else { 935124Sgblack@eecs.umich.edu newEntry = entryList.back(); 945124Sgblack@eecs.umich.edu entryList.pop_back(); 955124Sgblack@eecs.umich.edu } 965124Sgblack@eecs.umich.edu *newEntry = entry; 975124Sgblack@eecs.umich.edu newEntry->vaddr = vpn; 985124Sgblack@eecs.umich.edu entryList.push_front(newEntry); 995124Sgblack@eecs.umich.edu} 1005124Sgblack@eecs.umich.edu 1015124Sgblack@eecs.umich.eduTlbEntry * 1025124Sgblack@eecs.umich.eduTLB::lookup(Addr va, bool update_lru) 1035124Sgblack@eecs.umich.edu{ 1045124Sgblack@eecs.umich.edu //TODO make this smarter at some point 1055124Sgblack@eecs.umich.edu EntryList::iterator entry; 1065124Sgblack@eecs.umich.edu for (entry = entryList.begin(); entry != entryList.end(); entry++) { 1075124Sgblack@eecs.umich.edu if ((*entry)->vaddr <= va && (*entry)->vaddr + (*entry)->size > va) { 1085124Sgblack@eecs.umich.edu DPRINTF(TLB, "Matched vaddr %#x to entry starting at %#x " 1095124Sgblack@eecs.umich.edu "with size %#x.\n", va, (*entry)->vaddr, (*entry)->size); 1105124Sgblack@eecs.umich.edu TlbEntry *e = *entry; 1115124Sgblack@eecs.umich.edu if (update_lru) { 1125124Sgblack@eecs.umich.edu entryList.erase(entry); 1135124Sgblack@eecs.umich.edu entryList.push_front(e); 1145124Sgblack@eecs.umich.edu } 1155124Sgblack@eecs.umich.edu return e; 1165124Sgblack@eecs.umich.edu } 1175124Sgblack@eecs.umich.edu } 1185124Sgblack@eecs.umich.edu return NULL; 1195124Sgblack@eecs.umich.edu} 1205124Sgblack@eecs.umich.edu 1215124Sgblack@eecs.umich.eduvoid 1225124Sgblack@eecs.umich.eduTLB::invalidateAll() 1235124Sgblack@eecs.umich.edu{ 1245124Sgblack@eecs.umich.edu} 1255124Sgblack@eecs.umich.edu 1265124Sgblack@eecs.umich.eduvoid 1275124Sgblack@eecs.umich.eduTLB::invalidateNonGlobal() 1285124Sgblack@eecs.umich.edu{ 1295124Sgblack@eecs.umich.edu} 1305124Sgblack@eecs.umich.edu 1315124Sgblack@eecs.umich.eduvoid 1325124Sgblack@eecs.umich.eduTLB::demapPage(Addr va) 1335086Sgblack@eecs.umich.edu{ 1345086Sgblack@eecs.umich.edu} 1355086Sgblack@eecs.umich.edu 1365140Sgblack@eecs.umich.edutemplate<class TlbFault> 1375086Sgblack@eecs.umich.eduFault 1385140Sgblack@eecs.umich.eduTLB::translate(RequestPtr &req, ThreadContext *tc, bool write, bool execute) 1395086Sgblack@eecs.umich.edu{ 1405124Sgblack@eecs.umich.edu Addr vaddr = req->getVaddr(); 1415140Sgblack@eecs.umich.edu DPRINTF(TLB, "Translating vaddr %#x.\n", vaddr); 1425124Sgblack@eecs.umich.edu uint32_t flags = req->getFlags(); 1435124Sgblack@eecs.umich.edu bool storeCheck = flags & StoreCheck; 1445140Sgblack@eecs.umich.edu 1455124Sgblack@eecs.umich.edu int seg = flags & (mask(NUM_SEGMENTREGS)); 1465124Sgblack@eecs.umich.edu 1475124Sgblack@eecs.umich.edu //XXX Junk code to surpress the warning 1485124Sgblack@eecs.umich.edu if (storeCheck) seg = seg; 1495124Sgblack@eecs.umich.edu 1505140Sgblack@eecs.umich.edu // Get cr0. This will tell us how to do translation. We'll assume it was 1515140Sgblack@eecs.umich.edu // verified to be correct and consistent when set. 1525140Sgblack@eecs.umich.edu CR0 cr0 = tc->readMiscRegNoEffect(MISCREG_CR0); 1535140Sgblack@eecs.umich.edu 1545140Sgblack@eecs.umich.edu // If protected mode has been enabled... 1555140Sgblack@eecs.umich.edu if (cr0.pe) { 1565140Sgblack@eecs.umich.edu Efer efer = tc->readMiscRegNoEffect(MISCREG_EFER); 1575140Sgblack@eecs.umich.edu SegAttr csAttr = tc->readMiscRegNoEffect(MISCREG_CS_ATTR); 1585140Sgblack@eecs.umich.edu // If we're not in 64-bit mode, do protection/limit checks 1595140Sgblack@eecs.umich.edu if (!efer.lma || !csAttr.longMode) { 1605140Sgblack@eecs.umich.edu SegAttr attr = tc->readMiscRegNoEffect(MISCREG_SEG_ATTR(seg)); 1615140Sgblack@eecs.umich.edu if (!attr.writable && write) 1625140Sgblack@eecs.umich.edu return new GeneralProtection(0); 1635140Sgblack@eecs.umich.edu if (!attr.readable && !write && !execute) 1645140Sgblack@eecs.umich.edu return new GeneralProtection(0); 1655140Sgblack@eecs.umich.edu Addr base = tc->readMiscRegNoEffect(MISCREG_SEG_BASE(seg)); 1665140Sgblack@eecs.umich.edu Addr limit = tc->readMiscRegNoEffect(MISCREG_SEG_LIMIT(seg)); 1675140Sgblack@eecs.umich.edu if (!attr.expandDown) { 1685140Sgblack@eecs.umich.edu // We don't have to worry about the access going around the 1695140Sgblack@eecs.umich.edu // end of memory because accesses will be broken up into 1705140Sgblack@eecs.umich.edu // pieces at boundaries aligned on sizes smaller than an 1715140Sgblack@eecs.umich.edu // entire address space. We do have to worry about the limit 1725140Sgblack@eecs.umich.edu // being less than the base. 1735140Sgblack@eecs.umich.edu if (limit < base) { 1745140Sgblack@eecs.umich.edu if (limit < vaddr + req->getSize() && vaddr < base) 1755140Sgblack@eecs.umich.edu return new GeneralProtection(0); 1765140Sgblack@eecs.umich.edu } else { 1775140Sgblack@eecs.umich.edu if (limit < vaddr + req->getSize()) 1785140Sgblack@eecs.umich.edu return new GeneralProtection(0); 1795140Sgblack@eecs.umich.edu } 1805140Sgblack@eecs.umich.edu } else { 1815140Sgblack@eecs.umich.edu if (limit < base) { 1825140Sgblack@eecs.umich.edu if (vaddr <= limit || vaddr + req->getSize() >= base) 1835140Sgblack@eecs.umich.edu return new GeneralProtection(0); 1845140Sgblack@eecs.umich.edu } else { 1855140Sgblack@eecs.umich.edu if (vaddr <= limit && vaddr + req->getSize() >= base) 1865140Sgblack@eecs.umich.edu return new GeneralProtection(0); 1875140Sgblack@eecs.umich.edu } 1885140Sgblack@eecs.umich.edu } 1895140Sgblack@eecs.umich.edu } 1905140Sgblack@eecs.umich.edu // If paging is enabled, do the translation. 1915140Sgblack@eecs.umich.edu if (cr0.pg) { 1925140Sgblack@eecs.umich.edu // The vaddr already has the segment base applied. 1935140Sgblack@eecs.umich.edu TlbEntry *entry = lookup(vaddr); 1945140Sgblack@eecs.umich.edu if (!entry) { 1955124Sgblack@eecs.umich.edu#if FULL_SYSTEM 1965140Sgblack@eecs.umich.edu return new TlbFault(); 1975124Sgblack@eecs.umich.edu#else 1985140Sgblack@eecs.umich.edu return new TlbFault(vaddr); 1995124Sgblack@eecs.umich.edu#endif 2005140Sgblack@eecs.umich.edu } else { 2015140Sgblack@eecs.umich.edu // Do paging protection checks. 2025140Sgblack@eecs.umich.edu Addr paddr = entry->pageStart | (vaddr & mask(12)); 2035140Sgblack@eecs.umich.edu req->setPaddr(paddr); 2045140Sgblack@eecs.umich.edu } 2055140Sgblack@eecs.umich.edu } else { 2065140Sgblack@eecs.umich.edu //Use the address which already has segmentation applied. 2075140Sgblack@eecs.umich.edu req->setPaddr(vaddr); 2085140Sgblack@eecs.umich.edu } 2095124Sgblack@eecs.umich.edu } else { 2105140Sgblack@eecs.umich.edu // Real mode 2115140Sgblack@eecs.umich.edu req->setPaddr(vaddr); 2125124Sgblack@eecs.umich.edu } 2135086Sgblack@eecs.umich.edu return NoFault; 2145086Sgblack@eecs.umich.edu}; 2155086Sgblack@eecs.umich.edu 2165140Sgblack@eecs.umich.eduFault 2175140Sgblack@eecs.umich.eduDTB::translate(RequestPtr &req, ThreadContext *tc, bool write) 2185140Sgblack@eecs.umich.edu{ 2195140Sgblack@eecs.umich.edu return TLB::translate<FakeDTLBFault>(req, tc, write, false); 2205140Sgblack@eecs.umich.edu} 2215140Sgblack@eecs.umich.edu 2225140Sgblack@eecs.umich.eduFault 2235140Sgblack@eecs.umich.eduITB::translate(RequestPtr &req, ThreadContext *tc) 2245140Sgblack@eecs.umich.edu{ 2255140Sgblack@eecs.umich.edu return TLB::translate<FakeITLBFault>(req, tc, false, true); 2265140Sgblack@eecs.umich.edu} 2275140Sgblack@eecs.umich.edu 2285086Sgblack@eecs.umich.edu#if FULL_SYSTEM 2295086Sgblack@eecs.umich.edu 2305086Sgblack@eecs.umich.eduTick 2315086Sgblack@eecs.umich.eduDTB::doMmuRegRead(ThreadContext *tc, Packet *pkt) 2325086Sgblack@eecs.umich.edu{ 2335100Ssaidi@eecs.umich.edu return tc->getCpuPtr()->ticks(1); 2345086Sgblack@eecs.umich.edu} 2355086Sgblack@eecs.umich.edu 2365086Sgblack@eecs.umich.eduTick 2375086Sgblack@eecs.umich.eduDTB::doMmuRegWrite(ThreadContext *tc, Packet *pkt) 2385086Sgblack@eecs.umich.edu{ 2395100Ssaidi@eecs.umich.edu return tc->getCpuPtr()->ticks(1); 2405086Sgblack@eecs.umich.edu} 2415086Sgblack@eecs.umich.edu 2425086Sgblack@eecs.umich.edu#endif 2435086Sgblack@eecs.umich.edu 2445086Sgblack@eecs.umich.eduvoid 2455086Sgblack@eecs.umich.eduTLB::serialize(std::ostream &os) 2465086Sgblack@eecs.umich.edu{ 2475086Sgblack@eecs.umich.edu} 2485086Sgblack@eecs.umich.edu 2495086Sgblack@eecs.umich.eduvoid 2505086Sgblack@eecs.umich.eduTLB::unserialize(Checkpoint *cp, const std::string §ion) 2515086Sgblack@eecs.umich.edu{ 2525086Sgblack@eecs.umich.edu} 2535086Sgblack@eecs.umich.edu 2545086Sgblack@eecs.umich.eduvoid 2555086Sgblack@eecs.umich.eduDTB::serialize(std::ostream &os) 2565086Sgblack@eecs.umich.edu{ 2575086Sgblack@eecs.umich.edu TLB::serialize(os); 2585086Sgblack@eecs.umich.edu} 2595086Sgblack@eecs.umich.edu 2605086Sgblack@eecs.umich.eduvoid 2615086Sgblack@eecs.umich.eduDTB::unserialize(Checkpoint *cp, const std::string §ion) 2625086Sgblack@eecs.umich.edu{ 2635086Sgblack@eecs.umich.edu TLB::unserialize(cp, section); 2645086Sgblack@eecs.umich.edu} 2655086Sgblack@eecs.umich.edu 2665086Sgblack@eecs.umich.edu/* end namespace X86ISA */ } 2675086Sgblack@eecs.umich.edu 2684997Sgblack@eecs.umich.eduX86ISA::ITB * 2694997Sgblack@eecs.umich.eduX86ITBParams::create() 2704997Sgblack@eecs.umich.edu{ 2715038Sgblack@eecs.umich.edu return new X86ISA::ITB(this); 2724997Sgblack@eecs.umich.edu} 2734997Sgblack@eecs.umich.edu 2744997Sgblack@eecs.umich.eduX86ISA::DTB * 2754997Sgblack@eecs.umich.eduX86DTBParams::create() 2764997Sgblack@eecs.umich.edu{ 2775038Sgblack@eecs.umich.edu return new X86ISA::DTB(this); 2784997Sgblack@eecs.umich.edu} 279