/** 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 */
/** 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 */
protected abstract void setCPUState(CPUState state);
protected Object clone() throws CloneNotSupportedException {
protected abstract void setCPUState(CPUState state);
protected Object clone() throws CloneNotSupportedException {
r.readPages = new int[totalPages][];
r.writePages = new int[totalPages][];
for(int i=0;i<totalPages;i++) {
r.readPages = new int[totalPages][];
r.writePages = new int[totalPages][];
for(int i=0;i<totalPages;i++) {
if(writePages[i] == null) r.readPages[i] = readPages[i];
else r.readPages[i] = r.writePages[i] = (int[])writePages[i].clone();
}
if(writePages[i] == null) r.readPages[i] = readPages[i];
else r.readPages[i] = r.writePages[i] = (int[])writePages[i].clone();
}
stackSize = max(stackSize,pageSize);
stackSize = (stackSize + pageSize - 1) & ~(pageSize-1);
stackPages = stackSize >>> pageShift;
stackSize = max(stackSize,pageSize);
stackSize = (stackSize + pageSize - 1) & ~(pageSize-1);
stackPages = stackSize >>> pageShift;
if(stackPages + STACK_GUARD_PAGES + (heapStart >>> pageShift) >= totalPages)
throw new IllegalArgumentException("total pages too small");
} else {
if(stackPages + STACK_GUARD_PAGES + (heapStart >>> pageShift) >= totalPages)
throw new IllegalArgumentException("total pages too small");
} else {
- for(int i=(stackBottom >>> pageShift);i<writePages.length;i++) {
- readPages[i] = writePages[i] = new int[pageSize>>2];
+ for(int i=(stackBottom >>> pageShift);i<writePages.length;i++) {
+ readPages[i] = writePages[i] = new int[pageSize>>2];
- 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);
- sp = argsAddr = addStringArray(args,sp);
- sp = envAddr = addStringArray(createEnv(environ),sp);
+ sp = argsAddr = addStringArray(args,sp);
+ sp = envAddr = addStringArray(createEnv(environ),sp);
} catch(FaultException e) {
throw new IllegalArgumentException("args/environ too big");
}
sp &= ~15;
if(top - sp > ARG_MAX) throw new IllegalArgumentException("args/environ too big");
} catch(FaultException e) {
throw new IllegalArgumentException("args/environ too big");
}
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;
CPUState cpuState = new CPUState();
cpuState.r[A0] = argsAddr;
cpuState.r[A1] = envAddr;
/** Duplicates the file descriptor <i>fdn</i> and returns the new fs */
public final int dupFD(int fdn) {
/** Duplicates the file descriptor <i>fdn</i> 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;i++) if(fds[i] == null) break;
+ int i;
+ if(fdn < 0 || fdn >= OPEN_MAX) return -1;
+ if(fds[fdn] == null) return -1;
+ for(i=0;i<OPEN_MAX;i++) if(fds[i] == null) break;
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_APPEND = 0x0008;
public static final int O_TRUNC = 0x0400;
public static final int O_NONBLOCK = 0x4000;
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) {
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) {
/** The open syscall */
private int sys_open(int addr, int flags, int mode) throws ErrnoException, FaultException {
/** The open syscall */
private int sys_open(int addr, int flags, int mode) throws ErrnoException, FaultException {
FD fd = _open(cstring(addr),flags,mode);
if(fd == null) return -ENOENT;
int fdn = addFD(fd);
FD fd = _open(cstring(addr),flags,mode);
if(fd == null) return -ENOENT;
int fdn = addFD(fd);
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);
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;
+ }
try {
for(int i=start;i<end;i++) readPages[i] = writePages[i] = new int[pageWords];
} catch(OutOfMemoryError e) {
try {
for(int i=start;i<end;i++) readPages[i] = writePages[i] = new int[pageWords];
} catch(OutOfMemoryError e) {
ret = callJavaCB.call(a,b,c,d);
} catch(RuntimeException e) {
System.err.println("Error while executing callJavaCB");
ret = callJavaCB.call(a,b,c,d);
} catch(RuntimeException e) {
System.err.println("Error while executing callJavaCB");
@see Runtime#state state */
protected final int syscall(int syscall, int a, int b, int c, int d) {
try {
@see Runtime#state state */
protected final int syscall(int syscall, int a, int b, int c, int d) {
try {
- return _syscall(syscall,a,b,c,d);
+ return _syscall(syscall,a,b,c,d);
// NOTE: There is race condition here but we can't fix it in pure java
if((flags&O_APPEND) != 0) seek(0,SEEK_END);
try {
// NOTE: There is race condition here but we can't fix it in pure java
if((flags&O_APPEND) != 0) seek(0,SEEK_END);
try {
}
}
public int read(byte[] a, int off, int length) throws ErrnoException {
if((flags&3) == WR_ONLY) throw new ErrnoException(EBADFD);
try {
}
}
public int read(byte[] a, int off, int length) throws ErrnoException {
if((flags&3) == WR_ONLY) throw new ErrnoException(EBADFD);
try {
public OutputStreamFD(OutputStream os) { this.os = os; }
public int write(byte[] a, int off, int length) throws ErrnoException {
try {
public OutputStreamFD(OutputStream os) { this.os = os; }
public int write(byte[] a, int off, int length) throws ErrnoException {
try {