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 "proc.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
|
||||||
struct cpu cpus[NCPU];
|
|
||||||
|
|
||||||
struct proc proc[NPROC];
|
struct proc proc[NPROC];
|
||||||
|
|
||||||
struct proc *initproc;
|
struct proc *initproc;
|
||||||
|
@ -31,8 +31,6 @@ struct cpu {
|
|||||||
int previous_interrupts_enabled; // Were interrupts enabled before push_off()?
|
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.
|
// per-process data for the trap handling code in trampoline.S.
|
||||||
// sits in a page by itself just under the trampoline page in the
|
// sits in a page by itself just under the trampoline page in the
|
||||||
// user page table. not specially mapped in the kernel page table.
|
// user page table. not specially mapped in the kernel page table.
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
arch::riscv::{plic_sclaim, plic_senable, plic_spriority, PLIC, UART0_IRQ, VIRTIO0_IRQ},
|
arch::riscv::{plic_sclaim, plic_senable, plic_spriority, PLIC, UART0_IRQ, VIRTIO0_IRQ},
|
||||||
proc::cpu::cpuid,
|
proc::cpu::Cpu,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub unsafe fn plicinit() {
|
pub unsafe fn plicinit() {
|
||||||
@ -12,7 +12,7 @@ pub unsafe fn plicinit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn plicinithart() {
|
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
|
// Set enable bits for this hart's S-mode
|
||||||
// for the UART and VIRTIO disk.
|
// for the UART and VIRTIO disk.
|
||||||
@ -24,12 +24,12 @@ pub unsafe fn plicinithart() {
|
|||||||
|
|
||||||
/// Ask the PLIC what interrupt we should serve.
|
/// Ask the PLIC what interrupt we should serve.
|
||||||
pub unsafe fn plic_claim() -> i32 {
|
pub unsafe fn plic_claim() -> i32 {
|
||||||
let hart = cpuid() as u64;
|
let hart = Cpu::current_id() as u64;
|
||||||
*(plic_sclaim(hart) as *const i32)
|
*(plic_sclaim(hart) as *const i32)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tell the PLIC we've served this IRQ.
|
/// Tell the PLIC we've served this IRQ.
|
||||||
pub unsafe fn plic_complete(irq: i32) {
|
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;
|
*(plic_sclaim(hart) as *mut i32) = irq;
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ mod sync;
|
|||||||
mod syscall;
|
mod syscall;
|
||||||
mod trap;
|
mod trap;
|
||||||
|
|
||||||
use crate::{proc::cpu::cpuid, sync::mutex::Mutex};
|
use crate::{proc::cpu::Cpu, sync::mutex::Mutex};
|
||||||
use core::ffi::{c_char, CStr};
|
use core::ffi::{c_char, CStr};
|
||||||
|
|
||||||
pub(crate) use crate::console::printf::{print, println};
|
pub(crate) use crate::console::printf::{print, println};
|
||||||
@ -57,7 +57,7 @@ pub const FSSIZE: usize = 2000;
|
|||||||
pub const MAXPATH: usize = 128;
|
pub const MAXPATH: usize = 128;
|
||||||
|
|
||||||
pub unsafe fn main() -> ! {
|
pub unsafe fn main() -> ! {
|
||||||
if cpuid() == 0 {
|
if Cpu::current_id() == 0 {
|
||||||
console::consoleinit();
|
console::consoleinit();
|
||||||
mem::kalloc::kinit();
|
mem::kalloc::kinit();
|
||||||
println!("\nxv6 kernel is booting");
|
println!("\nxv6 kernel is booting");
|
||||||
|
@ -2,9 +2,7 @@ use super::{context::Context, proc::Proc};
|
|||||||
use crate::arch::riscv::asm::r_tp;
|
use crate::arch::riscv::asm::r_tp;
|
||||||
use core::ptr::{addr_of_mut, null_mut};
|
use core::ptr::{addr_of_mut, null_mut};
|
||||||
|
|
||||||
extern "C" {
|
pub static mut CPUS: [Cpu; crate::NCPU] = [Cpu::new(); crate::NCPU];
|
||||||
pub static mut cpus: [Cpu; crate::NCPU];
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Per-CPU state.
|
/// Per-CPU state.
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@ -27,19 +25,27 @@ impl Cpu {
|
|||||||
previous_interrupts_enabled: 0,
|
previous_interrupts_enabled: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Must be called with interrupts disabled
|
/// Must be called with interrupts disabled
|
||||||
/// to prevent race with process being moved
|
/// to prevent race with process being moved
|
||||||
/// to a different CPU.
|
/// to a different CPU.
|
||||||
pub unsafe fn cpuid() -> i32 {
|
pub fn current_id() -> usize {
|
||||||
r_tp() as i32
|
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.
|
/// Return this CPU's cpu struct.
|
||||||
/// 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 {
|
||||||
let id = cpuid();
|
Cpu::current_raw()
|
||||||
addr_of_mut!(cpus[id as usize])
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#![allow(clippy::comparison_chain)]
|
#![allow(clippy::comparison_chain)]
|
||||||
|
|
||||||
use super::{context::Context, cpu::mycpu, trapframe::Trapframe};
|
use super::{context::Context, cpu::Cpu, trapframe::Trapframe};
|
||||||
use crate::{
|
use crate::{
|
||||||
arch::riscv::{intr_get, Pagetable, PTE_W},
|
arch::riscv::{intr_get, Pagetable, PTE_W},
|
||||||
fs::file::{File, Inode},
|
fs::file::{File, Inode},
|
||||||
@ -101,7 +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 = mycpu();
|
let c = Cpu::current_raw();
|
||||||
(*c).proc
|
(*c).proc
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ 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 = mycpu();
|
let c = Cpu::current_raw();
|
||||||
|
|
||||||
if (*c).interrupt_disable_layers != 1 {
|
if (*c).interrupt_disable_layers != 1 {
|
||||||
panic!("sched locks");
|
panic!("sched locks");
|
||||||
|
@ -40,7 +40,7 @@ pub unsafe extern "C" fn start() {
|
|||||||
// Ask for clock interrupts.
|
// Ask for clock interrupts.
|
||||||
timerinit();
|
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());
|
w_tp(r_mhartid());
|
||||||
|
|
||||||
// Switch to supervisor mode and jump to main().
|
// Switch to supervisor mode and jump to main().
|
||||||
|
@ -2,7 +2,7 @@ use crate::{
|
|||||||
arch::riscv::*,
|
arch::riscv::*,
|
||||||
println,
|
println,
|
||||||
proc::{
|
proc::{
|
||||||
cpu::{cpuid, mycpu},
|
cpu::Cpu,
|
||||||
proc::{exit, killed, myproc, r#yield, setkilled, wakeup, ProcState},
|
proc::{exit, killed, myproc, r#yield, setkilled, wakeup, ProcState},
|
||||||
},
|
},
|
||||||
sync::mutex::Mutex,
|
sync::mutex::Mutex,
|
||||||
@ -70,7 +70,7 @@ pub unsafe fn devintr() -> i32 {
|
|||||||
// Software interrupt from a machine-mode timer interrupt,
|
// Software interrupt from a machine-mode timer interrupt,
|
||||||
// forwarded by timervec in kernelvec.S.
|
// forwarded by timervec in kernelvec.S.
|
||||||
|
|
||||||
if cpuid() == 0 {
|
if Cpu::current_id() == 0 {
|
||||||
clockintr();
|
clockintr();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ impl InterruptBlocker {
|
|||||||
pub fn new() -> InterruptBlocker {
|
pub fn new() -> InterruptBlocker {
|
||||||
unsafe {
|
unsafe {
|
||||||
let interrupts_before = intr_get();
|
let interrupts_before = intr_get();
|
||||||
let cpu = mycpu();
|
let cpu = Cpu::current_raw();
|
||||||
|
|
||||||
intr_off();
|
intr_off();
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ 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 = mycpu();
|
let cpu = Cpu::current_raw();
|
||||||
|
|
||||||
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");
|
||||||
@ -146,7 +146,7 @@ pub unsafe extern "C" fn usertrapret() {
|
|||||||
// process's kernel stack
|
// process's kernel stack
|
||||||
(*(*p).trapframe).kernel_sp = (*p).kstack + PGSIZE;
|
(*(*p).trapframe).kernel_sp = (*p).kstack + PGSIZE;
|
||||||
(*(*p).trapframe).kernel_trap = usertrap as usize as u64;
|
(*(*p).trapframe).kernel_trap = usertrap as usize as u64;
|
||||||
// hartid for cpuid()
|
// hartid for Cpu::current_id()
|
||||||
(*(*p).trapframe).kernel_hartid = r_tp();
|
(*(*p).trapframe).kernel_hartid = r_tp();
|
||||||
|
|
||||||
// Set up the registers that trampoline.S's
|
// Set up the registers that trampoline.S's
|
||||||
@ -272,7 +272,7 @@ 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 = mycpu();
|
let cpu = Cpu::current_raw();
|
||||||
|
|
||||||
intr_off();
|
intr_off();
|
||||||
if (*cpu).interrupt_disable_layers == 0 {
|
if (*cpu).interrupt_disable_layers == 0 {
|
||||||
@ -281,7 +281,7 @@ pub unsafe fn push_intr_off() {
|
|||||||
(*cpu).interrupt_disable_layers += 1;
|
(*cpu).interrupt_disable_layers += 1;
|
||||||
}
|
}
|
||||||
pub unsafe fn pop_intr_off() {
|
pub unsafe fn pop_intr_off() {
|
||||||
let cpu = mycpu();
|
let cpu = Cpu::current_raw();
|
||||||
|
|
||||||
if intr_get() == 1 {
|
if intr_get() == 1 {
|
||||||
// crate::panic_byte(b'0');
|
// crate::panic_byte(b'0');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user