2 * Copyright (c) 2012, 2014, 2018 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Hansson
40 #include "mem/physical.hh"
44 #include <sys/types.h>
55 #include "base/trace.hh"
56 #include "debug/AddrRanges.hh"
57 #include "debug/Checkpoint.hh"
58 #include "mem/abstract_mem.hh"
61 * On Linux, MAP_NORESERVE allow us to simulate a very large memory
62 * without committing to actually providing the swap space on the
63 * host. On FreeBSD or OSX the MAP_NORESERVE flag does not exist,
64 * so simply make it 0.
66 #if defined(__APPLE__) || defined(__FreeBSD__)
68 #define MAP_NORESERVE 0
74 PhysicalMemory::PhysicalMemory(const string
& _name
,
75 const vector
<AbstractMemory
*>& _memories
,
76 bool mmap_using_noreserve
) :
77 _name(_name
), size(0), mmapUsingNoReserve(mmap_using_noreserve
)
79 if (mmap_using_noreserve
)
80 warn("Not reserving swap space. May cause SIGSEGV on actual usage\n");
82 // add the memories from the system to the address map as
84 for (const auto& m
: _memories
) {
85 // only add the memory if it is part of the global address map
86 if (m
->isInAddrMap()) {
87 memories
.push_back(m
);
89 // calculate the total size once and for all
92 // add the range to our interval tree and make sure it does not
93 // intersect an existing range
94 fatal_if(addrMap
.insert(m
->getAddrRange(), m
) == addrMap
.end(),
95 "Memory address range for %s is overlapping\n",
98 // this type of memory is used e.g. as reference memory by
99 // Ruby, and they also needs a backing store, but should
100 // not be part of the global address map
102 "Skipping memory %s that is not in global address map\n",
106 fatal_if(m
->getAddrRange().interleaved(),
107 "Memory %s that is not in the global address map cannot "
108 "be interleaved\n", m
->name());
110 // simply do it independently, also note that this kind of
111 // memories are allowed to overlap in the logic address
113 vector
<AbstractMemory
*> unmapped_mems
{m
};
114 createBackingStore(m
->getAddrRange(), unmapped_mems
,
115 m
->isConfReported(), m
->isInAddrMap(),
120 // iterate over the increasing addresses and chunks of contiguous
121 // space to be mapped to backing store, create it and inform the
123 vector
<AddrRange
> intlv_ranges
;
124 vector
<AbstractMemory
*> curr_memories
;
125 for (const auto& r
: addrMap
) {
126 // simply skip past all memories that are null and hence do
127 // not need any backing store
128 if (!r
.second
->isNull()) {
129 // if the range is interleaved then save it for now
130 if (r
.first
.interleaved()) {
131 // if we already got interleaved ranges that are not
132 // part of the same range, then first do a merge
133 // before we add the new one
134 if (!intlv_ranges
.empty() &&
135 !intlv_ranges
.back().mergesWith(r
.first
)) {
136 AddrRange
merged_range(intlv_ranges
);
138 AbstractMemory
*f
= curr_memories
.front();
139 for (const auto& c
: curr_memories
)
140 if (f
->isConfReported() != c
->isConfReported() ||
141 f
->isInAddrMap() != c
->isInAddrMap() ||
142 f
->isKvmMap() != c
->isKvmMap())
143 fatal("Inconsistent flags in an interleaved "
146 createBackingStore(merged_range
, curr_memories
,
147 f
->isConfReported(), f
->isInAddrMap(),
150 intlv_ranges
.clear();
151 curr_memories
.clear();
153 intlv_ranges
.push_back(r
.first
);
154 curr_memories
.push_back(r
.second
);
156 vector
<AbstractMemory
*> single_memory
{r
.second
};
157 createBackingStore(r
.first
, single_memory
,
158 r
.second
->isConfReported(),
159 r
.second
->isInAddrMap(),
160 r
.second
->isKvmMap());
165 // if there is still interleaved ranges waiting to be merged, go
167 if (!intlv_ranges
.empty()) {
168 AddrRange
merged_range(intlv_ranges
);
170 AbstractMemory
*f
= curr_memories
.front();
171 for (const auto& c
: curr_memories
)
172 if (f
->isConfReported() != c
->isConfReported() ||
173 f
->isInAddrMap() != c
->isInAddrMap() ||
174 f
->isKvmMap() != c
->isKvmMap())
175 fatal("Inconsistent flags in an interleaved "
178 createBackingStore(merged_range
, curr_memories
,
179 f
->isConfReported(), f
->isInAddrMap(),
185 PhysicalMemory::createBackingStore(AddrRange range
,
186 const vector
<AbstractMemory
*>& _memories
,
187 bool conf_table_reported
,
188 bool in_addr_map
, bool kvm_map
)
190 panic_if(range
.interleaved(),
191 "Cannot create backing store for interleaved range %s\n",
194 // perform the actual mmap
195 DPRINTF(AddrRanges
, "Creating backing store for range %s with size %d\n",
196 range
.to_string(), range
.size());
197 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
199 // to be able to simulate very large memories, the user can opt to
200 // pass noreserve to mmap
201 if (mmapUsingNoReserve
) {
202 map_flags
|= MAP_NORESERVE
;
205 uint8_t* pmem
= (uint8_t*) mmap(NULL
, range
.size(),
206 PROT_READ
| PROT_WRITE
,
209 if (pmem
== (uint8_t*) MAP_FAILED
) {
211 fatal("Could not mmap %d bytes for range %s!\n", range
.size(),
215 // remember this backing store so we can checkpoint it and unmap
217 backingStore
.emplace_back(range
, pmem
,
218 conf_table_reported
, in_addr_map
, kvm_map
);
220 // point the memories to their backing store
221 for (const auto& m
: _memories
) {
222 DPRINTF(AddrRanges
, "Mapping memory %s to backing store\n",
224 m
->setBackingStore(pmem
);
228 PhysicalMemory::~PhysicalMemory()
230 // unmap the backing store
231 for (auto& s
: backingStore
)
232 munmap((char*)s
.pmem
, s
.range
.size());
236 PhysicalMemory::isMemAddr(Addr addr
) const
238 return addrMap
.contains(addr
) != addrMap
.end();
242 PhysicalMemory::getConfAddrRanges() const
244 // this could be done once in the constructor, but since it is unlikely to
245 // be called more than once the iteration should not be a problem
246 AddrRangeList ranges
;
247 vector
<AddrRange
> intlv_ranges
;
248 for (const auto& r
: addrMap
) {
249 if (r
.second
->isConfReported()) {
250 // if the range is interleaved then save it for now
251 if (r
.first
.interleaved()) {
252 // if we already got interleaved ranges that are not
253 // part of the same range, then first do a merge
254 // before we add the new one
255 if (!intlv_ranges
.empty() &&
256 !intlv_ranges
.back().mergesWith(r
.first
)) {
257 ranges
.push_back(AddrRange(intlv_ranges
));
258 intlv_ranges
.clear();
260 intlv_ranges
.push_back(r
.first
);
262 // keep the current range
263 ranges
.push_back(r
.first
);
268 // if there is still interleaved ranges waiting to be merged,
269 // go ahead and do it
270 if (!intlv_ranges
.empty()) {
271 ranges
.push_back(AddrRange(intlv_ranges
));
278 PhysicalMemory::access(PacketPtr pkt
)
280 assert(pkt
->isRequest());
281 AddrRange addr_range
= RangeSize(pkt
->getAddr(), pkt
->getSize());
282 const auto& m
= addrMap
.contains(addr_range
);
283 assert(m
!= addrMap
.end());
284 m
->second
->access(pkt
);
288 PhysicalMemory::functionalAccess(PacketPtr pkt
)
290 assert(pkt
->isRequest());
291 AddrRange addr_range
= RangeSize(pkt
->getAddr(), pkt
->getSize());
292 const auto& m
= addrMap
.contains(addr_range
);
293 assert(m
!= addrMap
.end());
294 m
->second
->functionalAccess(pkt
);
298 PhysicalMemory::serialize(CheckpointOut
&cp
) const
300 // serialize all the locked addresses and their context ids
301 vector
<Addr
> lal_addr
;
302 vector
<ContextID
> lal_cid
;
304 for (auto& m
: memories
) {
305 const list
<LockedAddr
>& locked_addrs
= m
->getLockedAddrList();
306 for (const auto& l
: locked_addrs
) {
307 lal_addr
.push_back(l
.addr
);
308 lal_cid
.push_back(l
.contextId
);
312 SERIALIZE_CONTAINER(lal_addr
);
313 SERIALIZE_CONTAINER(lal_cid
);
315 // serialize the backing stores
316 unsigned int nbr_of_stores
= backingStore
.size();
317 SERIALIZE_SCALAR(nbr_of_stores
);
319 unsigned int store_id
= 0;
320 // store each backing store memory segment in a file
321 for (auto& s
: backingStore
) {
322 ScopedCheckpointSection
sec(cp
, csprintf("store%d", store_id
));
323 serializeStore(cp
, store_id
++, s
.range
, s
.pmem
);
328 PhysicalMemory::serializeStore(CheckpointOut
&cp
, unsigned int store_id
,
329 AddrRange range
, uint8_t* pmem
) const
331 // we cannot use the address range for the name as the
332 // memories that are not part of the address map can overlap
333 string filename
= name() + ".store" + to_string(store_id
) + ".pmem";
334 long range_size
= range
.size();
336 DPRINTF(Checkpoint
, "Serializing physical memory %s with size %d\n",
337 filename
, range_size
);
339 SERIALIZE_SCALAR(store_id
);
340 SERIALIZE_SCALAR(filename
);
341 SERIALIZE_SCALAR(range_size
);
344 string filepath
= CheckpointIn::dir() + "/" + filename
.c_str();
345 gzFile compressed_mem
= gzopen(filepath
.c_str(), "wb");
346 if (compressed_mem
== NULL
)
347 fatal("Can't open physical memory checkpoint file '%s'\n",
350 uint64_t pass_size
= 0;
352 // gzwrite fails if (int)len < 0 (gzwrite returns int)
353 for (uint64_t written
= 0; written
< range
.size();
354 written
+= pass_size
) {
355 pass_size
= (uint64_t)INT_MAX
< (range
.size() - written
) ?
356 (uint64_t)INT_MAX
: (range
.size() - written
);
358 if (gzwrite(compressed_mem
, pmem
+ written
,
359 (unsigned int) pass_size
) != (int) pass_size
) {
360 fatal("Write failed on physical memory checkpoint file '%s'\n",
365 // close the compressed stream and check that the exit status
367 if (gzclose(compressed_mem
))
368 fatal("Close failed on physical memory checkpoint file '%s'\n",
374 PhysicalMemory::unserialize(CheckpointIn
&cp
)
376 // unserialize the locked addresses and map them to the
377 // appropriate memory controller
378 vector
<Addr
> lal_addr
;
379 vector
<ContextID
> lal_cid
;
380 UNSERIALIZE_CONTAINER(lal_addr
);
381 UNSERIALIZE_CONTAINER(lal_cid
);
382 for (size_t i
= 0; i
< lal_addr
.size(); ++i
) {
383 const auto& m
= addrMap
.contains(lal_addr
[i
]);
384 m
->second
->addLockedAddr(LockedAddr(lal_addr
[i
], lal_cid
[i
]));
387 // unserialize the backing stores
388 unsigned int nbr_of_stores
;
389 UNSERIALIZE_SCALAR(nbr_of_stores
);
391 for (unsigned int i
= 0; i
< nbr_of_stores
; ++i
) {
392 ScopedCheckpointSection
sec(cp
, csprintf("store%d", i
));
393 unserializeStore(cp
);
399 PhysicalMemory::unserializeStore(CheckpointIn
&cp
)
401 const uint32_t chunk_size
= 16384;
403 unsigned int store_id
;
404 UNSERIALIZE_SCALAR(store_id
);
407 UNSERIALIZE_SCALAR(filename
);
408 string filepath
= cp
.cptDir
+ "/" + filename
;
411 gzFile compressed_mem
= gzopen(filepath
.c_str(), "rb");
412 if (compressed_mem
== NULL
)
413 fatal("Can't open physical memory checkpoint file '%s'", filename
);
415 // we've already got the actual backing store mapped
416 uint8_t* pmem
= backingStore
[store_id
].pmem
;
417 AddrRange range
= backingStore
[store_id
].range
;
420 UNSERIALIZE_SCALAR(range_size
);
422 DPRINTF(Checkpoint
, "Unserializing physical memory %s with size %d\n",
423 filename
, range_size
);
425 if (range_size
!= range
.size())
426 fatal("Memory range size has changed! Saw %lld, expected %lld\n",
427 range_size
, range
.size());
429 uint64_t curr_size
= 0;
430 long* temp_page
= new long[chunk_size
];
433 while (curr_size
< range
.size()) {
434 bytes_read
= gzread(compressed_mem
, temp_page
, chunk_size
);
438 assert(bytes_read
% sizeof(long) == 0);
440 for (uint32_t x
= 0; x
< bytes_read
/ sizeof(long); x
++) {
441 // Only copy bytes that are non-zero, so we don't give
442 // the VM system hell
443 if (*(temp_page
+ x
) != 0) {
444 pmem_current
= (long*)(pmem
+ curr_size
+ x
* sizeof(long));
445 *pmem_current
= *(temp_page
+ x
);
448 curr_size
+= bytes_read
;
453 if (gzclose(compressed_mem
))
454 fatal("Close failed on physical memory checkpoint file '%s'\n",