/*
- * Copyright (c) 2012 ARM Limited
+ * Copyright (c) 2012, 2014, 2018 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
* Authors: Andreas Hansson
*/
+#include "mem/physical.hh"
+
+#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/user.h>
-#include <fcntl.h>
#include <unistd.h>
#include <zlib.h>
#include <string>
#include "base/trace.hh"
-#include "debug/BusAddrRanges.hh"
+#include "debug/AddrRanges.hh"
#include "debug/Checkpoint.hh"
#include "mem/abstract_mem.hh"
-#include "mem/physical.hh"
+
+/**
+ * On Linux, MAP_NORESERVE allow us to simulate a very large memory
+ * without committing to actually providing the swap space on the
+ * host. On FreeBSD or OSX the MAP_NORESERVE flag does not exist,
+ * so simply make it 0.
+ */
+#if defined(__APPLE__) || defined(__FreeBSD__)
+#ifndef MAP_NORESERVE
+#define MAP_NORESERVE 0
+#endif
+#endif
using namespace std;
PhysicalMemory::PhysicalMemory(const string& _name,
- const vector<AbstractMemory*>& _memories) :
- _name(_name), size(0)
+ const vector<AbstractMemory*>& _memories,
+ bool mmap_using_noreserve) :
+ _name(_name), size(0), mmapUsingNoReserve(mmap_using_noreserve)
{
+ if (mmap_using_noreserve)
+ warn("Not reserving swap space. May cause SIGSEGV on actual usage\n");
+
// add the memories from the system to the address map as
// appropriate
- for (vector<AbstractMemory*>::const_iterator m = _memories.begin();
- m != _memories.end(); ++m) {
+ for (const auto& m : _memories) {
// only add the memory if it is part of the global address map
- if ((*m)->isInAddrMap()) {
- memories.push_back(*m);
+ if (m->isInAddrMap()) {
+ memories.push_back(m);
// calculate the total size once and for all
- size += (*m)->size();
+ size += m->size();
// add the range to our interval tree and make sure it does not
// intersect an existing range
- if (addrMap.insert((*m)->getAddrRange(), *m) == addrMap.end())
- fatal("Memory address range for %s is overlapping\n",
- (*m)->name());
+ fatal_if(addrMap.insert(m->getAddrRange(), m) == addrMap.end(),
+ "Memory address range for %s is overlapping\n",
+ m->name());
} else {
- DPRINTF(BusAddrRanges,
- "Skipping memory %s that is not in global address map\n",
- (*m)->name());
// this type of memory is used e.g. as reference memory by
// Ruby, and they also needs a backing store, but should
// not be part of the global address map
+ DPRINTF(AddrRanges,
+ "Skipping memory %s that is not in global address map\n",
+ m->name());
+
+ // sanity check
+ fatal_if(m->getAddrRange().interleaved(),
+ "Memory %s that is not in the global address map cannot "
+ "be interleaved\n", m->name());
// simply do it independently, also note that this kind of
// memories are allowed to overlap in the logic address
// map
- vector<AbstractMemory*> unmapped_mems;
- unmapped_mems.push_back(*m);
- createBackingStore((*m)->getAddrRange(), unmapped_mems);
+ vector<AbstractMemory*> unmapped_mems{m};
+ createBackingStore(m->getAddrRange(), unmapped_mems,
+ m->isConfReported(), m->isInAddrMap(),
+ m->isKvmMap());
}
}
- // iterate over the increasing addresses and chunks of contigous
- // space to be mapped to backing store, also remember what
- // memories constitute the range so we can go and find out if we
- // have to init their parts to zero
+ // iterate over the increasing addresses and chunks of contiguous
+ // space to be mapped to backing store, create it and inform the
+ // memories
vector<AddrRange> intlv_ranges;
vector<AbstractMemory*> curr_memories;
- for (AddrRangeMap<AbstractMemory*>::const_iterator r = addrMap.begin();
- r != addrMap.end(); ++r) {
+ for (const auto& r : addrMap) {
// simply skip past all memories that are null and hence do
// not need any backing store
- if (!r->second->isNull()) {
+ if (!r.second->isNull()) {
// if the range is interleaved then save it for now
- if (r->first.interleaved()) {
+ if (r.first.interleaved()) {
// if we already got interleaved ranges that are not
// part of the same range, then first do a merge
// before we add the new one
if (!intlv_ranges.empty() &&
- !intlv_ranges.back().mergesWith(r->first)) {
+ !intlv_ranges.back().mergesWith(r.first)) {
AddrRange merged_range(intlv_ranges);
- createBackingStore(merged_range, curr_memories);
+
+ AbstractMemory *f = curr_memories.front();
+ for (const auto& c : curr_memories)
+ if (f->isConfReported() != c->isConfReported() ||
+ f->isInAddrMap() != c->isInAddrMap() ||
+ f->isKvmMap() != c->isKvmMap())
+ fatal("Inconsistent flags in an interleaved "
+ "range\n");
+
+ createBackingStore(merged_range, curr_memories,
+ f->isConfReported(), f->isInAddrMap(),
+ f->isKvmMap());
+
intlv_ranges.clear();
curr_memories.clear();
}
- intlv_ranges.push_back(r->first);
- curr_memories.push_back(r->second);
+ intlv_ranges.push_back(r.first);
+ curr_memories.push_back(r.second);
} else {
- vector<AbstractMemory*> single_memory;
- single_memory.push_back(r->second);
- createBackingStore(r->first, single_memory);
+ vector<AbstractMemory*> single_memory{r.second};
+ createBackingStore(r.first, single_memory,
+ r.second->isConfReported(),
+ r.second->isInAddrMap(),
+ r.second->isKvmMap());
}
}
}
// ahead and do it
if (!intlv_ranges.empty()) {
AddrRange merged_range(intlv_ranges);
- createBackingStore(merged_range, curr_memories);
+
+ AbstractMemory *f = curr_memories.front();
+ for (const auto& c : curr_memories)
+ if (f->isConfReported() != c->isConfReported() ||
+ f->isInAddrMap() != c->isInAddrMap() ||
+ f->isKvmMap() != c->isKvmMap())
+ fatal("Inconsistent flags in an interleaved "
+ "range\n");
+
+ createBackingStore(merged_range, curr_memories,
+ f->isConfReported(), f->isInAddrMap(),
+ f->isKvmMap());
}
}
void
PhysicalMemory::createBackingStore(AddrRange range,
- const vector<AbstractMemory*>& _memories)
+ const vector<AbstractMemory*>& _memories,
+ bool conf_table_reported,
+ bool in_addr_map, bool kvm_map)
{
- if (range.interleaved())
- panic("Cannot create backing store for interleaved range %s\n",
+ panic_if(range.interleaved(),
+ "Cannot create backing store for interleaved range %s\n",
range.to_string());
// perform the actual mmap
- DPRINTF(BusAddrRanges, "Creating backing store for range %s with size %d\n",
+ DPRINTF(AddrRanges, "Creating backing store for range %s with size %d\n",
range.to_string(), range.size());
int map_flags = MAP_ANON | MAP_PRIVATE;
+
+ // to be able to simulate very large memories, the user can opt to
+ // pass noreserve to mmap
+ if (mmapUsingNoReserve) {
+ map_flags |= MAP_NORESERVE;
+ }
+
uint8_t* pmem = (uint8_t*) mmap(NULL, range.size(),
PROT_READ | PROT_WRITE,
map_flags, -1, 0);
// remember this backing store so we can checkpoint it and unmap
// it appropriately
- backingStore.push_back(make_pair(range, pmem));
-
- // count how many of the memories are to be zero initialized so we
- // can see if some but not all have this parameter set
- uint32_t init_to_zero = 0;
-
- // point the memories to their backing store, and if requested,
- // initialize the memory range to 0
- for (vector<AbstractMemory*>::const_iterator m = _memories.begin();
- m != _memories.end(); ++m) {
- DPRINTF(BusAddrRanges, "Mapping memory %s to backing store\n",
- (*m)->name());
- (*m)->setBackingStore(pmem);
-
- // if it should be zero, then go and make it so
- if ((*m)->initToZero()) {
- ++init_to_zero;
- }
- }
-
- if (init_to_zero != 0) {
- if (init_to_zero != _memories.size())
- fatal("Some, but not all memories in range %s are set zero\n",
- range.to_string());
-
- memset(pmem, 0, range.size());
+ backingStore.emplace_back(range, pmem,
+ conf_table_reported, in_addr_map, kvm_map);
+
+ // point the memories to their backing store
+ for (const auto& m : _memories) {
+ DPRINTF(AddrRanges, "Mapping memory %s to backing store\n",
+ m->name());
+ m->setBackingStore(pmem);
}
}
PhysicalMemory::~PhysicalMemory()
{
// unmap the backing store
- for (vector<pair<AddrRange, uint8_t*> >::iterator s = backingStore.begin();
- s != backingStore.end(); ++s)
- munmap((char*)s->second, s->first.size());
+ for (auto& s : backingStore)
+ munmap((char*)s.pmem, s.range.size());
}
bool
PhysicalMemory::isMemAddr(Addr addr) const
{
- // see if the address is within the last matched range
- if (!rangeCache.contains(addr)) {
- // lookup in the interval tree
- AddrRangeMap<AbstractMemory*>::const_iterator r = addrMap.find(addr);
- if (r == addrMap.end()) {
- // not in the cache, and not in the tree
- return false;
- }
- // the range is in the tree, update the cache
- rangeCache = r->first;
- }
-
- assert(addrMap.find(addr) != addrMap.end());
-
- // either matched the cache or found in the tree
- return true;
+ return addrMap.contains(addr) != addrMap.end();
}
AddrRangeList
// be called more than once the iteration should not be a problem
AddrRangeList ranges;
vector<AddrRange> intlv_ranges;
- for (AddrRangeMap<AbstractMemory*>::const_iterator r = addrMap.begin();
- r != addrMap.end(); ++r) {
- if (r->second->isConfReported()) {
+ for (const auto& r : addrMap) {
+ if (r.second->isConfReported()) {
// if the range is interleaved then save it for now
- if (r->first.interleaved()) {
+ if (r.first.interleaved()) {
// if we already got interleaved ranges that are not
// part of the same range, then first do a merge
// before we add the new one
if (!intlv_ranges.empty() &&
- !intlv_ranges.back().mergesWith(r->first)) {
+ !intlv_ranges.back().mergesWith(r.first)) {
ranges.push_back(AddrRange(intlv_ranges));
intlv_ranges.clear();
}
- intlv_ranges.push_back(r->first);
+ intlv_ranges.push_back(r.first);
} else {
// keep the current range
- ranges.push_back(r->first);
+ ranges.push_back(r.first);
}
}
}
PhysicalMemory::access(PacketPtr pkt)
{
assert(pkt->isRequest());
- Addr addr = pkt->getAddr();
- AddrRangeMap<AbstractMemory*>::const_iterator m = addrMap.find(addr);
+ const auto& m = addrMap.contains(pkt->getAddrRange());
assert(m != addrMap.end());
m->second->access(pkt);
}
PhysicalMemory::functionalAccess(PacketPtr pkt)
{
assert(pkt->isRequest());
- Addr addr = pkt->getAddr();
- AddrRangeMap<AbstractMemory*>::const_iterator m = addrMap.find(addr);
+ const auto& m = addrMap.contains(pkt->getAddrRange());
assert(m != addrMap.end());
m->second->functionalAccess(pkt);
}
void
-PhysicalMemory::serialize(ostream& os)
+PhysicalMemory::serialize(CheckpointOut &cp) const
{
// serialize all the locked addresses and their context ids
vector<Addr> lal_addr;
- vector<int> lal_cid;
-
- for (vector<AbstractMemory*>::iterator m = memories.begin();
- m != memories.end(); ++m) {
- const list<LockedAddr>& locked_addrs = (*m)->getLockedAddrList();
- for (list<LockedAddr>::const_iterator l = locked_addrs.begin();
- l != locked_addrs.end(); ++l) {
- lal_addr.push_back(l->addr);
- lal_cid.push_back(l->contextId);
+ vector<ContextID> lal_cid;
+
+ for (auto& m : memories) {
+ const list<LockedAddr>& locked_addrs = m->getLockedAddrList();
+ for (const auto& l : locked_addrs) {
+ lal_addr.push_back(l.addr);
+ lal_cid.push_back(l.contextId);
}
}
- arrayParamOut(os, "lal_addr", lal_addr);
- arrayParamOut(os, "lal_cid", lal_cid);
+ SERIALIZE_CONTAINER(lal_addr);
+ SERIALIZE_CONTAINER(lal_cid);
// serialize the backing stores
unsigned int nbr_of_stores = backingStore.size();
unsigned int store_id = 0;
// store each backing store memory segment in a file
- for (vector<pair<AddrRange, uint8_t*> >::iterator s = backingStore.begin();
- s != backingStore.end(); ++s) {
- nameOut(os, csprintf("%s.store%d", name(), store_id));
- serializeStore(os, store_id++, s->first, s->second);
+ for (auto& s : backingStore) {
+ ScopedCheckpointSection sec(cp, csprintf("store%d", store_id));
+ serializeStore(cp, store_id++, s.range, s.pmem);
}
}
void
-PhysicalMemory::serializeStore(ostream& os, unsigned int store_id,
- AddrRange range, uint8_t* pmem)
+PhysicalMemory::serializeStore(CheckpointOut &cp, unsigned int store_id,
+ AddrRange range, uint8_t* pmem) const
{
// we cannot use the address range for the name as the
// memories that are not part of the address map can overlap
SERIALIZE_SCALAR(range_size);
// write memory file
- string filepath = Checkpoint::dir() + "/" + filename.c_str();
- int fd = creat(filepath.c_str(), 0664);
- if (fd < 0) {
- perror("creat");
- fatal("Can't open physical memory checkpoint file '%s'\n",
- filename);
- }
-
- gzFile compressed_mem = gzdopen(fd, "wb");
+ string filepath = CheckpointIn::dir() + "/" + filename.c_str();
+ gzFile compressed_mem = gzopen(filepath.c_str(), "wb");
if (compressed_mem == NULL)
- fatal("Insufficient memory to allocate compression state for %s\n",
+ fatal("Can't open physical memory checkpoint file '%s'\n",
filename);
uint64_t pass_size = 0;
}
void
-PhysicalMemory::unserialize(Checkpoint* cp, const string& section)
+PhysicalMemory::unserialize(CheckpointIn &cp)
{
// unserialize the locked addresses and map them to the
// appropriate memory controller
vector<Addr> lal_addr;
- vector<int> lal_cid;
- arrayParamIn(cp, section, "lal_addr", lal_addr);
- arrayParamIn(cp, section, "lal_cid", lal_cid);
- for(size_t i = 0; i < lal_addr.size(); ++i) {
- AddrRangeMap<AbstractMemory*>::const_iterator m =
- addrMap.find(lal_addr[i]);
+ vector<ContextID> lal_cid;
+ UNSERIALIZE_CONTAINER(lal_addr);
+ UNSERIALIZE_CONTAINER(lal_cid);
+ for (size_t i = 0; i < lal_addr.size(); ++i) {
+ const auto& m = addrMap.contains(lal_addr[i]);
m->second->addLockedAddr(LockedAddr(lal_addr[i], lal_cid[i]));
}
UNSERIALIZE_SCALAR(nbr_of_stores);
for (unsigned int i = 0; i < nbr_of_stores; ++i) {
- unserializeStore(cp, csprintf("%s.store%d", section, i));
+ ScopedCheckpointSection sec(cp, csprintf("store%d", i));
+ unserializeStore(cp);
}
}
void
-PhysicalMemory::unserializeStore(Checkpoint* cp, const string& section)
+PhysicalMemory::unserializeStore(CheckpointIn &cp)
{
const uint32_t chunk_size = 16384;
string filename;
UNSERIALIZE_SCALAR(filename);
- string filepath = cp->cptDir + "/" + filename;
+ string filepath = cp.cptDir + "/" + filename;
// mmap memoryfile
- int fd = open(filepath.c_str(), O_RDONLY);
- if (fd < 0) {
- perror("open");
- fatal("Can't open physical memory checkpoint file '%s'", filename);
- }
-
- gzFile compressed_mem = gzdopen(fd, "rb");
+ gzFile compressed_mem = gzopen(filepath.c_str(), "rb");
if (compressed_mem == NULL)
- fatal("Insufficient memory to allocate compression state for %s\n",
- filename);
-
- uint8_t* pmem = backingStore[store_id].second;
- AddrRange range = backingStore[store_id].first;
+ fatal("Can't open physical memory checkpoint file '%s'", filename);
- // unmap file that was mmapped in the constructor, this is
- // done here to make sure that gzip and open don't muck with
- // our nice large space of memory before we reallocate it
- munmap((char*) pmem, range.size());
+ // we've already got the actual backing store mapped
+ uint8_t* pmem = backingStore[store_id].pmem;
+ AddrRange range = backingStore[store_id].range;
long range_size;
UNSERIALIZE_SCALAR(range_size);
fatal("Memory range size has changed! Saw %lld, expected %lld\n",
range_size, range.size());
- pmem = (uint8_t*) mmap(NULL, range.size(), PROT_READ | PROT_WRITE,
- MAP_ANON | MAP_PRIVATE, -1, 0);
-
- if (pmem == (void*) MAP_FAILED) {
- perror("mmap");
- fatal("Could not mmap physical memory!\n");
- }
-
uint64_t curr_size = 0;
long* temp_page = new long[chunk_size];
long* pmem_current;