gem5  v21.1.0.2
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 namespace gem5
45 {
46 
47 void
48 EmulationPageTable::map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags)
49 {
50  bool clobber = flags & Clobber;
51  // starting address must be page aligned
52  assert(pageOffset(vaddr) == 0);
53 
54  DPRINTF(MMU, "Allocating Page: %#x-%#x\n", vaddr, vaddr + size);
55 
56  while (size > 0) {
57  auto it = pTable.find(vaddr);
58  if (it != pTable.end()) {
59  // already mapped
60  panic_if(!clobber,
61  "EmulationPageTable::allocate: addr %#x already mapped",
62  vaddr);
63  it->second = Entry(paddr, flags);
64  } else {
65  pTable.emplace(vaddr, Entry(paddr, flags));
66  }
67 
68  size -= _pageSize;
69  vaddr += _pageSize;
70  paddr += _pageSize;
71  }
72 }
73 
74 void
75 EmulationPageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
76 {
77  assert(pageOffset(vaddr) == 0);
78  assert(pageOffset(new_vaddr) == 0);
79 
80  DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
81  new_vaddr, size);
82 
83  while (size > 0) {
84  GEM5_VAR_USED auto new_it = pTable.find(new_vaddr);
85  auto old_it = pTable.find(vaddr);
86  assert(old_it != pTable.end() && new_it == pTable.end());
87 
88  pTable.emplace(new_vaddr, old_it->second);
89  pTable.erase(old_it);
90  size -= _pageSize;
91  vaddr += _pageSize;
92  new_vaddr += _pageSize;
93  }
94 }
95 
96 void
98 {
99  for (auto &iter : pTable)
100  addr_maps->push_back(std::make_pair(iter.first, iter.second.paddr));
101 }
102 
103 void
105 {
106  assert(pageOffset(vaddr) == 0);
107 
108  DPRINTF(MMU, "Unmapping page: %#x-%#x\n", vaddr, vaddr + size);
109 
110  while (size > 0) {
111  auto it = pTable.find(vaddr);
112  assert(it != pTable.end());
113  pTable.erase(it);
114  size -= _pageSize;
115  vaddr += _pageSize;
116  }
117 }
118 
119 bool
121 {
122  // starting address must be page aligned
123  assert(pageOffset(vaddr) == 0);
124 
125  for (int64_t offset = 0; offset < size; offset += _pageSize)
126  if (pTable.find(vaddr + offset) != pTable.end())
127  return false;
128 
129  return true;
130 }
131 
134 {
135  Addr page_addr = pageAlign(vaddr);
136  PTableItr iter = pTable.find(page_addr);
137  if (iter == pTable.end())
138  return nullptr;
139  return &(iter->second);
140 }
141 
142 bool
144 {
145  const Entry *entry = lookup(vaddr);
146  if (!entry) {
147  DPRINTF(MMU, "Couldn't Translate: %#x\n", vaddr);
148  return false;
149  }
150  paddr = pageOffset(vaddr) + entry->paddr;
151  DPRINTF(MMU, "Translating: %#x->%#x\n", vaddr, paddr);
152  return true;
153 }
154 
155 Fault
157 {
158  Addr paddr;
159  assert(pageAlign(req->getVaddr() + req->getSize() - 1) ==
160  pageAlign(req->getVaddr()));
161  if (!translate(req->getVaddr(), paddr))
162  return Fault(new GenericPageTableFault(req->getVaddr()));
163  req->setPaddr(paddr);
164  if ((paddr & (_pageSize - 1)) + req->getSize() > _pageSize) {
165  panic("Request spans page boundaries!\n");
166  return NoFault;
167  }
168  return NoFault;
169 }
170 
171 void
173 {
174  ScopedCheckpointSection sec(cp, "ptable");
175  paramOut(cp, "size", pTable.size());
176 
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 
188 void
190 {
191  int count;
192  ScopedCheckpointSection sec(cp, "ptable");
193  paramIn(cp, "size", count);
194 
195  for (int i = 0; i < count; ++i) {
196  ScopedCheckpointSection sec(cp, csprintf("Entry%d", i));
197 
198  Addr vaddr;
200  Addr paddr;
201  uint64_t flags;
202  UNSERIALIZE_SCALAR(paddr);
203  UNSERIALIZE_SCALAR(flags);
204 
205  pTable.emplace(vaddr, Entry(paddr, flags));
206  }
207 }
208 
209 const std::string
211 {
212  std::stringstream ss;
213  for (PTable::const_iterator it=pTable.begin(); it != pTable.end(); ++it) {
214  ss << std::hex << it->first << ":" << it->second.paddr << ";";
215  }
216  return ss.str();
217 }
218 
219 } // namespace gem5
gem5::NoFault
constexpr decltype(nullptr) NoFault
Definition: types.hh:260
serialize.hh
UNSERIALIZE_SCALAR
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:575
gem5::EmulationPageTable::PTableItr
PTable::iterator PTableItr
Definition: page_table.hh:66
gem5::EmulationPageTable::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: page_table.cc:172
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::EmulationPageTable::unmap
virtual void unmap(Addr vaddr, int64_t size)
Definition: page_table.cc:104
gem5::EmulationPageTable::externalize
const std::string externalize() const
Dump all items in the pTable, to a concatenation of strings of the form Addr:Entry;.
Definition: page_table.cc:210
gem5::EmulationPageTable::translate
bool translate(Addr vaddr, Addr &paddr)
Translate function.
Definition: page_table.cc:143
std::vector
STL vector class.
Definition: stl.hh:37
gem5::EmulationPageTable::getMappings
void getMappings(std::vector< std::pair< Addr, Addr >> *addr_mappings)
Definition: page_table.cc:97
gem5::csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:161
gem5::EmulationPageTable::lookup
const Entry * lookup(Addr vaddr)
Lookup function.
Definition: page_table.cc:133
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
faults.hh
gem5::GenericPageTableFault
Definition: faults.hh:116
gem5::Fault
std::shared_ptr< FaultBase > Fault
Definition: types.hh:255
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
gem5::X86ISA::count
count
Definition: misc.hh:709
gem5::EmulationPageTable::remap
virtual void remap(Addr vaddr, int64_t size, Addr new_vaddr)
Definition: page_table.cc:75
gem5::RequestPtr
std::shared_ptr< Request > RequestPtr
Definition: request.hh:92
gem5::ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:144
ss
std::stringstream ss
Definition: trace.test.cc:45
compiler.hh
gem5::EmulationPageTable::pageAlign
Addr pageAlign(Addr a)
Definition: page_table.hh:110
std::pair< Addr, Addr >
gem5::statistics::size_type
unsigned int size_type
Definition: types.hh:60
gem5::auxv::Entry
@ Entry
Definition: aux_vector.hh:78
gem5::Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
gem5::EmulationPageTable::pTable
PTable pTable
Definition: page_table.hh:67
gem5::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:48
gem5::EmulationPageTable::Entry::paddr
Addr paddr
Definition: page_table.hh:57
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:203
gem5::paramOut
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
Definition: types.cc:40
gem5::paramIn
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
Definition: types.cc:72
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
trace.hh
gem5::MipsISA::vaddr
vaddr
Definition: pra_constants.hh:278
page_table.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::EmulationPageTable::pageOffset
Addr pageOffset(Addr a)
Definition: page_table.hh:111
gem5::ArmISA::MMU
Definition: mmu.hh:52
gem5::Serializable::ScopedCheckpointSection
Definition: serialize.hh:172
gem5::EmulationPageTable::Entry
Definition: page_table.hh:55
gem5::EmulationPageTable::Clobber
@ Clobber
Definition: page_table.hh:97
gem5::EmulationPageTable::isUnmapped
virtual bool isUnmapped(Addr vaddr, int64_t size)
Check if any pages in a region are already allocated.
Definition: page_table.cc:120
gem5::EmulationPageTable::_pageSize
const Addr _pageSize
Definition: page_table.hh:69
gem5::EmulationPageTable::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: page_table.cc:189
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177

Generated on Tue Sep 21 2021 12:25:34 for gem5 by doxygen 1.8.17