The Java Virtual Machine and Mobile Devices John Buford, Ph.D. buford@alum.mit.edu Oct 2003 Presented to Gordon College CS 311
Objectives Review virtual machine concept Introduce stack machine architecture and relate it to CISC and RISC architectures Present key features of Java VM Describe use of JVM technology in mobile device area
Virtual Machine A processor specification implemented in a software interpreter with the following characteristics: intended to execute programs written in some high level language includes an instruction set, a register set, and a memory model usually designed for portability frequently used inside compilers as an intermediate language to which generic optimizations are made Historical examples: Pascal P-Code, Smalltalk, Portable Scheme, etc. Current examples: Java VM, Microsoft.NET Common Language Runtime
Virtual Machine A specification of a machine language and the CPU and memory organization Typically designed as a generic model a stack machine is one such a generic model Inside view a machine that can be simulated in software application program opcodes stored in files produced by compiler VM reads opcodes into an array VM interprets each opcode one-by-one just as a hardware CPU would Each opcode is interpreted; the execution of the opcode causes changes to other data structures in the program that represent other parts of the machine organization
Stack Machine A CPU architecture where all operations are performed on a stack Most instructions use implied addressing mode so instruction size is short compared with other architectures But more instructions are needed for a complex operation than in other designs Use of stack machines in some early computers Burroughs B 5000 (circa 1960s) HP3000 minicomputers (circa 1970s) Stack machine has been more popular as a virtual machine than as an actual hardware design Software implementation relatively easy Most computers have a hardware stack Why not use an abstract RISC VM instead of a stack machine? object ref 4 a[0] MAX TOS MIN
Implied Addressing Mode Register addressing mode (Intel) ADD AX,BX Direct address mode ADD Var1, Var2 Implied mode PUSH Var1 // destination is implied PUSH Var2 // destination is implied ADD // addends are implied Implied addressing gives shorter instructions, but generally more instructions for given operation
Stack vs CISC vs RISC You re building a new distributed agent software platform that will run on every future computing device. You ve decided to use a virtual machine. How would you compare the architectural choices: CISC RISC Stack machine
Java Virtual Machine 32-bit stack machine Floating point and integer data types ~200 instructions Most instructions are generic, but some are related to the Java object model
Typical Java Runtime System Applet or Application Class Files Standard Built-in Java Classes Dynamic Class Loader and Verifier Class and Method Area Garbage Collected Heap Support Code: Exceptions Threads Security... Native Methods.dll or.so Native Method Linker Native Method Area Execution Engine Operating System
Execution Engine System Thread Event Loop Object Heap MAX MAX Allocated Free Being Reclaimed object ref TOS object ref TOS 4 a[0] Application Thread 1 MIN 4 a[0] Application Thread N MIN Classes MAX MAX Constant Pool object ref TOS object ref TOS 4 a[0] MIN 4 a[0] MIN
Instruction Categories Arithmetic Logical Conversion operations Control transfer Function return Stack operations Pushing constants on to the stack Loading/storing local variables on to/from the stack Managing arrays Method invocation Manipulating object fields Exception handling Miscellaneous object operations Monitors
Examples Add Two values 16 05 bipush 5 => 5 16 06 bipush 6..., 5 => 5, 6 96 iadd 5, 6 => 11 HelloWorld public class HelloWorld { public static void main(string args[ ] ) { } } System.out.println( Hello world! );
Inside HelloWorld javap -c HelloWorld Compiled from HelloWorld.java public class HelloWorld extends java.lang.object /* ACC_SUPER bit set */ { public static void main(java.lang.string []); public HelloWorld(); ; call Object's constructor Method void main(java.lang.string []) 0 getstatic #7 <Field java.lang.system.out Ljava/io/PrintStream;> ; #7 = index into the constant pool 3 ldc #1 <String "Hello World!"> ; push item which is a constant 5 invokevirtual #8 <Method java.io.printstream.println(ljava/lang/string;)v> ; invoke the instance method 8 return ; Push the output stream and the string "Hello World" onto the stack, ; then invoke the println method: Method HelloWorld() 0 aload_0 ; load the object ref onto the stack 1 invokespecial #6 <Method java.lang.object.<init>()v> ; invoke the object initializer method 4 return }
JVM Organization 32-bit operand stack Longs and doubles take 2 entries Stack frame, one for each executing method Local variables Execution environment Operand stack Local variables Stored in 32-bit cells 64-bit types use two cells Execution environment Copies of registers prior to new call frame (PC, frame ptr, TOS) private data (implementation specific, e.g. method and object ref, class table, etc) Registers Program counter (PC) Optop (TOS pointer) Frame Ptr: reference to the execution environment for the currently executing method Vars: reference to the local variables for the currently executing method Stack PC vars FramePtr Optop local vars prev PC prev TOS prev frame ptr prev method s stack area prev frame MAX MIN
JVM Organization (cont d) Constant pool Each class has a constant pool Constants are stored in the class file produced by compiler There are constants for each method and field referenced by an object of this class Method area Stores method byte code Example graphical simulator for VM at http://www.artima.com/insidejvm/applets/index.html
Basic Structure of JVM Interpreter read machine code from file initialize state of vm while(true) { opcode = machine_code[pc]; pc += 1; switch(opcode) { case BIPUSH: break; case SIPUSH: break; /* etc. */ } }
Questions What issues are there in dealing with mixed datatypes in a fixed-width stack? How could stack corruption (accidental or otherwise) effect VM integrity and security? How could bytecode be written to adversely effect VM integrity and security? How does the VM prevent inadvertant stack corruption and bytecode errors? How does this impact performance?
Java on Embedded and Mobile Devices
Cell Phones See http://www.mobiletechnews.com/ PDAs Telematics and Info Appliances
Features of Mobile Platforms PDAs OS: Palm, PocketPC, Embedded Linux, Symbian Processor: ARM, StrongARM, OMAP, Xscale, 68K Memory: 16MB to 64MB IO: WiFi, mini-keyboard/touch screen, display Cell Phones OS: various real-time embedded OS and proprietary software, Qualcomm BREW Processor: ARM; Motorola, SHx Memory: IO: flash memory, keypad, audio, video Large variety of systems software and hardware Java offers portability for applications to run But J2SE is too resource intensive
Java 2 Microedition
Java 2 Microedition Edition http://jcp.org Optional JSRs CDC JSR 135 Mobile Media JSR 179 Location API JSR 205 WMA 2.0 JSR 209 Adv Graphics and UI JSR 211 Content Handler JSR 184 Mobile 3D Graphics JSR 229 Payment API CLDC/MIDP PP 1.0 PP 1.1 JTWI PBP 1.0 PBP 1.1 WMA 1.0 FP 1.0 FP 1.1 MIDP 1.0 MIDP 2.0 MIDP 2.0 CDC 1.0 CDC 1.1 CLDC 1.0 CLDC 1.1 CLDC 1.1
Further Information Java VM Specifications http://java.sun.com/docs/books/vmspec/2nd-edition/html/vmspectoc.doc.html Java VM resources http://www.artima.com/insidejvm/resources/index.html Open source JVM implementations Kaffe (http://www.kaffe.org) Wonka (http://www.acunia.com/wonka/001_about.php)
Summary Review virtual machine concept Introduce stack machine architecture and relate it to CISC and RISC architectures Present key features of Java VM Describe use of JVM technology in mobile device area
Discussion