2 * Copyright (c) 2014 Advanced Micro Devices, Inc.
3 * Copyright (c) 2003 The Regents of The University of Michigan
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.
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.
29 * Authors: Steve Reinhardt
36 * Definitions of functional page table.
38 #include "mem/page_table.hh"
42 #include "base/trace.hh"
43 #include "config/the_isa.hh"
44 #include "debug/MMU.hh"
45 #include "sim/faults.hh"
46 #include "sim/serialize.hh"
49 using namespace TheISA
;
51 EmulationPageTable::~EmulationPageTable()
53 for (auto &iter
: pTable
)
58 EmulationPageTable::map(Addr vaddr
, Addr paddr
, int64_t size
, uint64_t flags
)
60 bool clobber
= flags
& Clobber
;
61 // starting address must be page aligned
62 assert(pageOffset(vaddr
) == 0);
64 DPRINTF(MMU
, "Allocating Page: %#x-%#x\n", vaddr
, vaddr
+ size
);
67 auto it
= pTable
.find(vaddr
);
68 if (it
!= pTable
.end()) {
73 fatal("EmulationPageTable::allocate: addr %#x already mapped",
77 it
= pTable
.emplace(vaddr
, nullptr).first
;
80 it
->second
= new TheISA::TlbEntry(pid
, vaddr
, paddr
,
90 EmulationPageTable::remap(Addr vaddr
, int64_t size
, Addr new_vaddr
)
92 assert(pageOffset(vaddr
) == 0);
93 assert(pageOffset(new_vaddr
) == 0);
95 DPRINTF(MMU
, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr
,
99 auto new_it
= pTable
.find(new_vaddr
);
100 auto old_it
= pTable
.find(vaddr
);
101 assert(old_it
!= pTable
.end() && new_it
== pTable
.end());
103 new_it
->second
= old_it
->second
;
104 pTable
.erase(old_it
);
105 new_it
->second
->updateVaddr(new_vaddr
);
108 new_vaddr
+= pageSize
;
113 EmulationPageTable::getMappings(std::vector
<std::pair
<Addr
, Addr
>> *addr_maps
)
115 for (auto &iter
: pTable
)
116 addr_maps
->push_back(make_pair(iter
.first
, iter
.second
->pageStart()));
120 EmulationPageTable::unmap(Addr vaddr
, int64_t size
)
122 assert(pageOffset(vaddr
) == 0);
124 DPRINTF(MMU
, "Unmapping page: %#x-%#x\n", vaddr
, vaddr
+ size
);
127 auto it
= pTable
.find(vaddr
);
128 assert(it
!= pTable
.end());
137 EmulationPageTable::isUnmapped(Addr vaddr
, int64_t size
)
139 // starting address must be page aligned
140 assert(pageOffset(vaddr
) == 0);
142 for (int64_t offset
= 0; offset
< size
; offset
+= pageSize
)
143 if (pTable
.find(vaddr
+ offset
) != pTable
.end())
150 EmulationPageTable::lookup(Addr vaddr
, TheISA::TlbEntry
&entry
)
152 Addr page_addr
= pageAlign(vaddr
);
154 PTableItr iter
= pTable
.find(page_addr
);
156 if (iter
== pTable
.end())
159 entry
= *iter
->second
;
164 EmulationPageTable::translate(Addr vaddr
, Addr
&paddr
)
166 TheISA::TlbEntry entry
;
167 if (!lookup(vaddr
, entry
)) {
168 DPRINTF(MMU
, "Couldn't Translate: %#x\n", vaddr
);
171 paddr
= pageOffset(vaddr
) + entry
.pageStart();
172 DPRINTF(MMU
, "Translating: %#x->%#x\n", vaddr
, paddr
);
177 EmulationPageTable::translate(RequestPtr req
)
180 assert(pageAlign(req
->getVaddr() + req
->getSize() - 1) ==
181 pageAlign(req
->getVaddr()));
182 if (!translate(req
->getVaddr(), paddr
))
183 return Fault(new GenericPageTableFault(req
->getVaddr()));
184 req
->setPaddr(paddr
);
185 if ((paddr
& (pageSize
- 1)) + req
->getSize() > pageSize
) {
186 panic("Request spans page boundaries!\n");
193 EmulationPageTable::serialize(CheckpointOut
&cp
) const
195 paramOut(cp
, "ptable.size", pTable
.size());
197 PTable::size_type count
= 0;
198 for (auto &pte
: pTable
) {
199 ScopedCheckpointSection
sec(cp
, csprintf("Entry%d", count
++));
201 paramOut(cp
, "vaddr", pte
.first
);
202 pte
.second
->serialize(cp
);
204 assert(count
== pTable
.size());
208 EmulationPageTable::unserialize(CheckpointIn
&cp
)
211 paramIn(cp
, "ptable.size", count
);
213 for (int i
= 0; i
< count
; ++i
) {
214 ScopedCheckpointSection
sec(cp
, csprintf("Entry%d", i
));
216 TheISA::TlbEntry
*entry
= new TheISA::TlbEntry();
217 entry
->unserialize(cp
);
220 paramIn(cp
, "vaddr", vaddr
);
222 pTable
[vaddr
] = entry
;