X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fnestedvm%2FRuntime.java;h=8b577372a11aa9d2fc589d32d652ed451bab84ef;hb=b5282b0a1a8ca5212c75623610be6d0483fd35bd;hp=2a4567fb87552bfc01c0899648f9fe7a009e8007;hpb=896193619af1e0534356ac622494ae77c7e42594;p=nestedvm.git diff --git a/src/org/ibex/nestedvm/Runtime.java b/src/org/ibex/nestedvm/Runtime.java index 2a4567f..8b57737 100644 --- a/src/org/ibex/nestedvm/Runtime.java +++ b/src/org/ibex/nestedvm/Runtime.java @@ -1,3 +1,7 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + // Copyright 2003 Brian Alliet // Based on org.xwt.imp.MIPS by Adam Megacz // Portions Copyright 2003 Adam Megacz @@ -6,9 +10,10 @@ package org.ibex.nestedvm; import org.ibex.nestedvm.util.*; import java.io.*; -import java.util.Arrays; public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { + public static final String VERSION = "1.0"; + /** True to write useful diagnostic information to stderr when things go wrong */ final static boolean STDERR_DIAG = true; @@ -45,10 +50,10 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { /** When the process started */ private long startTime; - /** Text/Data loaded in memory */ - public final static int STOPPED = 0; /** Program is executing instructions */ - public final static int RUNNING = 1; + public final static int RUNNING = 0; // Horrible things will happen if this isn't 0 + /** Text/Data loaded in memory */ + public final static int STOPPED = 1; /** Prgram has been started but is paused */ public final static int PAUSED = 2; /** Program is executing a callJava() method */ @@ -69,8 +74,8 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { public ExecutionException exitException; /** Table containing all open file descriptors. (Entries are null if the fd is not in use */ - FD[] fds = new FD[OPEN_MAX]; // package-private for UnixRuntime - boolean closeOnExec[] = new boolean[OPEN_MAX]; + FD[] fds; // package-private for UnixRuntime + boolean closeOnExec[]; /** Pointer to a SecurityManager for this process */ SecurityManager sm; @@ -101,6 +106,16 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { /** Subclasses should set the CPUState to the state held in state */ protected abstract void setCPUState(CPUState state); + /** True to enabled a few hacks to better support the win32 console */ + final static boolean win32Hacks; + + static { + String os = Platform.getProperty("os.name"); + String prop = Platform.getProperty("nestedvm.win32hacks"); + if(prop != null) { win32Hacks = Boolean.valueOf(prop).booleanValue(); } + else { win32Hacks = os != null && os.toLowerCase().indexOf("windows") != -1; } + } + protected Object clone() throws CloneNotSupportedException { Runtime r = (Runtime) super.clone(); r._byteBuf = null; @@ -118,7 +133,8 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { return r; } - protected Runtime(int pageSize, int totalPages) { + protected Runtime(int pageSize, int totalPages) { this(pageSize, totalPages,false); } + protected Runtime(int pageSize, int totalPages, boolean exec) { if(pageSize <= 0) throw new IllegalArgumentException("pageSize <= 0"); if(totalPages <= 0) throw new IllegalArgumentException("totalPages <= 0"); if((pageSize&(pageSize-1)) != 0) throw new IllegalArgumentException("pageSize not a power of two"); @@ -156,11 +172,16 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { readPages[i] = writePages[i] = new int[pageSize>>2]; } } - - 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)); + + if(!exec) { + fds = new FD[OPEN_MAX]; + closeOnExec = new boolean[OPEN_MAX]; + + InputStream stdin = win32Hacks ? new Win32ConsoleIS(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. @@ -357,7 +378,8 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { if(page == null) throw new WriteFaultException(a<<2); int index = a&pageWordMask; int n = min(c,pageWords-index); - Arrays.fill(page,index,index+n,fourBytes); + /* Arrays.fill(page,index,index+n,fourBytes);*/ + for(int i=index;i= 0) throw new ErrnoException(EACCES); return null; @@ -752,8 +771,13 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { /** The open syscall */ private int sys_open(int addr, int flags, int mode) throws ErrnoException, FaultException { + String name = cstring(addr); + + // HACK: TeX, or GPC, or something really sucks + if(name.length() == 1024 && getClass().getName().equals("tests.TeX")) name = name.trim(); + flags &= ~O_NOCTTY; // this is meaningless under nestedvm - FD fd = _open(cstring(addr),flags,mode); + FD fd = _open(name,flags,mode); if(fd == null) return -ENOENT; int fdn = addFD(fd); if(fdn == -1) { fd.close(); return -ENFILE; } @@ -803,12 +827,11 @@ 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 - memWrite(addr+8,1<<16); // st_nlink (top 16) // st_uid (bottom 16) - memWrite(addr+12,0); // st_gid (top 16) // st_rdev (bottom 16) + memWrite(addr+8,fs.nlink()<<16|fs.uid()&0xffff); // st_nlink (top 16) // st_uid (bottom 16) + memWrite(addr+12,fs.gid()<<16|0); // st_gid (top 16) // st_rdev (bottom 16) memWrite(addr+16,fs.size()); // st_size memWrite(addr+20,fs.atime()); // st_atime // memWrite(addr+24,0) // st_spare1 @@ -935,7 +958,7 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { ret = callJavaCB.call(a,b,c,d); } catch(RuntimeException e) { System.err.println("Error while executing callJavaCB"); - e.printStackTrace(); + e.printStackTrace(); ret = 0; } state = RUNNING; @@ -956,11 +979,21 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { /** Hook for subclasses to do something when the process exits */ void _exited() { } - private int sys_exit(int status) { + void exit(int status, boolean fromSignal) { + if(fromSignal && fds[2] != null) { + try { + byte[] msg = getBytes("Process exited on signal " + (status - 128) + "\n"); + fds[2].write(msg,0,msg.length); + } catch(ErrnoException e) { } + } exitStatus = status; for(int i=0;i b ? a : b; }