physical.cc revision 9707:1305bec2733f
1/* 2 * Copyright (c) 2012 ARM Limited 3 * All rights reserved 4 * 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. 13 * 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. 24 * 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. 36 * 37 * Authors: Andreas Hansson 38 */ 39 40#include <sys/mman.h> 41#include <sys/types.h> 42#include <sys/user.h> 43#include <fcntl.h> 44#include <unistd.h> 45#include <zlib.h> 46 47#include <cerrno> 48#include <climits> 49#include <cstdio> 50#include <iostream> 51#include <string> 52 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" 58 59using namespace std; 60 61PhysicalMemory::PhysicalMemory(const string& _name, 62 const vector<AbstractMemory*>& _memories) : 63 _name(_name), size(0) 64{ 65 // add the memories from the system to the address map as 66 // appropriate 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); 72 73 // calculate the total size once and for all 74 size += (*m)->size(); 75 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", 80 (*m)->name()); 81 } else { 82 DPRINTF(BusAddrRanges, 83 "Skipping memory %s that is not in global address map\n", 84 (*m)->name()); 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 88 89 // simply do it independently, also note that this kind of 90 // memories are allowed to overlap in the logic address 91 // map 92 vector<AbstractMemory*> unmapped_mems; 93 unmapped_mems.push_back(*m); 94 createBackingStore((*m)->getAddrRange(), unmapped_mems); 95 } 96 } 97 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<AddrRange> intlv_ranges; 103 vector<AbstractMemory*> curr_memories; 104 for (AddrRangeMap<AbstractMemory*>::const_iterator r = addrMap.begin(); 105 r != addrMap.end(); ++r) { 106 // simply skip past all memories that are null and hence do 107 // not need any backing store 108 if (!r->second->isNull()) { 109 // if the range is interleaved then save it for now 110 if (r->first.interleaved()) { 111 // if we already got interleaved ranges that are not 112 // part of the same range, then first do a merge 113 // before we add the new one 114 if (!intlv_ranges.empty() && 115 !intlv_ranges.back().mergesWith(r->first)) { 116 AddrRange merged_range(intlv_ranges); 117 createBackingStore(merged_range, curr_memories); 118 intlv_ranges.clear(); 119 curr_memories.clear(); 120 } 121 intlv_ranges.push_back(r->first); 122 curr_memories.push_back(r->second); 123 } else { 124 vector<AbstractMemory*> single_memory; 125 single_memory.push_back(r->second); 126 createBackingStore(r->first, single_memory); 127 } 128 } 129 } 130 131 // if there is still interleaved ranges waiting to be merged, go 132 // ahead and do it 133 if (!intlv_ranges.empty()) { 134 AddrRange merged_range(intlv_ranges); 135 createBackingStore(merged_range, curr_memories); 136 } 137} 138 139void 140PhysicalMemory::createBackingStore(AddrRange range, 141 const vector<AbstractMemory*>& _memories) 142{ 143 if (range.interleaved()) 144 panic("Cannot create backing store for interleaved range %s\n", 145 range.to_string()); 146 147 // perform the actual mmap 148 DPRINTF(BusAddrRanges, "Creating backing store for range %s with size %d\n", 149 range.to_string(), range.size()); 150 int map_flags = MAP_ANON | MAP_PRIVATE; 151 uint8_t* pmem = (uint8_t*) mmap(NULL, range.size(), 152 PROT_READ | PROT_WRITE, 153 map_flags, -1, 0); 154 155 if (pmem == (uint8_t*) MAP_FAILED) { 156 perror("mmap"); 157 fatal("Could not mmap %d bytes for range %s!\n", range.size(), 158 range.to_string()); 159 } 160 161 // remember this backing store so we can checkpoint it and unmap 162 // it appropriately 163 backingStore.push_back(make_pair(range, pmem)); 164 165 // point the memories to their backing store, and if requested, 166 // initialize the memory range to 0 167 for (vector<AbstractMemory*>::const_iterator m = _memories.begin(); 168 m != _memories.end(); ++m) { 169 DPRINTF(BusAddrRanges, "Mapping memory %s to backing store\n", 170 (*m)->name()); 171 (*m)->setBackingStore(pmem); 172 } 173} 174 175PhysicalMemory::~PhysicalMemory() 176{ 177 // unmap the backing store 178 for (vector<pair<AddrRange, uint8_t*> >::iterator s = backingStore.begin(); 179 s != backingStore.end(); ++s) 180 munmap((char*)s->second, s->first.size()); 181} 182 183bool 184PhysicalMemory::isMemAddr(Addr addr) const 185{ 186 // see if the address is within the last matched range 187 if (!rangeCache.contains(addr)) { 188 // lookup in the interval tree 189 AddrRangeMap<AbstractMemory*>::const_iterator r = addrMap.find(addr); 190 if (r == addrMap.end()) { 191 // not in the cache, and not in the tree 192 return false; 193 } 194 // the range is in the tree, update the cache 195 rangeCache = r->first; 196 } 197 198 assert(addrMap.find(addr) != addrMap.end()); 199 200 // either matched the cache or found in the tree 201 return true; 202} 203 204AddrRangeList 205PhysicalMemory::getConfAddrRanges() const 206{ 207 // this could be done once in the constructor, but since it is unlikely to 208 // be called more than once the iteration should not be a problem 209 AddrRangeList ranges; 210 vector<AddrRange> intlv_ranges; 211 for (AddrRangeMap<AbstractMemory*>::const_iterator r = addrMap.begin(); 212 r != addrMap.end(); ++r) { 213 if (r->second->isConfReported()) { 214 // if the range is interleaved then save it for now 215 if (r->first.interleaved()) { 216 // if we already got interleaved ranges that are not 217 // part of the same range, then first do a merge 218 // before we add the new one 219 if (!intlv_ranges.empty() && 220 !intlv_ranges.back().mergesWith(r->first)) { 221 ranges.push_back(AddrRange(intlv_ranges)); 222 intlv_ranges.clear(); 223 } 224 intlv_ranges.push_back(r->first); 225 } else { 226 // keep the current range 227 ranges.push_back(r->first); 228 } 229 } 230 } 231 232 // if there is still interleaved ranges waiting to be merged, 233 // go ahead and do it 234 if (!intlv_ranges.empty()) { 235 ranges.push_back(AddrRange(intlv_ranges)); 236 } 237 238 return ranges; 239} 240 241void 242PhysicalMemory::access(PacketPtr pkt) 243{ 244 assert(pkt->isRequest()); 245 Addr addr = pkt->getAddr(); 246 AddrRangeMap<AbstractMemory*>::const_iterator m = addrMap.find(addr); 247 assert(m != addrMap.end()); 248 m->second->access(pkt); 249} 250 251void 252PhysicalMemory::functionalAccess(PacketPtr pkt) 253{ 254 assert(pkt->isRequest()); 255 Addr addr = pkt->getAddr(); 256 AddrRangeMap<AbstractMemory*>::const_iterator m = addrMap.find(addr); 257 assert(m != addrMap.end()); 258 m->second->functionalAccess(pkt); 259} 260 261void 262PhysicalMemory::serialize(ostream& os) 263{ 264 // serialize all the locked addresses and their context ids 265 vector<Addr> lal_addr; 266 vector<int> lal_cid; 267 268 for (vector<AbstractMemory*>::iterator m = memories.begin(); 269 m != memories.end(); ++m) { 270 const list<LockedAddr>& locked_addrs = (*m)->getLockedAddrList(); 271 for (list<LockedAddr>::const_iterator l = locked_addrs.begin(); 272 l != locked_addrs.end(); ++l) { 273 lal_addr.push_back(l->addr); 274 lal_cid.push_back(l->contextId); 275 } 276 } 277 278 arrayParamOut(os, "lal_addr", lal_addr); 279 arrayParamOut(os, "lal_cid", lal_cid); 280 281 // serialize the backing stores 282 unsigned int nbr_of_stores = backingStore.size(); 283 SERIALIZE_SCALAR(nbr_of_stores); 284 285 unsigned int store_id = 0; 286 // store each backing store memory segment in a file 287 for (vector<pair<AddrRange, uint8_t*> >::iterator s = backingStore.begin(); 288 s != backingStore.end(); ++s) { 289 nameOut(os, csprintf("%s.store%d", name(), store_id)); 290 serializeStore(os, store_id++, s->first, s->second); 291 } 292} 293 294void 295PhysicalMemory::serializeStore(ostream& os, unsigned int store_id, 296 AddrRange range, uint8_t* pmem) 297{ 298 // we cannot use the address range for the name as the 299 // memories that are not part of the address map can overlap 300 string filename = name() + ".store" + to_string(store_id) + ".pmem"; 301 long range_size = range.size(); 302 303 DPRINTF(Checkpoint, "Serializing physical memory %s with size %d\n", 304 filename, range_size); 305 306 SERIALIZE_SCALAR(store_id); 307 SERIALIZE_SCALAR(filename); 308 SERIALIZE_SCALAR(range_size); 309 310 // write memory file 311 string filepath = Checkpoint::dir() + "/" + filename.c_str(); 312 int fd = creat(filepath.c_str(), 0664); 313 if (fd < 0) { 314 perror("creat"); 315 fatal("Can't open physical memory checkpoint file '%s'\n", 316 filename); 317 } 318 319 gzFile compressed_mem = gzdopen(fd, "wb"); 320 if (compressed_mem == NULL) 321 fatal("Insufficient memory to allocate compression state for %s\n", 322 filename); 323 324 uint64_t pass_size = 0; 325 326 // gzwrite fails if (int)len < 0 (gzwrite returns int) 327 for (uint64_t written = 0; written < range.size(); 328 written += pass_size) { 329 pass_size = (uint64_t)INT_MAX < (range.size() - written) ? 330 (uint64_t)INT_MAX : (range.size() - written); 331 332 if (gzwrite(compressed_mem, pmem + written, 333 (unsigned int) pass_size) != (int) pass_size) { 334 fatal("Write failed on physical memory checkpoint file '%s'\n", 335 filename); 336 } 337 } 338 339 // close the compressed stream and check that the exit status 340 // is zero 341 if (gzclose(compressed_mem)) 342 fatal("Close failed on physical memory checkpoint file '%s'\n", 343 filename); 344 345} 346 347void 348PhysicalMemory::unserialize(Checkpoint* cp, const string& section) 349{ 350 // unserialize the locked addresses and map them to the 351 // appropriate memory controller 352 vector<Addr> lal_addr; 353 vector<int> lal_cid; 354 arrayParamIn(cp, section, "lal_addr", lal_addr); 355 arrayParamIn(cp, section, "lal_cid", lal_cid); 356 for(size_t i = 0; i < lal_addr.size(); ++i) { 357 AddrRangeMap<AbstractMemory*>::const_iterator m = 358 addrMap.find(lal_addr[i]); 359 m->second->addLockedAddr(LockedAddr(lal_addr[i], lal_cid[i])); 360 } 361 362 // unserialize the backing stores 363 unsigned int nbr_of_stores; 364 UNSERIALIZE_SCALAR(nbr_of_stores); 365 366 for (unsigned int i = 0; i < nbr_of_stores; ++i) { 367 unserializeStore(cp, csprintf("%s.store%d", section, i)); 368 } 369 370} 371 372void 373PhysicalMemory::unserializeStore(Checkpoint* cp, const string& section) 374{ 375 const uint32_t chunk_size = 16384; 376 377 unsigned int store_id; 378 UNSERIALIZE_SCALAR(store_id); 379 380 string filename; 381 UNSERIALIZE_SCALAR(filename); 382 string filepath = cp->cptDir + "/" + filename; 383 384 // mmap memoryfile 385 int fd = open(filepath.c_str(), O_RDONLY); 386 if (fd < 0) { 387 perror("open"); 388 fatal("Can't open physical memory checkpoint file '%s'", filename); 389 } 390 391 gzFile compressed_mem = gzdopen(fd, "rb"); 392 if (compressed_mem == NULL) 393 fatal("Insufficient memory to allocate compression state for %s\n", 394 filename); 395 396 uint8_t* pmem = backingStore[store_id].second; 397 AddrRange range = backingStore[store_id].first; 398 399 // unmap file that was mmapped in the constructor, this is 400 // done here to make sure that gzip and open don't muck with 401 // our nice large space of memory before we reallocate it 402 munmap((char*) pmem, range.size()); 403 404 long range_size; 405 UNSERIALIZE_SCALAR(range_size); 406 407 DPRINTF(Checkpoint, "Unserializing physical memory %s with size %d\n", 408 filename, range_size); 409 410 if (range_size != range.size()) 411 fatal("Memory range size has changed! Saw %lld, expected %lld\n", 412 range_size, range.size()); 413 414 pmem = (uint8_t*) mmap(NULL, range.size(), PROT_READ | PROT_WRITE, 415 MAP_ANON | MAP_PRIVATE, -1, 0); 416 417 if (pmem == (void*) MAP_FAILED) { 418 perror("mmap"); 419 fatal("Could not mmap physical memory!\n"); 420 } 421 422 uint64_t curr_size = 0; 423 long* temp_page = new long[chunk_size]; 424 long* pmem_current; 425 uint32_t bytes_read; 426 while (curr_size < range.size()) { 427 bytes_read = gzread(compressed_mem, temp_page, chunk_size); 428 if (bytes_read == 0) 429 break; 430 431 assert(bytes_read % sizeof(long) == 0); 432 433 for (uint32_t x = 0; x < bytes_read / sizeof(long); x++) { 434 // Only copy bytes that are non-zero, so we don't give 435 // the VM system hell 436 if (*(temp_page + x) != 0) { 437 pmem_current = (long*)(pmem + curr_size + x * sizeof(long)); 438 *pmem_current = *(temp_page + x); 439 } 440 } 441 curr_size += bytes_read; 442 } 443 444 delete[] temp_page; 445 446 if (gzclose(compressed_mem)) 447 fatal("Close failed on physical memory checkpoint file '%s'\n", 448 filename); 449} 450