gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Steve Reinhardt
30  * Ron Dreslinski
31  * Ali Saidi
32  */
33 
38 #include "mem/page_table.hh"
39 
40 #include <string>
41 
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"
47 
48 void
49 EmulationPageTable::map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags)
50 {
51  bool clobber = flags & Clobber;
52  // starting address must be page aligned
53  assert(pageOffset(vaddr) == 0);
54 
55  DPRINTF(MMU, "Allocating Page: %#x-%#x\n", vaddr, vaddr + size);
56 
57  while (size > 0) {
58  auto it = pTable.find(vaddr);
59  if (it != pTable.end()) {
60  // already mapped
61  panic_if(!clobber,
62  "EmulationPageTable::allocate: addr %#x already mapped",
63  vaddr);
64  it->second = Entry(paddr, flags);
65  } else {
66  pTable.emplace(vaddr, Entry(paddr, flags));
67  }
68 
69  size -= pageSize;
70  vaddr += pageSize;
71  paddr += pageSize;
72  }
73 }
74 
75 void
76 EmulationPageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
77 {
78  assert(pageOffset(vaddr) == 0);
79  assert(pageOffset(new_vaddr) == 0);
80 
81  DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
82  new_vaddr, size);
83 
84  while (size > 0) {
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());
88 
89  pTable.emplace(new_vaddr, old_it->second);
90  pTable.erase(old_it);
91  size -= pageSize;
92  vaddr += pageSize;
93  new_vaddr += pageSize;
94  }
95 }
96 
97 void
99 {
100  for (auto &iter : pTable)
101  addr_maps->push_back(std::make_pair(iter.first, iter.second.paddr));
102 }
103 
104 void
106 {
107  assert(pageOffset(vaddr) == 0);
108 
109  DPRINTF(MMU, "Unmapping page: %#x-%#x\n", vaddr, vaddr + size);
110 
111  while (size > 0) {
112  auto it = pTable.find(vaddr);
113  assert(it != pTable.end());
114  pTable.erase(it);
115  size -= pageSize;
116  vaddr += pageSize;
117  }
118 }
119 
120 bool
122 {
123  // starting address must be page aligned
124  assert(pageOffset(vaddr) == 0);
125 
126  for (int64_t offset = 0; offset < size; offset += pageSize)
127  if (pTable.find(vaddr + offset) != pTable.end())
128  return false;
129 
130  return true;
131 }
132 
135 {
136  Addr page_addr = pageAlign(vaddr);
137  PTableItr iter = pTable.find(page_addr);
138  if (iter == pTable.end())
139  return nullptr;
140  return &(iter->second);
141 }
142 
143 bool
145 {
146  const Entry *entry = lookup(vaddr);
147  if (!entry) {
148  DPRINTF(MMU, "Couldn't Translate: %#x\n", vaddr);
149  return false;
150  }
151  paddr = pageOffset(vaddr) + entry->paddr;
152  DPRINTF(MMU, "Translating: %#x->%#x\n", vaddr, paddr);
153  return true;
154 }
155 
156 Fault
158 {
159  Addr paddr;
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");
167  return NoFault;
168  }
169  return NoFault;
170 }
171 
172 void
174 {
175  paramOut(cp, "ptable.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  paramIn(cp, "ptable.size", count);
193 
194  for (int i = 0; i < count; ++i) {
195  ScopedCheckpointSection sec(cp, csprintf("Entry%d", i));
196 
197  Addr vaddr;
198  UNSERIALIZE_SCALAR(vaddr);
199  Addr paddr;
200  uint64_t flags;
201  UNSERIALIZE_SCALAR(paddr);
202  UNSERIALIZE_SCALAR(flags);
203 
204  pTable.emplace(vaddr, Entry(paddr, flags));
205  }
206 }
207 
count
Definition: misc.hh:705
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
#define DPRINTF(x,...)
Definition: trace.hh:229
virtual void remap(Addr vaddr, int64_t size, Addr new_vaddr)
Definition: page_table.cc:76
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:49
decltype(nullptr) constexpr NoFault
Definition: types.hh:245
Bitfield< 7 > i
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: page_table.cc:189
std::shared_ptr< Request > RequestPtr
Definition: request.hh:83
Bitfield< 23, 0 > offset
Definition: types.hh:154
Definition: cprintf.cc:42
const Addr pageSize
Definition: page_table.hh:67
unsigned int size_type
Definition: types.hh:56
STL vector class.
Definition: stl.hh:40
#define UNSERIALIZE_SCALAR(scalar)
Definition: serialize.hh:645
Addr pageOffset(Addr a)
Definition: page_table.hh:108
Addr pageAlign(Addr a)
Definition: page_table.hh:107
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:162
bool translate(Addr vaddr, Addr &paddr)
Translate function.
Definition: page_table.cc:144
void paramOut(CheckpointOut &cp, const string &name, ExtMachInst const &machInst)
Definition: types.cc:40
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
Declarations of a non-full system Page Table.
virtual bool isUnmapped(Addr vaddr, int64_t size)
Check if any pages in a region are already allocated.
Definition: page_table.cc:121
std::ostream CheckpointOut
Definition: serialize.hh:68
PTable::iterator PTableItr
Definition: page_table.hh:64
const Entry * lookup(Addr vaddr)
Lookup function.
Definition: page_table.cc:134
void paramIn(CheckpointIn &cp, const string &name, ExtMachInst &machInst)
Definition: types.cc:71
virtual void unmap(Addr vaddr, int64_t size)
Definition: page_table.cc:105
void getMappings(std::vector< std::pair< Addr, Addr >> *addr_mappings)
Definition: page_table.cc:98
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: page_table.cc:173
static const int NumArgumentRegs M5_VAR_USED
Definition: process.cc:84
Scoped checkpoint section helper class.
Definition: serialize.hh:173
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:185
std::shared_ptr< FaultBase > Fault
Definition: types.hh:240

Generated on Fri Feb 28 2020 16:27:02 for gem5 by doxygen 1.8.13