interpreter fixes
[nestedvm.git] / src / org / ibex / nestedvm / Runtime.java
index b790374..63e8835 100644 (file)
@@ -135,7 +135,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
             stackSize = max(stackSize,pageSize);
             stackSize = (stackSize + pageSize - 1) & ~(pageSize-1);
             stackPages = stackSize >>> pageShift;
-            heapStart = (heapStart + pageSize) & ~(pageSize-1);
+            heapStart = (heapStart + pageSize - 1) & ~(pageSize-1);
             if(stackPages + STACK_GUARD_PAGES + (heapStart >>> pageShift) >= totalPages)
                 throw new IllegalArgumentException("total pages too small");
         } else {
@@ -547,6 +547,15 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
         sp &= ~15;
         if(top - sp > ARG_MAX) throw new IllegalArgumentException("args/environ too big");
 
+        // HACK: heapStart() isn't always available when the constructor
+        // is run and this sometimes doesn't get initialized
+        if(heapEnd == 0) {
+            heapEnd = heapStart();
+            if(heapEnd == 0) throw new Error("heapEnd == 0");
+            int pageSize = writePages.length == 1 ? 4096 : (1<<pageShift);
+            heapEnd = (heapEnd + pageSize - 1) & ~(pageSize-1);
+        }
+
         CPUState cpuState = new CPUState();
         cpuState.r[A0] = argsAddr;
         cpuState.r[A1] = envAddr;
@@ -693,10 +702,14 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
     public static final int O_APPEND = 0x0008;
     public static final int O_TRUNC = 0x0400;
     public static final int O_NONBLOCK = 0x4000;
+    public static final int O_NOCTTY = 0x8000;
+    
     
     FD hostFSOpen(final File f, int flags, int mode, final Object data) throws ErrnoException {
         if((flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)) != 0) {
-            if(STDERR_DIAG) System.err.println("WARNING: Unsupported flags passed to open(): " + toHex(flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)));
+            if(STDERR_DIAG)
+                System.err.println("WARNING: Unsupported flags passed to open(\"" + f + "\"): " + toHex(flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)));
+           
             throw new ErrnoException(ENOTSUP);
         }
         boolean write = (flags&3) != RD_ONLY;
@@ -737,6 +750,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
     
     /** The open syscall */
     private int sys_open(int addr, int flags, int mode) throws ErrnoException, FaultException {
+        flags &= ~O_NOCTTY; // this is meaningless under nestedvm
         FD fd = _open(cstring(addr),flags,mode);
         if(fd == null) return -ENOENT;
         int fdn = addFD(fd);
@@ -869,6 +883,8 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable {
     private int sys_sysconf(int n) {
         switch(n) {
             case _SC_CLK_TCK: return 1000;
+            case _SC_PAGESIZE: return  writePages.length == 1 ? 4096 : (1<<pageShift);
+            case _SC_PHYS_PAGES: return writePages.length == 1 ? (1<<pageShift)/4096 : writePages.length;
             default:
                 if(STDERR_DIAG) System.err.println("WARNING: Attempted to use unknown sysconf key: " + n);
                 return -EINVAL;