From ec9c0cbe4ddbc443f85c86692ea6d6d25d9b309c Mon Sep 17 00:00:00 2001 From: Garen Tyler Date: Fri, 3 Nov 2023 17:57:06 -0600 Subject: [PATCH] move cpu-related functions into associated methods of the Cpu struct --- kernel/proc.c | 2 -- kernel/proc.h | 2 -- kernel/rustkernel/src/arch/riscv/plic.rs | 8 +++---- kernel/rustkernel/src/lib.rs | 4 ++-- kernel/rustkernel/src/proc/cpu.rs | 30 ++++++++++++++---------- kernel/rustkernel/src/proc/proc.rs | 6 ++--- kernel/rustkernel/src/start.rs | 2 +- kernel/rustkernel/src/trap.rs | 14 +++++------ 8 files changed, 35 insertions(+), 33 deletions(-) diff --git a/kernel/proc.c b/kernel/proc.c index 8a7a048..d4003eb 100644 --- a/kernel/proc.c +++ b/kernel/proc.c @@ -6,8 +6,6 @@ #include "proc.h" #include "defs.h" -struct cpu cpus[NCPU]; - struct proc proc[NPROC]; struct proc *initproc; diff --git a/kernel/proc.h b/kernel/proc.h index 664c0ac..910f4c2 100644 --- a/kernel/proc.h +++ b/kernel/proc.h @@ -31,8 +31,6 @@ struct cpu { int previous_interrupts_enabled; // Were interrupts enabled before push_off()? }; -extern struct cpu cpus[NCPU]; - // per-process data for the trap handling code in trampoline.S. // sits in a page by itself just under the trampoline page in the // user page table. not specially mapped in the kernel page table. diff --git a/kernel/rustkernel/src/arch/riscv/plic.rs b/kernel/rustkernel/src/arch/riscv/plic.rs index ed8793b..3729c72 100644 --- a/kernel/rustkernel/src/arch/riscv/plic.rs +++ b/kernel/rustkernel/src/arch/riscv/plic.rs @@ -2,7 +2,7 @@ use crate::{ arch::riscv::{plic_sclaim, plic_senable, plic_spriority, PLIC, UART0_IRQ, VIRTIO0_IRQ}, - proc::cpu::cpuid, + proc::cpu::Cpu, }; pub unsafe fn plicinit() { @@ -12,7 +12,7 @@ pub unsafe fn plicinit() { } pub unsafe fn plicinithart() { - let hart = cpuid() as u64; + let hart = Cpu::current_id() as u64; // Set enable bits for this hart's S-mode // for the UART and VIRTIO disk. @@ -24,12 +24,12 @@ pub unsafe fn plicinithart() { /// Ask the PLIC what interrupt we should serve. pub unsafe fn plic_claim() -> i32 { - let hart = cpuid() as u64; + let hart = Cpu::current_id() as u64; *(plic_sclaim(hart) as *const i32) } /// Tell the PLIC we've served this IRQ. pub unsafe fn plic_complete(irq: i32) { - let hart = cpuid() as u64; + let hart = Cpu::current_id() as u64; *(plic_sclaim(hart) as *mut i32) = irq; } diff --git a/kernel/rustkernel/src/lib.rs b/kernel/rustkernel/src/lib.rs index 44794f5..4fc4e91 100644 --- a/kernel/rustkernel/src/lib.rs +++ b/kernel/rustkernel/src/lib.rs @@ -21,7 +21,7 @@ mod sync; mod syscall; mod trap; -use crate::{proc::cpu::cpuid, sync::mutex::Mutex}; +use crate::{proc::cpu::Cpu, sync::mutex::Mutex}; use core::ffi::{c_char, CStr}; pub(crate) use crate::console::printf::{print, println}; @@ -57,7 +57,7 @@ pub const FSSIZE: usize = 2000; pub const MAXPATH: usize = 128; pub unsafe fn main() -> ! { - if cpuid() == 0 { + if Cpu::current_id() == 0 { console::consoleinit(); mem::kalloc::kinit(); println!("\nxv6 kernel is booting"); diff --git a/kernel/rustkernel/src/proc/cpu.rs b/kernel/rustkernel/src/proc/cpu.rs index ad9f3b5..565de9b 100644 --- a/kernel/rustkernel/src/proc/cpu.rs +++ b/kernel/rustkernel/src/proc/cpu.rs @@ -2,9 +2,7 @@ use super::{context::Context, proc::Proc}; use crate::arch::riscv::asm::r_tp; use core::ptr::{addr_of_mut, null_mut}; -extern "C" { - pub static mut cpus: [Cpu; crate::NCPU]; -} +pub static mut CPUS: [Cpu; crate::NCPU] = [Cpu::new(); crate::NCPU]; /// Per-CPU state. #[repr(C)] @@ -27,19 +25,27 @@ impl Cpu { previous_interrupts_enabled: 0, } } -} - -/// Must be called with interrupts disabled -/// to prevent race with process being moved -/// to a different CPU. -pub unsafe fn cpuid() -> i32 { - r_tp() as i32 + /// Must be called with interrupts disabled + /// to prevent race with process being moved + /// to a different CPU. + pub fn current_id() -> usize { + unsafe { r_tp() as usize } + } + /// 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()]) + } } /// Return this CPU's cpu struct. /// Interrupts must be disabled. #[no_mangle] pub unsafe extern "C" fn mycpu() -> *mut Cpu { - let id = cpuid(); - addr_of_mut!(cpus[id as usize]) + Cpu::current_raw() } diff --git a/kernel/rustkernel/src/proc/proc.rs b/kernel/rustkernel/src/proc/proc.rs index 1fd9345..439c2ed 100644 --- a/kernel/rustkernel/src/proc/proc.rs +++ b/kernel/rustkernel/src/proc/proc.rs @@ -1,6 +1,6 @@ #![allow(clippy::comparison_chain)] -use super::{context::Context, cpu::mycpu, trapframe::Trapframe}; +use super::{context::Context, cpu::Cpu, trapframe::Trapframe}; use crate::{ arch::riscv::{intr_get, Pagetable, PTE_W}, fs::file::{File, Inode}, @@ -101,7 +101,7 @@ pub struct Proc { #[no_mangle] pub unsafe extern "C" fn myproc() -> *mut Proc { let _ = crate::trap::InterruptBlocker::new(); - let c = mycpu(); + let c = Cpu::current_raw(); (*c).proc } @@ -183,7 +183,7 @@ pub unsafe fn r#yield() { #[no_mangle] pub unsafe extern "C" fn sched() { let p = myproc(); - let c = mycpu(); + let c = Cpu::current_raw(); if (*c).interrupt_disable_layers != 1 { panic!("sched locks"); diff --git a/kernel/rustkernel/src/start.rs b/kernel/rustkernel/src/start.rs index 963cf52..70f84c1 100644 --- a/kernel/rustkernel/src/start.rs +++ b/kernel/rustkernel/src/start.rs @@ -40,7 +40,7 @@ pub unsafe extern "C" fn start() { // Ask for clock interrupts. timerinit(); - // Keep each CPU's hartid in its tp register, for cpuid(). + // Keep each CPU's hartid in its tp register, for Cpu::current_id(). w_tp(r_mhartid()); // Switch to supervisor mode and jump to main(). diff --git a/kernel/rustkernel/src/trap.rs b/kernel/rustkernel/src/trap.rs index 7ef06cd..c4980ea 100644 --- a/kernel/rustkernel/src/trap.rs +++ b/kernel/rustkernel/src/trap.rs @@ -2,7 +2,7 @@ use crate::{ arch::riscv::*, println, proc::{ - cpu::{cpuid, mycpu}, + cpu::Cpu, proc::{exit, killed, myproc, r#yield, setkilled, wakeup, ProcState}, }, sync::mutex::Mutex, @@ -70,7 +70,7 @@ pub unsafe fn devintr() -> i32 { // Software interrupt from a machine-mode timer interrupt, // forwarded by timervec in kernelvec.S. - if cpuid() == 0 { + if Cpu::current_id() == 0 { clockintr(); } @@ -90,7 +90,7 @@ impl InterruptBlocker { pub fn new() -> InterruptBlocker { unsafe { let interrupts_before = intr_get(); - let cpu = mycpu(); + let cpu = Cpu::current_raw(); intr_off(); @@ -106,7 +106,7 @@ impl InterruptBlocker { impl core::ops::Drop for InterruptBlocker { fn drop(&mut self) { unsafe { - let cpu = mycpu(); + let cpu = Cpu::current_raw(); if intr_get() == 1 || (*cpu).interrupt_disable_layers < 1 { // panic!("pop_off mismatched"); @@ -146,7 +146,7 @@ pub unsafe extern "C" fn usertrapret() { // process's kernel stack (*(*p).trapframe).kernel_sp = (*p).kstack + PGSIZE; (*(*p).trapframe).kernel_trap = usertrap as usize as u64; - // hartid for cpuid() + // hartid for Cpu::current_id() (*(*p).trapframe).kernel_hartid = r_tp(); // Set up the registers that trampoline.S's @@ -272,7 +272,7 @@ pub unsafe extern "C" fn usertrap() { pub unsafe fn push_intr_off() { let old = intr_get(); - let cpu = mycpu(); + let cpu = Cpu::current_raw(); intr_off(); if (*cpu).interrupt_disable_layers == 0 { @@ -281,7 +281,7 @@ pub unsafe fn push_intr_off() { (*cpu).interrupt_disable_layers += 1; } pub unsafe fn pop_intr_off() { - let cpu = mycpu(); + let cpu = Cpu::current_raw(); if intr_get() == 1 { // crate::panic_byte(b'0');