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 == pageSize) */
protected final int pageShift;
/** Bottom of region of memory allocated to the stack */
addr += 4;
}
} catch(FaultException e) {
- // should never happen
- throw new Error(e.toString());
+ throw new RuntimeException(e.toString());
}
return start;
}
}
/** 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;
}
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;
}
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) {
- 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(): " + toHex(flags & ~(3|O_CREAT|O_EXCL|O_APPEND|O_TRUNC)));
throw new ErrnoException(ENOTSUP);
}
boolean write = (flags&3) != RD_ONLY;
/** The write syscall */
- // FIXME: Handle pipe closed exception
private int sys_write(int fdn, int addr, int count) throws FaultException, ErrnoException {
count = Math.min(count,MAX_CHUNK);
if(fdn < 0 || fdn >= 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 */
switch(n) {
case _SC_CLK_TCK: return 1000;
default:
- System.err.println("WARNING: Attempted to use unknown sysconf key: " + n);
+ if(STDERR_DIAG) System.err.println("WARNING: Attempted to use unknown sysconf key: " + n);
return -EINVAL;
}
}
try {
for(int i=start;i<end;i++) readPages[i] = writePages[i] = new int[pageWords];
} catch(OutOfMemoryError e) {
- System.err.println("WARNING: Caught OOM Exception in sbrk: " + e);
+ if(STDERR_DIAG) System.err.println("WARNING: Caught OOM Exception in sbrk: " + e);
return -ENOMEM;
}
}
state = RUNNING;
return ret;
} else {
- System.err.println("WARNING: calljava syscall invoked without a calljava callback set");
+ if(STDERR_DIAG) System.err.println("WARNING: calljava syscall invoked without a calljava callback set");
return 0;
}
}
case F_GETFD:
return closeOnExec[fdn] ? 1 : 0;
default:
- System.err.println("WARNING: Unknown fcntl command: " + cmd);
+ if(STDERR_DIAG) System.err.println("WARNING: Unknown fcntl command: " + cmd);
return -ENOSYS;
}
}
try {
return _syscall(syscall,a,b,c,d);
} catch(ErrnoException e) {
- e.printStackTrace();
return -e.errno;
} catch(FaultException e) {
return -EFAULT;
case SYS_mkdir:
case SYS_getcwd:
case SYS_chdir:
- System.err.println("Attempted to use a UnixRuntime syscall in Runtime (" + syscall + ")");
+ if(STDERR_DIAG) System.err.println("Attempted to use a UnixRuntime syscall in Runtime (" + syscall + ")");
return -ENOSYS;
default:
- System.err.println("Attempted to use unknown syscall: " + syscall);
+ if(STDERR_DIAG) System.err.println("Attempted to use unknown syscall: " + syscall);
return -ENOSYS;
}
}