2 * Copyright (c) 2012 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
41 #include <sys/types.h>
53 #include "base/trace.hh"
54 #include "debug/BusAddrRanges.hh"
55 #include "debug/Checkpoint.hh"
56 #include "mem/abstract_mem.hh"
57 #include "mem/physical.hh"
61 PhysicalMemory::PhysicalMemory(const string
& _name
,
62 const vector
<AbstractMemory
*>& _memories
) :
65 // add the memories from the system to the address map as
67 for (vector
<AbstractMemory
*>::const_iterator m
= _memories
.begin();
68 m
!= _memories
.end(); ++m
) {
69 // only add the memory if it is part of the global address map
70 if ((*m
)->isInAddrMap()) {
71 memories
.push_back(*m
);
73 // calculate the total size once and for all
76 // add the range to our interval tree and make sure it does not
77 // intersect an existing range
78 if (addrMap
.insert((*m
)->getAddrRange(), *m
) == addrMap
.end())
79 fatal("Memory address range for %s is overlapping\n",
82 DPRINTF(BusAddrRanges
,
83 "Skipping memory %s that is not in global address map\n",
85 // this type of memory is used e.g. as reference memory by
86 // Ruby, and they also needs a backing store, but should
87 // not be part of the global address map
89 // simply do it independently, also note that this kind of
90 // memories are allowed to overlap in the logic address
92 vector
<AbstractMemory
*> unmapped_mems
;
93 unmapped_mems
.push_back(*m
);
94 createBackingStore((*m
)->getAddrRange(), unmapped_mems
);
98 // iterate over the increasing addresses and chunks of contigous
99 // space to be mapped to backing store, also remember what
100 // memories constitute the range so we can go and find out if we
101 // have to init their parts to zero
102 vector
<AbstractMemory
*> curr_memories
;
103 for (AddrRangeMap
<AbstractMemory
*>::const_iterator r
= addrMap
.begin();
104 r
!= addrMap
.end(); ++r
) {
105 // simply skip past all memories that are null and hence do
106 // not need any backing store
107 if (!r
->second
->isNull()) {
108 // this will eventually be extended to support merging of
109 // interleaved address ranges, and although it might seem
110 // overly complicated at this point it will all be used
111 curr_memories
.push_back(r
->second
);
112 createBackingStore(r
->first
, curr_memories
);
113 curr_memories
.clear();
119 PhysicalMemory::createBackingStore(AddrRange range
,
120 const vector
<AbstractMemory
*>& _memories
)
122 if (range
.interleaved())
123 panic("Cannot create backing store for interleaved range %s\n",
126 // perform the actual mmap
127 DPRINTF(BusAddrRanges
, "Creating backing store for range %s with size %d\n",
128 range
.to_string(), range
.size());
129 int map_flags
= MAP_ANON
| MAP_PRIVATE
;
130 uint8_t* pmem
= (uint8_t*) mmap(NULL
, range
.size(),
131 PROT_READ
| PROT_WRITE
,
134 if (pmem
== (uint8_t*) MAP_FAILED
) {
136 fatal("Could not mmap %d bytes for range %s!\n", range
.size(),
140 // remember this backing store so we can checkpoint it and unmap
142 backingStore
.push_back(make_pair(range
, pmem
));
144 // count how many of the memories are to be zero initialized so we
145 // can see if some but not all have this parameter set
146 uint32_t init_to_zero
= 0;
148 // point the memories to their backing store, and if requested,
149 // initialize the memory range to 0
150 for (vector
<AbstractMemory
*>::const_iterator m
= _memories
.begin();
151 m
!= _memories
.end(); ++m
) {
152 DPRINTF(BusAddrRanges
, "Mapping memory %s to backing store\n",
154 (*m
)->setBackingStore(pmem
);
156 // if it should be zero, then go and make it so
157 if ((*m
)->initToZero()) {
162 if (init_to_zero
!= 0) {
163 if (init_to_zero
!= _memories
.size())
164 fatal("Some, but not all memories in range %s are set zero\n",
167 memset(pmem
, 0, range
.size());
171 PhysicalMemory::~PhysicalMemory()
173 // unmap the backing store
174 for (vector
<pair
<AddrRange
, uint8_t*> >::iterator s
= backingStore
.begin();
175 s
!= backingStore
.end(); ++s
)
176 munmap((char*)s
->second
, s
->first
.size());
180 PhysicalMemory::isMemAddr(Addr addr
) const
182 // see if the address is within the last matched range
183 if (!rangeCache
.contains(addr
)) {
184 // lookup in the interval tree
185 AddrRangeMap
<AbstractMemory
*>::const_iterator r
= addrMap
.find(addr
);
186 if (r
== addrMap
.end()) {
187 // not in the cache, and not in the tree
190 // the range is in the tree, update the cache
191 rangeCache
= r
->first
;
194 assert(addrMap
.find(addr
) != addrMap
.end());
196 // either matched the cache or found in the tree
201 PhysicalMemory::getConfAddrRanges() const
203 // this could be done once in the constructor, but since it is unlikely to
204 // be called more than once the iteration should not be a problem
205 AddrRangeList ranges
;
206 vector
<AddrRange
> intlv_ranges
;
207 for (AddrRangeMap
<AbstractMemory
*>::const_iterator r
= addrMap
.begin();
208 r
!= addrMap
.end(); ++r
) {
209 if (r
->second
->isConfReported()) {
210 // if the range is interleaved then save it for now
211 if (r
->first
.interleaved()) {
212 // if we already got interleaved ranges that are not
213 // part of the same range, then first do a merge
214 // before we add the new one
215 if (!intlv_ranges
.empty() &&
216 !intlv_ranges
.back().mergesWith(r
->first
)) {
217 ranges
.push_back(AddrRange(intlv_ranges
));
218 intlv_ranges
.clear();
220 intlv_ranges
.push_back(r
->first
);
222 // keep the current range
223 ranges
.push_back(r
->first
);
228 // if there is still interleaved ranges waiting to be merged,
229 // go ahead and do it
230 if (!intlv_ranges
.empty()) {
231 ranges
.push_back(AddrRange(intlv_ranges
));
238 PhysicalMemory::access(PacketPtr pkt
)
240 assert(pkt
->isRequest());
241 Addr addr
= pkt
->getAddr();
242 AddrRangeMap
<AbstractMemory
*>::const_iterator m
= addrMap
.find(addr
);
243 assert(m
!= addrMap
.end());
244 m
->second
->access(pkt
);
248 PhysicalMemory::functionalAccess(PacketPtr pkt
)
250 assert(pkt
->isRequest());
251 Addr addr
= pkt
->getAddr();
252 AddrRangeMap
<AbstractMemory
*>::const_iterator m
= addrMap
.find(addr
);
253 assert(m
!= addrMap
.end());
254 m
->second
->functionalAccess(pkt
);
258 PhysicalMemory::serialize(ostream
& os
)
260 // serialize all the locked addresses and their context ids
261 vector
<Addr
> lal_addr
;
264 for (vector
<AbstractMemory
*>::iterator m
= memories
.begin();
265 m
!= memories
.end(); ++m
) {
266 const list
<LockedAddr
>& locked_addrs
= (*m
)->getLockedAddrList();
267 for (list
<LockedAddr
>::const_iterator l
= locked_addrs
.begin();
268 l
!= locked_addrs
.end(); ++l
) {
269 lal_addr
.push_back(l
->addr
);
270 lal_cid
.push_back(l
->contextId
);
274 arrayParamOut(os
, "lal_addr", lal_addr
);
275 arrayParamOut(os
, "lal_cid", lal_cid
);
277 // serialize the backing stores
278 unsigned int nbr_of_stores
= backingStore
.size();
279 SERIALIZE_SCALAR(nbr_of_stores
);
281 unsigned int store_id
= 0;
282 // store each backing store memory segment in a file
283 for (vector
<pair
<AddrRange
, uint8_t*> >::iterator s
= backingStore
.begin();
284 s
!= backingStore
.end(); ++s
) {
285 nameOut(os
, csprintf("%s.store%d", name(), store_id
));
286 serializeStore(os
, store_id
++, s
->first
, s
->second
);
291 PhysicalMemory::serializeStore(ostream
& os
, unsigned int store_id
,
292 AddrRange range
, uint8_t* pmem
)
294 // we cannot use the address range for the name as the
295 // memories that are not part of the address map can overlap
296 string filename
= name() + ".store" + to_string(store_id
) + ".pmem";
297 long range_size
= range
.size();
299 DPRINTF(Checkpoint
, "Serializing physical memory %s with size %d\n",
300 filename
, range_size
);
302 SERIALIZE_SCALAR(store_id
);
303 SERIALIZE_SCALAR(filename
);
304 SERIALIZE_SCALAR(range_size
);
307 string filepath
= Checkpoint::dir() + "/" + filename
.c_str();
308 int fd
= creat(filepath
.c_str(), 0664);
311 fatal("Can't open physical memory checkpoint file '%s'\n",
315 gzFile compressed_mem
= gzdopen(fd
, "wb");
316 if (compressed_mem
== NULL
)
317 fatal("Insufficient memory to allocate compression state for %s\n",
320 uint64_t pass_size
= 0;
322 // gzwrite fails if (int)len < 0 (gzwrite returns int)
323 for (uint64_t written
= 0; written
< range
.size();
324 written
+= pass_size
) {
325 pass_size
= (uint64_t)INT_MAX
< (range
.size() - written
) ?
326 (uint64_t)INT_MAX
: (range
.size() - written
);
328 if (gzwrite(compressed_mem
, pmem
+ written
,
329 (unsigned int) pass_size
) != (int) pass_size
) {
330 fatal("Write failed on physical memory checkpoint file '%s'\n",
335 // close the compressed stream and check that the exit status
337 if (gzclose(compressed_mem
))
338 fatal("Close failed on physical memory checkpoint file '%s'\n",
344 PhysicalMemory::unserialize(Checkpoint
* cp
, const string
& section
)
346 // unserialize the locked addresses and map them to the
347 // appropriate memory controller
348 vector
<Addr
> lal_addr
;
350 arrayParamIn(cp
, section
, "lal_addr", lal_addr
);
351 arrayParamIn(cp
, section
, "lal_cid", lal_cid
);
352 for(size_t i
= 0; i
< lal_addr
.size(); ++i
) {
353 AddrRangeMap
<AbstractMemory
*>::const_iterator m
=
354 addrMap
.find(lal_addr
[i
]);
355 m
->second
->addLockedAddr(LockedAddr(lal_addr
[i
], lal_cid
[i
]));
358 // unserialize the backing stores
359 unsigned int nbr_of_stores
;
360 UNSERIALIZE_SCALAR(nbr_of_stores
);
362 for (unsigned int i
= 0; i
< nbr_of_stores
; ++i
) {
363 unserializeStore(cp
, csprintf("%s.store%d", section
, i
));
369 PhysicalMemory::unserializeStore(Checkpoint
* cp
, const string
& section
)
371 const uint32_t chunk_size
= 16384;
373 unsigned int store_id
;
374 UNSERIALIZE_SCALAR(store_id
);
377 UNSERIALIZE_SCALAR(filename
);
378 string filepath
= cp
->cptDir
+ "/" + filename
;
381 int fd
= open(filepath
.c_str(), O_RDONLY
);
384 fatal("Can't open physical memory checkpoint file '%s'", filename
);
387 gzFile compressed_mem
= gzdopen(fd
, "rb");
388 if (compressed_mem
== NULL
)
389 fatal("Insufficient memory to allocate compression state for %s\n",
392 uint8_t* pmem
= backingStore
[store_id
].second
;
393 AddrRange range
= backingStore
[store_id
].first
;
395 // unmap file that was mmapped in the constructor, this is
396 // done here to make sure that gzip and open don't muck with
397 // our nice large space of memory before we reallocate it
398 munmap((char*) pmem
, range
.size());
401 UNSERIALIZE_SCALAR(range_size
);
403 DPRINTF(Checkpoint
, "Unserializing physical memory %s with size %d\n",
404 filename
, range_size
);
406 if (range_size
!= range
.size())
407 fatal("Memory range size has changed! Saw %lld, expected %lld\n",
408 range_size
, range
.size());
410 pmem
= (uint8_t*) mmap(NULL
, range
.size(), PROT_READ
| PROT_WRITE
,
411 MAP_ANON
| MAP_PRIVATE
, -1, 0);
413 if (pmem
== (void*) MAP_FAILED
) {
415 fatal("Could not mmap physical memory!\n");
418 uint64_t curr_size
= 0;
419 long* temp_page
= new long[chunk_size
];
422 while (curr_size
< range
.size()) {
423 bytes_read
= gzread(compressed_mem
, temp_page
, chunk_size
);
427 assert(bytes_read
% sizeof(long) == 0);
429 for (uint32_t x
= 0; x
< bytes_read
/ sizeof(long); x
++) {
430 // Only copy bytes that are non-zero, so we don't give
431 // the VM system hell
432 if (*(temp_page
+ x
) != 0) {
433 pmem_current
= (long*)(pmem
+ curr_size
+ x
* sizeof(long));
434 *pmem_current
= *(temp_page
+ x
);
437 curr_size
+= bytes_read
;
442 if (gzclose(compressed_mem
))
443 fatal("Close failed on physical memory checkpoint file '%s'\n",