mem-cache: Create an address aware TempCacheBlk
[gem5.git] / src / mem / physical.cc
index 8eacfccb1c4b1519b2a78c5927e7b9c6e88fcce8..586f1c4756bed9e0ce93847efe63a7ce772328c4 100644 (file)
  * 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 "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), rangeCache(addrMap.end()), 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 (const auto& m : _memories) {
@@ -94,7 +112,9 @@ PhysicalMemory::PhysicalMemory(const string& _name,
             // memories are allowed to overlap in the logic address
             // map
             vector<AbstractMemory*> unmapped_mems{m};
-            createBackingStore(m->getAddrRange(), unmapped_mems);
+            createBackingStore(m->getAddrRange(), unmapped_mems,
+                               m->isConfReported(), m->isInAddrMap(),
+                               m->isKvmMap());
         }
     }
 
@@ -115,7 +135,19 @@ PhysicalMemory::PhysicalMemory(const string& _name,
                 if (!intlv_ranges.empty() &&
                     !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();
                 }
@@ -123,7 +155,10 @@ PhysicalMemory::PhysicalMemory(const string& _name,
                 curr_memories.push_back(r.second);
             } else {
                 vector<AbstractMemory*> single_memory{r.second};
-                createBackingStore(r.first, single_memory);
+                createBackingStore(r.first, single_memory,
+                                   r.second->isConfReported(),
+                                   r.second->isInAddrMap(),
+                                   r.second->isKvmMap());
             }
         }
     }
@@ -132,13 +167,26 @@ PhysicalMemory::PhysicalMemory(const string& _name,
     // 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)
 {
     panic_if(range.interleaved(),
              "Cannot create backing store for interleaved range %s\n",
@@ -148,6 +196,13 @@ PhysicalMemory::createBackingStore(AddrRange range,
     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);
@@ -160,7 +215,8 @@ PhysicalMemory::createBackingStore(AddrRange range,
 
     // remember this backing store so we can checkpoint it and unmap
     // it appropriately
-    backingStore.push_back(make_pair(range, pmem));
+    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) {
@@ -174,14 +230,16 @@ PhysicalMemory::~PhysicalMemory()
 {
     // unmap the backing store
     for (auto& s : backingStore)
-        munmap((char*)s.second, s.first.size());
+        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)) {
+    if (rangeCache != addrMap.end() && rangeCache->first.contains(addr)) {
+        return true;
+    } else {
         // lookup in the interval tree
         const auto& r = addrMap.find(addr);
         if (r == addrMap.end()) {
@@ -189,13 +247,9 @@ PhysicalMemory::isMemAddr(Addr addr) const
             return false;
         }
         // the range is in the tree, update the cache
-        rangeCache = r->first;
+        rangeCache = r;
+        return true;
     }
-
-    assert(addrMap.find(addr) != addrMap.end());
-
-    // either matched the cache or found in the tree
-    return true;
 }
 
 AddrRangeList
@@ -239,9 +293,15 @@ PhysicalMemory::access(PacketPtr pkt)
 {
     assert(pkt->isRequest());
     Addr addr = pkt->getAddr();
-    const auto& m = addrMap.find(addr);
-    assert(m != addrMap.end());
-    m->second->access(pkt);
+    if (rangeCache != addrMap.end() && rangeCache->first.contains(addr)) {
+        rangeCache->second->access(pkt);
+    } else {
+        // do not update the cache here, as we typically call
+        // isMemAddr before calling access
+        const auto& m = addrMap.find(addr);
+        assert(m != addrMap.end());
+        m->second->access(pkt);
+    }
 }
 
 void
@@ -249,17 +309,23 @@ PhysicalMemory::functionalAccess(PacketPtr pkt)
 {
     assert(pkt->isRequest());
     Addr addr = pkt->getAddr();
-    const auto& m = addrMap.find(addr);
-    assert(m != addrMap.end());
-    m->second->functionalAccess(pkt);
+    if (rangeCache != addrMap.end() && rangeCache->first.contains(addr)) {
+        rangeCache->second->functionalAccess(pkt);
+    } else {
+        // do not update the cache here, as we typically call
+        // isMemAddr before calling functionalAccess
+        const auto& m = addrMap.find(addr);
+        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;
+    vector<ContextID> lal_cid;
 
     for (auto& m : memories) {
         const list<LockedAddr>& locked_addrs = m->getLockedAddrList();
@@ -269,8 +335,8 @@ PhysicalMemory::serialize(ostream& os)
         }
     }
 
-    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();
@@ -279,14 +345,14 @@ PhysicalMemory::serialize(ostream& os)
     unsigned int store_id = 0;
     // store each backing store memory segment in a file
     for (auto& s : backingStore) {
-        nameOut(os, csprintf("%s.store%d", name(), store_id));
-        serializeStore(os, store_id++, s.first, s.second);
+        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
@@ -301,7 +367,7 @@ PhysicalMemory::serializeStore(ostream& os, unsigned int store_id,
     SERIALIZE_SCALAR(range_size);
 
     // write memory file
-    string filepath = Checkpoint::dir() + "/" + filename.c_str();
+    string filepath = CheckpointIn::dir() + "/" + filename.c_str();
     gzFile compressed_mem = gzopen(filepath.c_str(), "wb");
     if (compressed_mem == NULL)
         fatal("Can't open physical memory checkpoint file '%s'\n",
@@ -331,15 +397,15 @@ PhysicalMemory::serializeStore(ostream& os, unsigned int store_id,
 }
 
 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) {
+    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.find(lal_addr[i]);
         m->second->addLockedAddr(LockedAddr(lal_addr[i], lal_cid[i]));
     }
@@ -349,13 +415,14 @@ PhysicalMemory::unserialize(Checkpoint* cp, const string& section)
     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;
 
@@ -364,7 +431,7 @@ PhysicalMemory::unserializeStore(Checkpoint* cp, const string& section)
 
     string filename;
     UNSERIALIZE_SCALAR(filename);
-    string filepath = cp->cptDir + "/" + filename;
+    string filepath = cp.cptDir + "/" + filename;
 
     // mmap memoryfile
     gzFile compressed_mem = gzopen(filepath.c_str(), "rb");
@@ -372,8 +439,8 @@ PhysicalMemory::unserializeStore(Checkpoint* cp, const string& section)
         fatal("Can't open physical memory checkpoint file '%s'", filename);
 
     // we've already got the actual backing store mapped
-    uint8_t* pmem = backingStore[store_id].second;
-    AddrRange range = backingStore[store_id].first;
+    uint8_t* pmem = backingStore[store_id].pmem;
+    AddrRange range = backingStore[store_id].range;
 
     long range_size;
     UNSERIALIZE_SCALAR(range_size);