From 87caccf6237c781a4af63c87ea86f07e72c6e7c4 Mon Sep 17 00:00:00 2001 From: Garen Tyler Date: Fri, 3 Nov 2023 17:44:27 -0600 Subject: [PATCH] separate proc.rs into multiple modules --- kernel/rustkernel/src/arch/riscv/plic.rs | 2 +- kernel/rustkernel/src/console/mod.rs | 2 +- kernel/rustkernel/src/console/uart.rs | 3 +- kernel/rustkernel/src/fs/file.rs | 2 +- kernel/rustkernel/src/io/pipe.rs | 2 +- kernel/rustkernel/src/lib.rs | 8 +- kernel/rustkernel/src/mem/virtual_memory.rs | 2 +- kernel/rustkernel/src/proc/context.rs | 41 ++++++ kernel/rustkernel/src/proc/cpu.rs | 45 ++++++ kernel/rustkernel/src/proc/mod.rs | 4 + kernel/rustkernel/src/{ => proc}/proc.rs | 146 +------------------- kernel/rustkernel/src/proc/trapframe.rs | 100 ++++++++++++++ kernel/rustkernel/src/sync/lock.rs | 2 +- kernel/rustkernel/src/sync/sleeplock.rs | 2 +- kernel/rustkernel/src/sync/spinlock.rs | 2 +- kernel/rustkernel/src/syscall.rs | 2 +- kernel/rustkernel/src/trap.rs | 5 +- 17 files changed, 212 insertions(+), 158 deletions(-) create mode 100644 kernel/rustkernel/src/proc/context.rs create mode 100644 kernel/rustkernel/src/proc/cpu.rs create mode 100644 kernel/rustkernel/src/proc/mod.rs rename kernel/rustkernel/src/{ => proc}/proc.rs (66%) create mode 100644 kernel/rustkernel/src/proc/trapframe.rs diff --git a/kernel/rustkernel/src/arch/riscv/plic.rs b/kernel/rustkernel/src/arch/riscv/plic.rs index ac2122e..ed8793b 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::cpuid, + proc::cpu::cpuid, }; pub unsafe fn plicinit() { diff --git a/kernel/rustkernel/src/console/mod.rs b/kernel/rustkernel/src/console/mod.rs index 1c20fd5..bbe672c 100644 --- a/kernel/rustkernel/src/console/mod.rs +++ b/kernel/rustkernel/src/console/mod.rs @@ -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}; diff --git a/kernel/rustkernel/src/console/uart.rs b/kernel/rustkernel/src/console/uart.rs index a2b1b68..f40c4c9 100644 --- a/kernel/rustkernel/src/console/uart.rs +++ b/kernel/rustkernel/src/console/uart.rs @@ -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; diff --git a/kernel/rustkernel/src/fs/file.rs b/kernel/rustkernel/src/fs/file.rs index dbdf4e0..3e833fd 100644 --- a/kernel/rustkernel/src/fs/file.rs +++ b/kernel/rustkernel/src/fs/file.rs @@ -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}; diff --git a/kernel/rustkernel/src/io/pipe.rs b/kernel/rustkernel/src/io/pipe.rs index 9d0405d..47f3c6a 100644 --- a/kernel/rustkernel/src/io/pipe.rs +++ b/kernel/rustkernel/src/io/pipe.rs @@ -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}; diff --git a/kernel/rustkernel/src/lib.rs b/kernel/rustkernel/src/lib.rs index d4ef753..44794f5 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::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] diff --git a/kernel/rustkernel/src/mem/virtual_memory.rs b/kernel/rustkernel/src/mem/virtual_memory.rs index ef8a7b2..8339c37 100644 --- a/kernel/rustkernel/src/mem/virtual_memory.rs +++ b/kernel/rustkernel/src/mem/virtual_memory.rs @@ -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}; diff --git a/kernel/rustkernel/src/proc/context.rs b/kernel/rustkernel/src/proc/context.rs new file mode 100644 index 0000000..12ee7c8 --- /dev/null +++ b/kernel/rustkernel/src/proc/context.rs @@ -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, + } + } +} diff --git a/kernel/rustkernel/src/proc/cpu.rs b/kernel/rustkernel/src/proc/cpu.rs new file mode 100644 index 0000000..ad9f3b5 --- /dev/null +++ b/kernel/rustkernel/src/proc/cpu.rs @@ -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]) +} diff --git a/kernel/rustkernel/src/proc/mod.rs b/kernel/rustkernel/src/proc/mod.rs new file mode 100644 index 0000000..44134d1 --- /dev/null +++ b/kernel/rustkernel/src/proc/mod.rs @@ -0,0 +1,4 @@ +pub mod context; +pub mod cpu; +pub mod proc; +pub mod trapframe; diff --git a/kernel/rustkernel/src/proc.rs b/kernel/rustkernel/src/proc/proc.rs similarity index 66% rename from kernel/rustkernel/src/proc.rs rename to kernel/rustkernel/src/proc/proc.rs index 87f670a..1fd9345 100644 --- a/kernel/rustkernel/src/proc.rs +++ b/kernel/rustkernel/src/proc/proc.rs @@ -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 { diff --git a/kernel/rustkernel/src/proc/trapframe.rs b/kernel/rustkernel/src/proc/trapframe.rs new file mode 100644 index 0000000..2f835a7 --- /dev/null +++ b/kernel/rustkernel/src/proc/trapframe.rs @@ -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, + } + } +} diff --git a/kernel/rustkernel/src/sync/lock.rs b/kernel/rustkernel/src/sync/lock.rs index 52c50a2..bbaeba9 100644 --- a/kernel/rustkernel/src/sync/lock.rs +++ b/kernel/rustkernel/src/sync/lock.rs @@ -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, diff --git a/kernel/rustkernel/src/sync/sleeplock.rs b/kernel/rustkernel/src/sync/sleeplock.rs index 9ede6b2..339d16b 100644 --- a/kernel/rustkernel/src/sync/sleeplock.rs +++ b/kernel/rustkernel/src/sync/sleeplock.rs @@ -1,4 +1,4 @@ -use crate::proc::{sleep, wakeup}; +use crate::proc::proc::{sleep, wakeup}; use core::{ ffi::c_char, ptr::addr_of, diff --git a/kernel/rustkernel/src/sync/spinlock.rs b/kernel/rustkernel/src/sync/spinlock.rs index a034940..c8b0f3d 100644 --- a/kernel/rustkernel/src/sync/spinlock.rs +++ b/kernel/rustkernel/src/sync/spinlock.rs @@ -1,5 +1,5 @@ use crate::{ - proc::{myproc, sched, ProcState}, + proc::proc::{myproc, sched, ProcState}, trap::{pop_intr_off, push_intr_off}, }; use core::{ diff --git a/kernel/rustkernel/src/syscall.rs b/kernel/rustkernel/src/syscall.rs index 5827187..d888e79 100644 --- a/kernel/rustkernel/src/syscall.rs +++ b/kernel/rustkernel/src/syscall.rs @@ -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, diff --git a/kernel/rustkernel/src/trap.rs b/kernel/rustkernel/src/trap.rs index e9bd8d2..7ef06cd 100644 --- a/kernel/rustkernel/src/trap.rs +++ b/kernel/rustkernel/src/trap.rs @@ -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, };