separate proc.rs into multiple modules

This commit is contained in:
Garen Tyler 2023-11-03 17:44:27 -06:00
parent 4cb940606c
commit 87caccf623
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
17 changed files with 212 additions and 158 deletions

View File

@ -2,7 +2,7 @@
use crate::{
arch::riscv::{plic_sclaim, plic_senable, plic_spriority, PLIC, UART0_IRQ, VIRTIO0_IRQ},
proc::cpuid,
proc::cpu::cpuid,
};
pub unsafe fn plicinit() {

View File

@ -13,7 +13,7 @@ pub mod uart;
use crate::{
fs::file::{devsw, CONSOLE},
proc::{killed, myproc, procdump, wakeup},
proc::proc::{killed, myproc, procdump, wakeup},
sync::mutex::Mutex,
};
use core::{ffi::c_void, ptr::addr_of_mut};

View File

@ -2,7 +2,8 @@
#![allow(non_upper_case_globals)]
use crate::{
console::consoleintr, proc::wakeup, queue::Queue, sync::mutex::Mutex, trap::InterruptBlocker,
console::consoleintr, proc::proc::wakeup, queue::Queue, sync::mutex::Mutex,
trap::InterruptBlocker,
};
use core::ptr::addr_of;

View File

@ -4,7 +4,7 @@ use crate::{
fs::{log, stat::Stat},
io::pipe::Pipe,
mem::virtual_memory::copyout,
proc::myproc,
proc::proc::myproc,
sync::{sleeplock::Sleeplock, spinlock::Spinlock},
};
use core::ptr::{addr_of_mut, null_mut};

View File

@ -4,7 +4,7 @@ use crate::{
kalloc::{kalloc, kfree},
virtual_memory::{copyin, copyout},
},
proc::{killed, myproc, wakeup},
proc::proc::{killed, myproc, wakeup},
sync::spinlock::Spinlock,
};
use core::ptr::{addr_of, addr_of_mut};

View File

