The Java Virtual Machineand Mobile Devices
John Buford, [email protected]
Oct 2003Presented 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?
a[0]
4
object ref TOS
MAX
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
DynamicClass
Loaderand
Verifier
Classand
MethodArea
GarbageCollected
Heap
Support Code:
ExceptionsThreadsSecurity...
ExecutionEngine
NativeMethod
Area
NativeMethodLinker
Operating System
Native Methods.dll or .so
StandardBuilt-in
Java Classes
Applet orApplication Class Files
Execution Engine
Object Heap
Allocated
Free
Being Reclaimed
a[0]4
object ref TOS
MAX
MIN
System Thread
a[0]4
object ref TOS
MAX
MIN
Event Loop
a[0]4
object ref TOS
MAX
MIN
Application Thread 1
a[0]4
object ref TOS
MAX
MIN
Application Thread N
Classes
Constant Pool
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 values16 05 bipush 5 => 516 06 bipush 6 ..., 5 => 5, 696 iadd 5 , 6 => 11
• HelloWorldpublic class HelloWorld {
public static void main(String args[ ] ) {
System.out.println(“Hello world!”);
}
}
Inside “HelloWorld”javap -c HelloWorldCompiled from HelloWorld.javapublic class HelloWorld extends java.lang.Object
/* ACC_SUPER bit set */{
public static void main(java.lang.String []);public HelloWorld();
; call Object's constructorMethod 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 stack1 invokespecial #6 <Method java.lang.Object.<init>()V> ; invoke the object initializer method4 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
PC
Optop
FramePtr
vars
Stack
prev frame ptr
prev PC
prev TOS
local vars
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 fileinitialize state of vmwhile(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 Embeddedand Mobile Devices
Cell Phones
PDAsSee http://www.mobiletechnews.com/
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 Editionhttp://jcp.org
CLDC 1.0
MIDP 1.0
WMA 1.0
CLDC 1.1
MIDP 2.0
CLDC 1.1
MIDP 2.0
JTWI
CDC 1.0
FP 1.0
PBP 1.0
PP 1.0
CDC 1.1
FP 1.1
PBP 1.1
PP 1.1
CDC CLDC/MIDP
Optional JSRs
JSR 135 Mobile MediaJSR 179 Location APIJSR 205 WMA 2.0JSR 209 Adv Graphics and UIJSR 211 Content HandlerJSR 184 Mobile 3D GraphicsJSR 229 Payment API…
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