From e5cfcdf2d1d02ceeb4bcc127dbba68f2c826374d Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 28 Oct 2026 13:24:55 +0000 Subject: [PATCH] refactor(compiler): emit memory-service calls through the typed encoder --- .../src/x86_64/memory_context/address.rs | 60 +++--- .../src/x86_64/memory_context/callback.rs | 173 +++++++++--------- 2 files changed, 114 insertions(+), 119 deletions(-) diff --git a/crates/stela-compiler/src/x86_64/memory_context/address.rs b/crates/stela-compiler/src/x86_64/memory_context/address.rs index 05ccc0c132..beab714ded 100644 --- a/crates/stela-compiler/src/x86_64/memory_context/address.rs +++ b/crates/stela-compiler/src/x86_64/memory_context/address.rs @@ -1,6 +1,6 @@ //! SP alignment checks and address arithmetic for checked accesses. -use super::{fault, patch_branch}; -use crate::x86_64::asm::Code; +use super::fault; +use crate::x86_64::asm::{Code, Cond, Gpr, Mem, Size}; use crate::x86_64::load; use crate::{ aarch64::{AddressMode, Register, register_offset::IndexExtend}, @@ -8,17 +8,6 @@ use crate::{ }; const STACK_ALIGNMENT_MASK: u8 = 15; -const TEST_AL_IMM8: u8 = 0xa8; -const JE_REL32: &[u8] = &[0x0f, 0x84]; -const JMP_REL32: u8 = 0xe9; -const TEST_EAX_EAX: &[u8] = &[0x85, 0xc0]; -const JE_REL8: u8 = 0x74; -const RET: u8 = 0xc3; -const LEA_RAX_RAX_DISP8: &[u8] = &[0x48, 0x8d, 0x40]; -const LEA_RAX_RAX_DISP32: &[u8] = &[0x48, 0x8d, 0x80]; -const MOV_RDX_RAX: &[u8] = &[0x48, 0x89, 0xc2]; -const MOV_R8_RAX: &[u8] = &[0x49, 0x89, 0xc0]; -const MOV_RAX_R8: &[u8] = &[0x4c, 0x89, 0xc0]; /// Check SP before the offset is applied. The caller patches the returned rel32 /// to its cleanup. On failure RAX holds the faulting SP. Clobbers RAX/RCX/RDX @@ -28,24 +17,24 @@ pub(crate) fn check_sp(code: &mut Code, base: Register, pc: u64) -> Option() as u32; const CALLBACK_SAVE_BYTES: u32 = 16; -const SUB_RSP_SAVE: &[u8] = &[0x48, 0x83, 0xec, CALLBACK_SAVE_BYTES as u8]; -const ADD_RSP_SAVE: &[u8] = &[0x48, 0x83, 0xc4, CALLBACK_SAVE_BYTES as u8]; -const STMXCSR_RSP: &[u8] = &[0x0f, 0xae, 0x1c, 0x24]; -const LDMXCSR_RSP: &[u8] = &[0x0f, 0xae, 0x14, 0x24]; -// Reuse the existing 16-byte reservation: original MXCSR at 0, default at 4. -// This baseline recipe costs 2 instructions/13 bytes, with no literal relocation -// or additional GPR spill. Actual emitted-code/timing gates remain separate. -const MOV_DEFAULT_MXCSR_RSP_4: &[u8] = &[0xc7, 0x44, 0x24, 4, 0x80, 0x1f, 0, 0]; -const LDMXCSR_RSP_4: &[u8] = &[0x0f, 0xae, 0x54, 0x24, 4]; -const PUSH_RDI: &[u8] = &[0x57]; -const POP_RDI: &[u8] = &[0x5f]; -const MOV_RDI_CONTEXT_DISP8: &[u8] = &[0x49, 0x8b, 0x7f]; -const LEA_RSI_RSP_DISP32: &[u8] = &[0x48, 0x8d, 0xb4, 0x24]; -const CALL_RDI_DISP8: &[u8] = &[0xff, 0x57]; -const TEST_EAX_EAX: &[u8] = &[0x85, 0xc0]; -const JNE_REL32: &[u8] = &[0x0f, 0x85]; -const MOV_RSP_DISP32_RAX: &[u8] = &[0x48, 0x89, 0x84, 0x24]; -const LEA_RDX_RSP_DISP32: &[u8] = &[0x48, 0x8d, 0x94, 0x24]; -const MOV_RSP_DISP32_RDX: &[u8] = &[0x48, 0x89, 0x94, 0x24]; -const MOV_RSP_DISP32_IMM32: &[u8] = &[0xc7, 0x84, 0x24]; -const MOVABS_RCX_IMM64: &[u8] = &[0x48, 0xb9]; -const MOV_RSP_DISP32_RCX: &[u8] = &[0x48, 0x89, 0x8c, 0x24]; -const MOV_RDX_CONTEXT_DISP8: &[u8] = &[0x49, 0x8b, 0x57]; -const MOV_RDX_DISP8_IMM32: &[u8] = &[0xc7, 0x42]; -const MOV_RDX_DISP8_RAX: &[u8] = &[0x48, 0x89, 0x42]; -const MOV_RDX_DISP8_RCX: &[u8] = &[0x48, 0x89, 0x4a]; -const MOV_RDX_DISP8_IMM32_QWORD: &[u8] = &[0x48, 0xc7, 0x42]; -const MOV_EAX_IMM32: u8 = 0xb8; +/// MXCSR's power-on value: every exception masked, round to nearest. +const DEFAULT_MXCSR: i32 = 0x1f80; +/// The caller's MXCSR, in the 16-byte reservation. +const SAVED_MXCSR: Mem = Mem::base(Gpr::Rsp, 0); +/// The default MXCSR, after the caller's. This baseline recipe costs 2 +/// instructions/13 bytes, with no literal relocation or additional GPR spill. +const LOADED_MXCSR: Mem = Mem::base(Gpr::Rsp, 4); -fn field_offset(offset: usize) -> u8 { - u8::try_from(offset).expect("memory view field fits positive disp8") +/// The memory view field at `offset` from RDX. +fn field(offset: usize) -> Mem { + Mem::base_disp8( + Gpr::Rdx, + i8::try_from(offset).expect("memory view field fits positive disp8"), + ) +} + +/// The memory view pointer in the execution context at R15. +const fn memory_view() -> Mem { + Mem::base_disp8(Gpr::R15, EXECUTION_CONTEXT_MEMORY_OFFSET as i8) } fn memory_pointer(code: &mut Code) { - code.extend(MOV_RDX_CONTEXT_DISP8); - code.push(EXECUTION_CONTEXT_MEMORY_OFFSET as u8); + code.asm().mov_load(Size::Qword, Gpr::Rdx, memory_view()); } /// Start one instruction's exact reached-prefix record. Clobbers RDX only. @@ -56,12 +43,17 @@ pub(crate) fn begin(code: &mut Code) { memory_pointer(code); // v2 places the validated fault-access code directly after fault_kind. // Clear both before each reached instruction, including local faults. - code.extend(MOV_RDX_DISP8_IMM32_QWORD); - code.push(field_offset(offset_of!(MemoryContextView, fault_kind))); - code.extend((MemoryStatus::Success as u32).to_le_bytes()); - code.extend(MOV_RDX_DISP8_IMM32_QWORD); - code.push(field_offset(offset_of!(MemoryContextView, completed_bytes))); - code.extend(0_u32.to_le_bytes()); + let mut asm = code.asm(); + asm.mov_rm_imm32( + Size::Qword, + field(offset_of!(MemoryContextView, fault_kind)), + MemoryStatus::Success as i32, + ); + asm.mov_rm_imm32( + Size::Qword, + field(offset_of!(MemoryContextView, completed_bytes)), + 0, + ); } /// Build a request at a private stack offset. RAX supplies the guest address; @@ -78,24 +70,31 @@ pub(crate) fn request( MEMORY_REQUEST_ADDRESS_OFFSET, MEMORY_REQUEST_DATA_OFFSET, MEMORY_REQUEST_FLAGS_OFFSET, MEMORY_REQUEST_LENGTH_OFFSET, MEMORY_REQUEST_PC_OFFSET, }; - code.extend(MOV_RSP_DISP32_RAX); - code.extend((request_offset + MEMORY_REQUEST_ADDRESS_OFFSET).to_le_bytes()); - code.extend(LEA_RDX_RSP_DISP32); - code.extend(data_offset.to_le_bytes()); - code.extend(MOV_RSP_DISP32_RDX); - code.extend((request_offset + MEMORY_REQUEST_DATA_OFFSET).to_le_bytes()); + let stack = |offset: u32| Mem::base_disp32(Gpr::Rsp, offset as i32); + let mut asm = code.asm(); + asm.mov_store( + Size::Qword, + stack(request_offset + MEMORY_REQUEST_ADDRESS_OFFSET), + Gpr::Rax, + ); + asm.lea(Size::Qword, Gpr::Rdx, stack(data_offset)); + asm.mov_store( + Size::Qword, + stack(request_offset + MEMORY_REQUEST_DATA_OFFSET), + Gpr::Rdx, + ); for (field, value) in [ (MEMORY_REQUEST_LENGTH_OFFSET, bytes), (MEMORY_REQUEST_FLAGS_OFFSET, flags), ] { - code.extend(MOV_RSP_DISP32_IMM32); - code.extend((request_offset + field).to_le_bytes()); - code.extend(value.to_le_bytes()); + asm.mov_rm_imm32(Size::Dword, stack(request_offset + field), value as i32); } - code.extend(MOVABS_RCX_IMM64); - code.extend(pc.to_le_bytes()); - code.extend(MOV_RSP_DISP32_RCX); - code.extend((request_offset + MEMORY_REQUEST_PC_OFFSET).to_le_bytes()); + asm.mov_imm64(Gpr::Rcx, pc); + asm.mov_store( + Size::Qword, + stack(request_offset + MEMORY_REQUEST_PC_OFFSET), + Gpr::Rcx, + ); } /// Invoke the validated memory service. Before entry RSP must be 8 modulo16, @@ -105,29 +104,32 @@ pub(crate) fn request( /// MXCSR, including exception status bits, on success and refusal. Returns the /// typed MemoryStatus encoding in EAX. No callback address is embedded. pub(crate) fn call(code: &mut Code, request_offset: u32) { - code.extend(PUSH_RDI); - code.extend(SUB_RSP_SAVE); - code.extend(STMXCSR_RSP); - code.extend(MOV_DEFAULT_MXCSR_RSP_4); - code.extend(LDMXCSR_RSP_4); - code.extend(MOV_RDI_CONTEXT_DISP8); - code.push(EXECUTION_CONTEXT_MEMORY_OFFSET as u8); - code.extend(LEA_RSI_RSP_DISP32); - code.extend((request_offset + HOST_POINTER_BYTES + CALLBACK_SAVE_BYTES).to_le_bytes()); - code.extend(CALL_RDI_DISP8); - code.push(MEMORY_CALLBACK_OFFSET as u8); - code.extend(LDMXCSR_RSP); - code.extend(ADD_RSP_SAVE); - code.extend(POP_RDI); + let mut asm = code.asm(); + asm.push(Gpr::Rdi); + asm.alu_imm8(Alu::Sub, Size::Qword, Gpr::Rsp, CALLBACK_SAVE_BYTES as i8); + asm.stmxcsr(SAVED_MXCSR); + asm.mov_rm_imm32(Size::Dword, LOADED_MXCSR, DEFAULT_MXCSR); + asm.ldmxcsr(LOADED_MXCSR); + asm.mov_load(Size::Qword, Gpr::Rdi, memory_view()); + asm.lea( + Size::Qword, + Gpr::Rsi, + Mem::base_disp32( + Gpr::Rsp, + (request_offset + HOST_POINTER_BYTES + CALLBACK_SAVE_BYTES) as i32, + ), + ); + asm.call_rm(Mem::base_disp8(Gpr::Rdi, MEMORY_CALLBACK_OFFSET as i8)); + asm.ldmxcsr(SAVED_MXCSR); + asm.alu_imm8(Alu::Add, Size::Qword, Gpr::Rsp, CALLBACK_SAVE_BYTES as i8); + asm.pop(Gpr::Rdi); } /// Emit a failure branch; patch the returned displacement to family cleanup. pub(crate) fn branch_failed(code: &mut Code) -> usize { - code.extend(TEST_EAX_EAX); - code.extend(JNE_REL32); - let displacement = code.len(); - code.extend(0_i32.to_le_bytes()); - displacement + let mut asm = code.asm(); + asm.test(Size::Dword, Gpr::Rax, Gpr::Rax); + asm.jcc(Cond::Ne).offset() } pub(crate) fn patch_branch(code: &mut [u8], displacement: usize, destination: usize) { @@ -142,15 +144,22 @@ pub(crate) fn patch_branch(code: &mut [u8], displacement: usize, destination: us pub(crate) fn fault(code: &mut Code, status: MemoryStatus, pc: u64) { use crate::context::memory::MemoryContextView; memory_pointer(code); - code.extend(MOV_RDX_DISP8_IMM32); - code.push(field_offset(offset_of!(MemoryContextView, fault_kind))); - code.extend((status as u32).to_le_bytes()); - code.extend(MOV_RDX_DISP8_RAX); - code.push(field_offset(offset_of!(MemoryContextView, fault_address))); - code.extend(MOVABS_RCX_IMM64); - code.extend(pc.to_le_bytes()); - code.extend(MOV_RDX_DISP8_RCX); - code.push(field_offset(offset_of!(MemoryContextView, fault_pc))); - code.push(MOV_EAX_IMM32); - code.extend((status as u32).to_le_bytes()); + let mut asm = code.asm(); + asm.mov_rm_imm32( + Size::Dword, + field(offset_of!(MemoryContextView, fault_kind)), + status as i32, + ); + asm.mov_store( + Size::Qword, + field(offset_of!(MemoryContextView, fault_address)), + Gpr::Rax, + ); + asm.mov_imm64(Gpr::Rcx, pc); + asm.mov_store( + Size::Qword, + field(offset_of!(MemoryContextView, fault_pc)), + Gpr::Rcx, + ); + asm.mov_imm32(Gpr::Rax, status as u32); }