mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 12:50:43 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c107f3a43f | ||
|
|
044f036ea5 |
No files matched your search
@@ -246,6 +246,15 @@ impl<'a> Interpreter<'a> {
|
||||
|
||||
/// Single-step execution. Returns Ok(Some(ExitReason)) on clean exit (HLT/SYSCALL).
|
||||
pub fn step(&mut self) -> Result<Option<ExitReason>, ExitReason> {
|
||||
let start = self.cpu.rip;
|
||||
let result = self.execute_instruction();
|
||||
if result.is_err() {
|
||||
self.cpu.rip = start;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn execute_instruction(&mut self) -> Result<Option<ExitReason>, ExitReason> {
|
||||
let mut rex_w = false;
|
||||
let mut rex_r = false;
|
||||
let mut rex_b = false;
|
||||
@@ -276,16 +285,21 @@ impl<'a> Interpreter<'a> {
|
||||
// HLT: Halt execution
|
||||
0xF4 => Ok(Some(ExitReason::Halt)),
|
||||
|
||||
// SYSCALL: 0x0F 0x05
|
||||
0x0F => {
|
||||
let second = self.fetch_u8()?;
|
||||
if second == 0x05 {
|
||||
let syscall_num = self.cpu.get(Register::Rax);
|
||||
Ok(Some(ExitReason::Syscall(syscall_num)))
|
||||
} else {
|
||||
Err(ExitReason::IllegalInstruction(second))
|
||||
0x0F => match self.fetch_u8()? {
|
||||
0x05 => Ok(Some(ExitReason::Syscall(self.cpu.get(Register::Rax)))),
|
||||
op @ 0xC8..=0xCF => {
|
||||
let reg = Register::from_index((op & 7) | (u8::from(rex_b) << 3));
|
||||
let value = self.cpu.get(reg);
|
||||
let reversed = if rex_w {
|
||||
value.swap_bytes()
|
||||
} else {
|
||||
u64::from((value as u32).swap_bytes())
|
||||
};
|
||||
self.cpu.set(reg, reversed);
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
op => Err(ExitReason::IllegalInstruction(op)),
|
||||
},
|
||||
|
||||
// MOV r32, imm32 (0xB8 + reg) or MOV r64, imm64 with REX.W
|
||||
0xB8..=0xBF => {
|
||||
|
||||
@@ -112,7 +112,19 @@ impl DbtEngine {
|
||||
|
||||
// Look up or translate block at cpu.rip
|
||||
let block_limit = (max_instructions - instructions_count).min(MAX_BLOCK_INSTRUCTIONS);
|
||||
let block_fn_ptr = match self.get_or_compile_block(cpu.rip, memory, block_limit) {
|
||||
let mut block = self.get_or_compile_block(cpu.rip, memory, block_limit);
|
||||
// A decode failure can belong to a later instruction in the block.
|
||||
// Execute the current instruction alone so earlier effects retire
|
||||
// and an earlier data fault takes precedence over speculative fetch.
|
||||
if block_limit > 1
|
||||
&& matches!(
|
||||
block,
|
||||
Err(ExitReason::IllegalInstruction(_) | ExitReason::MemoryFault(_))
|
||||
)
|
||||
{
|
||||
block = self.get_or_compile_block(cpu.rip, memory, 1);
|
||||
}
|
||||
let block_fn_ptr = match block {
|
||||
Ok(ptr) => ptr,
|
||||
Err(reason) => {
|
||||
return ExecutionResult {
|
||||
@@ -1077,6 +1089,16 @@ impl DbtEngine {
|
||||
cur_rip += 1;
|
||||
|
||||
match op2 {
|
||||
// BSWAP r32/r64: the register is encoded in the opcode.
|
||||
0xC8..=0xCF => {
|
||||
insn_count += 1;
|
||||
let reg = (op2 & 7) | (u8::from(rex_b) << 3);
|
||||
let offset = u32::from(reg) * 8;
|
||||
asm.ldr_uoff_64(X9, X19, offset);
|
||||
// REV X9, X9 or REV W9, W9. A W write clears the upper half.
|
||||
asm.emit(if rex_w { 0xdac0_0d29 } else { 0x5ac0_0929 });
|
||||
asm.str_uoff_64(X9, X19, offset);
|
||||
}
|
||||
// Jcc rel32 (0x0F 0x80..=0x8F)
|
||||
0x80..=0x8F => {
|
||||
insn_count += 1;
|
||||
@@ -1651,6 +1673,58 @@ mod tests {
|
||||
assert_eq!(unsupported.instructions_executed, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completed_store_survives_the_next_fault_or_budget_boundary() {
|
||||
for native in [false, true] {
|
||||
for (tail, fault) in [
|
||||
(&[0xcc][..], ExitReason::IllegalInstruction(0xcc)),
|
||||
(
|
||||
&[0xb8, 1][..],
|
||||
ExitReason::MemoryFault(MemoryError::UnmappedAddress(ENTRY + 5)),
|
||||
),
|
||||
(
|
||||
&[0x48, 0x8b, 0x02, 0xf4][..],
|
||||
ExitReason::MemoryFault(MemoryError::UnmappedAddress(0x4000)),
|
||||
),
|
||||
] {
|
||||
for budget in [0, 1, 16] {
|
||||
// mov [rcx], rax; then an illegal, truncated or faulting instruction.
|
||||
let code = [&[0x48, 0x89, 0x01][..], tail].concat();
|
||||
let (mut engine, mut cpu, mut memory) = fixture(&code);
|
||||
memory
|
||||
.map_region(0x3000, 8, MemoryPermissions::READ_WRITE)
|
||||
.unwrap();
|
||||
cpu.set(Register::Rax, 0x1234);
|
||||
cpu.set(Register::Rcx, 0x3000);
|
||||
cpu.set(Register::Rdx, 0x4000);
|
||||
let mut expected = cpu.clone();
|
||||
if budget > 0 {
|
||||
expected.rip += 3;
|
||||
}
|
||||
let result = if native {
|
||||
engine.run(&mut cpu, &mut memory, budget)
|
||||
} else {
|
||||
crate::cpu::Interpreter::new(cpu, &mut memory, budget).run()
|
||||
};
|
||||
assert_eq!(result.instructions_executed, budget.min(1));
|
||||
assert_eq!(
|
||||
result.exit_reason,
|
||||
if budget <= 1 {
|
||||
ExitReason::StepLimitExceeded
|
||||
} else {
|
||||
fault
|
||||
}
|
||||
);
|
||||
assert_eq!(result.final_state, expected);
|
||||
assert_eq!(
|
||||
memory.read_u64(0x3000).unwrap(),
|
||||
if budget == 0 { 0 } else { 0x1234 }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hlt_preserves_first_memory_fault() {
|
||||
let code = [
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
use xenora_loader::cpu::{CpuState, ExitReason, Interpreter, RFlags};
|
||||
use xenora_loader::dbt::DbtEngine;
|
||||
use xenora_loader::memory::{GuestMemory, MemoryPermissions};
|
||||
|
||||
fn check_bswap(native: bool) {
|
||||
const ENTRY: u64 = 0x1000;
|
||||
const INPUT: u64 = 0x1122_3344_5566_7788;
|
||||
for reg in 0_u8..16 {
|
||||
for width in [16, 32, 64] {
|
||||
let wide = width == 64;
|
||||
let mut code = Vec::new();
|
||||
if width == 16 {
|
||||
code.push(0x66);
|
||||
}
|
||||
let rex = 0x40 | (u8::from(wide) << 3) | (reg >> 3);
|
||||
if rex != 0x40 {
|
||||
code.push(rex);
|
||||
}
|
||||
code.extend([0x0f, 0xc8 + (reg & 7)]);
|
||||
let mut memory = GuestMemory::new();
|
||||
memory
|
||||
.map_region_with_data(ENTRY, code.len(), MemoryPermissions::READ_EXECUTE, &code)
|
||||
.unwrap();
|
||||
let mut cpu = CpuState::new(ENTRY, 0);
|
||||
cpu.gpr.fill(0xdead_beef);
|
||||
cpu.gpr[reg as usize] = INPUT;
|
||||
cpu.flags = RFlags {
|
||||
cf: wide,
|
||||
zf: !wide,
|
||||
sf: wide,
|
||||
of: !wide,
|
||||
};
|
||||
let flags = cpu.flags;
|
||||
let mut expected = cpu.gpr;
|
||||
let result = if native {
|
||||
DbtEngine::new(16 * 1024)
|
||||
.unwrap()
|
||||
.run(&mut cpu, &mut memory, 1)
|
||||
} else {
|
||||
Interpreter::new(cpu, &mut memory, 1).run()
|
||||
};
|
||||
if width == 16 {
|
||||
assert!(matches!(
|
||||
result.exit_reason,
|
||||
ExitReason::IllegalInstruction(_)
|
||||
));
|
||||
assert_eq!(result.instructions_executed, 0);
|
||||
} else {
|
||||
assert_eq!(
|
||||
result.exit_reason,
|
||||
ExitReason::StepLimitExceeded,
|
||||
"{code:02x?}"
|
||||
);
|
||||
assert_eq!(result.instructions_executed, 1);
|
||||
assert_eq!(result.final_state.rip, ENTRY + code.len() as u64);
|
||||
expected[reg as usize] = if wide {
|
||||
0x8877_6655_4433_2211
|
||||
} else {
|
||||
0x8877_6655
|
||||
};
|
||||
}
|
||||
assert_eq!(result.final_state.gpr, expected);
|
||||
assert_eq!(result.final_state.flags, flags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interpreter_bswap() {
|
||||
check_bswap(false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_bswap() {
|
||||
check_bswap(true);
|
||||
}
|
||||
Reference in new issue
Block a user