gem5  v20.1.0.0
page_table.cc
Go to the documentation of this file.
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 
34 #include "mem/page_table.hh"
35 
36 #include <string>
37 
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"
43 
44 void
45 EmulationPageTable::map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags)
46 {
47  bool clobber = flags & Clobber;
48  // starting address must be page aligned
49  assert(pageOffset(vaddr) == 0);
50 
51  DPRINTF(MMU, "Allocating Page: %#x-%#x\n", vaddr, vaddr + size);
52 
53  while (size > 0) {
54  auto it = pTable.find(vaddr);
55  if (it != pTable.end()) {
56  // already mapped
57  panic_if(!clobber,
58  "EmulationPageTable::allocate: addr %#x already mapped",
59  vaddr);
60  it->second = Entry(paddr, flags);
61  } else {
62  pTable.emplace(vaddr, Entry(paddr, flags));
63  }
64 
65  size -= pageSize;
66  vaddr += pageSize;
67  paddr += pageSize;
68  }
69 }
70 
71 void
72 EmulationPageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
73 {
74  assert(pageOffset(vaddr) == 0);
75  assert(pageOffset(new_vaddr) == 0);
76 
77  DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
78  new_vaddr, size);
79 
80  while (size > 0) {
81  auto new_it M5_VAR_USED = pTable.find(new_vaddr);
82  auto old_it = pTable.find(vaddr);
83  assert(old_it != pTable.end() && new_it == pTable.end());
84 
85  pTable.emplace(new_vaddr, old_it->second);
86  pTable.erase(old_it);
87  size -= pageSize;
88  vaddr += pageSize;
89  new_vaddr += pageSize;
90  }
91 }
92 
93 void
95 {
96  for (auto &iter : pTable)
97  addr_maps->push_back(std::make_pair(iter.first, iter.second.paddr));
98 }
99 
100 void
102 {
103  assert(pageOffset(vaddr) == 0);
104 
105  DPRINTF(MMU, "Unmapping page: %#x-%#x\n", vaddr, vaddr + size);
106 
107  while (size > 0) {
108  auto it = pTable.find(vaddr);
109  assert(it != pTable.end());
110  pTable.erase(it);
111  size -= pageSize;
112  vaddr += pageSize;
113  }
114 }
115 
116 bool
118 {
119  // starting address must be page aligned
120  assert(pageOffset(vaddr) == 0);
121 
122  for (int64_t offset = 0; offset < size; offset += pageSize)
123  if (pTable.find(vaddr + offset) != pTable.end())
124  return false;
125 
126  return true;
127 }
128 
131 {
132  Addr page_addr = pageAlign(vaddr);
133  PTableItr iter = pTable.find(page_addr);
134  if (iter == pTable.end())
135  return nullptr;
136  return &(iter->second);
137 }
138 
139 bool
141 {
142  const Entry *entry = lookup(vaddr);
143  if (!entry) {
144  DPRINTF(MMU, "Couldn't Translate: %#x\n", vaddr);
145  return false;
146  }
147  paddr = pageOffset(vaddr) + entry->paddr;
148  DPRINTF(MMU, "Translating: %#x->%#x\n", vaddr, paddr);
149  return true;
150 }
151 
152 Fault
154 {
155  Addr paddr;
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");
163  return NoFault;
164  }
165  return NoFault;
166 }
167 
168 void
170 {
171  ScopedCheckpointSection sec(cp, "ptable");
172  paramOut(cp, "size", pTable.size());
173 
175  for (auto &pte : pTable) {
176  ScopedCheckpointSection sec(cp, csprintf("Entry%d", count++));
177 
178  paramOut(cp, "vaddr", pte.first);
179  paramOut(cp, "paddr", pte.second.paddr);
180  paramOut(cp, "flags", pte.second.flags);
181  }
182  assert(count == pTable.size());
183 }
184 
185 void
187 {
188  int count;
189  ScopedCheckpointSection sec(cp, "ptable");
190  paramIn(cp, "size", count);
191 
192  for (int i = 0; i < count; ++i) {
193  ScopedCheckpointSection sec(cp, csprintf("Entry%d", i));
194 
195  Addr vaddr;
197  Addr paddr;
198  uint64_t flags;
199  UNSERIALIZE_SCALAR(paddr);
200  UNSERIALIZE_SCALAR(flags);
201 
202  pTable.emplace(vaddr, Entry(paddr, flags));
203  }
204 }
205 
EmulationPageTable::Entry
Definition: page_table.hh:51
serialize.hh
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:797
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
RequestPtr
std::shared_ptr< Request > RequestPtr
Definition: request.hh:82
std::vector
STL vector class.
Definition: stl.hh:37
EmulationPageTable::unmap
virtual void unmap(Addr vaddr, int64_t size)
Definition: page_table.cc:101
X86ISA::count
count
Definition: misc.hh:703
EmulationPageTable::isUnmapped
virtual bool isUnmapped(Addr vaddr, int64_t size)
Check if any pages in a region are already allocated.
Definition: page_table.cc:117
faults.hh
EmulationPageTable::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: page_table.cc:186
paramOut
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:38
EmulationPageTable::lookup
const Entry * lookup(Addr vaddr)
Lookup function.
Definition: page_table.cc:130
EmulationPageTable::getMappings
void getMappings(std::vector< std::pair< Addr, Addr >> *addr_mappings)
Definition: page_table.cc:94
EmulationPageTable::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: page_table.cc:169
cp
Definition: cprintf.cc:40
EmulationPageTable::pageSize
const Addr pageSize
Definition: page_table.hh:65
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
EmulationPageTable::remap
virtual void remap(Addr vaddr, int64_t size, Addr new_vaddr)
Definition: page_table.cc:72
Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:240
MipsISA::vaddr
vaddr
Definition: pra_constants.hh:275
EmulationPageTable::Clobber
@ Clobber
Definition: page_table.hh:92
compiler.hh
std::pair< Addr, Addr >
NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:245
EmulationPageTable::pageOffset
Addr pageOffset(Addr a)
Definition: page_table.hh:106
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Serializable::ScopedCheckpointSection
Definition: serialize.hh:175
EmulationPageTable::pTable
PTable pTable
Definition: page_table.hh:63
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
GenericPageTableFault
Definition: faults.hh:112
EmulationPageTable::Entry::paddr
Addr paddr
Definition: page_table.hh:53
paramIn
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:69
CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:63
EmulationPageTable::pageAlign
Addr pageAlign(Addr a)
Definition: page_table.hh:105
trace.hh
Stats::size_type
unsigned int size_type
Definition: types.hh:54
EmulationPageTable::map
virtual void map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags=0)
Maps a virtual memory region to a physical memory region.
Definition: page_table.cc:45
page_table.hh
CheckpointIn
Definition: serialize.hh:67
EmulationPageTable::PTableItr
PTable::iterator PTableItr
Definition: page_table.hh:62
EmulationPageTable::translate
bool translate(Addr vaddr, Addr &paddr)
Translate function.
Definition: page_table.cc:140
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153

Generated on Wed Sep 30 2020 14:02:13 for gem5 by doxygen 1.8.17