sim-se: Add prlimit system call
[gem5.git] / src / sim / process.cc
index 7cfaf6530656ba4c53a6310a9cd7f4573eca9d13..bfc52c36122b0cda4b4edf3d71a70cd5ad70a3ce 100644 (file)
@@ -50,6 +50,7 @@
 #include <unistd.h>
 
 #include <array>
+#include <csignal>
 #include <map>
 #include <string>
 #include <vector>
 
 #if THE_ISA == ALPHA_ISA
 #include "arch/alpha/linux/process.hh"
+
 #elif THE_ISA == SPARC_ISA
 #include "arch/sparc/linux/process.hh"
 #include "arch/sparc/solaris/process.hh"
+
 #elif THE_ISA == MIPS_ISA
 #include "arch/mips/linux/process.hh"
+
 #elif THE_ISA == ARM_ISA
-#include "arch/arm/linux/process.hh"
 #include "arch/arm/freebsd/process.hh"
+#include "arch/arm/linux/process.hh"
+
 #elif THE_ISA == X86_ISA
 #include "arch/x86/linux/process.hh"
+
 #elif THE_ISA == POWER_ISA
 #include "arch/power/linux/process.hh"
+
 #elif THE_ISA == RISCV_ISA
 #include "arch/riscv/linux/process.hh"
+
 #else
 #error "THE_ISA not set"
 #endif
@@ -111,7 +119,6 @@ Process::Process(ProcessParams * params, ObjectFile * obj_file)
       _pid(params->pid), _ppid(params->ppid),
       _pgid(params->pgid), drivers(params->drivers),
       fds(make_shared<FDArray>(params->input, params->output, params->errout)),
-      maxStackSize(params->maxStackSize),
       childClearTID(0)
 {
     if (_pid >= System::maxPID)
@@ -136,7 +143,6 @@ Process::Process(ProcessParams * params, ObjectFile * obj_file)
     _tgid = params->pid;
 
     exitGroup = new bool();
-    memState = new MemState();
     sigchld = new bool();
 
     if (!debugSymbolTable) {
@@ -145,7 +151,7 @@ Process::Process(ProcessParams * params, ObjectFile * obj_file)
             !objFile->loadLocalSymbols(debugSymbolTable) ||
             !objFile->loadWeakSymbols(debugSymbolTable)) {
             delete debugSymbolTable;
-            debugSymbolTable = NULL;
+            debugSymbolTable = nullptr;
         }
     }
 }
