Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/rust/cpu/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(non_upper_case_globals)]

use crate::config;
use crate::cpu::eip_increment::increment_instruction_pointer;
use crate::cpu::fpu::fpu_set_tag_word;
use crate::cpu::global_pointers::*;
use crate::cpu::memory;
Expand Down Expand Up @@ -2363,7 +2364,7 @@ pub unsafe fn read_imm8() -> OrPageFault<i32> {
}
dbg_assert!(!memory::in_mapped_range((*eip_phys ^ eip) as u32));
let data8 = *memory::mem8.offset((*eip_phys ^ eip) as isize) as i32;
*instruction_pointer = eip + 1;
*instruction_pointer = increment_instruction_pointer(eip, 1, is_asize_32(), get_seg_cs());
return Ok(data8);
}

Expand All @@ -2380,7 +2381,8 @@ pub unsafe fn read_imm16() -> OrPageFault<i32> {
}
else {
let data16 = memory::read16((*eip_phys ^ *instruction_pointer) as u32);
*instruction_pointer = *instruction_pointer + 2;
*instruction_pointer =
increment_instruction_pointer(*instruction_pointer, 2, is_asize_32(), get_seg_cs());
return Ok(data16);
};
}
Expand All @@ -2394,7 +2396,8 @@ pub unsafe fn read_imm32s() -> OrPageFault<i32> {
}
else {
let data32 = memory::read32s((*eip_phys ^ *instruction_pointer) as u32);
*instruction_pointer = *instruction_pointer + 4;
*instruction_pointer =
increment_instruction_pointer(*instruction_pointer, 4, is_asize_32(), get_seg_cs());
return Ok(data32);
};
}
Expand Down Expand Up @@ -3084,7 +3087,8 @@ unsafe fn jit_run_interpreted(mut phys_addr: u32) {
i += 1;
let start_eip = *instruction_pointer;
let opcode = *memory::mem8.offset(phys_addr as isize) as i32;
*instruction_pointer += 1;
*instruction_pointer =
increment_instruction_pointer(*instruction_pointer, 1, is_asize_32(), get_seg_cs());
dbg_assert!(*prefixes == 0);
run_instruction(opcode | (*is_32 as i32) << 8);
dbg_assert!(*prefixes == 0);
Expand Down
8 changes: 8 additions & 0 deletions src/rust/cpu/eip_increment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub fn increment_instruction_pointer(eip: i32, delta: i32, is_asize_32: bool, cs: i32) -> i32 {
eip.wrapping_add(if is_asize_32 {
delta
}
else {
((eip - cs + delta) & 0xFFFF) - ((eip - cs) & 0xFFFF)
})
}
1 change: 1 addition & 0 deletions src/rust/cpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod apic;
pub mod arith;
pub mod call_indirect;
pub mod cpu;
pub mod eip_increment;
pub mod fpu;
pub mod global_pointers;
pub mod instructions;
Expand Down
36 changes: 31 additions & 5 deletions src/rust/cpu_context.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cpu::eip_increment::increment_instruction_pointer;
use crate::cpu::memory;
use crate::prefix::{PREFIX_MASK_ADDRSIZE, PREFIX_MASK_OPSIZE};
use crate::state_flags::CachedStateFlags;
Expand All @@ -13,11 +14,21 @@ pub struct CpuContext {
impl CpuContext {
pub fn advance16(&mut self) {
dbg_assert!(self.eip & 0xFFF < 0xFFE);
self.eip += 2;
self.eip = increment_instruction_pointer(
self.eip as i32,
2,
self.asize_32(),
self.cs_offset as i32,
) as u32;
}
pub fn advance32(&mut self) {
dbg_assert!(self.eip & 0xFFF < 0xFFC);
self.eip += 4;
self.eip = increment_instruction_pointer(
self.eip as i32,
4,
self.asize_32(),
self.cs_offset as i32,
) as u32;
}
#[allow(unused)]
pub fn advance_moffs(&mut self) {
Expand All @@ -32,20 +43,35 @@ impl CpuContext {
pub fn read_imm8(&mut self) -> u8 {
dbg_assert!(self.eip & 0xFFF < 0xFFF);
let v = memory::read8(self.eip) as u8;
self.eip += 1;
self.eip = increment_instruction_pointer(
self.eip as i32,
1,
self.asize_32(),
self.cs_offset as i32,
) as u32;
v
}
pub fn read_imm8s(&mut self) -> i8 { self.read_imm8() as i8 }
pub fn read_imm16(&mut self) -> u16 {
dbg_assert!(self.eip & 0xFFF < 0xFFE);
let v = memory::read16(self.eip) as u16;
self.eip += 2;
self.eip = increment_instruction_pointer(
self.eip as i32,
2,
self.asize_32(),
self.cs_offset as i32,
) as u32;
v
}
pub fn read_imm32(&mut self) -> u32 {
dbg_assert!(self.eip & 0xFFF < 0xFFC);
let v = memory::read32s(self.eip) as u32;
self.eip += 4;
self.eip = increment_instruction_pointer(
self.eip as i32,
4,
self.asize_32(),
self.cs_offset as i32,
) as u32;
v
}
pub fn read_moffs(&mut self) -> u32 {
Expand Down
Loading