perf(compiler): an exit's patch point comes before its target and word, which only the unlinked path loads
This commit is contained in:
1 parent
46a21592d3
commit
290ba11388
17 files changed
+289
-217
No files matched your search
@@ -26,7 +26,7 @@ use super::local;
|
||||
use crate::{apic::rdtsc, output};
|
||||
use alloc::vec::Vec;
|
||||
use stela_compiler::{
|
||||
Anchor, GuestState,
|
||||
Anchor, GuestAddress, GuestState,
|
||||
block::ExitKind,
|
||||
x86_64::{
|
||||
chain::{self, ChainControl, ExitWord, LookupEntry, LookupTable},
|
||||
@@ -178,10 +178,12 @@ fn ring_code(ring: Ring, base: u64, table: &mut LookupTable, key: u64) -> Option
|
||||
let next = (block + 1) % BLOCKS;
|
||||
let at = block_at(block);
|
||||
let tail = tail_at(block);
|
||||
// The block: `movabs rax, next pc; jmp tail`, as a direct exit.
|
||||
let mut exit = alloc::vec![0x48, 0xb8];
|
||||
exit.extend(pc(next).to_le_bytes());
|
||||
exit.extend(chain::jump(at + exit.len() as u64, tail)?);
|
||||
// The block: a direct exit to the next PC whose patch point jumps
|
||||
// to its tail, as a linked one does.
|
||||
let mut exit = chain::direct_exit_at(at, GuestAddress(pc(next)), word);
|
||||
let point = chain::direct_patch(at);
|
||||
let offset = (point - at) as usize;
|
||||
exit[offset..offset + chain::PATCH_BYTES].copy_from_slice(&chain::jump(point, tail)?);
|
||||
put(BLOCKS_AT + block * STRIDE, &exit);
|
||||
let tail_code = match ring {
|
||||
Ring::Near => chain::thunk(
|
||||
@@ -189,13 +191,13 @@ fn ring_code(ring: Ring, base: u64, table: &mut LookupTable, key: u64) -> Option
|
||||
1,
|
||||
next as u32,
|
||||
block_at(next),
|
||||
chain::Fallback::Return(word),
|
||||
chain::unlinked_return(point),
|
||||
)?,
|
||||
Ring::Cell => {
|
||||
let current = table.find(table.slot(pc(next), key), pc(next), key);
|
||||
let cell = table.link_cell(tail, pc(next), Some(current));
|
||||
let probe = base + (DISPATCHER + chain::CELL_PROBE_ENTRY) as u64;
|
||||
chain::cell_tail(tail, cell, word, 1, probe)?.to_vec()
|
||||
chain::cell_tail(tail, cell, pc(next), word, 1, probe)?.to_vec()
|
||||
}
|
||||
Ring::Table => {
|
||||
let probe = base + (DISPATCHER + chain::PROBE_ENTRY) as u64;
|
||||
|
||||
@@ -1076,8 +1076,6 @@ fn system_profile_has_one_source_bound_exit_and_rejects_memory_prefixes() {
|
||||
const UDF: u32 = 0x0000_0042;
|
||||
const IRG: u32 = 0x9ac0_1000;
|
||||
const LDG: u32 = 0xd960_0000;
|
||||
const MOV_RAX_IMMEDIATE: [u8; 2] = [0x48, 0xb8];
|
||||
const MOV_EDX_IMMEDIATE: u8 = 0xba;
|
||||
for word in [SVC, UDF, IRG, LDG] {
|
||||
let source: Vec<_> = [ADD, word, ADD]
|
||||
.into_iter()
|
||||
@@ -1099,15 +1097,12 @@ fn system_profile_has_one_source_bound_exit_and_rejects_memory_prefixes() {
|
||||
)
|
||||
.unwrap();
|
||||
let at = compiled.native.source_map.last().unwrap().1;
|
||||
let mut expected = MOV_RAX_IMMEDIATE.to_vec();
|
||||
expected.extend(instruction.instruction_pc.0.to_le_bytes());
|
||||
expected.push(MOV_EDX_IMMEDIATE);
|
||||
expected.extend((ExitKind::System as u32).to_le_bytes());
|
||||
// The patch point's jump to the return after it, the return, the pad.
|
||||
expected.extend(crate::x86_64::chain::RET_ROOM);
|
||||
expected.extend(crate::x86_64::chain::EXIT_RETURN);
|
||||
expected.extend([0xcc; 4]);
|
||||
assert_eq!(&compiled.native.x86_64_code[at..], expected);
|
||||
// The patch point, then the PC, the word and the return.
|
||||
let expected = crate::x86_64::chain::direct_exit_word(
|
||||
instruction.instruction_pc,
|
||||
ExitKind::System as u32,
|
||||
);
|
||||
assert_eq!(compiled.native.x86_64_code[at..], expected);
|
||||
}
|
||||
// Ordinary RAM, exclusives and legacy FP/SIMD cannot bypass shared owners.
|
||||
for word in [0xf940_0000_u32, 0xf900_0000, 0xc85f_fc00, 0x1e20_2800] {
|
||||
@@ -1150,10 +1145,7 @@ fn half_division_continues_its_block_on_every_target() {
|
||||
.unwrap()
|
||||
};
|
||||
let exit = |pc: GuestAddress, kind: ExitKind| {
|
||||
let mut exit = vec![0x48, 0xb8]; // MOVABS RAX,PC
|
||||
exit.extend(pc.0.to_le_bytes());
|
||||
exit.extend(crate::x86_64::chain::exit_word(kind as u32));
|
||||
exit
|
||||
crate::x86_64::chain::direct_exit_word(pc, kind as u32).to_vec()
|
||||
};
|
||||
// VDIVPH XMM0,XMM0,XMM1 with EVEX.
|
||||
let native = |code: &[u8]| {
|
||||
|
||||
@@ -20,22 +20,21 @@ pub(super) fn exit_kind(code: &mut Code, kind: ExitKind) {
|
||||
exit_kind_with(&mut code.asm(), kind);
|
||||
}
|
||||
|
||||
/// `mov edx,word`, the patch point's `jmp` to the return after it, and the
|
||||
/// return to the engine: the exit to the target in RAX, unpadded
|
||||
/// (`chain::dynamic_exit`).
|
||||
/// The patch point, which falls into the rest while unlinked, `mov
|
||||
/// edx,word` and the return to the engine: the exit to the target in RAX,
|
||||
/// unpadded (`chain::dynamic_exit`).
|
||||
pub(super) fn exit_kind_with(asm: &mut Asm, word: impl Into<ExitWord>) {
|
||||
use super::chain::{DYNAMIC_EXIT_BYTES, EXIT_RETURN_BYTES, PATCH_BYTES, RET_ROOM};
|
||||
exit_point(asm);
|
||||
// MOV EDX zero-extends to the ABI's u64.
|
||||
asm.fixed(&super::chain::word_return(word.into().encoded()));
|
||||
}
|
||||
|
||||
/// The start of every exit: its patch point with the NOP after it that the
|
||||
/// pad before it takes where the code lands (`chain::exit_point`).
|
||||
fn exit_point(asm: &mut Asm) {
|
||||
// An exit hands the engine the state block, which must be current.
|
||||
asm.refuse_dirty_homes("a block exit");
|
||||
// MOV EDX zero-extends to the ABI's u64.
|
||||
asm.mov_imm32(Gpr::Rdx, word.into().encoded());
|
||||
asm.fixed(&RET_ROOM);
|
||||
super::chain::exit_return(asm);
|
||||
// The pad that makes the patch point patchable where the code lands
|
||||
// (`chain::dynamic_patch`).
|
||||
for _ in 5 + PATCH_BYTES + EXIT_RETURN_BYTES..DYNAMIC_EXIT_BYTES {
|
||||
asm.int3();
|
||||
}
|
||||
asm.fixed(&super::chain::exit_point(0));
|
||||
}
|
||||
|
||||
/// Pad the exits of `code`, a block's whose direct exits are `exits`, for
|
||||
@@ -68,14 +67,16 @@ pub(crate) fn direct_exit(code: &mut Code, target: GuestAddress, kind: ExitKind)
|
||||
direct_exit_with(&mut code.asm(), target, kind);
|
||||
}
|
||||
|
||||
/// `movabs rax,target` and the exit of `word`, after its empty link slot in
|
||||
/// code whose exits keep one.
|
||||
/// The exit of `word` to `target`, after its empty link slot in code whose
|
||||
/// exits keep one: the patch point, then what only the unlinked path runs,
|
||||
/// `movabs rax,target`, `mov edx,word` and the return.
|
||||
pub(super) fn direct_exit_with(asm: &mut Asm, target: GuestAddress, word: impl Into<ExitWord>) {
|
||||
let word = word.into();
|
||||
let slot = super::chain::link_slot(asm.linked_block(), target, word.kind);
|
||||
super::chain::empty_link(asm, slot);
|
||||
exit_point(asm);
|
||||
asm.guest_imm64(Gpr::Rax, target);
|
||||
exit_kind_with(asm, word);
|
||||
asm.fixed(&super::chain::word_return(word.encoded()));
|
||||
}
|
||||
|
||||
fn conditional_exit(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::super::chain::{DIRECT_EXIT_BYTES, DYNAMIC_EXIT_BYTES};
|
||||
use super::super::chain::DIRECT_EXIT_BYTES;
|
||||
use super::*;
|
||||
use crate::block::{SynchronousTrap, TrapKind};
|
||||
use alloc::vec;
|
||||
@@ -1977,9 +1977,9 @@ fn conditional_exits_branch_over_the_fallthrough_exit() {
|
||||
assert_eq!(code[..6], [0x0f, 0x85, DIRECT_EXIT_BYTES as u8, 0, 0, 0]);
|
||||
assert_eq!(code[6..6 + DIRECT_EXIT_BYTES], fallthrough);
|
||||
assert_eq!(code[6 + DIRECT_EXIT_BYTES..], taken);
|
||||
// The branch, then MOVABS, MOV, the patch point, the return (one
|
||||
// emission) and four INT3s each.
|
||||
assert_eq!(code.instructions(), 17);
|
||||
// The branch, then each exit's point (one emission), MOVABS, and its
|
||||
// word and return (one emission).
|
||||
assert_eq!(code.instructions(), 7);
|
||||
}
|
||||
|
||||
#[cfg(all(target_arch = "x86_64", any(target_os = "macos", target_os = "linux")))]
|
||||
@@ -3003,9 +3003,7 @@ fn clock_exit_requests_runtime_service_without_counter_or_guest_writes() {
|
||||
Some(&(pc, Terminator::Clock(request))),
|
||||
request.next_pc,
|
||||
);
|
||||
let mut expected = vec![0x48, 0xb8];
|
||||
expected.extend(pc.0.to_le_bytes());
|
||||
expected.extend(super::super::chain::exit_word(7));
|
||||
let expected = super::super::chain::direct_exit_word(pc, 7);
|
||||
// No RDTSC, host helper address, guessed counter, destination write
|
||||
// or placeholder ISB: only the checked runtime service may complete.
|
||||
assert_eq!(native.x86_64_code, expected);
|
||||
@@ -3045,9 +3043,7 @@ fn pac_helper_exit_has_no_guest_writes_or_embedded_service_address() {
|
||||
Some(&(pc, Terminator::PointerAuthentication(request))),
|
||||
request.next_pc,
|
||||
);
|
||||
let mut expected = vec![0x48, 0xb8];
|
||||
expected.extend(pc.0.to_le_bytes());
|
||||
expected.extend(super::super::chain::exit_word(6));
|
||||
let expected = super::super::chain::direct_exit_word(pc, 6);
|
||||
// Only the SysV return registers RAX/RDX are touched, including for
|
||||
// RETAB: the runtime must keep signed LR while authenticating target.
|
||||
assert_eq!(native.x86_64_code, expected);
|
||||
@@ -3083,8 +3079,8 @@ fn ordinary_native_prefix_is_preserved_before_pac_boundary() {
|
||||
assert_eq!(native.source_map.len(), 2);
|
||||
assert_eq!(native.source_map[1].0, pc);
|
||||
assert_eq!(
|
||||
&native.x86_64_code[native.x86_64_code.len() - DYNAMIC_EXIT_BYTES..],
|
||||
super::super::chain::exit_word(6)
|
||||
native.x86_64_code[native.x86_64_code.len() - DIRECT_EXIT_BYTES..],
|
||||
super::super::chain::direct_exit_word(pc, 6)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3107,9 +3103,8 @@ fn trap_exit_uses_instruction_pc_and_only_native_return_scratch_registers() {
|
||||
Some(&(pc, Terminator::Trap(trap))),
|
||||
GuestAddress(pc.0 + 4),
|
||||
);
|
||||
let mut expected = vec![0x48, 0xb8]; // movabs rax,instruction_pc
|
||||
expected.extend(pc.0.to_le_bytes());
|
||||
expected.extend(super::super::chain::exit_word(5)); // mov edx,Trap; ret
|
||||
// The point, movabs rax,instruction_pc, mov edx,Trap and the return.
|
||||
let expected = super::super::chain::direct_exit_word(pc, 5);
|
||||
assert_eq!(native.x86_64_code, expected);
|
||||
assert_eq!(native.source_map, [(pc, 0)]);
|
||||
}
|
||||
|
||||
@@ -92,8 +92,9 @@ use alloc::{
|
||||
};
|
||||
use core::sync::atomic::{AtomicU32, AtomicU64, Ordering, fence};
|
||||
|
||||
/// Bytes in a direct exit: `movabs rax, target` and a [`DYNAMIC_EXIT_BYTES`]
|
||||
/// exit.
|
||||
/// Bytes in a direct exit: its patch point with the pads around it
|
||||
/// ([`EXIT_POINT_BYTES`]), `movabs rax, target`, `mov edx, word` and the
|
||||
/// [`EXIT_RETURN`]. Only the unlinked path runs past the point.
|
||||
pub const DIRECT_EXIT_BYTES: usize = 10 + DYNAMIC_EXIT_BYTES;
|
||||
/// Bytes of a `jmp rel32`: what an install writes over the start of a
|
||||
/// direct exit before the code is reachable.
|
||||
@@ -110,9 +111,9 @@ pub const LINK_BODY_BYTES: usize = 29;
|
||||
pub const POLLED_LINK_BODY_BYTES: usize = LINK_BODY_BYTES + 10;
|
||||
/// Bytes of the largest [`refund`] of a link that found no budget left.
|
||||
const REFUND_BYTES: usize = 7;
|
||||
/// Bytes in the largest [`thunk`]: the body, the refund and a return's
|
||||
/// `mov edx` and [`EXIT_RETURN`].
|
||||
pub const THUNK_BYTES: usize = POLLED_LINK_BODY_BYTES + REFUND_BYTES + 5 + EXIT_RETURN_BYTES;
|
||||
/// Bytes in the largest [`thunk`]: the body, the refund and the jump to
|
||||
/// the exit.
|
||||
pub const THUNK_BYTES: usize = POLLED_LINK_BODY_BYTES + REFUND_BYTES + JUMP_BYTES;
|
||||
/// Bytes of the link slot before an exit in its page ([`link_slot`]): the
|
||||
/// pad, the body, which starts at the patch point, and the refund, without
|
||||
/// and with the gate.
|
||||
@@ -120,10 +121,16 @@ pub const LINK_BYTES: usize = MAX_PAD + LINK_BODY_BYTES + REFUND_BYTES;
|
||||
pub const POLLED_LINK_BYTES: usize = MAX_PAD + POLLED_LINK_BODY_BYTES + REFUND_BYTES;
|
||||
/// The span within which a link needs no translation of its own.
|
||||
const LINK_PAGE: u64 = 4096;
|
||||
/// Bytes in an exit to a dynamic target in RAX: `mov edx, kind`, a pad,
|
||||
/// then the patch point, a `jmp rel32` ([`dynamic_patch`]), and the
|
||||
/// [`EXIT_RETURN`] it falls into while unlinked.
|
||||
pub const DYNAMIC_EXIT_BYTES: usize = 5 + MAX_PAD + PATCH_BYTES + EXIT_RETURN_BYTES;
|
||||
/// Bytes in an exit to a dynamic target in RAX: its patch point with the
|
||||
/// pads around it ([`EXIT_POINT_BYTES`], [`dynamic_patch`]), then
|
||||
/// `mov edx, kind` and the [`EXIT_RETURN`], which only the unlinked path
|
||||
/// runs: a linked point jumps to a tail that sets EDX where it needs it.
|
||||
pub const DYNAMIC_EXIT_BYTES: usize = EXIT_POINT_BYTES + 5 + EXIT_RETURN_BYTES;
|
||||
/// Bytes every exit starts with: a NOP of the pad that makes its patch
|
||||
/// point patchable where it lies ([`patch_pad`]), the point, and a NOP of
|
||||
/// the rest of [`MAX_PAD`], so what follows lies at one offset of the exit
|
||||
/// however it is padded (a target's guest address field among it).
|
||||
pub const EXIT_POINT_BYTES: usize = MAX_PAD + PATCH_BYTES;
|
||||
/// [`crate::GuestState::exit_rsp`], from RDI.
|
||||
pub const EXIT_RSP_OFFSET: i32 = core::mem::offset_of!(crate::GuestState, exit_rsp) as i32;
|
||||
/// Bytes of [`EXIT_RETURN`].
|
||||
@@ -145,9 +152,9 @@ pub const EXIT_RSP_STORE: [u8; 12] = {
|
||||
0x4c, 0x8d, 0x5c, 0x24, 0xf8, 0x4c, 0x89, 0x9f, d[0], d[1], d[2], d[3],
|
||||
]
|
||||
};
|
||||
/// An unlinked exit's patch point: a `jmp rel32` to the [`EXIT_RETURN`]
|
||||
/// right after it.
|
||||
pub const RET_ROOM: [u8; PATCH_BYTES] = [0xe9, 0, 0, 0, 0];
|
||||
/// An unlinked exit's patch point: a 5-byte NOP into the return to the
|
||||
/// engine after it ([`unlinked_return`]).
|
||||
pub const RET_ROOM: [u8; PATCH_BYTES] = NOP5;
|
||||
/// A linked slot's patch point ([`link`]): the first bytes of its body's
|
||||
/// first instruction, `mov rcx, [rdi + anchors + 8 * Control]`, the same
|
||||
/// in every slot.
|
||||
@@ -2229,7 +2236,7 @@ struct ThunkPlan {
|
||||
retired: u32,
|
||||
block: u32,
|
||||
entry: u64,
|
||||
fallback: Fallback,
|
||||
exit: u64,
|
||||
}
|
||||
|
||||
const _: () = assert!(core::mem::size_of::<LookupEntry>() == 32);
|
||||
@@ -2239,10 +2246,11 @@ const ENTRY_CODE: i32 = core::mem::offset_of!(LookupEntry, entry) as i32;
|
||||
const ENTRY_BLOCK: i32 = core::mem::offset_of!(LookupEntry, block) as i32;
|
||||
const ENTRY_TARGETS: i32 = core::mem::offset_of!(LookupEntry, targets) as i32;
|
||||
|
||||
/// A direct exit to `target` with `kind` as a block emits it: MOVABS RAX
|
||||
/// with the target, then the dynamic exit, unpadded. Code that ends up at
|
||||
/// an address its patch point does not suit is aligned before it runs
|
||||
/// ([`align_direct_exit`]); [`is_direct_exit`] accepts every padding.
|
||||
/// A direct exit to `target` with `kind` as a block emits it, unpadded:
|
||||
/// the patch point, MOVABS RAX with the target, MOV EDX with the word and
|
||||
/// the return. Code that ends up at an address its patch point does not
|
||||
/// suit is aligned before it runs ([`align_direct_exit`]);
|
||||
/// [`is_direct_exit`] accepts every padding.
|
||||
pub fn direct_exit(target: GuestAddress, kind: impl Into<ExitWord>) -> [u8; DIRECT_EXIT_BYTES] {
|
||||
direct_exit_padded(target, kind.into().encoded(), 0)
|
||||
}
|
||||
@@ -2254,38 +2262,75 @@ pub fn direct_exit_at(
|
||||
target: GuestAddress,
|
||||
kind: impl Into<ExitWord>,
|
||||
) -> [u8; DIRECT_EXIT_BYTES] {
|
||||
direct_exit_padded(target, kind.into().encoded(), patch_pad(at + 15))
|
||||
direct_exit_padded(target, kind.into().encoded(), patch_pad(at))
|
||||
}
|
||||
|
||||
/// Where a direct exit's MOVABS RAX starts, however it is padded.
|
||||
const DIRECT_TARGET: usize = EXIT_POINT_BYTES;
|
||||
const MOVABS_RAX: [u8; 2] = [0x48, 0xb8];
|
||||
|
||||
fn direct_exit_padded(target: GuestAddress, word: u32, pad: usize) -> [u8; DIRECT_EXIT_BYTES] {
|
||||
const MOVABS_RAX: [u8; 2] = [0x48, 0xb8];
|
||||
let mut bytes = [0; DIRECT_EXIT_BYTES];
|
||||
bytes[..2].copy_from_slice(&MOVABS_RAX);
|
||||
bytes[2..10].copy_from_slice(&target.0.to_le_bytes());
|
||||
bytes[10..].copy_from_slice(&exit_word_bytes(word, pad));
|
||||
bytes[..EXIT_POINT_BYTES].copy_from_slice(&exit_point(pad));
|
||||
bytes[DIRECT_TARGET..DIRECT_TARGET + 2].copy_from_slice(&MOVABS_RAX);
|
||||
bytes[DIRECT_TARGET + 2..DIRECT_TARGET + 10].copy_from_slice(&target.0.to_le_bytes());
|
||||
bytes[DIRECT_TARGET + 10..].copy_from_slice(&word_return(word));
|
||||
bytes
|
||||
}
|
||||
|
||||
/// MOV EDX with `word`, a NOP of `pad` bytes, then the patch point, which
|
||||
/// jumps to the [`EXIT_RETURN`] right after it, and INT3s to the exit's end
|
||||
/// ([`super::block::exit_kind_with`]).
|
||||
fn exit_word_bytes(word: u32, pad: usize) -> [u8; DYNAMIC_EXIT_BYTES] {
|
||||
/// A NOP of `pad` bytes, the unlinked patch point ([`RET_ROOM`]) and a NOP
|
||||
/// of the rest of [`MAX_PAD`]: the start of every exit.
|
||||
pub(crate) const fn exit_point(pad: usize) -> [u8; EXIT_POINT_BYTES] {
|
||||
let mut bytes = [0; EXIT_POINT_BYTES];
|
||||
let mut at = 0;
|
||||
while at < pad {
|
||||
bytes[at] = NOPS[pad][at];
|
||||
at += 1;
|
||||
}
|
||||
while at < pad + PATCH_BYTES {
|
||||
bytes[at] = RET_ROOM[at - pad];
|
||||
at += 1;
|
||||
}
|
||||
let rest = MAX_PAD - pad;
|
||||
while at < EXIT_POINT_BYTES {
|
||||
bytes[at] = NOPS[rest][at - pad - PATCH_BYTES];
|
||||
at += 1;
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
/// MOV EDX with `word` and the [`EXIT_RETURN`]: how every exit ends.
|
||||
pub(crate) const fn word_return(word: u32) -> [u8; 5 + EXIT_RETURN_BYTES] {
|
||||
const MOV_EDX: u8 = 0xba;
|
||||
let mut bytes = [0xcc; DYNAMIC_EXIT_BYTES];
|
||||
let mut bytes = [0; 5 + EXIT_RETURN_BYTES];
|
||||
bytes[0] = MOV_EDX;
|
||||
bytes[1..5].copy_from_slice(&word.to_le_bytes());
|
||||
bytes[5..5 + pad].copy_from_slice(NOPS[pad]);
|
||||
let point = 5 + pad;
|
||||
bytes[point..point + PATCH_BYTES].copy_from_slice(&RET_ROOM);
|
||||
let back = point + PATCH_BYTES;
|
||||
bytes[back..back + EXIT_RETURN_BYTES].copy_from_slice(&EXIT_RETURN);
|
||||
let word = word.to_le_bytes();
|
||||
let mut at = 0;
|
||||
while at < 4 {
|
||||
bytes[1 + at] = word[at];
|
||||
at += 1;
|
||||
}
|
||||
while at < 4 + EXIT_RETURN_BYTES {
|
||||
bytes[1 + at] = EXIT_RETURN[at - 4];
|
||||
at += 1;
|
||||
}
|
||||
bytes
|
||||
}
|
||||
|
||||
/// The unpadded dynamic exit of the encoded `word` ([`ExitWord::encoded`]).
|
||||
/// The patch point padded by `pad`, MOV EDX with `word` and the return:
|
||||
/// the dynamic exit ([`super::block::exit_kind_with`]).
|
||||
fn exit_word_bytes(word: u32, pad: usize) -> [u8; DYNAMIC_EXIT_BYTES] {
|
||||
let mut bytes = [0; DYNAMIC_EXIT_BYTES];
|
||||
bytes[..EXIT_POINT_BYTES].copy_from_slice(&exit_point(pad));
|
||||
bytes[EXIT_POINT_BYTES..].copy_from_slice(&word_return(word));
|
||||
bytes
|
||||
}
|
||||
|
||||
/// The unpadded direct exit to `pc` of the encoded `word`
|
||||
/// ([`ExitWord::encoded`]).
|
||||
#[cfg(test)]
|
||||
pub(crate) fn exit_word(word: u32) -> [u8; DYNAMIC_EXIT_BYTES] {
|
||||
exit_word_bytes(word, 0)
|
||||
pub(crate) fn direct_exit_word(pc: GuestAddress, word: u32) -> [u8; DIRECT_EXIT_BYTES] {
|
||||
direct_exit_padded(pc, word, 0)
|
||||
}
|
||||
|
||||
/// The return to the engine ([`EXIT_RETURN`]).
|
||||
@@ -2329,14 +2374,23 @@ pub fn patch_word(word: u64, point: u64, instruction: [u8; PATCH_BYTES]) -> u64
|
||||
}
|
||||
|
||||
/// The patch point of the dynamic exit at host address `at`, aligned for
|
||||
/// it: its `ret`, which a lookup's jump replaces.
|
||||
/// it: what a lookup's jump replaces, the exit's first instruction but its
|
||||
/// pad.
|
||||
pub const fn dynamic_patch(at: u64) -> u64 {
|
||||
at + 5 + patch_pad(at + 5) as u64
|
||||
at + patch_pad(at) as u64
|
||||
}
|
||||
|
||||
/// The patch point of the direct exit at host address `at`, aligned for it.
|
||||
pub const fn direct_patch(at: u64) -> u64 {
|
||||
dynamic_patch(at + 10)
|
||||
dynamic_patch(at)
|
||||
}
|
||||
|
||||
/// Where the exit whose patch point is `point` returns to the engine
|
||||
/// from while unlinked: right after the point, the target loaded (a direct
|
||||
/// exit's), the word and the return. A thunk at the point that allows no
|
||||
/// transfer goes on there.
|
||||
pub const fn unlinked_return(point: u64) -> u64 {
|
||||
point + PATCH_BYTES as u64
|
||||
}
|
||||
|
||||
/// The link slot of `bytes` at host address `at`'s patch point: the jump
|
||||
@@ -2347,7 +2401,7 @@ pub const fn slot_patch(at: u64) -> u64 {
|
||||
|
||||
/// Whether the direct exit at `offset` of `code`, which runs from host
|
||||
/// address `at`, has its patch point where a link patches it: its slot's
|
||||
/// point jumping to it when `slot` is nonzero, else its own `ret`.
|
||||
/// point jumping to it when `slot` is nonzero, else its own.
|
||||
pub fn exit_ready(code: &[u8], offset: usize, at: u64, slot: usize) -> bool {
|
||||
let site = at + offset as u64;
|
||||
match slot {
|
||||
@@ -2360,7 +2414,7 @@ pub fn exit_ready(code: &[u8], offset: usize, at: u64, slot: usize) -> bool {
|
||||
}
|
||||
|
||||
/// Whether the dynamic exit at `offset` of `code`, which runs from host
|
||||
/// address `at`, has its patch point's `ret` where a lookup patches it.
|
||||
/// address `at`, has its unlinked patch point where a lookup patches it.
|
||||
pub fn dynamic_ready(code: &[u8], offset: usize, at: u64) -> bool {
|
||||
point_holds(code, at, dynamic_patch(at + offset as u64), RET_ROOM)
|
||||
}
|
||||
@@ -2377,7 +2431,8 @@ fn point_holds(code: &[u8], at: u64, point: u64, instruction: [u8; PATCH_BYTES])
|
||||
|
||||
/// The encoded word of the dynamic exit `bytes` is, of any padding.
|
||||
fn exit_word_of(bytes: &[u8]) -> Option<u32> {
|
||||
let word = u32::from_le_bytes(bytes.get(1..5)?.try_into().ok()?);
|
||||
let field = EXIT_POINT_BYTES + 1;
|
||||
let word = u32::from_le_bytes(bytes.get(field..field + 4)?.try_into().ok()?);
|
||||
(0..=MAX_PAD)
|
||||
.any(|pad| bytes == exit_word_bytes(word, pad))
|
||||
.then_some(word)
|
||||
@@ -2400,20 +2455,38 @@ pub fn is_dynamic_exit_of_any_kind(bytes: &[u8]) -> bool {
|
||||
exit_word_of(bytes).is_some()
|
||||
}
|
||||
|
||||
/// The target and encoded word of the direct exit `bytes` are, of any
|
||||
/// padding.
|
||||
fn direct_exit_of(bytes: &[u8]) -> Option<(GuestAddress, u32)> {
|
||||
let target = bytes.get(DIRECT_TARGET + 2..DIRECT_TARGET + 10)?;
|
||||
let target = GuestAddress(u64::from_le_bytes(target.try_into().ok()?));
|
||||
let field = DIRECT_TARGET + 10 + 1;
|
||||
let word = u32::from_le_bytes(bytes.get(field..field + 4)?.try_into().ok()?);
|
||||
(0..=MAX_PAD)
|
||||
.any(|pad| bytes == direct_exit_padded(target, word, pad))
|
||||
.then_some((target, word))
|
||||
}
|
||||
|
||||
/// Whether `bytes` are a direct exit of any target, kind and padding: what
|
||||
/// ends a block whose exits are not linked.
|
||||
pub fn is_direct_exit_of_any_kind(bytes: &[u8]) -> bool {
|
||||
direct_exit_of(bytes).is_some()
|
||||
}
|
||||
|
||||
/// Pad the direct exit at `offset` of `code`, which runs at host address
|
||||
/// `at + offset`, for its patch point there. Whether it is one.
|
||||
pub fn align_direct_exit(code: &mut [u8], offset: usize, at: u64) -> bool {
|
||||
let Some(bytes) = code.get_mut(offset..offset + DIRECT_EXIT_BYTES) else {
|
||||
return false;
|
||||
};
|
||||
let Some(word) = exit_word_of(&bytes[10..]) else {
|
||||
let Some((target, word)) = direct_exit_of(bytes) else {
|
||||
return false;
|
||||
};
|
||||
if bytes[..2] != [0x48, 0xb8] {
|
||||
return false;
|
||||
}
|
||||
let pad = patch_pad(at + offset as u64 + 15);
|
||||
bytes[10..].copy_from_slice(&exit_word_bytes(word, pad));
|
||||
bytes.copy_from_slice(&direct_exit_padded(
|
||||
target,
|
||||
word,
|
||||
patch_pad(at + offset as u64),
|
||||
));
|
||||
true
|
||||
}
|
||||
|
||||
@@ -2426,7 +2499,7 @@ pub fn align_dynamic_exit(code: &mut [u8], offset: usize, at: u64) -> bool {
|
||||
let Some(word) = exit_word_of(bytes) else {
|
||||
return false;
|
||||
};
|
||||
bytes.copy_from_slice(&exit_word_bytes(word, patch_pad(at + offset as u64 + 5)));
|
||||
bytes.copy_from_slice(&exit_word_bytes(word, patch_pad(at + offset as u64)));
|
||||
true
|
||||
}
|
||||
|
||||
@@ -2562,31 +2635,22 @@ impl SlotLink {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where a link's body goes when the control allows no transfer.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Fallback {
|
||||
/// Return to the engine with this word in EDX, which the gate may have
|
||||
/// clobbered: the exit has loaded RAX already, as for a thunk its patch
|
||||
/// point jumps to.
|
||||
Return(ExitWord),
|
||||
/// Jump to the exit at this host address, as for a link slot.
|
||||
Jump(u64),
|
||||
}
|
||||
|
||||
/// A thunk at host address `at` that chains a block of `retired`
|
||||
/// instructions into block `block`, whose code is at `entry`, or takes
|
||||
/// `fallback` when the control allows no transfer: what an exit's patch
|
||||
/// point jumps to when its slot's body is used or it has none. It always
|
||||
/// looks for an interruption. `None` when `entry` or the fallback is out of
|
||||
/// a 32-bit jump's reach or an operand exceeds its 32-bit field.
|
||||
pub fn thunk(at: u64, retired: u32, block: u32, entry: u64, fallback: Fallback) -> Option<Vec<u8>> {
|
||||
/// instructions into block `block`, whose code is at `entry`, or jumps to
|
||||
/// `exit` when the control allows no transfer: what an exit's patch point
|
||||
/// jumps to when its slot's body is used or it has none. `exit` is the
|
||||
/// exit's own unlinked path ([`unlinked_return`]), or for a slot's point
|
||||
/// the exit after the slot. It always looks for an interruption. `None`
|
||||
/// when `entry` or `exit` is out of a 32-bit jump's reach or an operand
|
||||
/// exceeds its 32-bit field.
|
||||
pub fn thunk(at: u64, retired: u32, block: u32, entry: u64, exit: u64) -> Option<Vec<u8>> {
|
||||
thunk_with_policy(
|
||||
ThunkPlan {
|
||||
at,
|
||||
retired,
|
||||
block,
|
||||
entry,
|
||||
fallback,
|
||||
exit,
|
||||
},
|
||||
Interruption::Epoch,
|
||||
)
|
||||
@@ -2597,24 +2661,16 @@ fn thunk_with_policy(plan: ThunkPlan, policy: Interruption) -> Option<Vec<u8>> {
|
||||
retired,
|
||||
block,
|
||||
entry,
|
||||
fallback,
|
||||
exit,
|
||||
} = plan;
|
||||
link_body(
|
||||
at,
|
||||
true,
|
||||
retired,
|
||||
block,
|
||||
entry,
|
||||
Fall::Thunk(fallback),
|
||||
policy,
|
||||
)
|
||||
link_body(at, true, retired, block, entry, Fall::Thunk(exit), policy)
|
||||
}
|
||||
|
||||
/// Where a link's body goes when the control allows no transfer.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Fall {
|
||||
/// A thunk's [`Fallback`], after its body.
|
||||
Thunk(Fallback),
|
||||
/// A thunk's: a jump to the exit at this host address, after its body.
|
||||
Thunk(u64),
|
||||
/// A slot's: the exit at this host address, a few bytes after the body.
|
||||
After(u64),
|
||||
}
|
||||
@@ -2651,22 +2707,13 @@ fn link_body(
|
||||
let refunded = refunded.into_bytes();
|
||||
// A spent budget's refund, then where an interruption goes.
|
||||
let (spent, fallen) = match fall {
|
||||
Fall::Thunk(fallback) => {
|
||||
Fall::Thunk(exit) => {
|
||||
let spent = code.len();
|
||||
code.asm().fixed(&refunded);
|
||||
let fallen = code.len();
|
||||
match fallback {
|
||||
Fallback::Return(word) => {
|
||||
let mut asm = code.asm();
|
||||
asm.mov_imm32(Rdx, word.encoded());
|
||||
exit_return(&mut asm);
|
||||
}
|
||||
Fallback::Jump(exit) => {
|
||||
let back = code.asm().jmp();
|
||||
let back_distance = relative(at + back.end() as u64, exit)?;
|
||||
code.patch(back.offset(), back_distance.to_le_bytes());
|
||||
}
|
||||
}
|
||||
let back = code.asm().jmp();
|
||||
let back_distance = relative(at + back.end() as u64, exit)?;
|
||||
code.patch(back.offset(), back_distance.to_le_bytes());
|
||||
(spent, fallen)
|
||||
}
|
||||
// The refund ends the slot and falls into the exit after it; the
|
||||
@@ -2835,8 +2882,9 @@ pub const PROBE_ENTRY: usize = CELL_PROBE_BYTES;
|
||||
pub const STAMPING_PROBE_ENTRY: usize = PROBE_ENTRY + 640;
|
||||
/// Bytes in a [`lookup_tail`]: `mov edx, word` and `jmp probe`.
|
||||
pub const TAIL_BYTES: usize = 5 + 5;
|
||||
/// Bytes in a [`cell_tail`].
|
||||
pub const CELL_TAIL_BYTES: usize = 10 + TAIL_BYTES;
|
||||
/// Bytes in a [`cell_tail`]: the target's and the cell's MOVABS and a
|
||||
/// [`lookup_tail`]'s plain tail.
|
||||
pub const CELL_TAIL_BYTES: usize = 10 + 10 + TAIL_BYTES;
|
||||
|
||||
/// The probes the per-site tails of the exits of branches that set `btype`
|
||||
/// jump to ([`lookup_tail`]): the target in RAX, the tail's word in EDX.
|
||||
@@ -3087,9 +3135,9 @@ fn stamp(asm: &mut crate::x86_64::asm::Asm) {
|
||||
asm.bind(kept);
|
||||
}
|
||||
|
||||
/// Bytes in a [`stamped_tail`]: 130 at most, with a 32-bit displacement
|
||||
/// Bytes in a [`stamped_tail`]: 170 at most, with a 32-bit displacement
|
||||
/// for the stamp of a cell numbered 8 or above.
|
||||
pub const STAMPED_TAIL_BYTES: usize = 144;
|
||||
pub const STAMPED_TAIL_BYTES: usize = 176;
|
||||
|
||||
/// The tail at host address `at` of a hot direct exit to `target` out of
|
||||
/// its page, whose stamped cell `number` at `cell` held `block`, entered at
|
||||
@@ -3225,6 +3273,7 @@ fn stamped_with_policy(
|
||||
let mut asm = code.asm();
|
||||
asm.bind(exhausted);
|
||||
refund(&mut asm, ran);
|
||||
asm.mov_imm64(Rax, target);
|
||||
asm.mov_imm32(Rdx, word.encoded() | ran << ExitWord::RAN_SHIFT);
|
||||
exit_return(&mut asm);
|
||||
// A miss counts its cause and takes the cell tail.
|
||||
@@ -3245,7 +3294,7 @@ fn stamped_with_policy(
|
||||
for patch in to_tail {
|
||||
crate::x86_64::asm::resolve(&mut code, patch, tail_at);
|
||||
}
|
||||
let cell_code = cell_tail(at + tail_at as u64, cell, word, ran, probe)?;
|
||||
let cell_code = cell_tail(at + tail_at as u64, cell, target, word, ran, probe)?;
|
||||
code.asm().fixed(&cell_code);
|
||||
let padding = STAMPED_TAIL_BYTES.checked_sub(code.len())?;
|
||||
for _ in 0..padding {
|
||||
@@ -3589,23 +3638,42 @@ fn fill_jump_cache(asm: &mut crate::x86_64::asm::Asm) {
|
||||
asm.bind(none);
|
||||
}
|
||||
|
||||
/// The tail at host address `at` of a direct exit out of its page, whose
|
||||
/// target the cell at `cell` holds ([`LookupTable::link_cell`]): the cell
|
||||
/// probe at `probe` ([`CELL_PROBE_ENTRY`]), then the jump or the exit as in
|
||||
/// a [`lookup_tail`]. `None` as for a [`lookup_tail`].
|
||||
/// The tail at host address `at` of a direct exit to `target` out of its
|
||||
/// page, whose target the cell at `cell` holds ([`LookupTable::link_cell`]):
|
||||
/// the target in RAX, which the exit's patch point did not load, then the
|
||||
/// cell probe at `probe` ([`CELL_PROBE_ENTRY`]), and the jump or the exit as
|
||||
/// in a [`lookup_tail`]. `None` as for a [`lookup_tail`].
|
||||
pub fn cell_tail(
|
||||
at: u64,
|
||||
cell: u64,
|
||||
target: u64,
|
||||
word: impl Into<ExitWord>,
|
||||
ran: u32,
|
||||
probe: u64,
|
||||
) -> Option<[u8; CELL_TAIL_BYTES]> {
|
||||
let mut code = Code::new();
|
||||
code.asm().mov_imm64(Rax, target);
|
||||
code.asm().mov_imm64(R9, cell);
|
||||
tail(&mut code, at, word.into(), ran, probe)?;
|
||||
code.try_into().ok()
|
||||
}
|
||||
|
||||
/// A [`cell_tail`] that takes the target in RAX as it is: for tests that
|
||||
/// send other targets through a cell.
|
||||
#[cfg(test)]
|
||||
fn rax_cell_tail(
|
||||
at: u64,
|
||||
cell: u64,
|
||||
word: impl Into<ExitWord>,
|
||||
ran: u32,
|
||||
probe: u64,
|
||||
) -> Option<Vec<u8>> {
|
||||
let mut code = Code::new();
|
||||
code.asm().mov_imm64(R9, cell);
|
||||
tail(&mut code, at, word.into(), ran, probe)?;
|
||||
Some(code.into_bytes())
|
||||
}
|
||||
|
||||
/// Where a [`predicted_tail`]'s direct exit to its predicted target begins:
|
||||
/// its patch point is [`direct_patch`] of the tail's address plus this.
|
||||
pub const PREDICTED_EXIT: usize = 15;
|
||||
@@ -3716,7 +3784,7 @@ pub fn dynamic_exit(kind: ExitKind) -> [u8; DYNAMIC_EXIT_BYTES] {
|
||||
/// [`dynamic_exit`] for host address `at`, its patch point
|
||||
/// ([`dynamic_patch`]) patchable there.
|
||||
pub fn dynamic_exit_at(at: u64, kind: ExitKind) -> [u8; DYNAMIC_EXIT_BYTES] {
|
||||
exit_word_bytes(ExitWord::from(kind).encoded(), patch_pad(at + 5))
|
||||
exit_word_bytes(ExitWord::from(kind).encoded(), patch_pad(at))
|
||||
}
|
||||
|
||||
/// `code` without the check failures that end a block, after its exits.
|
||||
@@ -4131,7 +4199,7 @@ mod tests {
|
||||
linked[start..start + linking.rest.len()].copy_from_slice(&linking.rest);
|
||||
linked[offset..offset + PATCH_BYTES].copy_from_slice(&linking.point);
|
||||
assert_eq!(linked[slot..], code[slot..]);
|
||||
// An exit without a slot is patched at its RET.
|
||||
// An exit without a slot is patched at its point.
|
||||
let (exit_point, ret) = unlinked(site, 0).unwrap();
|
||||
assert_eq!(exit_point, direct_patch(site));
|
||||
let exit = (exit_point - at) as usize;
|
||||
@@ -4140,16 +4208,17 @@ mod tests {
|
||||
}
|
||||
assert!(link(0, LINK_BYTES + 1, 3, 7, 0).is_none());
|
||||
let word = 0x1122_3344_5566_7788;
|
||||
assert_eq!(patch_word(word, 0x1003, RET_ROOM), 0x0000_0000_e966_7788);
|
||||
assert_eq!(patch_word(word, 0x1003, RET_ROOM), 0x0000_441f_0f66_7788);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thunks_count_name_and_jump_or_fall_back() {
|
||||
let word = ExitWord::from(ExitKind::Fallthrough);
|
||||
let code = thunk(0x1000, 7, 3, 0x2000, Fallback::Return(word)).unwrap();
|
||||
let _ = word;
|
||||
let code = thunk(0x1000, 7, 3, 0x2000, 0x800).unwrap();
|
||||
let mut expected = vec![0x48, 0x8b, 0x8f];
|
||||
expected.extend(824u32.to_le_bytes());
|
||||
// The gate, to the return past the refund.
|
||||
// The gate, to the jump back past the refund.
|
||||
expected.extend([0x48, 0x8b, 0x51, 0x40, 0x48, 0x3b, 0x51, 0x48, 0x75, 26]);
|
||||
// One transfer and 7 instructions, or the refund.
|
||||
expected.extend([0x48, 0x81, 0x01, 7, 0, 0, 0x80, 0x78, 13]);
|
||||
@@ -4157,25 +4226,20 @@ mod tests {
|
||||
// The jump ends 0x27 bytes into the thunk.
|
||||
expected.extend([0xe9]);
|
||||
expected.extend((0x2000i32 - 0x1027).to_le_bytes());
|
||||
// sub qword [rcx], 7, then the return.
|
||||
let refunded = POLLED_LINK_BODY_BYTES + 4;
|
||||
expected.extend([0x48, 0x83, 0x29, 7]);
|
||||
expected.push(0xba);
|
||||
expected.extend(word.encoded().to_le_bytes());
|
||||
expected.extend(EXIT_RETURN);
|
||||
assert_eq!(code, expected);
|
||||
let back = thunk(0x1000, 7, 3, 0x2000, Fallback::Jump(0x800)).unwrap();
|
||||
assert_eq!(back[..refunded], expected[..refunded]);
|
||||
assert_eq!(back.len(), refunded + JUMP_BYTES);
|
||||
assert_eq!(back[refunded], 0xe9);
|
||||
assert_eq!(code.len(), POLLED_LINK_BODY_BYTES + 4 + JUMP_BYTES);
|
||||
assert_eq!(code[..POLLED_LINK_BODY_BYTES], expected);
|
||||
// sub qword [rcx], 7, then the jump back.
|
||||
assert_eq!(code[POLLED_LINK_BODY_BYTES..][..4], [0x48, 0x83, 0x29, 7]);
|
||||
assert_eq!(code[POLLED_LINK_BODY_BYTES + 4], 0xe9);
|
||||
assert_eq!(
|
||||
back[refunded + 1..],
|
||||
(0x800i32 - 0x1000 - back.len() as i32).to_le_bytes()
|
||||
code[POLLED_LINK_BODY_BYTES + 5..],
|
||||
(0x800i32 - 0x1000 - code.len() as i32).to_le_bytes()
|
||||
);
|
||||
assert!(code.len() <= THUNK_BYTES);
|
||||
// A block of more instructions than an 8-bit refund holds.
|
||||
let long = thunk(0x1000, 200, 3, 0x2000, Fallback::Return(word)).unwrap();
|
||||
let long = thunk(0x1000, 200, 3, 0x2000, 0x800).unwrap();
|
||||
assert_eq!(long.len(), THUNK_BYTES);
|
||||
assert!(thunk(0, 0, 0, 1 << 40, Fallback::Return(word)).is_none());
|
||||
assert!(thunk(0, 0, 0, 1 << 40, 0x800).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -4703,11 +4767,21 @@ mod tests {
|
||||
assert!(lookup_tail(0x1000, ExitKind::Return, 3, 0x2000, btype).is_some());
|
||||
}
|
||||
assert!(lookup_tail(0x1000, ExitKind::Return, 3, 0x2000, 4).is_none());
|
||||
let cell = cell_tail(0x1000, 0xdead_beef_0000, ExitKind::Return, 3, 0x2000).unwrap();
|
||||
assert_eq!(&cell[..2], &[0x49, 0xb9]);
|
||||
assert_eq!(&cell[2..10], &0xdead_beef_0000u64.to_le_bytes());
|
||||
assert_eq!(&cell[10..15], &expected[..5]);
|
||||
assert_eq!(&cell[15..20], &[0xe9, 0xec, 0x0f, 0, 0]);
|
||||
let cell = cell_tail(
|
||||
0x1000,
|
||||
0xdead_beef_0000,
|
||||
0x4000,
|
||||
ExitKind::Return,
|
||||
3,
|
||||
0x2000,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(&cell[..2], &[0x48, 0xb8]);
|
||||
assert_eq!(&cell[2..10], &0x4000u64.to_le_bytes());
|
||||
assert_eq!(&cell[10..12], &[0x49, 0xb9]);
|
||||
assert_eq!(&cell[12..20], &0xdead_beef_0000u64.to_le_bytes());
|
||||
assert_eq!(&cell[20..25], &expected[..5]);
|
||||
assert_eq!(&cell[25..30], &[0xe9, 0xe2, 0x0f, 0, 0]);
|
||||
assert!(lookup_tail(0, ExitKind::Return, 0, 1 << 40, 0).is_none());
|
||||
assert!(lookup_tail(0, ExitKind::Return, 1 << 12, 0, 0).is_none());
|
||||
for btype in 0..4 {
|
||||
|
||||
@@ -85,7 +85,7 @@ fn frame(control: u64) -> Box<GuestState> {
|
||||
}
|
||||
/// The direct exit to [`TARGET`] at host address `at`, its patch point
|
||||
/// linked to a thunk right after it into `entry`, and the thunk, which
|
||||
/// returns through the exit's RAX and EDX.
|
||||
/// returns through the exit's unlinked path.
|
||||
fn exit_into_thunk(at: u64, entry: u64, policy: Interruption) -> Vec<u8> {
|
||||
let mut code = direct_exit_at(at, TARGET, ExitKind::Direct).to_vec();
|
||||
let thunk_at = at + code.len() as u64;
|
||||
@@ -98,7 +98,7 @@ fn exit_into_thunk(at: u64, entry: u64, policy: Interruption) -> Vec<u8> {
|
||||
retired: 7,
|
||||
block: 3,
|
||||
entry,
|
||||
fallback: Fallback::Return(ExitKind::Direct.into()),
|
||||
exit: unlinked_return(point),
|
||||
},
|
||||
policy,
|
||||
)
|
||||
@@ -453,7 +453,7 @@ fn an_already_checked_native_boundary_allows_one_transfer_then_stops() {
|
||||
let enter_tail = prefix.asm().jmp();
|
||||
let exit = native.address() + prefix.len() as u64;
|
||||
let at = exit + DIRECT_EXIT_BYTES as u64;
|
||||
let body = thunk(at, 7, 3, at, Fallback::Jump(exit)).unwrap();
|
||||
let body = thunk(at, 7, 3, at, exit).unwrap();
|
||||
// Golden legacy encodings identify the exact post-guard native window.
|
||||
// The first entry has already admitted a transfer with the old epoch;
|
||||
// its backedge re-enters the full, unchanged production thunk.
|
||||
@@ -578,6 +578,7 @@ fn native_interruption_gate_costs() {
|
||||
cell_tail(
|
||||
tail,
|
||||
cell,
|
||||
TARGET.0,
|
||||
ExitKind::Direct,
|
||||
7,
|
||||
probes + CELL_PROBE_ENTRY as u64,
|
||||
|
||||
@@ -271,7 +271,7 @@ fn cost(path: Path) -> (f64, f64, f64) {
|
||||
}
|
||||
_ => {
|
||||
let probe = base + (PROBES + CELL_PROBE_ENTRY) as u64;
|
||||
cell_tail(at, cell, word, RAN, probe).unwrap().to_vec()
|
||||
cell_tail(at, cell, pc, word, RAN, probe).unwrap().to_vec()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -280,7 +280,7 @@ fn cost(path: Path) -> (f64, f64, f64) {
|
||||
let number = world.table.site_cell_number(site).unwrap();
|
||||
let cell = LookupTable::cell_slot(&world.table.cells, number) as *const Slot as u64;
|
||||
let probe = base + (PROBES + CELL_PROBE_ENTRY) as u64;
|
||||
cell_tail(at, cell, word, RAN, probe).unwrap().to_vec()
|
||||
cell_tail(at, cell, pc, word, RAN, probe).unwrap().to_vec()
|
||||
}
|
||||
Path::Lookup(_) | Path::LookupRuns(..) => {
|
||||
let probe = base + (PROBES + PROBE_ENTRY) as u64;
|
||||
|
||||
@@ -407,15 +407,14 @@ fn probes_without_the_stack_transfer_as_the_probes_before_them() {
|
||||
let ran = word >> ExitWord::RAN_SHIFT;
|
||||
let kind = ExitWord::from(ExitKind::Direct);
|
||||
let fresh = match through_cell {
|
||||
true => cell_tail(
|
||||
true => rax_cell_tail(
|
||||
new.0 as u64 + 3,
|
||||
cell,
|
||||
kind,
|
||||
ran,
|
||||
new.0 as u64 + probe as u64,
|
||||
)
|
||||
.unwrap()
|
||||
.to_vec(),
|
||||
.unwrap(),
|
||||
false => lookup_tail(
|
||||
new.0 as u64 + 3,
|
||||
kind,
|
||||
|
||||
@@ -157,7 +157,7 @@ fn run_while_patching(
|
||||
}
|
||||
|
||||
/// The marker block, and thunks into it naming blocks 2 and 3.
|
||||
fn install_targets(page: &Page, fallback: Fallback) -> [u64; 2] {
|
||||
fn install_targets(page: &Page, fallback: u64) -> [u64; 2] {
|
||||
let block = page.address() + BLOCK_AT as u64;
|
||||
page.write(block, &[0xb8, 0x51, 0x51, 0, 0, 0xba, 99, 0, 0, 0, 0xc3]);
|
||||
[2, 3].map(|named| {
|
||||
@@ -180,7 +180,7 @@ fn a_slot_links_relinks_and_unlinks_while_it_runs() {
|
||||
page.write(at, &code);
|
||||
let point = slot_patch(at);
|
||||
assert_ne!(point, at);
|
||||
let thunks = install_targets(&page, Fallback::Jump(site));
|
||||
let thunks = install_targets(&page, site);
|
||||
let block = page.address() + BLOCK_AT as u64;
|
||||
let linking = link(at, POLLED_LINK_BYTES, 1, 1, block).unwrap();
|
||||
let (unlink_at, unlink) = unlinked(site, POLLED_LINK_BYTES).unwrap();
|
||||
@@ -224,8 +224,8 @@ fn an_exit_links_relinks_and_unlinks_while_it_runs() {
|
||||
for start in [0, 3, 6] {
|
||||
let site = page.address() + start;
|
||||
page.write(site, &direct_exit_at(site, TARGET, ExitKind::Direct));
|
||||
let thunks = install_targets(&page, Fallback::Return(ExitKind::Direct.into()));
|
||||
let (point, unlink) = unlinked(site, 0).unwrap();
|
||||
let thunks = install_targets(&page, unlinked_return(point));
|
||||
let seen = run_while_patching(&page, site, |step| {
|
||||
if step == 3 * ROUNDS {
|
||||
return None;
|
||||
|
||||
@@ -291,6 +291,7 @@ impl Run {
|
||||
None => cell_tail(
|
||||
at,
|
||||
cell,
|
||||
predicted_of(table, site),
|
||||
word,
|
||||
RAN,
|
||||
self.region.at(PROBES + CELL_PROBE_ENTRY),
|
||||
@@ -780,7 +781,7 @@ fn branch_cell_probes_take_entries_that_accept_their_btype() {
|
||||
let at = region.at(code.len());
|
||||
let word = ExitWord::from(ExitKind::Indirect);
|
||||
code.extend(
|
||||
cell_tail(at, cell, word, 2, region.at(PROBES + CELL_PROBE_ENTRY)).unwrap(),
|
||||
cell_tail(at, cell, pc, word, 2, region.at(PROBES + CELL_PROBE_ENTRY)).unwrap(),
|
||||
);
|
||||
put(®ion, 0, &code);
|
||||
let mut control = ChainControl {
|
||||
|
||||
@@ -97,7 +97,7 @@ fn readers_take_whole_entries_while_one_writer_rewrites_them() {
|
||||
}
|
||||
bytes[offset..offset + piece.len()].copy_from_slice(piece);
|
||||
};
|
||||
let cell_tail = cell_tail(at(3), cell, ExitKind::Call, 3, probes).unwrap();
|
||||
let cell_tail = rax_cell_tail(at(3), cell, ExitKind::Call, 3, probes).unwrap();
|
||||
put(0, &[0x48, 0x89, 0xf0]); // mov rax,rsi
|
||||
put(3, &cell_tail);
|
||||
put(64, &[0x48, 0x89, 0xf0]);
|
||||
|
||||
@@ -142,7 +142,7 @@ fn tail(
|
||||
)
|
||||
.unwrap()
|
||||
.to_vec(),
|
||||
None => cell_tail(at, cell, kind, ran, probe).unwrap().to_vec(),
|
||||
None => cell_tail(at, cell, pc, kind, ran, probe).unwrap().to_vec(),
|
||||
};
|
||||
put(region, TAIL, &code);
|
||||
}
|
||||
|
||||
@@ -664,11 +664,11 @@ fn the_cost_decoder_classes_transfer_instructions() {
|
||||
fn transfers_cost_what_their_path_runs() {
|
||||
let costs = transfer_costs();
|
||||
let expected = [
|
||||
("b forward", Cost::new(6, 1, 1, 1)),
|
||||
("b back", Cost::new(9, 3, 1, 1)),
|
||||
("cbz back, fallthrough", Cost::new(8, 1, 1, 1)),
|
||||
("cbz back, taken", Cost::new(12, 3, 1, 1)),
|
||||
("cbz forward, side exit", Cost::new(8, 1, 1, 1)),
|
||||
("b forward", Cost::new(5, 1, 1, 1)),
|
||||
("b back", Cost::new(8, 3, 1, 1)),
|
||||
("cbz back, fallthrough", Cost::new(7, 1, 1, 1)),
|
||||
("cbz back, taken", Cost::new(10, 3, 1, 1)),
|
||||
("cbz forward, side exit", Cost::new(7, 1, 1, 1)),
|
||||
];
|
||||
assert_eq!(costs, expected);
|
||||
// An iteration inside a self-loop, and one through its polled link.
|
||||
|
||||
@@ -1406,11 +1406,16 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
spans.push((exit.offset as u32, end as u32, Exit));
|
||||
terminal_end = terminal_end.min(start);
|
||||
}
|
||||
// RET, BR and BLR end their terminal with an exit to RAX.
|
||||
// RET, BR and BLR end their terminal with an exit to RAX; a block
|
||||
// whose exits are not linked ends with its direct exit.
|
||||
if let Some(start) = terminal_end.checked_sub(chain::DYNAMIC_EXIT_BYTES)
|
||||
&& chain::is_dynamic_exit_of_any_kind(&bytes[start..terminal_end])
|
||||
{
|
||||
spans.push((start as u32, terminal_end as u32, Exit));
|
||||
} else if let Some(start) = terminal_end.checked_sub(chain::DIRECT_EXIT_BYTES)
|
||||
&& chain::is_direct_exit_of_any_kind(&bytes[start..terminal_end])
|
||||
{
|
||||
spans.push((start as u32, terminal_end as u32, Exit));
|
||||
}
|
||||
let regions = step_timing::block_regions(bytes.len() as u32, &mut spans);
|
||||
self.host.profile_code(code, bytes.len(), ®ions);
|
||||
|
||||
@@ -782,14 +782,14 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
return true;
|
||||
}
|
||||
// Otherwise a thunk: from the slot's point, back to the exit when
|
||||
// no transfer is allowed; or from the exit's own point, which has
|
||||
// loaded the target, returning.
|
||||
// no transfer is allowed; or from the exit's own point, back to its
|
||||
// unlinked path, which loads the target and returns.
|
||||
let (point, fallback) = match exit.slot {
|
||||
0 => (chain::direct_patch(site), chain::Fallback::Return(word)),
|
||||
slot => (
|
||||
chain::slot_patch(site - u64::from(slot)),
|
||||
chain::Fallback::Jump(site),
|
||||
),
|
||||
0 => {
|
||||
let point = chain::direct_patch(site);
|
||||
(point, chain::unlinked_return(point))
|
||||
}
|
||||
slot => (chain::slot_patch(site - u64::from(slot)), site),
|
||||
};
|
||||
if !chain::patchable(point) {
|
||||
return false;
|
||||
@@ -1102,7 +1102,9 @@ impl<H: Host, D: Devices> Machine<H, D> {
|
||||
let stamped_cell = stamped.map(|(_, number)| number);
|
||||
let code = match stamped {
|
||||
Some((code, _)) => Some(code.to_vec()),
|
||||
None => chain::cell_tail(at, cell, word, retired, entry).map(|code| code.to_vec()),
|
||||
None => {
|
||||
chain::cell_tail(at, cell, next_pc, word, retired, entry).map(|code| code.to_vec())
|
||||
}
|
||||
};
|
||||
let (Some(code), Some(jump)) = (code, chain::jump(site, at)) else {
|
||||
cache.targets.release_cells(site..=site);
|
||||
|
||||
@@ -3556,13 +3556,16 @@ fn returns_and_exits_out_of_the_page_go_through_lookup_dispatchers() {
|
||||
let (tail, code) = &machine.host.patches[2 * index];
|
||||
let at = *tail as u64;
|
||||
let expected = if index == 0 {
|
||||
let cell_at = u64::from_le_bytes(code[2..10].try_into().unwrap());
|
||||
// MOVABS RAX with the target, then MOVABS R9 with the cell.
|
||||
let target = u64::from_le_bytes(code[2..10].try_into().unwrap());
|
||||
assert_eq!(target, cell.pc);
|
||||
let cell_at = u64::from_le_bytes(code[12..20].try_into().unwrap());
|
||||
// SAFETY: the table keeps the cell at its address while its exit holds it.
|
||||
let raw = unsafe { *(cell_at as *const chain::LookupEntry) };
|
||||
let targets = raw.targets & chain::META_TARGETS;
|
||||
assert_eq!(chain::LookupEntry { targets, ..raw }, cell);
|
||||
let entry = probes + chain::CELL_PROBE_ENTRY as u64;
|
||||
chain::cell_tail(at, cell_at, kind, ran, entry)
|
||||
chain::cell_tail(at, cell_at, target, kind, ran, entry)
|
||||
.unwrap()
|
||||
.to_vec()
|
||||
} else {
|
||||
|
||||
@@ -1175,14 +1175,11 @@ fn entered(code: &board::Code, slot: usize, position: usize) -> Option<(usize, u
|
||||
let retired = u32::from(block.instructions) - u32::from(word.skipped);
|
||||
let near = block.code.0 as u64;
|
||||
let (point, fallback) = match exit.slot {
|
||||
0 => (
|
||||
chain::direct_patch(exit.site),
|
||||
chain::Fallback::Return(word),
|
||||
),
|
||||
bytes => (
|
||||
chain::slot_patch(exit.site - u64::from(bytes)),
|
||||
chain::Fallback::Jump(exit.site),
|
||||
),
|
||||
0 => {
|
||||
let point = chain::direct_patch(exit.site);
|
||||
(point, chain::unlinked_return(point))
|
||||
}
|
||||
bytes => (chain::slot_patch(exit.site - u64::from(bytes)), exit.site),
|
||||
};
|
||||
let instruction = arena_bytes(arena, point, chain::PATCH_BYTES)?;
|
||||
if exit.slot != 0 && instruction == chain::LINKED_SLOT {
|
||||
|
||||
Reference in new issue
Block a user