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::{ use crate::{
fs::file::{devsw, CONSOLE}, fs::file::{devsw, CONSOLE},
proc::proc::{procdump, wakeup, Proc}, proc::process::{procdump, wakeup, Process},
sync::mutex::Mutex, sync::mutex::Mutex,
}; };
use core::{ffi::c_void, ptr::addr_of_mut}; 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 // Wait until interrupt handler has put
// some input into cons.buffer. // some input into cons.buffer.
while console.read_index == console.write_index { while console.read_index == console.write_index {
if Proc::current().unwrap().is_killed() { if Process::current().unwrap().is_killed() {
// cons.lock.unlock(); // cons.lock.unlock();
return -1; return -1;
} }

View File

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

View File

@ -4,7 +4,7 @@ use crate::{
fs::{log, stat::Stat}, fs::{log, stat::Stat},
io::pipe::Pipe, io::pipe::Pipe,
mem::virtual_memory::copyout, mem::virtual_memory::copyout,
proc::proc::Proc, proc::process::Process,
sync::{sleeplock::Sleeplock, spinlock::Spinlock}, sync::{sleeplock::Sleeplock, spinlock::Spinlock},
}; };
use core::ptr::{addr_of_mut, null_mut}; 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. /// `addr` is a user virtual address, pointing to a Stat.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn filestat(file: *mut File, addr: u64) -> i32 { 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(); let mut stat = Stat::default();
if (*file).kind == FileType::Inode || (*file).kind == FileType::Device { if (*file).kind == FileType::Inode || (*file).kind == FileType::Device {

View File

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

View File

@ -63,7 +63,7 @@ pub unsafe fn main() -> ! {
println!("\nxv6 kernel is booting"); println!("\nxv6 kernel is booting");
mem::virtual_memory::kvminit(); mem::virtual_memory::kvminit();
mem::virtual_memory::kvminithart(); mem::virtual_memory::kvminithart();
proc::proc::procinit(); proc::process::procinit();
trap::trapinithart(); trap::trapinithart();
arch::riscv::plic::plicinit(); arch::riscv::plic::plicinit();
arch::riscv::plic::plicinithart(); arch::riscv::plic::plicinithart();
@ -71,7 +71,7 @@ pub unsafe fn main() -> ! {
fs::iinit(); fs::iinit();
fs::file::fileinit(); fs::file::fileinit();
fs::virtio_disk::virtio_disk_init(); fs::virtio_disk::virtio_disk_init();
proc::proc::userinit(); proc::process::userinit();
STARTED = true; STARTED = true;
} else { } else {
while !STARTED { while !STARTED {
@ -82,7 +82,7 @@ pub unsafe fn main() -> ! {
arch::riscv::plic::plicinithart(); arch::riscv::plic::plicinithart();
} }
proc::proc::scheduler(); proc::process::scheduler();
} }
#[panic_handler] #[panic_handler]

View File

@ -7,7 +7,7 @@ use crate::{
kalloc::{kalloc, kfree}, kalloc::{kalloc, kfree},
memmove, memset, memmove, memset,
}, },
proc::proc::proc_mapstacks, proc::process::proc_mapstacks,
}; };
use core::ptr::{addr_of, addr_of_mut, null_mut}; 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 crate::arch::riscv::asm::r_tp;
use core::ptr::{addr_of_mut, null_mut}; 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)] #[repr(C)]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Cpu { pub struct Cpu {
pub proc: *mut Proc, pub proc: *mut Process,
/// swtch() here to enter scheduler() /// swtch() here to enter scheduler()
pub context: Context, pub context: Context,
/// Depth of push_off() nesting. /// Depth of push_off() nesting.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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