mem-cache: Fix setting prefetch bit
[gem5.git] / src / mem / page_table.hh
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
30 /**
31 * @file
32 * Declarations of a non-full system Page Table.
33 */
34
35 #ifndef __MEM_PAGE_TABLE_HH__
36 #define __MEM_PAGE_TABLE_HH__
37
38 #include <string>
39 #include <unordered_map>
40
41 #include "base/bitfield.hh"
42 #include "base/intmath.hh"
43 #include "base/types.hh"
44 #include "mem/request.hh"
45 #include "sim/serialize.hh"
46
47 class ThreadContext;
48
49 class EmulationPageTable : public Serializable
50 {
51 public:
52 struct Entry
53 {
54 Addr paddr;
55 uint64_t flags;
56
57 Entry(Addr paddr, uint64_t flags) : paddr(paddr), flags(flags) {}
58 Entry() {}
59 };
60
61 protected:
62 typedef std::unordered_map<Addr, Entry> PTable;
63 typedef PTable::iterator PTableItr;
64 PTable pTable;
65
66 const Addr _pageSize;
67 const Addr offsetMask;
68
69 const uint64_t _pid;
70 const std::string _name;
71
72 public:
73
74 EmulationPageTable(
75 const std::string &__name, uint64_t _pid, Addr _pageSize) :
76 _pageSize(_pageSize), offsetMask(mask(floorLog2(_pageSize))),
77 _pid(_pid), _name(__name), shared(false)
78 {
79 assert(isPowerOf2(_pageSize));
80 }
81
82 uint64_t pid() const { return _pid; };
83
84 virtual ~EmulationPageTable() {};
85
86 /* generic page table mapping flags
87 * unset | set
88 * bit 0 - no-clobber | clobber
89 * bit 2 - cacheable | uncacheable
90 * bit 3 - read-write | read-only
91 */
92 enum MappingFlags : uint32_t {
93 Clobber = 1,
94 Uncacheable = 4,
95 ReadOnly = 8,
96 };
97
98 // flag which marks the page table as shared among software threads
99 bool shared;
100
101 virtual void initState() {};
102
103 // for DPRINTF compatibility
104 const std::string name() const { return _name; }
105
106 Addr pageAlign(Addr a) { return (a & ~offsetMask); }
107 Addr pageOffset(Addr a) { return (a & offsetMask); }
108 // Page size can technically vary based on the virtual address, but we'll
109 // ignore that for now.
110 Addr pageSize() { return _pageSize; }
111
112 /**
113 * Maps a virtual memory region to a physical memory region.
114 * @param vaddr The starting virtual address of the region.
115 * @param paddr The starting physical address where the region is mapped.
116 * @param size The length of the region.
117 * @param flags Generic mapping flags that can be set by or-ing values
118 * from MappingFlags enum.
119 */
120 virtual void map(Addr vaddr, Addr paddr, int64_t size, uint64_t flags = 0);
121 virtual void remap(Addr vaddr, int64_t size, Addr new_vaddr);
122 virtual void unmap(Addr vaddr, int64_t size);
123
124 /**
125 * Check if any pages in a region are already allocated
126 * @param vaddr The starting virtual address of the region.
127 * @param size The length of the region.
128 * @return True if no pages in the region are mapped.
129 */
130 virtual bool isUnmapped(Addr vaddr, int64_t size);
131
132 /**
133 * Lookup function
134 * @param vaddr The virtual address.
135 * @return The page table entry corresponding to vaddr.
136 */
137 const Entry *lookup(Addr vaddr);
138
139 /**
140 * Translate function
141 * @param vaddr The virtual address.
142 * @param paddr Physical address from translation.
143 * @return True if translation exists
144 */
145 bool translate(Addr vaddr, Addr &paddr);
146
147 /**
148 * Simplified translate function (just check for translation)
149 * @param vaddr The virtual address.
150 * @return True if translation exists
151 */
152 bool translate(Addr vaddr) { Addr dummy; return translate(vaddr, dummy); }
153
154 /**
155 * Perform a translation on the memory request, fills in paddr
156 * field of req.
157 * @param req The memory request.
158 */
159 Fault translate(const RequestPtr &req);
160
161 void getMappings(std::vector<std::pair<Addr, Addr>> *addr_mappings);
162
163 void serialize(CheckpointOut &cp) const override;
164 void unserialize(CheckpointIn &cp) override;
165 };
166
167 #endif // __MEM_PAGE_TABLE_HH__