sim-se: Add prlimit system call
[gem5.git] / src / sim / system.cc
index 67fb7480a185eae51805e103a433f4e7f39a9d4f..42cd5e720e0d5c2a92d0059b2e3b82b287e10632 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2012 ARM Limited
+ * Copyright (c) 2011-2014 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
  *          Rick Strong
  */
 
-#include "arch/isa_traits.hh"
+#include "sim/system.hh"
+
 #include "arch/remote_gdb.hh"
 #include "arch/utility.hh"
-#include "arch/vtophys.hh"
 #include "base/loader/object_file.hh"
 #include "base/loader/symtab.hh"
+#include "base/str.hh"
 #include "base/trace.hh"
-#include "config/the_isa.hh"
+#include "config/use_kvm.hh"
+#if USE_KVM
+#include "cpu/kvm/base.hh"
+#include "cpu/kvm/vm.hh"
+#endif
+#include "cpu/base.hh"
 #include "cpu/thread_context.hh"
 #include "debug/Loader.hh"
 #include "debug/WorkItems.hh"
-#include "kern/kernel_stats.hh"
+#include "mem/abstract_mem.hh"
 #include "mem/physical.hh"
 #include "params/System.hh"
 #include "sim/byteswap.hh"
 #include "sim/debug.hh"
 #include "sim/full_system.hh"
-#include "sim/system.hh"
+
+/**
+ * To avoid linking errors with LTO, only include the header if we
+ * actually have a definition.
+ */
+#if THE_ISA != NULL_ISA
+#include "kern/kernel_stats.hh"
+
+#endif
 
 using namespace std;
 using namespace TheISA;
@@ -74,17 +88,26 @@ int System::numSystemsRunning = 0;
 System::System(Params *p)
     : MemObject(p), _systemPort("system_port", this),
       _numContexts(0),
+      multiThread(p->multi_thread),
       pagePtr(0),
       init_param(p->init_param),
-      physProxy(_systemPort),
-      virtProxy(_systemPort),
+      physProxy(_systemPort, p->cache_line_size),
+      kernelSymtab(nullptr),
+      kernel(nullptr),
       loadAddrMask(p->load_addr_mask),
-      nextPID(0),
-      physmem(p->memories),
+      loadAddrOffset(p->load_offset),
+#if USE_KVM
+      kvmVM(p->kvm_vm),
+#else
+      kvmVM(nullptr),
+#endif
+      physmem(name() + ".physmem", p->memories, p->mmap_using_noreserve),
       memoryMode(p->mem_mode),
+      _cacheLineSize(p->cache_line_size),
       workItemsBegin(0),
       workItemsEnd(0),
       numWorkIds(p->num_work_ids),
+      thermalModel(p->thermal_model),
       _params(p),
       totalNumInsts(0),
       instEventQueue("system instruction-based event queue")
@@ -92,12 +115,23 @@ System::System(Params *p)
     // add self to global system list
     systemList.push_back(this);
 
+#if USE_KVM
+    if (kvmVM) {
+        kvmVM->setSystem(this);
+    }
+#endif
+
     if (FullSystem) {
         kernelSymtab = new SymbolTable;
         if (!debugSymbolTable)
             debugSymbolTable = new SymbolTable;
     }
 
+    // check if the cache line size is a value known to work
+    if (!(_cacheLineSize == 16 || _cacheLineSize == 32 ||
+          _cacheLineSize == 64 || _cacheLineSize == 128))
+        warn_once("Cache line size is neither 16, 32, 64 nor 128 bytes.\n");
+
     // Get the generic system master IDs
     MasterID tmp_id M5_VAR_USED;
     tmp_id = getMasterId("writebacks");
@@ -110,7 +144,7 @@ System::System(Params *p)
     if (FullSystem) {
         if (params()->kernel == "") {
             inform("No kernel set for full system simulation. "
-                    "Assuming you know what you're doing if not SPARC ISA\n");
+                   "Assuming you know what you're doing\n");
         } else {
             // Get the kernel code
             kernel = createObjectFile(params()->kernel);
@@ -142,7 +176,7 @@ System::System(Params *p)
         }
     }
 
-    // increment the number of running systms
+    // increment the number of running systems
     numSystemsRunning++;
 
     // Set back pointers to the system in all memories
@@ -167,8 +201,8 @@ System::init()
         panic("System port on %s is not connected.\n", name());
 }
 
