Beruflich Dokumente
Kultur Dokumente
The K Virtual
Machine (KVM)
Frank Yellin
Sun Microsystems, Inc.
K Virtual Machine
KVM is a low tech Java virtual machine
No dynamic compilation or other advanced
performance optimization techniques
Easy to read and port
KVM Portability
Source code available under SCSL
(Sun Community Source License)
KVM Design
Unlike some other small Java technology
implementations
KVM supports dynamic class loading and
regular class files
Supports JAR file format
JVM/KVM Compatibility
General goal
Full Java language and Java virtual machine
specification compatibility
Features of the VM
10
Memory model
Garbage collector
Interpreter
Frames
Threads and monitors
File loading
Verification
Security
Native methods
Romizer
Palm-romizer
Inflater
Restartability
Port specific changes
Compiler flags
Porting dangers
In the future
OBJECT
INSTANCE
ARRAY
STRING_INSTANCE
INSTANCE_CLASS
ARRAY_CLASS
11
FIELD
METHOD
FIELDTABLE
METHODTABLE
FRAME
THREAD
BYTEARRAY
SHORTARRAY
POINTERLIST
HASHTABLE
Variable Length
typedef struct instanceStruct {
INSTANCE_CLASS ofClass;
MONITOR monitor;
union {
cell *cellp;
cell cell;
} data[1];
}
#define SIZEOF_INSTANCE(n) \
StructSizeInCells(instanceStruct) + (n - 1)
12
Hash Tables
Three system hashtables
ClassTable
Package/base to CLASS
InternStringTable
Char*/length to unique java.lang.String
UTFStringTable
Char*/length to unique instance
13
next
next
len
len
key
abcde....
key
java.lang
next
len
key
Connector
14
UString Creation
getUString(const char *string)
getUStringX(const char *string,
int length)
strcmp(x,y) == 0 iff
getUString(x) == getUString(y)
15
16
ofClass
ofClass
ofClass
monitor
monitor
monitor
base name
base name
base name
package name
package name
package name
next
next
next
superclass
superclass
primitiveType
field table
field table
itemSize
method table
method table
GC type
constant pool
constant pool
interfaces
interfaces
key
key
next
next
string
string
string
java.langString
monitor
17
charArray
class [C
offset
monitor
length
length
Key Spaces
Classes (and field types)
Raw classes
Array classes
Names
Field and method names
Class packages
Class base names
Method signatures
18
Class Keys
0 - 0xff: primitive type
Integer = I; boolean = Z, etc
19
Name Keys
1. Look up name in UTFStringTable
2. Get the key
getUTFString(name)->key
20
Method Signature
1. Encode signature into a string
Algorithm
1 byte gives number of arguments
1-3 bytes for each argument
1-3 bytes for the return value
Encoding
Primitive types encoded as A - Z
Non primitives encoded as high/low
unless high in range A - Z, then use L
2. getUTFStringX(str, len)->key
21
Signature Encoding
Class com.sun.cldc.io.j2me.datagram.Protocol {
Datagram newDatagram(byte[], int, String);
}
([BILjava/lang/String;)Ljavax/microedition/io/Datagram;
0x20
byte[]
0x01
0x19
0x01 0x70
java.lang.String
javax.microedition.io.Datagram
22
Why Keys?
Saves space for C strings
Makes comparisons faster
Much less space
But. . . .
Makes debugging more complicated
Debug functions exist to convert keys
to strings, and vice versa
23
Memory Model
24
Object Header
Reserved
24 Bits Size (in Words)
25
Type
Marked
Header Types
Objects that are visible to the user
GCT_INSTANCE
GCT_ARRAY
GCT_OBJECTARRAY
GCT_INSTANCE_CLASS
GCT_ARRAY_CLASS
26
Memory Layout
FreePointer
Free
Free
Free
28
Creating Roots
Global roots (permanent)
Temporary roots (stack discipline)
Transient roots (non stack discipline)
29
Global Roots
Created using makeGlobalRoot
Cannot be undone
Native code can create new roots
makeGlobalRoot(&globalVariable)
30
Temporary Roots
Roots used in a stack-like manner
Can be nested
START_TEMPORARY_ROOTS
MAKE_TEMPORARY_ROOT(x)
...
MAKE_TEMPORARY_ROOT(y)
...
END_TEMPORARY_ROOTS
START_TEMPORARY_ROOT(x)
code
END_TEMPORARY_ROOT
31
Transient Roots
Non-stack like behavior
Special handling of NULL
i = makeTransientRoot(x)
makeTransientRoot(y)
. . . . .
removeTransientRootByIndex(i);
or
removeTransientRootByValue(y);
32
Allocating Objects
mallocBytes()
mallocHeapObject(size, type)
mallocObject(size, type)
callocObject(size, type)
instantiate(instance_class)
instantiateArray(arrayclass, count)
instantiateMultiArray(class,
dims,count)
instantiateString(string, length)
33
Garbage Collecting
Can happen any time an object is
allocated
EXCESSIVE_GARBAGE_COLLECTION
34
KVM Design
Garbage collector
Non-Compacting
Garbage Collectors
Advantages
Does not move objects => simple and clean
codebase
Single space => uses less memory
Disadvantages
Object allocation not so fast
Memory fragmentation can cause it to run out
of heap
Non-incremental => long GC pauses when
using large heaps
36
Interpreter
Runnable Threads
CurrentThread
Java stack of the
current thread
Straightforward
bytecode
interpreter with five
VM registers:
UP thread pointer
IP instruction pointer
SP stack pointer
FP frame pointer
LP locals pointer
(instruction pointer)
IP
(top of stack)
(current frame)
(current thread)
(locals of the
current frame)
37
SP
FP
LP
Optimizations
Space optimizations
System class preloading (romizing) using
JavaCodeCompact
Runtime simonizing of immutable structures
Chunky stacks and segmented heap
Performance optimizations
Quick byte codes
Monomorphic inline caching
Class Loading
Early interning of Strings
Early binding of Classes to Class
structure
Removal of all UTF Strings from
constant pool
39
Stack Frames
operands
locals
syncObject
previousIp
previousFp
returnCode
thisMethod
constPool
locals
params
this
40
FP (frame pointer)
LP (parameters + local variables)
Thread Design
KVM supports platform-independent
multithreading (green threads)
Fully deterministic; no complex mutual
exclusion issues
This makes porting the KVM to new consumer
devices easier and faster
Substantially more portable and much cleaner
code base than with native threads
All active threads kept in a simple linked
queue, and given execution time based
on priority
41
42
Condvars
Timer Queue
Owner
File Loading
Loaderfile.c
43
44
45
Zip Inflater
Old implementation
Two new implementations
FILE* contains compressed bytes
Char* containing compressed bytes
46
64-Bit Support
#DEFINE COMPILER_SUPPORTS_LONGS 1
long64, ulong64
NEED_LONG_ALIGNMENT
NEED_DOUBLE_ALIGNMENT
#Define COMPILER_SUPPORTS_LONGS 0
ll_mul, ll_div, ll_rem,
ll_shl, ll_shr, ll_ushr
BIG_ENDIAN, LITTLE_ENDIAN
47
Security
Cannot support full J2SE platform security model
in CLDC target devices:
J2SE platform security model is much larger than the
entire CLDC implementation
48
Security
CLDC sandbox model requires that:
Classfiles have been properly verified and
guaranteed to be valid
Only a limited, predefined set of APIs are
available to the application programmer
(as defined by the CLDC, profiles and
licensee open classes)
Downloading and management of applications
takes place at the native code level, and the
programmer cannot override the standard class
loading mechanisms or the system classes of
the virtual machine
49
Security
Sandbox requirements continued
The set of native functions accessible to the
virtual machine is closed, meaning that the
application programmer cannot download any
new libraries containing native functionality, or
access any native functions that are not part of
the APIs provided by CLDC, profiles and
licensee open classes
50
51
MyApp.java
download...
javac
MyApp.class
preverifier
MyApp.class
52
verifier
interpreter
Old Verifier
Theorem prover
Consistent set of values for the stack
Consistent set of values for each register
Consistent use of jsr/ret instruction
54
Old Verifier
Required no information outside
the class
Complex data flow analysis
Can require multiple passes
Can require large amount of space
Jsr and ret instructions cause massive
difficulties
55
New Verifier
56
Theorem verifier
Single pass through the code
Very little space needed
Theorem proving occurs off line
May improve garbage collection in
the future!
57
Old Verifier
Long
<>
Long
Long
0. aload_0
1. astore_1
Long
Long
<>
Long
Long
Number
<>
Long
Long
Number
Long
6. invokeStatic nextValue(Number)
Long Long
Number
Number
Long
Number
<>
Long Long
11. invokevirtual intValue()
Number
Long
2. goto 10
5. aload_1
Number
9. astore_1
Long
10. aload_1
Long
Long
Number
int
Long
Long
Number
<>
Number
14. ifne 5
17. return
58
Done!
New Verifier
Long
<>
Long
Long
0. aload_0
1. astore_1
Long
Long
<>
Long
Number
<>
Long
Number
Number
6. invokeStatic nextValue(Number)
Long Number
Number
2. goto 10
5. aload_1
9. astore_1
Long
Number
<>
10. aload_1
Long Number
11. invokevirtual intValue()
Number
Long
Number
int
Long
Number
<>
14. ifne 5
17. return
59
Done!
Native Methods
void <JNI name for Function> (void) {
pop arguments off stack;
pop <this> off stack (if instance)
perform calculations
push result (if any) onto stack;
return;
}
60
61
JNI Caveats
Object allocation functions
Using the right push/pop macro
Dont use type coercion!
62
Asynchronous JNI
Readers and writers
Two implementations
Call back with continuation
Start a separate thread to perform the task
63
Event Handling
Synchronous notification
Polling in Java programming
language code
Polling in the interpreter
Asynchronous notification
64
Romizer
Based on romizer used in PersonalJava
API implementation
Generates data structures that JVM would
have generated
Classes are loaded but not initialized
65
Palm Romizer
Problems
64K limit per resource
Resources are relocatable
Native code is independently relocatable
Solutions
66
Restartability
67
Starting a Port
machine_md.h
runtime_md.c
main.c
68
Porting: Functions
AlertUser()
allocateHeap(), freeHeap()
InitializeNativeCode(),
FinalizeNativeCode()
InitializeVM(), FinalizeVM()
CurrentTime_md()
Ulong64, long64
BIG_ENDIAN or LITTLE_ENDIAN
69
70
Port-Specific Changes
71
Compiler Flags
72
Tracing flags
Floating point
Alignment issues
Romizing
Generic networking
Generic storage
Directory Structure
73
Api
Bin
Build
Doc
Jam
Samples
Tools
Kvm
TS-1507: Inside the K Virtual Machine
Subdirectores of tools/
tools/jcc
tools/palm
Subdirectories of kvm/
kvm/VMCommon/h, kvm/VMCommon/src
kvm/VMExtra/h, kvm/VMExtra/h
kvm/VM<port>/h, kvm/VM<port>/h,
kvm/VM<port>/build
74
Porting
Create machine_md.h
Create runtime_md.h
Determine values of compiler flags
75
Application Representation
Public representation of applications
Whenever applications intended for a CLDC
device are stored publicly, compressed JAR
format must be used
Class files must have been preverified and
contain the preverification information
78