mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Preserve precise guest fault state and retired instructions
Signed-off-by: ates <ahmetrizaates@hotmail.com>
This commit is contained in:
1 parent
044f036ea5
commit
c107f3a43f
2 files changed
+74
-1
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;
|
||||
|
||||
@@ -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 {
|
||||
@@ -1661,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 = [
|
||||
|
||||
Reference in new issue
Block a user