rewrite to remove Cpu::current_raw()

This commit is contained in:
Garen Tyler 2023-11-03 18:03:48 -06:00
parent ec9c0cbe4d
commit 73e3ab9ebc
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
3 changed files with 24 additions and 30 deletions

View File

@ -34,12 +34,7 @@ impl Cpu {
/// Return this CPU's cpu struct. /// Return this CPU's cpu struct.
/// Interrupts must be disabled. /// Interrupts must be disabled.
pub fn current() -> &'static mut Cpu { pub fn current() -> &'static mut Cpu {
unsafe { &mut *Cpu::current_raw() } unsafe { &mut CPUS[Cpu::current_id()] }
}
/// Return this CPU's cpu struct.
/// Interrupts must be disabled.
pub unsafe fn current_raw() -> *mut Cpu {
addr_of_mut!(CPUS[Cpu::current_id()])
} }
} }
@ -47,5 +42,5 @@ impl Cpu {
/// Interrupts must be disabled. /// Interrupts must be disabled.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn mycpu() -> *mut Cpu { pub unsafe extern "C" fn mycpu() -> *mut Cpu {
Cpu::current_raw() addr_of_mut!(*Cpu::current())
} }

View File

@ -101,8 +101,7 @@ pub struct Proc {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn myproc() -> *mut Proc { pub unsafe extern "C" fn myproc() -> *mut Proc {
let _ = crate::trap::InterruptBlocker::new(); let _ = crate::trap::InterruptBlocker::new();
let c = Cpu::current_raw(); Cpu::current().proc
(*c).proc
} }
#[no_mangle] #[no_mangle]
@ -183,9 +182,9 @@ pub unsafe fn r#yield() {
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sched() { pub unsafe extern "C" fn sched() {
let p = myproc(); let p = myproc();
let c = Cpu::current_raw(); let cpu = Cpu::current();
if (*c).interrupt_disable_layers != 1 { if cpu.interrupt_disable_layers != 1 {
panic!("sched locks"); panic!("sched locks");
} else if (*p).state == ProcState::Running { } else if (*p).state == ProcState::Running {
panic!("sched running"); panic!("sched running");
@ -193,9 +192,9 @@ pub unsafe extern "C" fn sched() {
panic!("sched interruptible"); panic!("sched interruptible");
} }
let previous_interrupts_enabled = (*c).previous_interrupts_enabled; let previous_interrupts_enabled = cpu.previous_interrupts_enabled;
swtch(addr_of_mut!((*p).context), addr_of_mut!((*c).context)); swtch(addr_of_mut!((*p).context), addr_of_mut!(cpu.context));
(*c).previous_interrupts_enabled = previous_interrupts_enabled; cpu.previous_interrupts_enabled = previous_interrupts_enabled;
} }
/// The lock should already be locked. /// The lock should already be locked.

View File

@ -90,14 +90,14 @@ impl InterruptBlocker {
pub fn new() -> InterruptBlocker { pub fn new() -> InterruptBlocker {
unsafe { unsafe {
let interrupts_before = intr_get(); let interrupts_before = intr_get();
let cpu = Cpu::current_raw(); let cpu = Cpu::current();
intr_off(); intr_off();
if (*cpu).interrupt_disable_layers == 0 { if cpu.interrupt_disable_layers == 0 {
(*cpu).previous_interrupts_enabled = interrupts_before; cpu.previous_interrupts_enabled = interrupts_before;
} }
(*cpu).interrupt_disable_layers += 1; cpu.interrupt_disable_layers += 1;
// crate::sync::spinlock::push_off(); // crate::sync::spinlock::push_off();
} }
InterruptBlocker InterruptBlocker
@ -106,16 +106,16 @@ impl InterruptBlocker {
impl core::ops::Drop for InterruptBlocker { impl core::ops::Drop for InterruptBlocker {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
let cpu = Cpu::current_raw(); let cpu = Cpu::current();
if intr_get() == 1 || (*cpu).interrupt_disable_layers < 1 { if intr_get() == 1 || cpu.interrupt_disable_layers < 1 {
// panic!("pop_off mismatched"); // panic!("pop_off mismatched");
return; return;
} }
(*cpu).interrupt_disable_layers -= 1; cpu.interrupt_disable_layers -= 1;
if (*cpu).interrupt_disable_layers == 0 && (*cpu).previous_interrupts_enabled == 1 { if cpu.interrupt_disable_layers == 0 && cpu.previous_interrupts_enabled == 1 {
intr_on(); intr_on();
} }
// crate::sync::spinlock::pop_off(); // crate::sync::spinlock::pop_off();
@ -272,28 +272,28 @@ pub unsafe extern "C" fn usertrap() {
pub unsafe fn push_intr_off() { pub unsafe fn push_intr_off() {
let old = intr_get(); let old = intr_get();
let cpu = Cpu::current_raw(); let cpu = Cpu::current();
intr_off(); intr_off();
if (*cpu).interrupt_disable_layers == 0 { if cpu.interrupt_disable_layers == 0 {
(*cpu).previous_interrupts_enabled = old; cpu.previous_interrupts_enabled = old;
} }
(*cpu).interrupt_disable_layers += 1; cpu.interrupt_disable_layers += 1;
} }
pub unsafe fn pop_intr_off() { pub unsafe fn pop_intr_off() {
let cpu = Cpu::current_raw(); let cpu = Cpu::current();
if intr_get() == 1 { if intr_get() == 1 {
// crate::panic_byte(b'0'); // crate::panic_byte(b'0');
panic!("pop_intr_off - interruptible"); panic!("pop_intr_off - interruptible");
} else if (*cpu).interrupt_disable_layers < 1 { } else if cpu.interrupt_disable_layers < 1 {
// crate::panic_byte(b'1'); // crate::panic_byte(b'1');
panic!("pop_intr_off"); panic!("pop_intr_off");
} }
(*cpu).interrupt_disable_layers -= 1; cpu.interrupt_disable_layers -= 1;
if (*cpu).interrupt_disable_layers == 0 && (*cpu).previous_interrupts_enabled == 1 { if cpu.interrupt_disable_layers == 0 && cpu.previous_interrupts_enabled == 1 {
intr_on(); intr_on();
} }
} }