X-Git-Url: http://git.megacz.com/?p=nestedvm.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fnestedvm%2FRuntime.java;h=50b5162a03c3acf679d6a0667c1b97e77e7d2189;hp=fc97e71ecf3984b5e631ceeab207e3fcde3aa400;hb=b6be9bcc91cf8e995a0e616a480813cdbef09dc2;hpb=98f786ce8ee1fcd9568d1c367160851d32e1c786 diff --git a/src/org/ibex/nestedvm/Runtime.java b/src/org/ibex/nestedvm/Runtime.java index fc97e71..50b5162 100644 --- a/src/org/ibex/nestedvm/Runtime.java +++ b/src/org/ibex/nestedvm/Runtime.java @@ -9,6 +9,9 @@ import java.io.*; import java.util.Arrays; public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { + /** True to write useful diagnostic information to stderr when things go wrong */ + final static boolean STDERR_DIAG = true; + /** Number of bits to shift to get the page number (1<<>> 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 { @@ -149,14 +152,15 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { if(totalPages == 1) { readPages[0] = writePages[0] = new int[pageSize>>2]; } else { - for(int i=(stackBottom >>> pageShift);i>2]; + for(int i=(stackBottom >>> pageShift);i>2]; } } - addFD(new StdinFD(System.in)); - addFD(new StdoutFD(System.out)); - addFD(new StdoutFD(System.err)); + InputStream stdin = Boolean.valueOf(getSystemProperty("nestedvm.textstdin")).booleanValue() ? new TextInputStream(System.in) : System.in; + addFD(new TerminalFD(stdin)); + addFD(new TerminalFD(System.out)); + addFD(new TerminalFD(System.err)); } /** Copy everything from src to addr initializing uninitialized pages if required. @@ -446,8 +450,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { addr += 4; } } catch(FaultException e) { - // should never happen - throw new Error(e.toString()); + throw new RuntimeException(e.toString()); } return start; } @@ -476,16 +479,16 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { } /** Calls _execute() (subclass's execute()) and catches exceptions */ + // FEATURE: Have these call kill() so we get a pretty message to stdout private void __execute() { try { _execute(); } catch(FaultException e) { - e.printStackTrace(); + if(STDERR_DIAG) e.printStackTrace(); sys_exit(128+11); // SIGSEGV exitException = e; } catch(ExecutionException e) { - e.printStackTrace(); - System.err.println(e); + if(STDERR_DIAG) e.printStackTrace(); sys_exit(128+4); // SIGILL exitException = e; } @@ -503,8 +506,8 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { } static String[] concatArgv(String argv0, String[] rest) { - String[] argv = new String[rest.length+1]; - System.arraycopy(rest,0,argv,1,rest.length); + String[] argv = new String[rest.length+1]; + System.arraycopy(rest,0,argv,1,rest.length); argv[0] = argv0; return argv; } @@ -519,9 +522,9 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { start(args,env); for(;;) { if(execute()) break; - System.err.println("WARNING: Pause requested while executing run()"); + if(STDERR_DIAG) System.err.println("WARNING: Pause requested while executing run()"); } - if(state == EXECED) System.err.println("WARNING: Process exec()ed while being run under run()"); + if(state == EXECED && STDERR_DIAG) System.err.println("WARNING: Process exec()ed while being run under run()"); return state == EXITED ? exitStatus() : 0; } @@ -537,14 +540,23 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { sp = top = writePages.length*(1< 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<fdn and returns the new fs */ public final int dupFD(int fdn) { - int i; - if(fdn < 0 || fdn >= OPEN_MAX) return -1; - if(fds[fdn] == null) return -1; - for(i=0;i= OPEN_MAX) return -1; + if(fds[fdn] == null) return -1; + for(i=0;i= OPEN_MAX) return -EBADFD; if(fds[fdn] == null) return -EBADFD; byte[] buf = byteBuf(count); copyin(addr,buf,count); - return fds[fdn].write(buf,0,count); + try { + return fds[fdn].write(buf,0,count); + } catch(ErrnoException e) { + if(e.errno == EPIPE) sys_exit(128+13); + throw e; + } } /** The read syscall */ @@ -781,6 +803,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { } /** The stat/fstat syscall helper */ + // FIXME: Populate uid/gid/nlink int stat(FStat fs, int addr) throws FaultException { memWrite(addr+0,(fs.dev()<<16)|(fs.inode()&0xffff)); // st_dev (top 16), // st_ino (bottom 16) memWrite(addr+4,((fs.type()&0xf000))|(fs.mode()&0xfff)); // st_mode @@ -863,8 +886,10 @@ 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< 0) { + buf[0] = (byte) pushedBack; + pushedBack = -1; + pos++; len--; pb = true; + } + int n = parent.read(buf,pos,len); + if(n == -1) return -1; + for(int i=0;i