Merge zizzer.eecs.umich.edu:/z/m5/Bitkeeper/m5
[gem5.git] / sim / process.cc
index ee5d347a189b5ed48b7aaf51c44433c25f986b75..0a7e4608209cc0e3bcde0e554253929471cc196b 100644 (file)
@@ -46,6 +46,7 @@
 #include "sim/fake_syscall.hh"
 #include "sim/process.hh"
 #include "sim/stats.hh"
+#include "sim/syscall_emul.hh"
 
 #ifdef TARGET_ALPHA
 #include "arch/alpha/alpha_tru64_process.hh"
@@ -53,6 +54,7 @@
 #endif
 
 using namespace std;
+using namespace TheISA;
 
 //
 // The purpose of this code is to fake the loader & syscall mechanism
@@ -191,23 +193,32 @@ Process::dup_fd(int sim_fd, int tgt_fd)
 
 // generate new target fd for sim_fd
 int
-Process::open_fd(int sim_fd)
+Process::alloc_fd(int sim_fd)
 {
-    int free_fd;
-
     // in case open() returns an error, don't allocate a new fd
     if (sim_fd == -1)
         return -1;
 
     // find first free target fd
-    for (free_fd = 0; fd_map[free_fd] >= 0; ++free_fd) {
-        if (free_fd == MAX_FD)
-            panic("Process::open_fd: out of file descriptors!");
+    for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) {
+        if (fd_map[free_fd] == -1) {
+            fd_map[free_fd] = sim_fd;
+            return free_fd;
+        }
     }
 
-    fd_map[free_fd] = sim_fd;
+    panic("Process::alloc_fd: out of file descriptors!");
+}
+
+
+// free target fd (e.g., after close)
+void
+Process::free_fd(int tgt_fd)
+{
+    if (fd_map[tgt_fd] == -1)
+        warn("Process::free_fd: request to free unused fd %d", tgt_fd);
 
-    return free_fd;
+    fd_map[tgt_fd] = -1;
 }
 
 
@@ -242,8 +253,10 @@ static void
 copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr,
                 FunctionalMemory *memory)
 {
+    Addr data_ptr_swap;
     for (int i = 0; i < strings.size(); ++i) {
-        memory->access(Write, array_ptr, &data_ptr, sizeof(Addr));
+        data_ptr_swap = htog(data_ptr);
+        memory->access(Write, array_ptr, &data_ptr_swap, sizeof(Addr));
         memory->writeString(data_ptr, strings[i].c_str());
         array_ptr += sizeof(Addr);
         data_ptr += strings[i].size() + 1;
@@ -265,7 +278,7 @@ LiveProcess::LiveProcess(const string &nm, ObjectFile *objFile,
     text_size = objFile->textSize();
     data_base = objFile->dataBase();
     data_size = objFile->dataSize() + objFile->bssSize();
-    brk_point = RoundUp<uint64_t>(data_base + data_size, VMPageSize);
+    brk_point = roundUp(data_base + data_size, VMPageSize);
 
     // load object file into target memory
     objFile->loadSections(memory);
@@ -325,6 +338,7 @@ LiveProcess::LiveProcess(const string &nm, ObjectFile *objFile,
 
     // write contents to stack
     uint64_t argc = argv.size();
+    argc = htog(argc);
     memory->access(Write, stack_min, &argc, sizeof(uint64_t));
 
     copyStringArray(argv, argv_array_base, arg_data_base, memory);
@@ -338,16 +352,30 @@ LiveProcess::LiveProcess(const string &nm, ObjectFile *objFile,
     init_regs->npc = prog_entry + sizeof(MachInst);
 }
 
+void
+LiveProcess::syscall(ExecContext *xc)
+{
+    num_syscalls++;
+
+    int64_t callnum = xc->regs.intRegFile[ReturnValueReg];
+
+    SyscallDesc *desc = getDesc(callnum);
+    if (desc == NULL)
+        fatal("Syscall %d out of range", callnum);
+
+    desc->doSyscall(callnum, this, xc);
+}
 
 LiveProcess *
 LiveProcess::create(const string &nm,
                     int stdin_fd, int stdout_fd, int stderr_fd,
+                    string executable,
                     vector<string> &argv, vector<string> &envp)
 {
     LiveProcess *process = NULL;
-    ObjectFile *objFile = createObjectFile(argv[0]);
+    ObjectFile *objFile = createObjectFile(executable);
     if (objFile == NULL) {
-        fatal("Can't load object file %s", argv[0]);
+        fatal("Can't load object file %s", executable);
     }
 
     // check object type & set up syscall emulation pointer
@@ -381,9 +409,11 @@ LiveProcess::create(const string &nm,
 }
 
 
+
 BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
 
     VectorParam<string> cmd;
+    Param<string> executable;
     Param<string> input;
     Param<string> output;
     VectorParam<string> env;
@@ -394,6 +424,7 @@ END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess)
 BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess)
 
     INIT_PARAM(cmd, "command line (executable plus arguments)"),
+    INIT_PARAM(executable, "executable (overrides cmd[0] if set)"),
     INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"),
     INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"),
     INIT_PARAM(env, "environment settings")
@@ -425,6 +456,7 @@ CREATE_SIM_OBJECT(LiveProcess)
 
     return LiveProcess::create(getInstanceName(),
                                stdin_fd, stdout_fd, stderr_fd,
+                               (string)executable == "" ? cmd[0] : executable,
                                cmd, env);
 }