@ -21,7 +21,7 @@ mod sync;
mod syscall;
mod trap;
use crate::{proc::cpuid, sync::mutex::Mutex};
use crate::{proc::cpu::cpuid, sync::mutex::Mutex};
use core::ffi::{c_char, CStr};
pub(crate) use crate::console::printf::{print, println};
@ -63,7 +63,7 @@ pub unsafe fn main() -> ! {
println!("\nxv6 kernel is booting");
mem::virtual_memory::kvminit();
mem::virtual_memory::kvminithart();
proc::procinit();
proc::proc::procinit();
trap::trapinithart();
arch::riscv::plic::plicinit();
arch::riscv::plic::plicinithart();
@ -71,7 +71,7 @@ pub unsafe fn main() -> ! {
fs::iinit();
fs::file::fileinit();
fs::virtio_disk::virtio_disk_init();
proc::userinit();
proc::proc::userinit();
STARTED = true;
} else {
while !STARTED {
@ -82,7 +82,7 @@ pub unsafe fn main() -> ! {
arch::riscv::plic::plicinithart();
}
proc::scheduler();
proc::proc::scheduler();
}
#[panic_handler]

View File

@ -7,7 +7,7 @@ use crate::{
kalloc::{kalloc, kfree},
memmove, memset,
},
proc::proc_mapstacks,
proc::proc::proc_mapstacks,
};
use core::ptr::{addr_of, addr_of_mut, null_mut};

View File

@ -0,0 +1,41 @@
/// Saved registers for kernel context switches.
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct Context {
pub ra: u64,
pub sp: u64,
// callee-saved
pub s0: u64,
pub s1: u64,
pub s2: u64,
pub s3: u64,
pub s4: u64,
pub s5: u64,
pub s6: u64,
pub s7: u64,
pub s8: u64,
pub s9: u64,
pub s10: u64,
pub s11: u64,
}
impl Context {
pub const fn new() -> Context {
Context {
ra: 0u64,
sp: 0u64,
s0: 0u64,
s1: 0u64,
s2: 0u64,
s3: 0u64,
s4: 0u64,
s5: 0u64,
s6: 0u64,
s7: 0u64,
s8: 0u64,
s9: 0u64,
s10: 0u64,
s11: 0u64,
}
}
}

View File

@ -0,0 +1,45 @@
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];
}
/// Per-CPU state.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Cpu {
pub proc: *mut Proc,
/// swtch() here to enter scheduler()
pub context: Context,
/// Depth of push_off() nesting.
pub interrupt_disable_layers: i32,
/// Were interrupts enabled before push_off()?
pub previous_interrupts_enabled: i32,
}
impl Cpu {
pub const fn new() -> Cpu {
Cpu {
proc: null_mut(),
context: Context::new(),
interrupt_disable_layers: 0,
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
}
/// 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])
}

View File

@ -0,0 +1,4 @@
pub mod context;
pub mod cpu;
pub mod proc;
pub mod trapframe;

View File

@ -1,7 +1,8 @@
#![allow(clippy::comparison_chain)]
use super::{context::Context, cpu::mycpu, trapframe::Trapframe};
use crate::{
arch::riscv::{intr_get, r_tp, Pagetable, PTE_W},
arch::riscv::{intr_get, Pagetable, PTE_W},
fs::file::{File, Inode},
mem::kalloc::kfree,
sync::spinlock::{Spinlock, SpinlockGuard},
@ -12,7 +13,6 @@ use core::{
};
extern "C" {
pub static mut cpus: [Cpu; crate::NCPU];
pub static mut proc: [Proc; crate::NPROC];
pub static mut initproc: *mut Proc;
pub static mut nextpid: i32;
@ -45,131 +45,6 @@ extern "C" {
pub fn swtch(a: *mut Context, b: *mut Context);
}
/// Saved registers for kernel context switches.
#[repr(C)]
#[derive(Copy, Clone, Default)]
pub struct Context {
pub ra: u64,
pub sp: u64,
// callee-saved
pub s0: u64,
pub s1: u64,
pub s2: u64,
pub s3: u64,
pub s4: u64,
pub s5: u64,
pub s6: u64,
pub s7: u64,
pub s8: u64,
pub s9: u64,
pub s10: u64,
pub s11: u64,
}
impl Context {
pub const fn new() -> Context {
Context {
ra: 0u64,
sp: 0u64,
s0: 0u64,
s1: 0u64,
s2: 0u64,
s3: 0u64,
s4: 0u64,
s5: 0u64,
s6: 0u64,
s7: 0u64,
s8: 0u64,
s9: 0u64,
s10: 0u64,
s11: 0u64,
}
}
}
/// Per-CPU state.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Cpu {
pub proc: *mut Proc,
/// swtch() here to enter scheduler()
pub context: Context,
/// Depth of push_off() nesting.
pub interrupt_disable_layers: i32,
/// Were interrupts enabled before push_off()?
pub previous_interrupts_enabled: i32,
}
impl Cpu {
pub const fn new() -> Cpu {
Cpu {
proc: null_mut(),
// proc: None,
context: Context::new(),
interrupt_disable_layers: 0,
previous_interrupts_enabled: 0,
}
}
}
/// 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.
/// uservec in trampoline.S saves user registers in the trapframe,
/// then initializes registers from the trapframe's
/// kernel_sp, kernel_hartid, kernel_satp, and jumps to kernel_trap.
/// usertrapret() and userret in trampoline.S set up
/// the trapframe's kernel_*, restore user registers from the
/// trapframe, switch to the user page table, and enter user space.
/// the trapframe includes callee-saved user registers like s0-s11 because the
/// return-to-user path via usertrapret() doesn't return through
/// the entire kernel call stack.
#[repr(C)]
#[derive(Default)]
pub struct TrapFrame {
/// Kernel page table.
pub kernel_satp: u64,
/// Top of process's kernel stack.
pub kernel_sp: u64,
/// usertrap()
pub kernel_trap: u64,
/// Saved user program counter.
pub epc: u64,
/// Saved kernel tp.
pub kernel_hartid: u64,
pub ra: u64,
pub sp: u64,
pub gp: u64,
pub tp: u64,
pub t0: u64,
pub t1: u64,
pub t2: u64,
pub s0: u64,
pub s1: u64,
pub a0: u64,
pub a1: u64,
pub a2: u64,
pub a3: u64,
pub a4: u64,
pub a5: u64,
pub a6: u64,
pub a7: u64,
pub s2: u64,
pub s3: u64,
pub s4: u64,
pub s5: u64,
pub s6: u64,
pub s7: u64,
pub s8: u64,
pub s9: u64,
pub s10: u64,
pub s11: u64,
pub t3: u64,
pub t4: u64,
pub t5: u64,
pub t6: u64,
}
#[repr(C)]
#[derive(PartialEq, Default)]
pub enum ProcState {
@ -211,7 +86,7 @@ pub struct Proc {
/// User page table
pub pagetable: Pagetable,
/// Data page for trampoline.S
pub trapframe: *mut TrapFrame,
pub trapframe: *mut Trapframe,
/// swtch() here to run process
pub context: Context,
/// Open files
@ -222,21 +97,6 @@ pub struct Proc {
pub name: [c_char; 16],
}
/// 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
}
/// 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])
}
/// Return the current struct proc *, or zero if none.
#[no_mangle]
pub unsafe extern "C" fn myproc() -> *mut Proc {

View File

@ -0,0 +1,100 @@
/// 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.
/// uservec in trampoline.S saves user registers in the trapframe,
/// then initializes registers from the trapframe's
/// kernel_sp, kernel_hartid, kernel_satp, and jumps to kernel_trap.
/// usertrapret() and userret in trampoline.S set up
/// the trapframe's kernel_*, restore user registers from the
/// trapframe, switch to the user page table, and enter user space.
/// the trapframe includes callee-saved user registers like s0-s11 because the
/// return-to-user path via usertrapret() doesn't return through
/// the entire kernel call stack.
#[repr(C)]
#[derive(Default)]
pub struct Trapframe {
/// Kernel page table.
pub kernel_satp: u64,
/// Top of process's kernel stack.
pub kernel_sp: u64,
/// usertrap()
pub kernel_trap: u64,
/// Saved user program counter.
pub epc: u64,
/// Saved kernel tp.
pub kernel_hartid: u64,
pub ra: u64,
pub sp: u64,
pub gp: u64,
pub tp: u64,
pub t0: u64,
pub t1: u64,
pub t2: u64,
pub s0: u64,
pub s1: u64,
pub a0: u64,
pub a1: u64,
pub a2: u64,
pub a3: u64,
pub a4: u64,
pub a5: u64,
pub a6: u64,
pub a7: u64,
pub s2: u64,
pub s3: u64,
pub s4: u64,
pub s5: u64,
pub s6: u64,
pub s7: u64,
pub s8: u64,
pub s9: u64,
pub s10: u64,
pub s11: u64,
pub t3: u64,
pub t4: u64,
pub t5: u64,
pub t6: u64,
}
impl Trapframe {
pub const fn new() -> Trapframe {
Trapframe {
kernel_satp: 0u64,
kernel_sp: 0u64,
kernel_trap: 0u64,
epc: 0u64,
kernel_hartid: 0u64,
ra: 0u64,
sp: 0u64,
gp: 0u64,
tp: 0u64,
t0: 0u64,
t1: 0u64,
t2: 0u64,
s0: 0u64,
s1: 0u64,
a0: 0u64,
a1: 0u64,
a2: 0u64,
a3: 0u64,
a4: 0u64,
a5: 0u64,
a6: 0u64,
a7: 0u64,
s2: 0u64,
s3: 0u64,
s4: 0u64,
s5: 0u64,
s6: 0u64,
s7: 0u64,
s8: 0u64,
s9: 0u64,
s10: 0u64,
s11: 0u64,
t3: 0u64,
t4: 0u64,
t5: 0u64,
t6: 0u64,
}
}
}

View File

@ -1,5 +1,5 @@
use super::LockStrategy;
use crate::proc::{myproc, sched, sleep, wakeup, ProcState};
use crate::proc::proc::{myproc, sched, sleep, wakeup, ProcState};
use core::{
cell::UnsafeCell,
ops::Drop,

View File

@ -1,4 +1,4 @@
use crate::proc::{sleep, wakeup};
use crate::proc::proc::{sleep, wakeup};
use core::{
ffi::c_char,
ptr::addr_of,

View File

@ -1,5 +1,5 @@
use crate::{
proc::{myproc, sched, ProcState},
proc::proc::{myproc, sched, ProcState},
trap::{pop_intr_off, push_intr_off},
};
use core::{

View File

@ -8,7 +8,7 @@ use crate::{
},
mem::virtual_memory::{copyin, copyinstr},
println,
proc::{self, myproc},
proc::proc::{self, myproc},
string::strlen,
trap::CLOCK_TICKS,
NOFILE,

View File

@ -1,7 +1,10 @@
use crate::{
arch::riscv::*,
println,
proc::{cpuid, exit, killed, mycpu, myproc, r#yield, setkilled, wakeup, ProcState},
proc::{
cpu::{cpuid, mycpu},
proc::{exit, killed, myproc, r#yield, setkilled, wakeup, ProcState},
},
sync::mutex::Mutex,
syscall::syscall,
};