diff --git a/kernel/rustkernel/src/proc/cpu.rs b/kernel/rustkernel/src/proc/cpu.rs index 565de9b..b854041 100644 --- a/kernel/rustkernel/src/proc/cpu.rs +++ b/kernel/rustkernel/src/proc/cpu.rs @@ -34,12 +34,7 @@ impl Cpu { /// Return this CPU's cpu struct. /// Interrupts must be disabled. pub fn current() -> &'static mut Cpu { - unsafe { &mut *Cpu::current_raw() } - } - /// Return this CPU's cpu struct. - /// Interrupts must be disabled. - pub unsafe fn current_raw() -> *mut Cpu { - addr_of_mut!(CPUS[Cpu::current_id()]) + unsafe { &mut CPUS[Cpu::current_id()] } } } @@ -47,5 +42,5 @@ impl Cpu { /// Interrupts must be disabled. #[no_mangle] pub unsafe extern "C" fn mycpu() -> *mut Cpu { - Cpu::current_raw() + addr_of_mut!(*Cpu::current()) } diff --git a/kernel/rustkernel/src/proc/proc.rs b/kernel/rustkernel/src/proc/proc.rs index 439c2ed..cc6d0aa 100644 --- a/kernel/rustkernel/src/proc/proc.rs +++ b/kernel/rustkernel/src/proc/proc.rs @@ -101,8 +101,7 @@ pub struct Proc { #[no_mangle] pub unsafe extern "C" fn myproc() -> *mut Proc { let _ = crate::trap::InterruptBlocker::new(); - let c = Cpu::current_raw(); - (*c).proc + Cpu::current().proc } #[no_mangle] @@ -183,9 +182,9 @@ pub unsafe fn r#yield() { #[no_mangle] pub unsafe extern "C" fn sched() { 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"); } else if (*p).state == ProcState::Running { panic!("sched running"); @@ -193,9 +192,9 @@ pub unsafe extern "C" fn sched() { panic!("sched interruptible"); } - let previous_interrupts_enabled = (*c).previous_interrupts_enabled; - swtch(addr_of_mut!((*p).context), addr_of_mut!((*c).context)); - (*c).previous_interrupts_enabled = previous_interrupts_enabled; + let previous_interrupts_enabled = cpu.previous_interrupts_enabled; + swtch(addr_of_mut!((*p).context), addr_of_mut!(cpu.context)); + cpu.previous_interrupts_enabled = previous_interrupts_enabled; } /// The lock should already be locked. diff --git a/kernel/rustkernel/src/trap.rs b/kernel/rustkernel/src/trap.rs index c4980ea..d475426 100644 --- a/kernel/rustkernel/src/trap.rs +++ b/kernel/rustkernel/src/trap.rs @@ -90,14 +90,14 @@ impl InterruptBlocker { pub fn new() -> InterruptBlocker { unsafe { let interrupts_before = intr_get(); - let cpu = Cpu::current_raw(); + let cpu = Cpu::current(); intr_off(); - if (*cpu).interrupt_disable_layers == 0 { - (*cpu).previous_interrupts_enabled = interrupts_before; + if cpu.interrupt_disable_layers == 0 { + cpu.previous_interrupts_enabled = interrupts_before; } - (*cpu).interrupt_disable_layers += 1; + cpu.interrupt_disable_layers += 1; // crate::sync::spinlock::push_off(); } InterruptBlocker @@ -106,16 +106,16 @@ impl InterruptBlocker { impl core::ops::Drop for InterruptBlocker { fn drop(&mut self) { 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"); 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(); } // crate::sync::spinlock::pop_off(); @@ -272,28 +272,28 @@ pub unsafe extern "C" fn usertrap() { pub unsafe fn push_intr_off() { let old = intr_get(); - let cpu = Cpu::current_raw(); + let cpu = Cpu::current(); intr_off(); - if (*cpu).interrupt_disable_layers == 0 { - (*cpu).previous_interrupts_enabled = old; + if cpu.interrupt_disable_layers == 0 { + cpu.previous_interrupts_enabled = old; } - (*cpu).interrupt_disable_layers += 1; + cpu.interrupt_disable_layers += 1; } pub unsafe fn pop_intr_off() { - let cpu = Cpu::current_raw(); + let cpu = Cpu::current(); if intr_get() == 1 { // crate::panic_byte(b'0'); panic!("pop_intr_off - interruptible"); - } else if (*cpu).interrupt_disable_layers < 1 { + } else if cpu.interrupt_disable_layers < 1 { // crate::panic_byte(b'1'); 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(); } }