rename Proc to Process

This commit is contained in:
Garen Tyler 2023-11-03 19:01:50 -06:00
parent 791b68aaa7
commit 71f9a49704
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
14 changed files with 81 additions and 82 deletions

View File

@ -13,7 +13,7 @@ pub mod uart;
use crate::{
fs::file::{devsw, CONSOLE},
proc::proc::{procdump, wakeup, Proc},
proc::process::{procdump, wakeup, Process},
sync::mutex::Mutex,
};
use core::{ffi::c_void, ptr::addr_of_mut};
@ -114,7 +114,7 @@ pub fn consoleread(user_dst: i32, mut dst: u64, mut n: i32) -> i32 {
// Wait until interrupt handler has put
// some input into cons.buffer.
while console.read_index == console.write_index {
if Proc::current().unwrap().is_killed() {
if Process::current().unwrap().is_killed() {
// cons.lock.unlock();
return -1;
}

View File

@ -2,7 +2,7 @@
#![allow(non_upper_case_globals)]
use crate::{
console::consoleintr, proc::proc::wakeup, queue::Queue, sync::mutex::Mutex,
console::consoleintr, proc::process::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::proc::Proc,
proc::process::Process,
sync::{sleeplock::Sleeplock, spinlock::Spinlock},
};
use core::ptr::{addr_of_mut, null_mut};
@ -206,7 +206,7 @@ pub unsafe extern "C" fn fileclose(file: *mut File) {
/// `addr` is a user virtual address, pointing to a Stat.
#[no_mangle]
pub unsafe extern "C" fn filestat(file: *mut File, addr: u64) -> i32 {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let mut stat = Stat::default();
if (*file).kind == FileType::Inode || (*file).kind == FileType::Device {

View File

@ -4,7 +4,7 @@ use crate::{
kalloc::{kalloc, kfree},
virtual_memory::{copyin, copyout},
},
proc::proc::{wakeup, Proc},
proc::process::{wakeup, Process},
sync::spinlock::Spinlock,
};
use core::ptr::{addr_of, addr_of_mut};
@ -88,7 +88,7 @@ impl Pipe {
}
pub unsafe fn write(&self, addr: u64, num_bytes: usize) -> Result<usize> {
let mut i = 0;
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let guard = self.lock.lock();
while i < num_bytes {
@ -116,7 +116,7 @@ impl Pipe {
#[allow(clippy::while_immutable_condition)]
pub unsafe fn read(&self, addr: u64, num_bytes: usize) -> Result<usize> {
let mut i = 0;
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let guard = self.lock.lock();
// DOC: pipe-empty

View File

@ -63,7 +63,7 @@ pub unsafe fn main() -> ! {
println!("\nxv6 kernel is booting");
mem::virtual_memory::kvminit();
mem::virtual_memory::kvminithart();
proc::proc::procinit();
proc::process::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::proc::userinit();
proc::process::userinit();
STARTED = true;
} else {
while !STARTED {
@ -82,7 +82,7 @@ pub unsafe fn main() -> ! {
arch::riscv::plic::plicinithart();
}
proc::proc::scheduler();
proc::process::scheduler();
}
#[panic_handler]

View File

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

View File

@ -1,4 +1,4 @@
use super::{context::Context, proc::Proc};
use super::{context::Context, process::Process};
use crate::arch::riscv::asm::r_tp;
use core::ptr::{addr_of_mut, null_mut};
@ -8,7 +8,7 @@ pub static mut CPUS: [Cpu; crate::NCPU] = [Cpu::new(); crate::NCPU];
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Cpu {
pub proc: *mut Proc,
pub proc: *mut Process,
/// swtch() here to enter scheduler()
pub context: Context,
/// Depth of push_off() nesting.

View File

@ -1,5 +1,4 @@
pub mod context;
pub mod cpu;
#[allow(clippy::module_inception)]
pub mod proc;
pub mod process;
pub mod trapframe;

View File

@ -14,8 +14,8 @@ use core::{
};
extern "C" {
pub static mut proc: [Proc; crate::NPROC];
pub static mut initproc: *mut Proc;
pub static mut proc: [Process; crate::NPROC];
pub static mut initproc: *mut Process;
pub static mut nextpid: i32;
pub static mut pid_lock: Spinlock;
/// Helps ensure that wakeups of wait()ing
@ -34,11 +34,11 @@ extern "C" {
pub fn wait(addr: u64) -> i32;
pub fn procdump();
pub fn proc_mapstacks(kpgtbl: Pagetable);
pub fn proc_pagetable(p: *mut Proc) -> Pagetable;
pub fn proc_pagetable(p: *mut Process) -> Pagetable;
pub fn proc_freepagetable(pagetable: Pagetable, sz: u64);
pub fn wakeup(chan: *const c_void);
pub fn allocproc() -> *mut Proc;
// pub fn freeproc(p: *mut Proc);
pub fn allocproc() -> *mut Process;
// pub fn freeproc(p: *mut Process);
pub fn uvmalloc(pagetable: Pagetable, oldsz: u64, newsz: u64, xperm: i32) -> u64;
pub fn uvmdealloc(pagetable: Pagetable, oldsz: u64, newsz: u64) -> u64;
// pub fn sched();
@ -50,7 +50,7 @@ pub static NEXT_PID: AtomicI32 = AtomicI32::new(1);
#[repr(C)]
#[derive(PartialEq, Default)]
pub enum ProcState {
pub enum ProcessState {
#[default]
Unused,
Used,
@ -62,12 +62,12 @@ pub enum ProcState {
/// Per-process state.
#[repr(C)]
pub struct Proc {
pub struct Process {
pub lock: Spinlock,
// p->lock must be held when using these:
/// Process state
pub state: ProcState,
pub state: ProcessState,
/// If non-zero, sleeping on chan
pub chan: *mut c_void,
/// If non-zero, have been killed
@ -79,7 +79,7 @@ pub struct Proc {
// wait_lock msut be held when using this:
/// Parent process
pub parent: *mut Proc,
pub parent: *mut Process,
// These are private to the process, so p->lock need not be held.
/// Virtual address of kernel stack
@ -99,11 +99,11 @@ pub struct Proc {
/// Process name (debugging)
pub name: [c_char; 16],
}
impl Proc {
pub const fn new() -> Proc {
Proc {
impl Process {
pub const fn new() -> Process {
Process {
lock: Spinlock::new(),
state: ProcState::Unused,
state: ProcessState::Unused,
chan: null_mut(),
killed: 0,
xstate: 0,
@ -119,7 +119,7 @@ impl Proc {
name: [0x00; 16],
}
}
pub fn current() -> Option<&'static mut Proc> {
pub fn current() -> Option<&'static mut Process> {
let _ = crate::trap::InterruptBlocker::new();
let p = Cpu::current().proc;
if p.is_null() {
@ -147,7 +147,7 @@ impl Proc {
self.chan = null_mut();
self.killed = 0;
self.xstate = 0;
self.state = ProcState::Unused;
self.state = ProcessState::Unused;
}
/// Kill the process with the given pid.
@ -157,15 +157,15 @@ impl Proc {
pub unsafe fn kill(pid: i32) -> bool {
for p in &mut proc {
let _guard = p.lock.lock();
if p.pid == pid {
p.killed = 1;
if p.state == ProcState::Sleeping {
if p.state == ProcessState::Sleeping {
// Wake process from sleep().
p.state = ProcState::Runnable;
p.state = ProcessState::Runnable;
}
return true;
}
}
@ -188,9 +188,9 @@ impl Proc {
/// Return the current struct proc *, or zero if none.
#[no_mangle]
pub extern "C" fn myproc() -> *mut Proc {
if let Some(p) = Proc::current() {
p as *mut Proc
pub extern "C" fn myproc() -> *mut Process {
if let Some(p) = Process::current() {
p as *mut Process
} else {
null_mut()
}
@ -204,15 +204,15 @@ pub extern "C" fn allocpid() -> i32 {
/// Free a proc structure and the data hanging from it, including user pages.
/// p->lock must be held.
#[no_mangle]
pub unsafe extern "C" fn freeproc(p: *mut Proc) {
pub unsafe extern "C" fn freeproc(p: *mut Process) {
(*p).free();
}
/// Pass p's abandoned children to init.
/// Caller must hold wait_lock.
#[no_mangle]
pub unsafe extern "C" fn reparent(p: *mut Proc) {
for pp in proc.iter_mut().map(|p: &mut Proc| addr_of_mut!(*p)) {
pub unsafe extern "C" fn reparent(p: *mut Process) {
for pp in proc.iter_mut().map(|p: &mut Process| addr_of_mut!(*p)) {
if (*pp).parent == p {
(*pp).parent = initproc;
wakeup(initproc.cast());
@ -223,7 +223,7 @@ pub unsafe extern "C" fn reparent(p: *mut Proc) {
/// Grow or shrink user memory by n bytes.
/// Return 0 on success, -1 on failure.
pub unsafe fn growproc(n: i32) -> i32 {
let p = Proc::current().unwrap();
let p = Process::current().unwrap();
let mut sz = p.sz;
if n > 0 {
@ -240,9 +240,9 @@ pub unsafe fn growproc(n: i32) -> i32 {
/// Give up the CPU for one scheduling round.
pub unsafe fn r#yield() {
let p = Proc::current().unwrap();
let p = Process::current().unwrap();
let _guard = p.lock.lock();
p.state = ProcState::Runnable;
p.state = ProcessState::Runnable;
sched();
}
@ -255,12 +255,12 @@ pub unsafe fn r#yield() {
/// there's no process.
#[no_mangle]
pub unsafe extern "C" fn sched() {
let p = Proc::current().unwrap();
let p = Process::current().unwrap();
let cpu = Cpu::current();
if cpu.interrupt_disable_layers != 1 {
panic!("sched locks");
} else if p.state == ProcState::Running {
} else if p.state == ProcessState::Running {
panic!("sched running");
} else if intr_get() > 0 {
panic!("sched interruptible");
@ -283,12 +283,12 @@ pub unsafe extern "C" fn sleep_lock(chan: *mut c_void, lock: *mut Spinlock) {
/// Sleep until `wakeup(chan)` is called somewhere else.
pub unsafe fn sleep(chan: *mut c_void) {
let p = Proc::current().unwrap();
let p = Process::current().unwrap();
let _guard = p.lock.lock();
// Go to sleep.
p.chan = chan;
p.state = ProcState::Sleeping;
p.state = ProcessState::Sleeping;
sched();
@ -301,7 +301,7 @@ pub unsafe fn sleep(chan: *mut c_void) {
/// to user space (see usertrap() in trap.c).
#[no_mangle]
pub unsafe extern "C" fn kill(pid: i32) -> i32 {
if Proc::kill(pid) {
if Process::kill(pid) {
1
} else {
0
@ -309,12 +309,12 @@ pub unsafe extern "C" fn kill(pid: i32) -> i32 {
}
#[no_mangle]
pub unsafe extern "C" fn setkilled(p: *mut Proc) {
pub unsafe extern "C" fn setkilled(p: *mut Process) {
(*p).set_killed(true);
}
#[no_mangle]
pub unsafe extern "C" fn killed(p: *mut Proc) -> i32 {
pub unsafe extern "C" fn killed(p: *mut Process) -> i32 {
if (*p).is_killed() {
1
} else {

View File

@ -1,5 +1,5 @@
use super::LockStrategy;
use crate::proc::proc::{sched, sleep, wakeup, Proc, ProcState};
use crate::proc::process::{sched, sleep, wakeup, Process, ProcessState};
use core::{
cell::UnsafeCell,
ops::Drop,
@ -83,14 +83,14 @@ impl<'l> LockGuard<'l> {
/// Sleep until `wakeup(chan)` is called somewhere
/// else, yielding access to the lock until then.
pub unsafe fn sleep(&self, chan: *mut core::ffi::c_void) {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let _guard = proc.lock.lock();
let strategy = self.lock.lock_strategy();
self.lock.unlock();
// Put the process to sleep.
proc.chan = chan;
proc.state = ProcState::Sleeping;
proc.state = ProcessState::Sleeping;
sched();
// Tidy up and reacquire the lock.

View File

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

View File

@ -1,5 +1,5 @@
use crate::{
proc::proc::{sched, Proc, ProcState},
proc::process::{sched, Process, ProcessState},
trap::{pop_intr_off, push_intr_off},
};
use core::{
@ -46,13 +46,13 @@ pub struct SpinlockGuard<'l> {
impl<'l> SpinlockGuard<'l> {
/// Sleep until `wakeup(chan)` is called somewhere else, yielding the lock until then.
pub unsafe fn sleep(&self, chan: *mut core::ffi::c_void) {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let _guard = proc.lock.lock();
self.lock.unlock();
// Put the process to sleep.
proc.chan = chan;
proc.state = ProcState::Sleeping;
proc.state = ProcessState::Sleeping;
sched();
// Tidy up and reacquire the lock.

View File

@ -8,7 +8,7 @@ use crate::{
},
mem::virtual_memory::{copyin, copyinstr},
println,
proc::proc::{self, Proc},
proc::process::{self, Process},
string::strlen,
trap::CLOCK_TICKS,
NOFILE,
@ -57,16 +57,16 @@ pub enum Syscall {
impl Syscall {
pub unsafe fn call(&self) -> u64 {
match self {
Syscall::Fork => proc::fork() as u64,
Syscall::Fork => process::fork() as u64,
Syscall::Exit => {
let mut n = 0i32;
argint(0, addr_of_mut!(n));
proc::exit(n)
process::exit(n)
}
Syscall::Wait => {
let mut p = 0u64;
argaddr(0, addr_of_mut!(p));
proc::wait(p) as u64
process::wait(p) as u64
}
Syscall::Pipe => sys_pipe(),
Syscall::Read => {
@ -85,7 +85,7 @@ impl Syscall {
Syscall::Kill => {
let mut pid = 0i32;
argint(0, addr_of_mut!(pid));
Proc::kill(pid) as u64
Process::kill(pid) as u64
}
Syscall::Exec => sys_exec(),
Syscall::Fstat => {
@ -102,7 +102,7 @@ impl Syscall {
}
Syscall::Chdir => {
let mut path = [0u8; crate::MAXPATH];
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let _operation = LogOperation::new();
@ -138,13 +138,13 @@ impl Syscall {
file::filedup(file);
file_descriptor as u64
}
Syscall::Getpid => Proc::current().unwrap().pid as u64,
Syscall::Getpid => Process::current().unwrap().pid as u64,
Syscall::Sbrk => {
let mut n = 0i32;
argint(0, addr_of_mut!(n));
let addr = Proc::current().unwrap().sz;
let addr = Process::current().unwrap().sz;
if proc::growproc(n) < 0 {
if process::growproc(n) < 0 {
-1i64 as u64
} else {
addr
@ -157,7 +157,7 @@ impl Syscall {
let mut ticks = CLOCK_TICKS.lock_spinning();
while *ticks < *ticks + n as usize {
if Proc::current().unwrap().is_killed() {
if Process::current().unwrap().is_killed() {
return -1i64 as u64;
}
// Sleep until the value changes.
@ -191,7 +191,7 @@ impl Syscall {
let mut file: *mut File = null_mut();
if argfd(0, addr_of_mut!(file_descriptor), addr_of_mut!(file)) >= 0 {
Proc::current().unwrap().ofile[file_descriptor as usize] = null_mut();
Process::current().unwrap().ofile[file_descriptor as usize] = null_mut();
file::fileclose(file);
0
} else {
@ -269,7 +269,7 @@ impl From<Syscall> for usize {
/// Fetch the u64 at addr from the current process.
#[no_mangle]
pub unsafe extern "C" fn fetchaddr(addr: u64, ip: *mut u64) -> i32 {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
// Both tests needed, in case of overflow.
if addr >= proc.sz
@ -292,7 +292,7 @@ pub unsafe extern "C" fn fetchaddr(addr: u64, ip: *mut u64) -> i32 {
/// Returns length of string, not including null, or -1 for error.
#[no_mangle]
pub unsafe extern "C" fn fetchstr(addr: u64, buf: *mut u8, max: i32) -> i32 {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
if copyinstr(proc.pagetable, buf, addr, max as u64) < 0 {
-1
@ -304,7 +304,7 @@ pub unsafe extern "C" fn fetchstr(addr: u64, buf: *mut u8, max: i32) -> i32 {
/// Allocate a file descriptor for the given file.
/// Takes over file reference from caller on success.
unsafe fn fdalloc(file: *mut File) -> Result<usize, ()> {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
for file_descriptor in 0..crate::NOFILE {
if proc.ofile[file_descriptor].is_null() {
@ -316,7 +316,7 @@ unsafe fn fdalloc(file: *mut File) -> Result<usize, ()> {
}
unsafe fn argraw(argument_index: usize) -> u64 {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
match argument_index {
0 => (*proc.trapframe).a0,
@ -357,7 +357,7 @@ pub unsafe extern "C" fn argfd(
return -1;
}
let file: *mut File = Proc::current().unwrap().ofile[file_descriptor];
let file: *mut File = Process::current().unwrap().ofile[file_descriptor];
if file.is_null() {
return -1;
}
@ -383,7 +383,7 @@ pub unsafe extern "C" fn argstr(n: i32, buf: *mut u8, max: i32) -> i32 {
}
pub unsafe fn syscall() {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
let num = (*proc.trapframe).a7;

View File

@ -3,7 +3,7 @@ use crate::{
println,
proc::{
cpu::Cpu,
proc::{exit, r#yield, wakeup, Proc, ProcState},
process::{exit, r#yield, wakeup, Process, ProcessState},
},
sync::mutex::Mutex,
syscall::syscall,
@ -127,7 +127,7 @@ impl !Send for InterruptBlocker {}
/// Return to user space
#[no_mangle]
pub unsafe extern "C" fn usertrapret() {
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
// We're about to switch the destination of traps from
// kerneltrap() to usertrap(), so turn off interrupts until
@ -196,8 +196,8 @@ pub unsafe extern "C" fn kerneltrap() {
println!("scause {}\nsepc={} stval={}", scause, r_sepc(), r_stval());
panic!("kerneltrap");
} else if which_dev == 2
&& Proc::current().is_some()
&& Proc::current().unwrap().state == ProcState::Running
&& Process::current().is_some()
&& Process::current().unwrap().state == ProcessState::Running
{
// Give up the CPU if this is a timer interrupt.
r#yield();
@ -222,14 +222,14 @@ pub unsafe extern "C" fn usertrap() {
// since we're now in the kernel.
w_stvec(kernelvec as usize as u64);
let proc = Proc::current().unwrap();
let proc = Process::current().unwrap();
// Save user program counter.
(*proc.trapframe).epc = r_sepc();
if r_scause() == 8 {
// System call
if proc.is_killed() {
exit(-1);
}