-
Notifications
You must be signed in to change notification settings - Fork 0
VM Magic
"Magic" in JNode refers to the mechanisms that allow Java code to bypass standard JVM constraints, enabling low-level OS operations like raw memory access and direct hardware manipulation.
A traditional JVM strongly types object references and prevents direct memory access to ensure safety. An operating system, however, must directly manipulate hardware and memory. JNode resolves this via the VmMagic framework and special annotations.
These classes and annotations act as intrinsics—the JIT compilers recognize them and emit specific native machine instructions instead of standard method calls or bounds-checked operations.
Location: core/src/classlib/org/jnode/annotation/
These annotations direct the JIT compiler to alter standard compilation behavior for specific methods or classes.
| Annotation | Purpose |
|---|---|
@MagicPermission |
Grants the class permission to invoke methods in org.jnode.vm.Unsafe or org.jnode.vm.VmMagic. |
@Uninterruptible |
Prevents the thread scheduler from context-switching out of this method, and prevents Garbage Collection safepoints. Crucial for kernel code. |
@KernelSpace |
Marks a method as executing with ring-0 / kernel privileges. |
@Inline / @NoInline
|
Hints to the JIT compiler regarding method inlining. |
@LoadStatics |
Tells BootImageBuilder not to serialize this class's static fields into the boot image, but instead re-initialize them during actual boot. |
@SharedStatics |
Ensures static fields are shared across all isolates (by default, statics are per-isolate). |
Location: core/src/vmmagic/org/vmmagic/unboxed/
These types represent raw machine-level primitives. They appear to be Java objects in the source code, but the JIT compiler "unboxes" them into raw register values, avoiding object allocation and object header overhead.
| Type | Represents | Use Case |
|---|---|---|
Address |
A raw memory pointer. | Accessing DMA memory, hardware registers. |
Word |
An unsigned machine-word (32-bit or 64-bit). | Low-level bitwise operations. |
Offset |
A signed offset from an Address. |
Pointer arithmetic. |
Extent |
An unsigned size/length. | Memory block allocation. |
ObjectReference |
A raw pointer to a Java object header. | Used heavily by the Garbage Collector and BootImageBuilder. |
Note: Operations on unboxed types (e.g., address.loadWord()) are intercepted by MagicHelper in the JIT compiler and translated directly into MOV instructions.
These classes provide the static API for low-level operations.
-
Unsafe.getAddress(...)- Fetch the raw physical address of an object or array. -
Unsafe.debug(...)- Raw output to the console beforeSystem.outis initialized. -
VmMagic.getObjectType(...)- Read the internalVmTypemetadata of an object.
The compiler's handling of magic is primarily driven by the MagicHelper classes within the l1a, l1b, and l2 compiler packages. When a method call is parsed, the compiler checks if the target class is an unboxed type or Unsafe. If so, it delegates code generation to MagicHelper, which emits the necessary inline assembly.
- VM-Unsafe — Direct memory access, port I/O, CPU operations
- JIT-Compilers — How the JIT handles magic calls.
- Code-Conventions — Guidelines on when and how to use these annotations.
-
Memory-Management — How the GC uses
ObjectReferenceandAddress.