1/*
2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2003 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Steve Reinhardt
30 *          Ron Dreslinski
31 *          Ali Saidi
32 */
33
34/**
35 * @file
36 * Definitions of functional page table.
37 */
38#include "mem/page_table.hh"
39
40#include <string>
41
42#include "base/compiler.hh"
43#include "base/trace.hh"
44#include "debug/MMU.hh"
45#include "sim/faults.hh"
46#include "sim/serialize.hh"
47
48void
49EmulationPageTable::map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags)
50{
51    bool clobber = flags & Clobber;
52    // starting address must be page aligned
53    assert(pageOffset(vaddr) == 0);
54
55    DPRINTF(MMU, "Allocating Page: %#x-%#x\n", vaddr, vaddr + size);
56
57    while (size > 0) {
58        auto it = pTable.find(vaddr);
59        if (it != pTable.end()) {
60            // already mapped
61            panic_if(!clobber,
62                     "EmulationPageTable::allocate: addr %#x already mapped",
63                     vaddr);
64            it->second = Entry(paddr, flags);
65        } else {
66            pTable.emplace(vaddr, Entry(paddr, flags));
67        }
68
69        size -= pageSize;
70        vaddr += pageSize;
71        paddr += pageSize;
72    }
73}
74
75void
76EmulationPageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
77{
78    assert(pageOffset(vaddr) == 0);
79    assert(pageOffset(new_vaddr) == 0);
80
81    DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
82            new_vaddr, size);
83
84    while (size > 0) {
85        auto new_it M5_VAR_USED = pTable.find(new_vaddr);
86        auto old_it = pTable.find(vaddr);
87        assert(old_it != pTable.end() && new_it == pTable.end());
88
89        pTable.emplace(new_vaddr, old_it->second);
90        pTable.erase(old_it);
91        size -= pageSize;
92        vaddr += pageSize;
93        new_vaddr += pageSize;
94    }
95}
96
97void
98EmulationPageTable::getMappings(std::vector<std::pair<Addr, Addr>> *addr_maps)
99{
100    for (auto &iter : pTable)
101        addr_maps->push_back(std::make_pair(iter.first, iter.second.paddr));
102}
103
104void
105EmulationPageTable::unmap(Addr vaddr, int64_t size)
106{
107    assert(pageOffset(vaddr) == 0);
108
109    DPRINTF(MMU, "Unmapping page: %#x-%#x\n", vaddr, vaddr + size);
110
111    while (size > 0) {
112        auto it = pTable.find(vaddr);
113        assert(it != pTable.end());
114        pTable.erase(it);
115        size -= pageSize;
116        vaddr += pageSize;
117    }
118}
119
120bool
121EmulationPageTable::isUnmapped(Addr vaddr, int64_t size)
122{
123    // starting address must be page aligned
124    assert(pageOffset(vaddr) == 0);
125
126    for (int64_t offset = 0; offset < size; offset += pageSize)
127        if (pTable.find(vaddr + offset) != pTable.end())
128            return false;
129
130    return true;
131}
132
133const EmulationPageTable::Entry *
134EmulationPageTable::lookup(Addr vaddr)
135{
136    Addr page_addr = pageAlign(vaddr);
137    PTableItr iter = pTable.find(page_addr);
138    if (iter == pTable.end())
139        return nullptr;
140    return &(iter->second);
141}
142
143bool
144EmulationPageTable::translate(Addr vaddr, Addr &paddr)
145{
146    const Entry *entry = lookup(vaddr);
147    if (!entry) {
148        DPRINTF(MMU, "Couldn't Translate: %#x\n", vaddr);
149        return false;
150    }
151    paddr = pageOffset(vaddr) + entry->paddr;
152    DPRINTF(MMU, "Translating: %#x->%#x\n", vaddr, paddr);
153    return true;
154}
155
156Fault
157EmulationPageTable::translate(const RequestPtr &req)
158{
159    Addr paddr;
160    assert(pageAlign(req->getVaddr() + req->getSize() - 1) ==
161           pageAlign(req->getVaddr()));
162    if (!translate(req->getVaddr(), paddr))
163        return Fault(new GenericPageTableFault(req->getVaddr()));
164    req->setPaddr(paddr);
165    if ((paddr & (pageSize - 1)) + req->getSize() > pageSize) {
166        panic("Request spans page boundaries!\n");
167        return NoFault;
168    }
169    return NoFault;
170}
171
172void
173EmulationPageTable::serialize(CheckpointOut &cp) const
174{
175    paramOut(cp, "ptable.size", pTable.size());
176
177    PTable::size_type count = 0;
178    for (auto &pte : pTable) {
179        ScopedCheckpointSection sec(cp, csprintf("Entry%d", count++));
180
181        paramOut(cp, "vaddr", pte.first);
182        paramOut(cp, "paddr", pte.second.paddr);
183        paramOut(cp, "flags", pte.second.flags);
184    }
185    assert(count == pTable.size());
186}
187
188void
189EmulationPageTable::unserialize(CheckpointIn &cp)
190{
191    int count;
192    paramIn(cp, "ptable.size", count);
193
194    for (int i = 0; i < count; ++i) {
195        ScopedCheckpointSection sec(cp, csprintf("Entry%d", i));
196
197        Addr vaddr;
198        UNSERIALIZE_SCALAR(vaddr);
199        Addr paddr;
200        uint64_t flags;
201        UNSERIALIZE_SCALAR(paddr);
202        UNSERIALIZE_SCALAR(flags);
203
204        pTable.emplace(vaddr, Entry(paddr, flags));
205    }
206}
207
208