refactor: fix all clippy warnings

This commit is contained in:
Mark Poliakov 2021-11-12 10:26:58 +02:00
parent c584e1182f
commit 34dbd9d902
7 changed files with 49 additions and 31 deletions

View File

@ -109,8 +109,9 @@ impl Context {
}
}
pub fn user_empty() -> Self {
let mut stack = Stack::new(8);
/// Constructs an uninitialized thread context
pub fn empty() -> Self {
let stack = Stack::new(8);
Self {
k_sp: stack.sp,
stack_base: stack.bp,
@ -118,6 +119,11 @@ impl Context {
}
}
/// Sets up a context for signal entry
///
/// # Safety
///
/// Unsafe: may clobber an already active context
pub unsafe fn setup_signal_entry(&mut self, entry: usize, arg: usize, ttbr0: usize, ustack: usize) {
let mut stack = Stack::from_base_size(self.stack_base, self.stack_page_count);

View File

@ -92,7 +92,7 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
if far < mem::KERNEL_OFFSET && sched::is_ready() {
let proc = Process::current();
if let Err(e) = proc.manipulate_space(|space| space.try_cow_copy(far)) {
if proc.manipulate_space(|space| space.try_cow_copy(far)).is_err() {
// Kill program
dump_data_abort(Level::Error, esr, far as u64);
proc.enter_signal(Signal::SegmentationFault);

View File

@ -122,21 +122,23 @@ unsafe impl Manager for SimpleManager {
Err(Errno::OutOfMemory)
}
fn free_page(&mut self, addr: usize) -> Result<(), Errno> {
let index = self.page_index(addr);
let page = &mut self.pages[index];
let usage = {
let index = self.page_index(addr);
let page = &mut self.pages[index];
let usage = page.usage;
assert!(page.usage != PageUsage::Reserved && page.usage != PageUsage::Available);
let usage = page.usage;
assert!(page.usage != PageUsage::Reserved && page.usage != PageUsage::Available);
if page.refcount > 1 {
page.refcount -= 1;
} else {
assert_eq!(page.refcount, 1);
page.usage = PageUsage::Available;
page.refcount = 0;
}
if page.refcount > 1 {
page.refcount -= 1;
} else {
assert_eq!(page.refcount, 1);
page.usage = PageUsage::Available;
page.refcount = 0;
}
drop(page);
usage
};
self.update_stats_free(usage, 1);
Ok(())

View File

@ -32,14 +32,16 @@ pub enum PageUsage {
Filesystem,
}
/// Represents counts of allocated/available pages
#[allow(missing_docs)]
#[derive(Clone, Debug)]
pub struct PageStatistics {
available: usize,
kernel: usize,
kernel_heap: usize,
paging: usize,
user_private: usize,
filesystem: usize,
pub available: usize,
pub kernel: usize,
pub kernel_heap: usize,
pub paging: usize,
pub user_private: usize,
pub filesystem: usize,
}
/// Data structure representing a single physical memory page
@ -143,6 +145,7 @@ pub unsafe fn free_page(page: usize) -> Result<(), Errno> {
MANAGER.lock().as_mut().unwrap().free_page(page)
}
/// Returns current statistics for page allocation
pub fn statistics() -> PageStatistics {
MANAGER.lock().as_ref().unwrap().statistics()
}

View File

@ -328,6 +328,7 @@ impl Space {
memset(space as *mut Space as *mut u8, 0, 4096);
}
/// Returns the physical address of this structure
pub fn address_phys(&mut self) -> usize {
(self as *mut _ as usize) - mem::KERNEL_OFFSET
}

View File

@ -165,8 +165,9 @@ impl Process {
PROCESSES.lock().get(&pid).cloned()
}
/// Sets a pending signal for a process
pub fn set_signal(&self, signal: Signal) {
let mut lock = self.inner.lock();
let lock = self.inner.lock();
match lock.state {
State::Running => {
@ -187,6 +188,7 @@ impl Process {
}
}
/// Switches current thread back from signal handler
pub fn return_from_signal(&self) {
if self.signal_pending.load(Ordering::Acquire) == 0 {
panic!("TODO handle cases when returning from no signal");
@ -203,6 +205,7 @@ impl Process {
}
}
/// Switches current thread to a signal handler
pub fn enter_signal(&self, signal: Signal) {
if self
.signal_pending
@ -243,6 +246,7 @@ impl Process {
}
}
/// Sets up values needed for signal entry
pub fn setup_signal_context(&self, entry: usize, stack: usize) {
let mut lock = self.inner.lock();
lock.signal_entry = entry;
@ -270,6 +274,7 @@ impl Process {
f(self.inner.lock().space.as_mut().unwrap())
}
#[allow(clippy::mut_from_ref)]
fn current_context(&self) -> &mut Context {
if self.signal_pending.load(Ordering::Acquire) != 0 {
unsafe { &mut *self.signal_ctx.get() }
@ -339,7 +344,7 @@ impl Process {
let id = Pid::new_kernel();
let res = Rc::new(Self {
ctx: UnsafeCell::new(Context::kernel(entry as usize, arg)),
signal_ctx: UnsafeCell::new(Context::user_empty()),
signal_ctx: UnsafeCell::new(Context::empty()),
io: IrqSafeSpinLock::new(ProcessIo::new()),
exit_wait: Wait::new(),
signal_state: AtomicU32::new(0),
@ -372,7 +377,7 @@ impl Process {
let dst = Rc::new(Self {
ctx: UnsafeCell::new(Context::fork(frame, dst_ttbr0)),
signal_ctx: UnsafeCell::new(Context::user_empty()),
signal_ctx: UnsafeCell::new(Context::empty()),
io: IrqSafeSpinLock::new(src_io.fork()?),
exit_wait: Wait::new(),
signal_state: AtomicU32::new(0),

View File

@ -6,11 +6,12 @@ use crate::proc::{elf, wait, Pid, Process, ProcessIo};
use core::mem::size_of;
use core::ops::DerefMut;
use core::time::Duration;
use core::cmp::Ordering;
use libsys::{
abi,
signal::Signal,
error::Errno,
ioctl::IoctlCmd,
signal::Signal,
stat::{FileMode, OpenFlags, Stat, AT_EMPTY_PATH, AT_FDCWD},
traits::{Read, Write},
};
@ -183,12 +184,12 @@ pub fn syscall(num: usize, args: &[usize]) -> Result<usize, Errno> {
abi::SYS_EX_KILL => {
let pid = args[0] as i32;
let signal = Signal::try_from(args[1] as u32)?;
let proc = if pid > 0 {
Process::get(unsafe { Pid::from_raw(pid as u32) }).ok_or(Errno::DoesNotExist)?
} else if pid == 0 {
Process::current()
} else {
todo!()
let proc = match pid.cmp(&0) {
Ordering::Greater => {
Process::get(unsafe { Pid::from_raw(pid as u32) }).ok_or(Errno::DoesNotExist)?
}
Ordering::Equal => Process::current(),
Ordering::Less => todo!(),
};
proc.set_signal(signal);
Ok(0)