move cpu-related functions into associated methods of the Cpu struct
This commit is contained in:
parent
87caccf623
commit
ec9c0cbe4d
@ -6,8 +6,6 @@
|
||||
#include "proc.h"
|
||||
#include "defs.h"
|
||||
|
||||
struct cpu cpus[NCPU];
|
||||
|
||||
struct proc proc[NPROC];
|
||||
|
||||
struct proc *initproc;
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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");
|
||||
|
@ -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().
|
||||
|
@ -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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user