From 9789eee00484fbe549c865d50d1f0f91b50dfa17 Mon Sep 17 00:00:00 2001 From: brian Date: Thu, 18 Mar 2004 17:26:22 -0800 Subject: [PATCH] more updates darcs-hash:20040319012622-24bed-828a577e538f71d8658ae0b4f30eee398a792820.gz --- doc/nestedvm.ivme04.tex | 186 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 134 insertions(+), 52 deletions(-) diff --git a/doc/nestedvm.ivme04.tex b/doc/nestedvm.ivme04.tex index 5511fcd..19ff75f 100644 --- a/doc/nestedvm.ivme04.tex +++ b/doc/nestedvm.ivme04.tex @@ -637,39 +637,101 @@ prepare for a invoke special call. By simple moving this outside the switch statement each case arm was reduced in size by one instruction. Similar optimizations were also done in other parts of the compiler. - \section{Interfacing with Java Code} -Java source code can create a copy of the translated binary by -instantiating the corresponding class, which extends {\tt Runtime}. -Invoking the {\tt main()} method on this class is equivalent to -calling the {\tt main()} function within the binary; the {\tt String} -arguments to this function are copied into the binary's memory space -and made available as {\tt **argv} and {\tt argc}. +NestedVM has two primary ways of executing code, the interpreter, and the binary translators. Both the interpreter and the output from the binary translators sit on top of a Runtime class. This class provides the public interface to both the interpreter and the translated binaries. -The translated binary communicates with the rest of the VM by -executing MIPS {\tt SYSCALL} instructions, which are translated into -invocations of the {\tt syscall()} method. This calls back to the -native Java world, which can manipulate the binary's environment by -reading and writing to its memory space, checking its exit status, -pausing the VM, and restarting the VM. +\subsection{The Runtime Class} +The Runtime class does the work that the operating system usually does. Conceptually the Runtime class can be though of as the operating system and itÕs subclasses (translated binaries and the interpreter) the CPU. The Runtime fulfills 5 primary goals: -\subsection{Virtualization} +\begin{itemize} -The {\tt Runtime} class implements the majority of the standard {\tt -libc} syscalls, providing a complete interface to the filesystem, -network socket library, time of day, (Brian: what else goes here?). +\item Provides a consistent external interface - Because only Runtime exposes a public interface the method of actually executing the code (currently only translated binaries and the interpreter) can be changed without any code changes. -\begin{itemize} +\item Provide an easy to use interface - The interpreter and the output from the binary translators only know how to execute code. The Runtime class provides an easy to use interface to the code. It contains methods to pass arguments to the main() function, read and write from memory, and call individual functions in the binary. -\item ability to provide the same interface to CNI code and - NestedVMified code - -\item security advantages (chroot the {\tt fork()}ed process) +\item Manage the processÕs memory - The Runtime class contains large int[] arrays that represent the processÕs entire memory space. Subclasses read and write to these arrays as required by the instructions they are executing. Subclasses can expend their memory space using the sbrk syscall. + +\item Provide access to the file system and streams - Subclasses access the file system through standard UNIX syscalls (read, write, open, etc). The Runtime manages the file descriptor table that maps UNIX file descriptors to Java RandomAccessFiles, InputStreams, OutputStreams, and sockets. + +\item Miscellaneous other syscalls - In additions to those mentioned above the Runtime class implements a variety of other syscalls (sleep, gettimeofday, getpagesize, sysconf, fcntl, etc). \end{itemize} +\subsection{Interacting with the Binary} + +Java source code can create a copy of the translated binary by instantiating the class generated by the binary translator or instantiating the interpreter. It can then interact with the process through the many facilities provided by the Runtime interface. Invoking the run() method of the Runtime interface will load the given arguments into the processÕs memory as invoke the binaries entry point (typically \_start() in crt0.o). This will pass control on to the main() function which will have the arguments passed to run() loaded into argv and argc. + +As the binary executes it often passes control back to the Runtime class through the MIPS {\tt SYSCALL} instruction. The interpreter and translated binaries invoke the {\tt syscall()} method of the Runtime class when the {\tt SYSCALL} instruction is executed. The Runtime class then can manipulate the processÕs environment (read and write to memory, modify the file descriptor table, etc) and interact with the rest of the JVM on behalf of the process (read and write to a file or stream, etc). There is even a syscall to pause the VM and temporarily return control to the caller. + +In addition to the interfaces provided by NestedVM, users can create their own interfaces between the MIPS and Java world. The Runtime provides a method called call() that will call a function by name in the MIPS binary. The call() method looks up the function name in the binaryÕs ELF symbol table and manipulating the stack and registers accordingly to execute the given function. This allows Java code to seamlessly invoke functions in the binary. + +{\footnotesize\begin{verbatim} +// Java +private Runtime rt = new MyBinary(); +public void foo(int n) { + for(int i=0;i<10;i++) { + int result = rt.call("do_work",i); + System.err.println("do_work(i) = " + result); + } +} +// C +void do_work(int n) { + int i; + int ret=0; + for(i=0;i