page_table.cc revision 12448
12379SN/A/*
210298Salexandru.dutu@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
32379SN/A * Copyright (c) 2003 The Regents of The University of Michigan
42379SN/A * All rights reserved.
52379SN/A *
62379SN/A * Redistribution and use in source and binary forms, with or without
72379SN/A * modification, are permitted provided that the following conditions are
82379SN/A * met: redistributions of source code must retain the above copyright
92379SN/A * notice, this list of conditions and the following disclaimer;
102379SN/A * redistributions in binary form must reproduce the above copyright
112379SN/A * notice, this list of conditions and the following disclaimer in the
122379SN/A * documentation and/or other materials provided with the distribution;
132379SN/A * neither the name of the copyright holders nor the names of its
142379SN/A * contributors may be used to endorse or promote products derived from
152379SN/A * this software without specific prior written permission.
162379SN/A *
172379SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
182379SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
192379SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
202379SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
212379SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
222379SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
232379SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
242379SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
252379SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
262379SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
272379SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282665Ssaidi@eecs.umich.edu *
292665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
302665Ssaidi@eecs.umich.edu *          Ron Dreslinski
313311Ssaidi@eecs.umich.edu *          Ali Saidi
322379SN/A */
332379SN/A
342379SN/A/**
352379SN/A * @file
3610298Salexandru.dutu@amd.com * Definitions of functional page table.
372379SN/A */
3811793Sbrandon.potter@amd.com#include "mem/page_table.hh"
3911793Sbrandon.potter@amd.com
402379SN/A#include <string>
412379SN/A
422379SN/A#include "base/trace.hh"
436658Snate@binkert.org#include "config/the_isa.hh"
448232Snate@binkert.org#include "debug/MMU.hh"
457678Sgblack@eecs.umich.edu#include "sim/faults.hh"
4611800Sbrandon.potter@amd.com#include "sim/serialize.hh"
472379SN/A
482399SN/Ausing namespace std;
492423SN/Ausing namespace TheISA;
502399SN/A
5112448Sgabeblack@google.comEmulationPageTable::~EmulationPageTable()
522379SN/A{
5312442Sgabeblack@google.com    for (auto &iter : pTable)
5412442Sgabeblack@google.com        delete iter.second;
552379SN/A}
562379SN/A
572399SN/Avoid
5812448Sgabeblack@google.comEmulationPageTable::map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags)
592399SN/A{
6010558Salexandru.dutu@amd.com    bool clobber = flags & Clobber;
612399SN/A    // starting address must be page aligned
622399SN/A    assert(pageOffset(vaddr) == 0);
632399SN/A
6412448Sgabeblack@google.com    DPRINTF(MMU, "Allocating Page: %#x-%#x\n", vaddr, vaddr + size);
653311Ssaidi@eecs.umich.edu
6612448Sgabeblack@google.com    while (size > 0) {
6712442Sgabeblack@google.com        auto it = pTable.find(vaddr);
6812442Sgabeblack@google.com        if (it != pTable.end()) {
6912442Sgabeblack@google.com            if (clobber) {
7012442Sgabeblack@google.com                delete it->second;
7112442Sgabeblack@google.com            } else {
7212442Sgabeblack@google.com                // already mapped
7312448Sgabeblack@google.com                fatal("EmulationPageTable::allocate: addr %#x already mapped",
7412442Sgabeblack@google.com                      vaddr);
7512442Sgabeblack@google.com            }
7612442Sgabeblack@google.com        } else {
7712442Sgabeblack@google.com            it = pTable.emplace(vaddr, nullptr).first;
782399SN/A        }
792399SN/A
8012442Sgabeblack@google.com        it->second = new TheISA::TlbEntry(pid, vaddr, paddr,
8110558Salexandru.dutu@amd.com                                         flags & Uncacheable,
8210558Salexandru.dutu@amd.com                                         flags & ReadOnly);
8312448Sgabeblack@google.com        size -= pageSize;
8412448Sgabeblack@google.com        vaddr += pageSize;
8512448Sgabeblack@google.com        paddr += pageSize;
862399SN/A    }
872399SN/A}
882399SN/A
895877Shsul@eecs.umich.eduvoid
9012448Sgabeblack@google.comEmulationPageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
915877Shsul@eecs.umich.edu{
925877Shsul@eecs.umich.edu    assert(pageOffset(vaddr) == 0);
935877Shsul@eecs.umich.edu    assert(pageOffset(new_vaddr) == 0);
945877Shsul@eecs.umich.edu
955877Shsul@eecs.umich.edu    DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
965877Shsul@eecs.umich.edu            new_vaddr, size);
975877Shsul@eecs.umich.edu
9812448Sgabeblack@google.com    while (size > 0) {
9912442Sgabeblack@google.com        auto new_it = pTable.find(new_vaddr);
10012442Sgabeblack@google.com        auto old_it = pTable.find(vaddr);
10112442Sgabeblack@google.com        assert(old_it != pTable.end() && new_it == pTable.end());
1025877Shsul@eecs.umich.edu
10312442Sgabeblack@google.com        new_it->second = old_it->second;
10412442Sgabeblack@google.com        pTable.erase(old_it);
10512442Sgabeblack@google.com        new_it->second->updateVaddr(new_vaddr);
10612448Sgabeblack@google.com        size -= pageSize;
10712448Sgabeblack@google.com        vaddr += pageSize;
10812448Sgabeblack@google.com        new_vaddr += pageSize;
1095877Shsul@eecs.umich.edu    }
1105877Shsul@eecs.umich.edu}
1115877Shsul@eecs.umich.edu
1125877Shsul@eecs.umich.eduvoid
11312448Sgabeblack@google.comEmulationPageTable::getMappings(std::vector<std::pair<Addr, Addr>> *addr_maps)
11411886Sbrandon.potter@amd.com{
11511886Sbrandon.potter@amd.com    for (auto &iter : pTable)
11612442Sgabeblack@google.com        addr_maps->push_back(make_pair(iter.first, iter.second->pageStart()));
11711886Sbrandon.potter@amd.com}
11811886Sbrandon.potter@amd.com
11911886Sbrandon.potter@amd.comvoid
12012448Sgabeblack@google.comEmulationPageTable::unmap(Addr vaddr, int64_t size)
1215877Shsul@eecs.umich.edu{
1225877Shsul@eecs.umich.edu    assert(pageOffset(vaddr) == 0);
1235877Shsul@eecs.umich.edu
1248601Ssteve.reinhardt@amd.com    DPRINTF(MMU, "Unmapping page: %#x-%#x\n", vaddr, vaddr+ size);
1255877Shsul@eecs.umich.edu
12612448Sgabeblack@google.com    while (size > 0) {
12712442Sgabeblack@google.com        auto it = pTable.find(vaddr);
12812442Sgabeblack@google.com        assert(it != pTable.end());
12912442Sgabeblack@google.com        delete it->second;
13012442Sgabeblack@google.com        pTable.erase(it);
13112448Sgabeblack@google.com        size -= pageSize;
13212448Sgabeblack@google.com        vaddr += pageSize;
1335877Shsul@eecs.umich.edu    }
1345877Shsul@eecs.umich.edu}
1355877Shsul@eecs.umich.edu
1362399SN/Abool
13712448Sgabeblack@google.comEmulationPageTable::isUnmapped(Addr vaddr, int64_t size)
1388600Ssteve.reinhardt@amd.com{
1398600Ssteve.reinhardt@amd.com    // starting address must be page aligned
1408600Ssteve.reinhardt@amd.com    assert(pageOffset(vaddr) == 0);
1418600Ssteve.reinhardt@amd.com
14212448Sgabeblack@google.com    for (int64_t offset = 0; offset < size; offset += pageSize)
14312448Sgabeblack@google.com        if (pTable.find(vaddr + offset) != pTable.end())
1448600Ssteve.reinhardt@amd.com            return false;
1458600Ssteve.reinhardt@amd.com
1468600Ssteve.reinhardt@amd.com    return true;
1478600Ssteve.reinhardt@amd.com}
1488600Ssteve.reinhardt@amd.com
1498600Ssteve.reinhardt@amd.combool
15012448Sgabeblack@google.comEmulationPageTable::lookup(Addr vaddr, TheISA::TlbEntry &entry)
1512399SN/A{
1522399SN/A    Addr page_addr = pageAlign(vaddr);
1532809Ssaidi@eecs.umich.edu
1545004Sgblack@eecs.umich.edu    PTableItr iter = pTable.find(page_addr);
1552399SN/A
15612448Sgabeblack@google.com    if (iter == pTable.end())
1572399SN/A        return false;
1582399SN/A
15912442Sgabeblack@google.com    entry = *iter->second;
1602399SN/A    return true;
1612399SN/A}
1622399SN/A
1635004Sgblack@eecs.umich.edubool
16412448Sgabeblack@google.comEmulationPageTable::translate(Addr vaddr, Addr &paddr)
1655004Sgblack@eecs.umich.edu{
1665004Sgblack@eecs.umich.edu    TheISA::TlbEntry entry;
1675183Ssaidi@eecs.umich.edu    if (!lookup(vaddr, entry)) {
1685183Ssaidi@eecs.umich.edu        DPRINTF(MMU, "Couldn't Translate: %#x\n", vaddr);
1695004Sgblack@eecs.umich.edu        return false;
1705183Ssaidi@eecs.umich.edu    }
1715184Sgblack@eecs.umich.edu    paddr = pageOffset(vaddr) + entry.pageStart();
1725183Ssaidi@eecs.umich.edu    DPRINTF(MMU, "Translating: %#x->%#x\n", vaddr, paddr);
1735004Sgblack@eecs.umich.edu    return true;
1745004Sgblack@eecs.umich.edu}
1752399SN/A
1762394SN/AFault
17712448Sgabeblack@google.comEmulationPageTable::translate(RequestPtr req)
1782394SN/A{
1792532SN/A    Addr paddr;
18012448Sgabeblack@google.com    assert(pageAlign(req->getVaddr() + req->getSize() - 1) ==
18112448Sgabeblack@google.com           pageAlign(req->getVaddr()));
18212448Sgabeblack@google.com    if (!translate(req->getVaddr(), paddr))
1835004Sgblack@eecs.umich.edu        return Fault(new GenericPageTableFault(req->getVaddr()));
1842532SN/A    req->setPaddr(paddr);
1855004Sgblack@eecs.umich.edu    if ((paddr & (pageSize - 1)) + req->getSize() > pageSize) {
1865004Sgblack@eecs.umich.edu        panic("Request spans page boundaries!\n");
1875004Sgblack@eecs.umich.edu        return NoFault;
1885004Sgblack@eecs.umich.edu    }
1895004Sgblack@eecs.umich.edu    return NoFault;
1902394SN/A}
1913311Ssaidi@eecs.umich.edu
1923311Ssaidi@eecs.umich.eduvoid
19312448Sgabeblack@google.comEmulationPageTable::serialize(CheckpointOut &cp) const
1943311Ssaidi@eecs.umich.edu{
19510905Sandreas.sandberg@arm.com    paramOut(cp, "ptable.size", pTable.size());
1963320Shsul@eecs.umich.edu
1976227Snate@binkert.org    PTable::size_type count = 0;
19810905Sandreas.sandberg@arm.com    for (auto &pte : pTable) {
19910905Sandreas.sandberg@arm.com        ScopedCheckpointSection sec(cp, csprintf("Entry%d", count++));
2003311Ssaidi@eecs.umich.edu
20110905Sandreas.sandberg@arm.com        paramOut(cp, "vaddr", pte.first);
20212442Sgabeblack@google.com        pte.second->serialize(cp);
2033311Ssaidi@eecs.umich.edu    }
2043311Ssaidi@eecs.umich.edu    assert(count == pTable.size());
2053311Ssaidi@eecs.umich.edu}
2063311Ssaidi@eecs.umich.edu
2073311Ssaidi@eecs.umich.eduvoid
20812448Sgabeblack@google.comEmulationPageTable::unserialize(CheckpointIn &cp)
2093311Ssaidi@eecs.umich.edu{
21010905Sandreas.sandberg@arm.com    int count;
21110905Sandreas.sandberg@arm.com    paramIn(cp, "ptable.size", count);
2123311Ssaidi@eecs.umich.edu
21310905Sandreas.sandberg@arm.com    for (int i = 0; i < count; ++i) {
21410905Sandreas.sandberg@arm.com        ScopedCheckpointSection sec(cp, csprintf("Entry%d", i));
2153311Ssaidi@eecs.umich.edu
21612442Sgabeblack@google.com        TheISA::TlbEntry *entry = new TheISA::TlbEntry();
21710905Sandreas.sandberg@arm.com        entry->unserialize(cp);
21810905Sandreas.sandberg@arm.com
21912442Sgabeblack@google.com        Addr vaddr;
22012442Sgabeblack@google.com        paramIn(cp, "vaddr", vaddr);
22112442Sgabeblack@google.com
22212442Sgabeblack@google.com        pTable[vaddr] = entry;
2236818SLisa.Hsu@amd.com    }
2243311Ssaidi@eecs.umich.edu}
2253311Ssaidi@eecs.umich.edu
226