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.
32 * Definitions of functional page table.
34 #include "mem/page_table.hh"
38 #include "base/compiler.hh"
39 #include "base/trace.hh"
40 #include "debug/MMU.hh"
41 #include "sim/faults.hh"
42 #include "sim/serialize.hh"
45 EmulationPageTable::map(Addr vaddr
, Addr paddr
, int64_t size
, uint64_t flags
)
47 bool clobber
= flags
& Clobber
;
48 // starting address must be page aligned
49 assert(pageOffset(vaddr
) == 0);
51 DPRINTF(MMU
, "Allocating Page: %#x-%#x\n", vaddr
, vaddr
+ size
);
54 auto it
= pTable
.find(vaddr
);
55 if (it
!= pTable
.end()) {
58 "EmulationPageTable::allocate: addr %#x already mapped",
60 it
->second
= Entry(paddr
, flags
);
62 pTable
.emplace(vaddr
, Entry(paddr
, flags
));
72 EmulationPageTable::remap(Addr vaddr
, int64_t size
, Addr new_vaddr
)
74 assert(pageOffset(vaddr
) == 0);
75 assert(pageOffset(new_vaddr
) == 0);
77 DPRINTF(MMU
, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr
,
81 M5_VAR_USED
auto new_it
= pTable
.find(new_vaddr
);
82 auto old_it
= pTable
.find(vaddr
);
83 assert(old_it
!= pTable
.end() && new_it
== pTable
.end());
85 pTable
.emplace(new_vaddr
, old_it
->second
);
89 new_vaddr
+= _pageSize
;
94 EmulationPageTable::getMappings(std::vector
<std::pair
<Addr
, Addr
>> *addr_maps
)
96 for (auto &iter
: pTable
)
97 addr_maps
->push_back(std::make_pair(iter
.first
, iter
.second
.paddr
));
101 EmulationPageTable::unmap(Addr vaddr
, int64_t size
)
103 assert(pageOffset(vaddr
) == 0);
105 DPRINTF(MMU
, "Unmapping page: %#x-%#x\n", vaddr
, vaddr
+ size
);
108 auto it
= pTable
.find(vaddr
);
109 assert(it
!= pTable
.end());
117 EmulationPageTable::isUnmapped(Addr vaddr
, int64_t size
)
119 // starting address must be page aligned
120 assert(pageOffset(vaddr
) == 0);
122 for (int64_t offset
= 0; offset
< size
; offset
+= _pageSize
)
123 if (pTable
.find(vaddr
+ offset
) != pTable
.end())
129 const EmulationPageTable::Entry
*
130 EmulationPageTable::lookup(Addr vaddr
)
132 Addr page_addr
= pageAlign(vaddr
);
133 PTableItr iter
= pTable
.find(page_addr
);
134 if (iter
== pTable
.end())
136 return &(iter
->second
);
140 EmulationPageTable::translate(Addr vaddr
, Addr
&paddr
)
142 const Entry
*entry
= lookup(vaddr
);
144 DPRINTF(MMU
, "Couldn't Translate: %#x\n", vaddr
);
147 paddr
= pageOffset(vaddr
) + entry
->paddr
;
148 DPRINTF(MMU
, "Translating: %#x->%#x\n", vaddr
, paddr
);
153 EmulationPageTable::translate(const RequestPtr
&req
)
156 assert(pageAlign(req
->getVaddr() + req
->getSize() - 1) ==
157 pageAlign(req
->getVaddr()));
158 if (!translate(req
->getVaddr(), paddr
))
159 return Fault(new GenericPageTableFault(req
->getVaddr()));
160 req
->setPaddr(paddr
);
161 if ((paddr
& (_pageSize
- 1)) + req
->getSize() > _pageSize
) {
162 panic("Request spans page boundaries!\n");
169 EmulationPageTable::serialize(CheckpointOut
&cp
) const
171 ScopedCheckpointSection
sec(cp
, "ptable");
172 paramOut(cp
, "size", pTable
.size());
174 PTable::size_type count
= 0;
175 for (auto &pte
: pTable
) {
176 ScopedCheckpointSection
sec(cp
, csprintf("Entry%d", count
++));
178 paramOut(cp
, "vaddr", pte
.first
);
179 paramOut(cp
, "paddr", pte
.second
.paddr
);
180 paramOut(cp
, "flags", pte
.second
.flags
);
182 assert(count
== pTable
.size());
186 EmulationPageTable::unserialize(CheckpointIn
&cp
)
189 ScopedCheckpointSection
sec(cp
, "ptable");
190 paramIn(cp
, "size", count
);
192 for (int i
= 0; i
< count
; ++i
) {
193 ScopedCheckpointSection
sec(cp
, csprintf("Entry%d", i
));
196 UNSERIALIZE_SCALAR(vaddr
);
199 UNSERIALIZE_SCALAR(paddr
);
200 UNSERIALIZE_SCALAR(flags
);
202 pTable
.emplace(vaddr
, Entry(paddr
, flags
));