-MasterPort&
-System::getMasterPort(const std::string &if_name, int idx)
+BaseMasterPort&
+System::getMasterPort(const std::string &if_name, PortID idx)
 {
     // no need to distinguish at the moment (besides checking)
     return _systemPort;
@@ -177,7 +211,7 @@ System::getMasterPort(const std::string &if_name, int idx)
 void
 System::setMemoryMode(Enums::MemoryMode mode)
 {
-    assert(getState() == Drained);
+    assert(drainState() == DrainState::Drained);
     memoryMode = mode;
 }
 
@@ -188,18 +222,11 @@ bool System::breakpoint()
     return false;
 }
 
-/**
- * Setting rgdb_wait to a positive integer waits for a remote debugger to
- * connect to that context ID before continuing.  This should really
-   be a parameter on the CPU object or something...
- */
-int rgdb_wait = -1;
-
-int
-System::registerThreadContext(ThreadContext *tc, int assigned)
+ContextID
+System::registerThreadContext(ThreadContext *tc, ContextID assigned)
 {
     int id;
-    if (assigned == -1) {
+    if (assigned == InvalidContextID) {
         for (id = 0; id < threadContexts.size(); id++) {
             if (!threadContexts[id])
                 break;
@@ -219,21 +246,27 @@ System::registerThreadContext(ThreadContext *tc, int assigned)
     threadContexts[id] = tc;
     _numContexts++;
 
+#if THE_ISA != NULL_ISA
     int port = getRemoteGDBPort();
     if (port) {
         RemoteGDB *rgdb = new RemoteGDB(this, tc);
         GDBListener *gdbl = new GDBListener(rgdb, port + id);
         gdbl->listen();
 
-        if (rgdb_wait != -1 && rgdb_wait == id)
-            gdbl->accept();
+        BaseCPU *cpu = tc->getCpuPtr();
+        if (cpu->waitForRemoteGDB()) {
+            inform("%s: Waiting for a remote GDB connection on port %d.\n",
+                   cpu->name(), gdbl->getPort());
 
+            gdbl->accept();
+        }
         if (remoteGDB.size() <= id) {
             remoteGDB.resize(id + 1);
         }
 
         remoteGDB[id] = rgdb;
     }
+#endif
 
     activeCpus.push_back(false);
 
@@ -254,9 +287,8 @@ System::numRunningContexts()
 void
 System::initState()
 {
-    int i;
     if (FullSystem) {
-        for (i = 0; i < threadContexts.size(); i++)
+        for (int i = 0; i < threadContexts.size(); i++)
             TheISA::startupCPU(threadContexts[i], i);
         // Moved from the constructor to here since it relies on the
         // address map being resolved in the interconnect
@@ -264,8 +296,22 @@ System::initState()
          * Load the kernel code into memory
          */
         if (params()->kernel != "")  {
+            if (params()->kernel_addr_check) {
+                // Validate kernel mapping before loading binary
+                if (!(isMemAddr((kernelStart & loadAddrMask) +
+                                loadAddrOffset) &&
+                      isMemAddr((kernelEnd & loadAddrMask) +
+                                loadAddrOffset))) {
+                    fatal("Kernel is mapped to invalid location (not memory). "
+                          "kernelStart 0x(%x) - kernelEnd 0x(%x) %#x:%#x\n",
+                          kernelStart,
+                          kernelEnd, (kernelStart & loadAddrMask) +
+                          loadAddrOffset,
+                          (kernelEnd & loadAddrMask) + loadAddrOffset);
+                }
+            }
             // Load program sections into memory
-            kernel->loadSections(physProxy, loadAddrMask);
+            kernel->loadSections(physProxy, loadAddrMask, loadAddrOffset);
 
             DPRINTF(Loader, "Kernel start = %#x\n", kernelStart);
             DPRINTF(Loader, "Kernel end   = %#x\n", kernelEnd);
@@ -273,18 +319,10 @@ System::initState()
             DPRINTF(Loader, "Kernel loaded...\n");
         }
     }
-
-    activeCpus.clear();
-
-    if (!FullSystem)
-        return;
-
-    for (i = 0; i < threadContexts.size(); i++)
-        TheISA::startupCPU(threadContexts[i], i);
 }
 
 void
-System::replaceThreadContext(ThreadContext *tc, int context_id)
+System::replaceThreadContext(ThreadContext *tc, ContextID context_id)
 {
     if (context_id >= threadContexts.size()) {
         panic("replaceThreadContext: bad id, %d >= %d\n",
@@ -296,12 +334,40 @@ System::replaceThreadContext(ThreadContext *tc, int context_id)
         remoteGDB[context_id]->replaceThreadContext(tc);
 }
 
+bool
+System::validKvmEnvironment() const
+{
+#if USE_KVM
+    if (threadContexts.empty())
+        return false;
+
+    for (auto tc : threadContexts) {
+        if (dynamic_cast<BaseKvmCPU*>(tc->getCpuPtr()) == nullptr) {
+            return false;
+        }
+    }
+    return true;
+#else
+    return false;
+#endif
+}
+
 Addr
 System::allocPhysPages(int npages)
 {
-    Addr return_addr = pagePtr << LogVMPageSize;
+    Addr return_addr = pagePtr << PageShift;
     pagePtr += npages;
-    if ((pagePtr << LogVMPageSize) > physmem.totalSize())
+
+    Addr next_return_addr = pagePtr << PageShift;
+
+    AddrRange m5opRange(0xffff0000, 0xffffffff);
+    if (m5opRange.contains(next_return_addr)) {
+        warn("Reached m5ops MMIO region\n");
+        return_addr = 0xffffffff;
+        pagePtr = 0xffffffff >> PageShift;
+    }
+
+    if ((pagePtr << PageShift) > physmem.totalSize())
         fatal("Out of memory, please increase size of physical memory.");
     return return_addr;
 }
@@ -315,7 +381,7 @@ System::memSize() const
 Addr
 System::freeMemSize() const
 {
-   return physmem.totalSize() - (pagePtr << LogVMPageSize);
+   return physmem.totalSize() - (pagePtr << PageShift);
 }
 
 bool
@@ -325,34 +391,41 @@ System::isMemAddr(Addr addr) const
 }
 
 void
-System::resume()
+System::drainResume()
 {
-    SimObject::resume();
     totalNumInsts = 0;
 }
 
 void
-System::serialize(ostream &os)
+System::serialize(CheckpointOut &cp) const
 {
     if (FullSystem)
-        kernelSymtab->serialize("kernel_symtab", os);
+        kernelSymtab->serialize("kernel_symtab", cp);
     SERIALIZE_SCALAR(pagePtr);
-    SERIALIZE_SCALAR(nextPID);
+    serializeSymtab(cp);
+
+    // also serialize the memories in the system
+    physmem.serializeSection(cp, "physmem");
 }
 
 
 void
-System::unserialize(Checkpoint *cp, const string &section)
+System::unserialize(CheckpointIn &cp)
 {
     if (FullSystem)
-        kernelSymtab->unserialize("kernel_symtab", cp, section);
+        kernelSymtab->unserialize("kernel_symtab", cp);
     UNSERIALIZE_SCALAR(pagePtr);
-    UNSERIALIZE_SCALAR(nextPID);
+    unserializeSymtab(cp);
+
+    // also unserialize the memories in the system
+    physmem.unserializeSection(cp, "physmem");
 }
 
 void
 System::regStats()
 {
+    MemObject::regStats();
+
     for (uint32_t j = 0; j < numWorkIds ; j++) {
         workItemStats[j] = new Stats::Histogram();
         stringstream namestr;
@@ -384,12 +457,16 @@ System::workItemEnd(uint32_t tid, uint32_t workid)
 void
 System::printSystems()
 {
+    ios::fmtflags flags(cerr.flags());
+
     vector<System *>::iterator i = systemList.begin();
     vector<System *>::iterator end = systemList.end();
     for (; i != end; ++i) {
         System *sys = *i;
         cerr << "System " << sys->name() << ": " << hex << sys << endl;
     }
+
+    cerr.flags(flags);
 }
 
 void
@@ -402,8 +479,7 @@ MasterID
 System::getMasterId(std::string master_name)
 {
     // strip off system name if the string starts with it
-    if (master_name.size() > name().size() &&
-                          master_name.compare(0, name().size(), name()) == 0)
+    if (startswith(master_name, name()))
         master_name = master_name.erase(0, name().size() + 1);
 
     // CPUs in switch_cpus ask for ids again after switching
@@ -417,9 +493,10 @@ System::getMasterId(std::string master_name)
     // Otherwise objects will have sized their stat buckets and
     // they will be too small
 
-    if (Stats::enabled())
-        fatal("Can't request a masterId after regStats(). \
-                You must do so in init().\n");
+    if (Stats::enabled()) {
+        fatal("Can't request a masterId after regStats(). "
+                "You must do so in init().\n");
+    }
 
     masterIds.push_back(master_name);
 
@@ -435,9 +512,6 @@ System::getMasterName(MasterID master_id)
     return masterIds[master_id];
 }
 
-const char *System::MemoryModeStrings[3] = {"invalid", "atomic",
-    "timing"};
-
 System *
 SystemParams::create()
 {