page_table.cc revision 11793:ef606668d247
12089SN/A/*
22089SN/A * Copyright (c) 2014 Advanced Micro Devices, Inc.
35222Sksewell@umich.edu * Copyright (c) 2003 The Regents of The University of Michigan
45222Sksewell@umich.edu * All rights reserved.
55222Sksewell@umich.edu *
65222Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
75222Sksewell@umich.edu * modification, are permitted provided that the following conditions are
85222Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
95222Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
105222Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
115222Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
125222Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
135222Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
145222Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
155222Sksewell@umich.edu * this software without specific prior written permission.
165222Sksewell@umich.edu *
175222Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
185222Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
195222Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
205222Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
215222Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
225222Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
235222Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
245222Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
255222Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
265222Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
275222Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
285222Sksewell@umich.edu *
295222Sksewell@umich.edu * Authors: Steve Reinhardt
305222Sksewell@umich.edu *          Ron Dreslinski
315222Sksewell@umich.edu *          Ali Saidi
325222Sksewell@umich.edu */
335222Sksewell@umich.edu
345222Sksewell@umich.edu/**
355222Sksewell@umich.edu * @file
362706Sksewell@umich.edu * Definitions of functional page table.
372088SN/A */
382088SN/A#include "mem/page_table.hh"
392089SN/A
402088SN/A#include <fstream>
412088SN/A#include <map>
422089SN/A#include <memory>
432088SN/A#include <string>
442239SN/A
452239SN/A#include "base/bitfield.hh"
462239SN/A#include "base/intmath.hh"
472131SN/A#include "base/trace.hh"
482131SN/A#include "config/the_isa.hh"
492131SN/A#include "debug/MMU.hh"
502131SN/A#include "sim/faults.hh"
512131SN/A#include "sim/sim_object.hh"
522131SN/A
532131SN/Ausing namespace std;
542131SN/Ausing namespace TheISA;
552131SN/A
562131SN/AFuncPageTable::FuncPageTable(const std::string &__name,
572088SN/A                             uint64_t _pid, Addr _pageSize)
582131SN/A        : PageTableBase(__name, _pid, _pageSize)
592088SN/A{
602131SN/A}
612131SN/A
622131SN/AFuncPageTable::~FuncPageTable()
632088SN/A{
642131SN/A}
652131SN/A
662088SN/Avoid
672088SN/AFuncPageTable::map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags)
682088SN/A{
692089SN/A    bool clobber = flags & Clobber;
702088SN/A    // starting address must be page aligned
712088SN/A    assert(pageOffset(vaddr) == 0);
722131SN/A
732131SN/A    DPRINTF(MMU, "Allocating Page: %#x-%#x\n", vaddr, vaddr+ size);
742131SN/A
752131SN/A    for (; size > 0; size -= pageSize, vaddr += pageSize, paddr += pageSize) {
762131SN/A        if (!clobber && (pTable.find(vaddr) != pTable.end())) {
772131SN/A            // already mapped
782131SN/A            fatal("FuncPageTable::allocate: addr 0x%x already mapped", vaddr);
792131SN/A        }
802131SN/A
812131SN/A        pTable[vaddr] = TheISA::TlbEntry(pid, vaddr, paddr,
822131SN/A                                         flags & Uncacheable,
832131SN/A                                         flags & ReadOnly);
842131SN/A        eraseCacheEntry(vaddr);
852131SN/A        updateCache(vaddr, pTable[vaddr]);
862131SN/A    }
872131SN/A}
882965Sksewell@umich.edu
892965Sksewell@umich.eduvoid
902965Sksewell@umich.eduFuncPageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
915222Sksewell@umich.edu{
922965Sksewell@umich.edu    assert(pageOffset(vaddr) == 0);
932965Sksewell@umich.edu    assert(pageOffset(new_vaddr) == 0);
942965Sksewell@umich.edu
952965Sksewell@umich.edu    DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
962965Sksewell@umich.edu            new_vaddr, size);
972965Sksewell@umich.edu
982965Sksewell@umich.edu    for (; size > 0;
992965Sksewell@umich.edu         size -= pageSize, vaddr += pageSize, new_vaddr += pageSize)
1002965Sksewell@umich.edu    {
1012965Sksewell@umich.edu        assert(pTable.find(vaddr) != pTable.end());
1022965Sksewell@umich.edu
1032965Sksewell@umich.edu        pTable[new_vaddr] = pTable[vaddr];
1042965Sksewell@umich.edu        pTable.erase(vaddr);
1052479SN/A        eraseCacheEntry(vaddr);
1062479SN/A        pTable[new_vaddr].updateVaddr(new_vaddr);
1072965Sksewell@umich.edu        updateCache(new_vaddr, pTable[new_vaddr]);
1082965Sksewell@umich.edu    }
1095222Sksewell@umich.edu}
1102492SN/A
1112131SN/Avoid
1122088SN/AFuncPageTable::unmap(Addr vaddr, int64_t size)
1132131SN/A{
1142131SN/A    assert(pageOffset(vaddr) == 0);
1152088SN/A
1162088SN/A    DPRINTF(MMU, "Unmapping page: %#x-%#x\n", vaddr, vaddr+ size);
1172088SN/A
118    for (; size > 0; size -= pageSize, vaddr += pageSize) {
119        assert(pTable.find(vaddr) != pTable.end());
120        pTable.erase(vaddr);
121        eraseCacheEntry(vaddr);
122    }
123
124}
125
126bool
127FuncPageTable::isUnmapped(Addr vaddr, int64_t size)
128{
129    // starting address must be page aligned
130    assert(pageOffset(vaddr) == 0);
131
132    for (; size > 0; size -= pageSize, vaddr += pageSize) {
133        if (pTable.find(vaddr) != pTable.end()) {
134            return false;
135        }
136    }
137
138    return true;
139}
140
141bool
142FuncPageTable::lookup(Addr vaddr, TheISA::TlbEntry &entry)
143{
144    Addr page_addr = pageAlign(vaddr);
145
146    if (pTableCache[0].valid && pTableCache[0].vaddr == page_addr) {
147        entry = pTableCache[0].entry;
148        return true;
149    }
150    if (pTableCache[1].valid && pTableCache[1].vaddr == page_addr) {
151        entry = pTableCache[1].entry;
152        return true;
153    }
154    if (pTableCache[2].valid && pTableCache[2].vaddr == page_addr) {
155        entry = pTableCache[2].entry;
156        return true;
157    }
158
159    PTableItr iter = pTable.find(page_addr);
160
161    if (iter == pTable.end()) {
162        return false;
163    }
164
165    updateCache(page_addr, iter->second);
166    entry = iter->second;
167    return true;
168}
169
170bool
171PageTableBase::translate(Addr vaddr, Addr &paddr)
172{
173    TheISA::TlbEntry entry;
174    if (!lookup(vaddr, entry)) {
175        DPRINTF(MMU, "Couldn't Translate: %#x\n", vaddr);
176        return false;
177    }
178    paddr = pageOffset(vaddr) + entry.pageStart();
179    DPRINTF(MMU, "Translating: %#x->%#x\n", vaddr, paddr);
180    return true;
181}
182
183Fault
184PageTableBase::translate(RequestPtr req)
185{
186    Addr paddr;
187    assert(pageAlign(req->getVaddr() + req->getSize() - 1)
188           == pageAlign(req->getVaddr()));
189    if (!translate(req->getVaddr(), paddr)) {
190        return Fault(new GenericPageTableFault(req->getVaddr()));
191    }
192    req->setPaddr(paddr);
193    if ((paddr & (pageSize - 1)) + req->getSize() > pageSize) {
194        panic("Request spans page boundaries!\n");
195        return NoFault;
196    }
197    return NoFault;
198}
199
200void
201FuncPageTable::serialize(CheckpointOut &cp) const
202{
203    paramOut(cp, "ptable.size", pTable.size());
204
205    PTable::size_type count = 0;
206    for (auto &pte : pTable) {
207        ScopedCheckpointSection sec(cp, csprintf("Entry%d", count++));
208
209        paramOut(cp, "vaddr", pte.first);
210        pte.second.serialize(cp);
211    }
212    assert(count == pTable.size());
213}
214
215void
216FuncPageTable::unserialize(CheckpointIn &cp)
217{
218    int count;
219    paramIn(cp, "ptable.size", count);
220
221    for (int i = 0; i < count; ++i) {
222        ScopedCheckpointSection sec(cp, csprintf("Entry%d", i));
223
224        std::unique_ptr<TheISA::TlbEntry> entry;
225        Addr vaddr;
226
227        paramIn(cp, "vaddr", vaddr);
228        entry.reset(new TheISA::TlbEntry());
229        entry->unserialize(cp);
230
231        pTable[vaddr] = *entry;
232    }
233}
234
235