Skip to content

Commit

Permalink
Make some properties private and rework some of the properties to be …
Browse files Browse the repository at this point in the history
…more functional.
  • Loading branch information
sciguyryan committed Feb 4, 2025
1 parent 28f2e9f commit c2cfbe9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
40 changes: 21 additions & 19 deletions redox-core/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ pub struct Cpu {
/// The registers associated with this CPU.
pub registers: Registers,
// Is the CPU currently halted?
pub is_halted: bool,
/// Is the CPU currently running in machine mode (superuser)?
pub is_machine_mode: bool,
is_halted: bool,
/// The current machine execution mode.
privilege_level: PrivilegeLevel,
/// Is the CPU currently in an interrupt handler?
pub is_in_interrupt_handler: bool,
is_in_interrupt_handler: bool,
/// The last interrupt handler that was "active" (i.e. was not terminated with intret).
pub last_interrupt_code: Option<u8>,
last_interrupt_code: Option<u8>,
/// The decoded expressions cache.
decode_expression_cache: HashMap<u32, Expression>,
}
Expand All @@ -99,7 +99,7 @@ impl Cpu {
Self {
registers: Registers::default(),
is_halted: false,
is_machine_mode: true,
privilege_level: PrivilegeLevel::Machine,
is_in_interrupt_handler: false,
last_interrupt_code: None,
decode_expression_cache: HashMap::new(),
Expand Down Expand Up @@ -196,11 +196,7 @@ impl Cpu {
/// A [`PrivilegeLevel`] giving the current privilege level of the processor.
#[inline(always)]
fn get_privilege(&self) -> PrivilegeLevel {
if self.is_machine_mode {
PrivilegeLevel::Machine
} else {
PrivilegeLevel::User
}
self.privilege_level
}

/// Fetch and decode the next instruction to be executed.
Expand Down Expand Up @@ -249,12 +245,18 @@ impl Cpu {
/// * `flag` - The [`CpuFlag`] to check.
#[allow(unused)]
#[inline(always)]
fn is_cpu_flag_set(&self, flag: &CpuFlag) -> bool {
fn is_flag_set(&self, flag: &CpuFlag) -> bool {
// Get the current CPU flags.
let flags = self.registers.read_reg_u32_unchecked(&RegisterId::EFL);
utils::is_bit_set(flags, *flag as u8)
}

/// Check whether the CPU is currently in machine mode.
#[inline(always)]
pub fn is_in_machine_mode(&self) -> bool {
self.privilege_level == PrivilegeLevel::Machine
}

/// Perform a checked add of two u32 values.
///
/// # Arguments
Expand Down Expand Up @@ -1327,15 +1329,15 @@ impl Cpu {
}
I::LoadIVTAddrU32Imm(addr) => {
// livt &addr
if !self.is_machine_mode {
if !self.is_in_machine_mode() {
self.trigger_interrupt(GENERAL_PROTECTION_FAULT_INT, &mut com_bus.mem);
return;
}

self.write_reg_u32_unchecked(&RegisterId::IDTR, *addr);
}
I::MachineReturn => {
self.set_machine_mode(false);
self.set_privilege_level(PrivilegeLevel::User);
}
I::Halt => {
self.set_halted(true);
Expand Down Expand Up @@ -1394,14 +1396,14 @@ impl Cpu {
self.write_reg_u32_unchecked(&RegisterId::EIP, value);
}

/// Set the machine mode privilege level of the processor.
/// Set the privilege level of the CPU.
///
/// # Arguments
///
/// * `state` - Whether the CPU should be running in machine mode or not.
/// * `level` - The new privilege level of the CPU.
#[inline(always)]
fn set_machine_mode(&mut self, state: bool) {
self.is_machine_mode = state;
fn set_privilege_level(&mut self, level: PrivilegeLevel) {
self.privilege_level = level;
}

/// Update the various segment registers in the CPU.
Expand Down Expand Up @@ -2595,7 +2597,7 @@ mod tests_cpu {
CpuTests::new(&tests).run_all_special(|id, vm| {
if let Some(v) = vm {
assert!(
!v.cpu.is_machine_mode,
!v.cpu.is_in_machine_mode(),
"Test {id} Failed - machine is still in machine mode after executing mret instruction!"
);
} else {
Expand Down
2 changes: 1 addition & 1 deletion redox-core/src/privilege_level.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PrivilegeLevel {
/// Execution of a command with machine-level privilege.
Machine,
Expand Down
2 changes: 1 addition & 1 deletion redox-terminal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn main() {
println!("----------[CPU]----------");
println!("Code successfully executed in {elapsed} ns!");
println!();
println!("Machine Mode? {}", vm.cpu.is_machine_mode);
println!("Machine Mode? {}", vm.cpu.is_in_machine_mode());
println!(
"Stack Frame Size: {}",
vm.com_bus.mem.get_stack_frame_size()
Expand Down

0 comments on commit c2cfbe9

Please sign in to comment.