@@ -154,6 +160,15 @@ void
 Process::clone(ThreadContext *otc, ThreadContext *ntc,
                Process *np, TheISA::IntReg flags)
 {
+#ifndef CLONE_VM
+#define CLONE_VM 0
+#endif
+#ifndef CLONE_FILES
+#define CLONE_FILES 0
+#endif
+#ifndef CLONE_THREAD
+#define CLONE_THREAD 0
+#endif
     if (CLONE_VM & flags) {
         /**
          * Share the process memory address space between the new process
@@ -164,7 +179,6 @@ Process::clone(ThreadContext *otc, ThreadContext *ntc,
         np->pTable = pTable;
         ntc->getMemProxy().setPageTable(np->pTable);
 
-        delete np->memState;
         np->memState = memState;
     } else {
         /**
@@ -254,7 +268,7 @@ Process::findFreeContext()
         if (ThreadContext::Halted == it->status())
             return it;
     }
-    return NULL;
+    return nullptr;
 }
 
 void
@@ -323,24 +337,28 @@ Process::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
 bool
 Process::fixupStackFault(Addr vaddr)
 {
+    Addr stack_min = memState->getStackMin();
+    Addr stack_base = memState->getStackBase();
+    Addr max_stack_size = memState->getMaxStackSize();
+
     // Check if this is already on the stack and there's just no page there
     // yet.
-    if (vaddr >= memState->stackMin && vaddr < memState->stackBase) {
+    if (vaddr >= stack_min && vaddr < stack_base) {
         allocateMem(roundDown(vaddr, PageBytes), PageBytes);
         return true;
     }
 
     // We've accessed the next page of the stack, so extend it to include
     // this address.
-    if (vaddr < memState->stackMin
-        && vaddr >= memState->stackBase - maxStackSize) {
-        while (vaddr < memState->stackMin) {
-            memState->stackMin -= TheISA::PageBytes;
-            if (memState->stackBase - memState->stackMin > maxStackSize)
+    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
+        while (vaddr < stack_min) {
+            stack_min -= TheISA::PageBytes;
+            if (stack_base - stack_min > max_stack_size)
                 fatal("Maximum stack size exceeded\n");
-            allocateMem(memState->stackMin, TheISA::PageBytes);
+            allocateMem(stack_min, TheISA::PageBytes);
             inform("Increasing stack size by one page.");
-        };
+        }
+        memState->setStackMin(stack_min);
         return true;
     }
     return false;
@@ -349,12 +367,6 @@ Process::fixupStackFault(Addr vaddr)
 void
 Process::serialize(CheckpointOut &cp) const
 {
-    SERIALIZE_SCALAR(memState->brkPoint);
-    SERIALIZE_SCALAR(memState->stackBase);
-    SERIALIZE_SCALAR(memState->stackSize);
-    SERIALIZE_SCALAR(memState->stackMin);
-    SERIALIZE_SCALAR(memState->nextThreadStackBase);
-    SERIALIZE_SCALAR(memState->mmapEnd);
     pTable->serialize(cp);
     /**
      * Checkpoints for file descriptors currently do not work. Need to
@@ -372,12 +384,6 @@ Process::serialize(CheckpointOut &cp) const
 void
 Process::unserialize(CheckpointIn &cp)
 {
-    UNSERIALIZE_SCALAR(memState->brkPoint);
-    UNSERIALIZE_SCALAR(memState->stackBase);
-    UNSERIALIZE_SCALAR(memState->stackSize);
-    UNSERIALIZE_SCALAR(memState->stackMin);
-    UNSERIALIZE_SCALAR(memState->nextThreadStackBase);
-    UNSERIALIZE_SCALAR(memState->mmapEnd);
     pTable->unserialize(cp);
     /**
      * Checkpoints for file descriptors currently do not work. Need to
@@ -409,7 +415,7 @@ Process::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
     numSyscalls++;
 
     SyscallDesc *desc = getDesc(callnum);
-    if (desc == NULL)
+    if (desc == nullptr)
         fatal("Syscall %d out of range", callnum);
 
     desc->doSyscall(callnum, this, tc, fault);
@@ -429,7 +435,7 @@ Process::findDriver(std::string filename)
             return d;
     }
 
-    return NULL;
+    return nullptr;
 }
 
 void
@@ -446,13 +452,14 @@ Process::updateBias()
 
     // We are allocating the memory area; set the bias to the lowest address
     // in the allocated memory region.
-    Addr *end = &memState->mmapEnd;
-    Addr ld_bias = mmapGrowsDown() ? *end - interp_mapsize : *end;
+    Addr mmap_end = memState->getMmapEnd();
+    Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
 
     // Adjust the process mmap area to give the interpreter room; the real
     // execve system call would just invoke the kernel's internal mmap
     // functions to make these adjustments.
-    *end = mmapGrowsDown() ? ld_bias : *end + interp_mapsize;
+    mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
+    memState->setMmapEnd(mmap_end);
 
     interp->updateBias(ld_bias);
 }
@@ -482,7 +489,7 @@ Process::getStartPC()
 Process *
 ProcessParams::create()
 {
-    Process *process = NULL;
+    Process *process = nullptr;
 
     // If not specified, set the executable parameter equal to the
     // simulated system's zeroth command line parameter
@@ -491,7 +498,7 @@ ProcessParams::create()
     }
 
     ObjectFile *obj_file = createObjectFile(executable);
-    if (obj_file == NULL) {
+    if (obj_file == nullptr) {
         fatal("Can't load object file %s", executable);
     }
 
@@ -630,7 +637,7 @@ ProcessParams::create()
 #error "THE_ISA not set"
 #endif
 
-    if (process == NULL)
+    if (process == nullptr)
         fatal("Unknown error creating process object.");
     return process;
 }