physical.cc revision 11614:29606f000389
1/* 2 * Copyright (c) 2012, 2014 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/AddrRanges.hh" 55#include "debug/Checkpoint.hh" 56#include "mem/abstract_mem.hh" 57#include "mem/physical.hh" 58 59/** 60 * On Linux, MAP_NORESERVE allow us to simulate a very large memory 61 * without committing to actually providing the swap space on the 62 * host. On FreeBSD or OSX the MAP_NORESERVE flag does not exist, 63 * so simply make it 0. 64 */ 65#if defined(__APPLE__) || defined(__FreeBSD__) 66#ifndef MAP_NORESERVE 67#define MAP_NORESERVE 0 68#endif 69#endif 70 71using namespace std; 72 73PhysicalMemory::PhysicalMemory(const string& _name, 74 const vector<AbstractMemory*>& _memories, 75 bool mmap_using_noreserve) : 76 _name(_name), rangeCache(addrMap.end()), size(0), 77 mmapUsingNoReserve(mmap_using_noreserve) 78{ 79 if (mmap_using_noreserve) 80 warn("Not reserving swap space. May cause SIGSEGV on actual usage\n"); 81 82 // add the memories from the system to the address map as 83 // appropriate 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); 88 89 // calculate the total size once and for all 90 size += m->size(); 91 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", 96 m->name()); 97 } else { 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 101 DPRINTF(AddrRanges, 102 "Skipping memory %s that is not in global address map\n", 103 m->name()); 104 105 // sanity check 106 fatal_if(m->getAddrRange().interleaved(), 107 "Memory %s that is not in the global address map cannot " 108 "be interleaved\n", m->name()); 109 110 // simply do it independently, also note that this kind of 111 // memories are allowed to overlap in the logic address 112 // map 113 vector<AbstractMemory*> unmapped_mems{m}; 114 createBackingStore(m->getAddrRange(), unmapped_mems, 115 m->isConfReported(), m->isInAddrMap(), 116 m->isKvmMap()); 117 } 118 } 119 120 // iterate over the increasing addresses and chunks of contiguous 121 // space to be mapped to backing store, create it and inform the 122 // memories 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); 137 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 " 144 "range\n"); 145 146 createBackingStore(merged_range, curr_memories, 147 f->isConfReported(), f->isInAddrMap(), 148 f->isKvmMap()); 149 150 intlv_ranges.clear(); 151 curr_memories.clear(); 152 } 153 intlv_ranges.push_back(r.first); 154 curr_memories.push_back(r.second); 155 } else { 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()); 161 } 162 } 163 } 164 165 // if there is still interleaved ranges waiting to be merged, go 166 // ahead and do it 167 if (!intlv_ranges.empty()) { 168 AddrRange merged_range(intlv_ranges); 169 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 " 176 "range\n"); 177 178 createBackingStore(merged_range, curr_memories, 179 f->isConfReported(), f->isInAddrMap(), 180 f->isKvmMap()); 181 } 182} 183 184void 185PhysicalMemory::createBackingStore(AddrRange range, 186 const vector<AbstractMemory*>& _memories, 187 bool conf_table_reported, 188 bool in_addr_map, bool kvm_map) 189{ 190 panic_if(range.interleaved(), 191 "Cannot create backing store for interleaved range %s\n", 192 range.to_string()); 193 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; 198 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; 203 } 204 205 uint8_t* pmem = (uint8_t*) mmap(NULL, range.size(), 206 PROT_READ | PROT_WRITE, 207 map_flags, -1, 0); 208 209 if (pmem == (uint8_t*) MAP_FAILED) { 210 perror("mmap"); 211 fatal("Could not mmap %d bytes for range %s!\n", range.size(), 212 range.to_string()); 213 } 214 215 // remember this backing store so we can checkpoint it and unmap 216 // it appropriately 217 backingStore.emplace_back(range, pmem, 218 conf_table_reported, in_addr_map, kvm_map); 219 220 // point the memories to their backing store 221 for (const auto& m : _memories) { 222 DPRINTF(AddrRanges, "Mapping memory %s to backing store\n", 223 m->name()); 224 m->setBackingStore(pmem); 225 } 226} 227 228PhysicalMemory::~PhysicalMemory() 229{ 230 // unmap the backing store 231 for (auto& s : backingStore) 232 munmap((char*)s.pmem, s.range.size()); 233} 234 235bool 236PhysicalMemory::isMemAddr(Addr addr) const 237{ 238 // see if the address is within the last matched range 239 if (rangeCache != addrMap.end() && rangeCache->first.contains(addr)) { 240 return true; 241 } else { 242 // lookup in the interval tree 243 const auto& r = addrMap.find(addr); 244 if (r == addrMap.end()) { 245 // not in the cache, and not in the tree 246 return false; 247 } 248 // the range is in the tree, update the cache 249 rangeCache = r; 250 return true; 251 } 252} 253 254AddrRangeList 255PhysicalMemory::getConfAddrRanges() const 256{ 257 // this could be done once in the constructor, but since it is unlikely to 258 // be called more than once the iteration should not be a problem 259 AddrRangeList ranges; 260 vector<AddrRange> intlv_ranges; 261 for (const auto& r : addrMap) { 262 if (r.second->isConfReported()) { 263 // if the range is interleaved then save it for now 264 if (r.first.interleaved()) { 265 // if we already got interleaved ranges that are not 266 // part of the same range, then first do a merge 267 // before we add the new one 268 if (!intlv_ranges.empty() && 269 !intlv_ranges.back().mergesWith(r.first)) { 270 ranges.push_back(AddrRange(intlv_ranges)); 271 intlv_ranges.clear(); 272 } 273 intlv_ranges.push_back(r.first); 274 } else { 275 // keep the current range 276 ranges.push_back(r.first); 277 } 278 } 279 } 280 281 // if there is still interleaved ranges waiting to be merged, 282 // go ahead and do it 283 if (!intlv_ranges.empty()) { 284 ranges.push_back(AddrRange(intlv_ranges)); 285 } 286 287 return ranges; 288} 289 290void 291PhysicalMemory::access(PacketPtr pkt) 292{ 293 assert(pkt->isRequest()); 294 Addr addr = pkt->getAddr(); 295 if (rangeCache != addrMap.end() && rangeCache->first.contains(addr)) { 296 rangeCache->second->access(pkt); 297 } else { 298 // do not update the cache here, as we typically call 299 // isMemAddr before calling access 300 const auto& m = addrMap.find(addr); 301 assert(m != addrMap.end()); 302 m->second->access(pkt); 303 } 304} 305 306void 307PhysicalMemory::functionalAccess(PacketPtr pkt) 308{ 309 assert(pkt->isRequest()); 310 Addr addr = pkt->getAddr(); 311 if (rangeCache != addrMap.end() && rangeCache->first.contains(addr)) { 312 rangeCache->second->functionalAccess(pkt); 313 } else { 314 // do not update the cache here, as we typically call 315 // isMemAddr before calling functionalAccess 316 const auto& m = addrMap.find(addr); 317 assert(m != addrMap.end()); 318 m->second->functionalAccess(pkt); 319 } 320} 321 322void 323PhysicalMemory::serialize(CheckpointOut &cp) const 324{ 325 // serialize all the locked addresses and their context ids 326 vector<Addr> lal_addr; 327 vector<ContextID> lal_cid; 328 329 for (auto& m : memories) { 330 const list<LockedAddr>& locked_addrs = m->getLockedAddrList(); 331 for (const auto& l : locked_addrs) { 332 lal_addr.push_back(l.addr); 333 lal_cid.push_back(l.contextId); 334 } 335 } 336 337 SERIALIZE_CONTAINER(lal_addr); 338 SERIALIZE_CONTAINER(lal_cid); 339 340 // serialize the backing stores 341 unsigned int nbr_of_stores = backingStore.size(); 342 SERIALIZE_SCALAR(nbr_of_stores); 343 344 unsigned int store_id = 0; 345 // store each backing store memory segment in a file 346 for (auto& s : backingStore) { 347 ScopedCheckpointSection sec(cp, csprintf("store%d", store_id)); 348 serializeStore(cp, store_id++, s.range, s.pmem); 349 } 350} 351 352void 353PhysicalMemory::serializeStore(CheckpointOut &cp, unsigned int store_id, 354 AddrRange range, uint8_t* pmem) const 355{ 356 // we cannot use the address range for the name as the 357 // memories that are not part of the address map can overlap 358 string filename = name() + ".store" + to_string(store_id) + ".pmem"; 359 long range_size = range.size(); 360 361 DPRINTF(Checkpoint, "Serializing physical memory %s with size %d\n", 362 filename, range_size); 363 364 SERIALIZE_SCALAR(store_id); 365 SERIALIZE_SCALAR(filename); 366 SERIALIZE_SCALAR(range_size); 367 368 // write memory file 369 string filepath = CheckpointIn::dir() + "/" + filename.c_str(); 370 gzFile compressed_mem = gzopen(filepath.c_str(), "wb"); 371 if (compressed_mem == NULL) 372 fatal("Can't open physical memory checkpoint file '%s'\n", 373 filename); 374 375 uint64_t pass_size = 0; 376 377 // gzwrite fails if (int)len < 0 (gzwrite returns int) 378 for (uint64_t written = 0; written < range.size(); 379 written += pass_size) { 380 pass_size = (uint64_t)INT_MAX < (range.size() - written) ? 381 (uint64_t)INT_MAX : (range.size() - written); 382 383 if (gzwrite(compressed_mem, pmem + written, 384 (unsigned int) pass_size) != (int) pass_size) { 385 fatal("Write failed on physical memory checkpoint file '%s'\n", 386 filename); 387 } 388 } 389 390 // close the compressed stream and check that the exit status 391 // is zero 392 if (gzclose(compressed_mem)) 393 fatal("Close failed on physical memory checkpoint file '%s'\n", 394 filename); 395 396} 397 398void 399PhysicalMemory::unserialize(CheckpointIn &cp) 400{ 401 // unserialize the locked addresses and map them to the 402 // appropriate memory controller 403 vector<Addr> lal_addr; 404 vector<ContextID> lal_cid; 405 UNSERIALIZE_CONTAINER(lal_addr); 406 UNSERIALIZE_CONTAINER(lal_cid); 407 for (size_t i = 0; i < lal_addr.size(); ++i) { 408 const auto& m = addrMap.find(lal_addr[i]); 409 m->second->addLockedAddr(LockedAddr(lal_addr[i], lal_cid[i])); 410 } 411 412 // unserialize the backing stores 413 unsigned int nbr_of_stores; 414 UNSERIALIZE_SCALAR(nbr_of_stores); 415 416 for (unsigned int i = 0; i < nbr_of_stores; ++i) { 417 ScopedCheckpointSection sec(cp, csprintf("store%d", i)); 418 unserializeStore(cp); 419 } 420 421} 422 423void 424PhysicalMemory::unserializeStore(CheckpointIn &cp) 425{ 426 const uint32_t chunk_size = 16384; 427 428 unsigned int store_id; 429 UNSERIALIZE_SCALAR(store_id); 430 431 string filename; 432 UNSERIALIZE_SCALAR(filename); 433 string filepath = cp.cptDir + "/" + filename; 434 435 // mmap memoryfile 436 gzFile compressed_mem = gzopen(filepath.c_str(), "rb"); 437 if (compressed_mem == NULL) 438 fatal("Can't open physical memory checkpoint file '%s'", filename); 439 440 // we've already got the actual backing store mapped 441 uint8_t* pmem = backingStore[store_id].pmem; 442 AddrRange range = backingStore[store_id].range; 443 444 long range_size; 445 UNSERIALIZE_SCALAR(range_size); 446 447 DPRINTF(Checkpoint, "Unserializing physical memory %s with size %d\n", 448 filename, range_size); 449 450 if (range_size != range.size()) 451 fatal("Memory range size has changed! Saw %lld, expected %lld\n", 452 range_size, range.size()); 453 454 uint64_t curr_size = 0; 455 long* temp_page = new long[chunk_size]; 456 long* pmem_current; 457 uint32_t bytes_read; 458 while (curr_size < range.size()) { 459 bytes_read = gzread(compressed_mem, temp_page, chunk_size); 460 if (bytes_read == 0) 461 break; 462 463 assert(bytes_read % sizeof(long) == 0); 464 465 for (uint32_t x = 0; x < bytes_read / sizeof(long); x++) { 466 // Only copy bytes that are non-zero, so we don't give 467 // the VM system hell 468 if (*(temp_page + x) != 0) { 469 pmem_current = (long*)(pmem + curr_size + x * sizeof(long)); 470 *pmem_current = *(temp_page + x); 471 } 472 } 473 curr_size += bytes_read; 474 } 475 476 delete[] temp_page; 477 478 if (gzclose(compressed_mem)) 479 fatal("Close failed on physical memory checkpoint file '%s'\n", 480 filename); 481} 482