sycalls: implement mremap() and add DATA flag for getrlimit(). mremap has been tested...
authorLisa Hsu <hsul@eecs.umich.edu>
Mon, 16 Feb 2009 22:47:39 +0000 (17:47 -0500)
committerLisa Hsu <hsul@eecs.umich.edu>
Mon, 16 Feb 2009 22:47:39 +0000 (17:47 -0500)
src/arch/alpha/linux/process.cc
src/arch/alpha/pagetable.hh
src/arch/mips/linux/process.cc
src/arch/mips/tlb.hh
src/arch/sparc/linux/syscalls.cc
src/arch/sparc/pagetable.hh
src/arch/x86/linux/syscalls.cc
src/arch/x86/pagetable.hh
src/mem/page_table.cc
src/mem/page_table.hh
src/sim/syscall_emul.hh

index 6684051afa35dbdaf42cb104241fa8abb4fa5a23..605e40627d2fa2d0304a131153aa55fcaf520a1f 100644 (file)
@@ -463,7 +463,7 @@ SyscallDesc AlphaLinuxProcess::syscallDescs[] = {
     /* 338 */ SyscallDesc("afs_syscall", unimplementedFunc),
     /* 339 */ SyscallDesc("uname", unameFunc),
     /* 340 */ SyscallDesc("nanosleep", unimplementedFunc),
-    /* 341 */ SyscallDesc("mremap", unimplementedFunc),
+    /* 341 */ SyscallDesc("mremap", mremapFunc<AlphaLinux>),
     /* 342 */ SyscallDesc("nfsservctl", unimplementedFunc),
     /* 343 */ SyscallDesc("setresuid", unimplementedFunc),
     /* 344 */ SyscallDesc("getresuid", unimplementedFunc),
index 6cf11be56143b1b0f7c814c5a79b07ec7293dd0b..59df93befe4c611bad3f430d926348805093c309 100644 (file)
@@ -123,6 +123,13 @@ struct TlbEntry
     TlbEntry()
     {}
 
+    void
+    updateVaddr(Addr new_vaddr)
+    {
+        VAddr vaddr(new_vaddr);
+        tag = vaddr.vpn();
+    }
+
     Addr
     pageStart()
     {
index ce09e162871be4f03cff191930e98c16a2357828..9d9d333253587f676c1dc14d7f3637a7a547fb57 100644 (file)
@@ -288,7 +288,7 @@ SyscallDesc MipsLinuxProcess::syscallDescs[] = {
     /* 164 */ SyscallDesc("sched_get_priority_min", unimplementedFunc),
     /* 165 */ SyscallDesc("sched_rr_get_interval", unimplementedFunc),
     /* 166 */ SyscallDesc("nanosleep", unimplementedFunc),
-    /* 167 */ SyscallDesc("mremap", unimplementedFunc),
+    /* 167 */ SyscallDesc("mremap", mremapFunc<MipsLinux>),
     /* 168 */ SyscallDesc("accept", unimplementedFunc),
     /* 169 */ SyscallDesc("bind", unimplementedFunc),
     /* 170 */ SyscallDesc("connect", unimplementedFunc),
index feb2509c561c9da47694d62f6a7514a418c1b6e9..4a8fc32acaad156574db7e6584cec30e97cb9237 100644 (file)
@@ -68,6 +68,9 @@ struct TlbEntry
         return _pageStart;
     }
 
+    void
+    updateVaddr(Addr new_vaddr) {}
+    
     void serialize(std::ostream &os)
     {
         SERIALIZE_SCALAR(_pageStart);
index 2845f7bec653a122f97901c2b314e444b8af942c..3e8c603cdceae4db9cc0e480f1675f1df3156c51 100644 (file)
@@ -339,7 +339,7 @@ SyscallDesc SparcLinuxProcess::syscall32Descs[] = {
     /* 247 */ SyscallDesc("sched_get_priority_min", unimplementedFunc), //32 bit
     /* 248 */ SyscallDesc("sched_rr_get_interval", unimplementedFunc), //32 bit
     /* 249 */ SyscallDesc("nanosleep", unimplementedFunc),
-    /* 250 */ SyscallDesc("mremap", unimplementedFunc), //32 bit
+    /* 250 */ SyscallDesc("mremap", mremapFunc<Sparc32Linux>), //32 bit
     /* 251 */ SyscallDesc("_sysctl", unimplementedFunc), //32 bit
     /* 252 */ SyscallDesc("getsid", unimplementedFunc), //32 bit
     /* 253 */ SyscallDesc("fdatasync", unimplementedFunc),
@@ -642,7 +642,7 @@ SyscallDesc SparcLinuxProcess::syscallDescs[] = {
     /* 247 */ SyscallDesc("sched_get_priority_min", unimplementedFunc),
     /* 248 */ SyscallDesc("sched_rr_get_interval", unimplementedFunc),
     /* 249 */ SyscallDesc("nanosleep", unimplementedFunc),
-    /* 250 */ SyscallDesc("mremap", unimplementedFunc),
+    /* 250 */ SyscallDesc("mremap", mremapFunc<SparcLinux>),
     /* 251 */ SyscallDesc("_sysctl", unimplementedFunc),
     /* 252 */ SyscallDesc("getsid", unimplementedFunc),
     /* 253 */ SyscallDesc("fdatasync", unimplementedFunc),
index ee4ca0c2c9d9d681a4697a0df4b7744b7be3bd2c..cbdabe4c3ccd2fce66cef8ee8340745f44425014 100644 (file)
@@ -266,6 +266,12 @@ struct TlbEntry
         return pte.paddr();
     }
 
+    void
+    updateVaddr(Addr new_vaddr)
+    {
+        range.va = new_vaddr;
+    }
+
     void serialize(std::ostream &os);
     void unserialize(Checkpoint *cp, const std::string &section);
 };
index 754fb2eaf6c01798d630860d698761653e9e4fab..ca35a5dd635152f74138c1831306fc383f36e785 100644 (file)
@@ -148,7 +148,7 @@ SyscallDesc X86LinuxProcess::syscallDescs[] = {
     /*  22 */ SyscallDesc("pipe", unimplementedFunc),
     /*  23 */ SyscallDesc("select", unimplementedFunc),
     /*  24 */ SyscallDesc("sched_yield", unimplementedFunc),
-    /*  25 */ SyscallDesc("mremap", unimplementedFunc),
+    /*  25 */ SyscallDesc("mremap", mremapFunc<X86Linux64>),
     /*  26 */ SyscallDesc("msync", unimplementedFunc),
     /*  27 */ SyscallDesc("mincore", unimplementedFunc),
     /*  28 */ SyscallDesc("madvise", unimplementedFunc),
index e42693c03d9d6a3c137b267d1bcf7be19786e0da..1a7a945e4fc61b027e4069069a3c9ec60a55782c 100644 (file)
@@ -113,6 +113,12 @@ namespace X86ISA
         TlbEntry(Addr asn, Addr _vaddr, Addr _paddr);
         TlbEntry() {}
 
+        void
+        updateVaddr(Addr new_vaddr)
+        {
+            vaddr = new_vaddr;
+        }
+
         Addr pageStart()
         {
             return paddr;
index 54165f293c882b6ee1be03991779dab2846daed7..bdcbbfec38c366ad1a4394c135a1e56410a59751 100644 (file)
@@ -87,6 +87,44 @@ PageTable::allocate(Addr vaddr, int64_t size)
     }
 }
 
+void
+PageTable::remap(Addr vaddr, int64_t size, Addr new_vaddr)
+{
+    assert(pageOffset(vaddr) == 0);
+    assert(pageOffset(new_vaddr) == 0);
+
+    DPRINTF(MMU, "moving pages from vaddr %08p to %08p, size = %d\n", vaddr,
+            new_vaddr, size);
+
+    for (; size > 0; size -= pageSize, vaddr += pageSize, new_vaddr += pageSize) {
+        PTableItr iter = pTable.find(vaddr);
+
+        assert(iter != pTable.end());
+
+        pTable[new_vaddr] = pTable[vaddr];
+        pTable.erase(vaddr);
+        pTable[new_vaddr].updateVaddr(new_vaddr);
+        updateCache(new_vaddr, pTable[new_vaddr]);
+    }
+}
+
+void
+PageTable::deallocate(Addr vaddr, int64_t size)
+{
+    assert(pageOffset(vaddr) == 0);
+
+    DPRINTF(MMU, "Deallocating page: %#x-%#x\n", vaddr, vaddr+ size);
+
+    for (; size > 0; size -= pageSize, vaddr += pageSize) {
+        PTableItr iter = pTable.find(vaddr);
+
+        assert(iter != pTable.end());
+
+        pTable.erase(vaddr);
+    }
+
+}
+
 bool
 PageTable::lookup(Addr vaddr, TheISA::TlbEntry &entry)
 {
index 6ff0be082060a713e1cd225df9801611bed70e5d..d4101c6bf4474467cde9f1581685a121330220a9 100644 (file)
@@ -80,6 +80,8 @@ class PageTable
     Addr pageOffset(Addr a) { return (a &  offsetMask); }
 
     void allocate(Addr vaddr, int64_t size);
+    void remap(Addr vaddr, int64_t size, Addr new_vaddr);
+    void deallocate(Addr vaddr, int64_t size);
 
     /**
      * Lookup function
index c334c1e260b13ee605efbca0b7187c332a856f97..0e34a835ec3cacd0bc971825c59cc5efdf2389ae 100644 (file)
@@ -607,6 +607,51 @@ fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
     return 0;
 }
 
+/// Target mremap() handler.
+template <class OS>
+SyscallReturn
+mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
+{
+    Addr start = tc->getSyscallArg(0);
+    uint64_t old_length = tc->getSyscallArg(1);
+    uint64_t new_length = tc->getSyscallArg(2);
+    uint64_t flags = tc->getSyscallArg(3);
+
+    if ((start % TheISA::VMPageSize != 0) ||
+            (new_length % TheISA::VMPageSize != 0)) {
+        warn("mremap failing: arguments not page aligned");
+        return -EINVAL;
+    }
+
+    if (new_length > old_length) {
+        if ((start + old_length) == process->mmap_end) {
+            uint64_t diff = new_length - old_length;
+            process->pTable->allocate(process->mmap_end, diff);
+            process->mmap_end += diff;
+            return start;
+        } else {
+            // sys/mman.h defined MREMAP_MAYMOVE
+            if (!(flags & 1)) {
+                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
+                return -ENOMEM;
+            } else {
+                process->pTable->remap(start, old_length, process->mmap_end);
+                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 
+                        process->mmap_end, process->mmap_end + new_length, new_length);
+                start = process->mmap_end;
+                // add on the remaining unallocated pages
+                process->pTable->allocate(start + old_length, new_length - old_length);
+                process->mmap_end += new_length;
+                warn("returning %08p as start\n", start);
+                return start;
+            }
+        }
+    } else {
+        process->pTable->deallocate(start + new_length, old_length -
+                new_length);
+        return start;
+    }
+}
 
 /// Target stat() handler.
 template <class OS>
@@ -892,6 +937,7 @@ mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
     // int fd = p->sim_fd(tc->getSyscallArg(4));
     // int offset = tc->getSyscallArg(5);
 
+
     if ((start  % TheISA::VMPageSize) != 0 ||
         (length % TheISA::VMPageSize) != 0) {
         warn("mmap failing: arguments not page-aligned: "
@@ -929,12 +975,19 @@ getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
 
     switch (resource) {
         case OS::TGT_RLIMIT_STACK:
-            // max stack size in bytes: make up a number (2MB for now)
+            // max stack size in bytes: make up a number (8MB for now)
             rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
             rlp->rlim_cur = htog(rlp->rlim_cur);
             rlp->rlim_max = htog(rlp->rlim_max);
             break;
 
+        case OS::TGT_RLIMIT_DATA:
+            // max data segment size in bytes: make up a number
+            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
+            rlp->rlim_cur = htog(rlp->rlim_cur);
+            rlp->rlim_max = htog(rlp->rlim_max);
+            break;
+
         default:
             std::cerr << "getrlimitFunc: unimplemented resource " << resource
                 << std::endl;