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/compiler.hh"
43 #include "base/trace.hh"
44 #include "debug/MMU.hh"
45 #include "sim/faults.hh"
46 #include "sim/serialize.hh"
49 EmulationPageTable::map(Addr vaddr
, Addr paddr
, int64_t size
, uint64_t flags
)
51 bool clobber
= flags
& Clobber
;
52 // starting address must be page aligned
53 assert(pageOffset(vaddr
) == 0);
55 DPRINTF(MMU
, "Allocating Page: %#x-%#x\n", vaddr
, vaddr
+ size
);
58 auto it
= pTable
.find(vaddr
);
59 if (it
!= pTable
.end()) {
62 "EmulationPageTable::allocate: addr %#x already mapped",
64 it
->second
= Entry(paddr
, flags
);
66 pTable
.emplace(vaddr
, Entry(paddr
, flags
));
76 EmulationPageTable::remap(Addr vaddr
, int64_t size
, Addr new_vaddr
)
78 assert(pageOffset(vaddr
) == 0);
79 assert(pageOffset(new_vaddr
) == 0);
81 DPRINTF(MMU
, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr
,
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());
89 pTable
.emplace(new_vaddr
, old_it
->second
);
93 new_vaddr
+= pageSize
;
98 EmulationPageTable::getMappings(std::vector
<std::pair
<Addr
, Addr
>> *addr_maps
)
100 for (auto &iter
: pTable
)
101 addr_maps
->push_back(std::make_pair(iter
.first
, iter
.second
.paddr
));
105 EmulationPageTable::unmap(Addr vaddr
, int64_t size
)
107 assert(pageOffset(vaddr
) == 0);
109 DPRINTF(MMU
, "Unmapping page: %#x-%#x\n", vaddr
, vaddr
+ size
);
112 auto it
= pTable
.find(vaddr
);
113 assert(it
!= pTable
.end());
121 EmulationPageTable::isUnmapped(Addr vaddr
, int64_t size
)
123 // starting address must be page aligned
124 assert(pageOffset(vaddr
) == 0);
126 for (int64_t offset
= 0; offset
< size
; offset
+= pageSize
)
127 if (pTable
.find(vaddr
+ offset
) != pTable
.end())
133 const EmulationPageTable::Entry
*
134 EmulationPageTable::lookup(Addr vaddr
)
136 Addr page_addr
= pageAlign(vaddr
);
137 PTableItr iter
= pTable
.find(page_addr
);
138 if (iter
== pTable
.end())
140 return &(iter
->second
);
144 EmulationPageTable::translate(Addr vaddr
, Addr
&paddr
)
146 const Entry
*entry
= lookup(vaddr
);
148 DPRINTF(MMU
, "Couldn't Translate: %#x\n", vaddr
);
151 paddr
= pageOffset(vaddr
) + entry
->paddr
;
152 DPRINTF(MMU
, "Translating: %#x->%#x\n", vaddr
, paddr
);
157 EmulationPageTable::translate(const RequestPtr
&req
)
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");
173 EmulationPageTable::serialize(CheckpointOut
&cp
) const
175 paramOut(cp
, "ptable.size", pTable
.size());
177 PTable::size_type count
= 0;
178 for (auto &pte
: pTable
) {
179 ScopedCheckpointSection
sec(cp
, csprintf("Entry%d", count
++));
181 paramOut(cp
, "vaddr", pte
.first
);
182 paramOut(cp
, "paddr", pte
.second
.paddr
);
183 paramOut(cp
, "flags", pte
.second
.flags
);
185 assert(count
== pTable
.size());
189 EmulationPageTable::unserialize(CheckpointIn
&cp
)
192 paramIn(cp
, "ptable.size", count
);
194 for (int i
= 0; i
< count
; ++i
) {
195 ScopedCheckpointSection
sec(cp
, csprintf("Entry%d", i
));
198 UNSERIALIZE_SCALAR(vaddr
);
201 UNSERIALIZE_SCALAR(paddr
);
202 UNSERIALIZE_SCALAR(flags
);
204 pTable
.emplace(vaddr
, Entry(paddr
, flags
));