javm_recompiler_x86/lib.rs
1#![no_std]
2
3//! PVM recompiler — compiles PVM bytecode to native x86-64 machine code.
4//!
5//! This crate is the no_std bytes-producer: it emits x86-64 machine
6//! code into a `Vec<u8>`. The runtime substrate that loads + executes
7//! the emitted code lives in `nub-arch-x86`, which compiles this
8//! crate with `default-features = false` and supplies its own
9//! per-invocation page table.
10//!
11//! Public surface:
12//! - [`JitContext`] — `#[repr(C)]` execution context, written by the
13//! driver before entry and read after exit. Layout is mirrored by
14//! the codegen-side `CTX_*` offset constants in
15//! `javm-recompiler-x86::codegen`.
16//! - [`asm`], [`codegen`] — codegen pipeline.
17
18extern crate alloc;
19
20pub mod asm;
21pub mod codegen;
22
23/// JIT execution context passed to compiled code via R15.
24/// Must be `#[repr(C)]` with exact field ordering matching the
25/// `CTX_*` offset constants in [`codegen`].
26#[repr(C)]
27pub struct JitContext {
28 /// PVM2 registers (offset 0, 15 × 8 = 120 bytes). Slots 0..12 are the
29 /// host-mapped GPRs (flushed to/from x86 registers at the prologue /
30 /// epilogue); slots 13/14 are the spilled `x3`/`x4`, which live here in
31 /// memory for the whole block and are materialised per access.
32 pub regs: [u64; 15],
33 /// Gas counter. Signed to detect underflow.
34 pub gas: i64,
35 /// Exit reason code.
36 pub exit_reason: u32,
37 /// Exit argument — host call ID, page fault addr, etc.
38 pub exit_arg: u32,
39 /// Heap base address.
40 pub heap_base: u32,
41 /// Current heap top.
42 pub heap_top: u32,
43 /// Entry PC for re-entry after host calls.
44 pub entry_pc: u32,
45 /// Current PC when execution stopped (offset 164).
46 pub pc: u32,
47 /// Dispatch table: PVM PC → native code offset (offset 168).
48 pub dispatch_table: *const i32,
49 /// Base address of native code (offset 176).
50 pub code_base: u64,
51 /// Flat guest memory buffer base pointer (offset 184).
52 pub flat_buf: *mut u8,
53 /// Fast re-entry flag.
54 pub fast_reentry: u32,
55 pub _pad2: u32,
56 /// Maximum heap pages — grow_heap refuses beyond this.
57 pub max_heap_pages: u32,
58 pub _pad3: u32,
59 /// RSP saved at JIT entry (after the prologue's callee-saved pushes
60 /// but before any guest code runs). The exit_label restores RSP
61 /// from this slot before popping the callee-saved registers, so an
62 /// OOG / page-fault redirect taken mid-sequence leaves the exit
63 /// path with a clean stack.
64 pub host_rsp_base: u64,
65}