Skip to content

Commit

Permalink
chore: tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Mar 19, 2023
1 parent a816374 commit 4fcba84
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 83 deletions.
1 change: 0 additions & 1 deletion pkg/boot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#[allow(dead_code)]
pub use uefi::data_types::chars::*;
pub use uefi::data_types::*;
pub use uefi::prelude::SystemTable;
Expand Down
6 changes: 3 additions & 3 deletions pkg/fs/src/device/fat16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
let end = (entry + 1) * DirEntry::LEN;
trace!("Entry: {}..{}", start, end);
let dir_entry = DirEntry::parse(&block.inner()[start..end])
.map_err(|x| VolumeError::FileNameError(x))?;
.map_err(VolumeError::FileNameError)?;

if dir_entry.is_eod() {
return Ok(());
Expand Down Expand Up @@ -123,7 +123,7 @@ where
dir: &Directory,
name: &str,
) -> Result<DirEntry, VolumeError> {
let match_name = ShortFileName::parse(name).map_err(|x| VolumeError::FileNameError(x))?;
let match_name = ShortFileName::parse(name).map_err(VolumeError::FileNameError)?;

let mut current_cluster = Some(dir.cluster);
let mut dir_sector_num = self.cluster_to_sector(&dir.cluster);
Expand Down Expand Up @@ -168,7 +168,7 @@ where
pub fn next_cluster(&self, cluster: Cluster) -> Result<Cluster, VolumeError> {
let fat_offset = (cluster.0 * 2) as usize;
let cur_fat_sector = self.fat_start + fat_offset / Block::SIZE;
let offset = (fat_offset % Block::SIZE) as usize;
let offset = fat_offset % Block::SIZE;
let block = self.volume.read_block(cur_fat_sector).unwrap();
let fat_entry = u16::from_le_bytes(
block.inner()[offset..=offset + 1]
Expand Down
14 changes: 6 additions & 8 deletions pkg/fs/src/device/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ impl Device<u8> for Random {
}
}
Ok(size)
} else {
if let Some(mut rng) = GLOBAL_RNG.get().and_then(spin::Mutex::try_lock) {
for i in 0..size {
buf[offset + i] = rng.next_u32() as u8;
}
Ok(size)
} else {
Err(DeviceError::ReadError)
} else if let Some(mut rng) = GLOBAL_RNG.get().and_then(spin::Mutex::try_lock) {
for i in 0..size {
buf[offset + i] = rng.next_u32() as u8;
}
Ok(size)
} else {
Err(DeviceError::ReadError)
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ where
let mut length = file.length() as usize;
for i in 0..=file.length() as usize / Block::SIZE {
let sector = volume.cluster_to_sector(&file.start_cluster());
let block = volume.read_block(sector as usize + i).unwrap();
let block = volume.read_block(sector + i).unwrap();
if length > Block::SIZE {
data[i * Block::SIZE..(i + 1) * Block::SIZE].copy_from_slice(&block.inner()[..]);
length -= Block::SIZE;
} else {
data[i * Block::SIZE..i * Block::SIZE + length as usize]
.copy_from_slice(&block.inner()[..length as usize]);
data[i * Block::SIZE..i * Block::SIZE + length]
.copy_from_slice(&block.inner()[..length]);
break;
}
}
Expand All @@ -168,13 +168,13 @@ where
let mut length = file.length() as usize;
for i in 0..=file.length() as usize / Block::SIZE {
let sector = volume.cluster_to_sector(&file.start_cluster());
let block = volume.read_block(sector as usize + i).unwrap();
let block = volume.read_block(sector + i).unwrap();
if length > Block::SIZE {
buf[i * Block::SIZE..(i + 1) * Block::SIZE].copy_from_slice(&block.inner()[..]);
length -= Block::SIZE;
} else {
buf[i * Block::SIZE..i * Block::SIZE + length as usize]
.copy_from_slice(&block.inner()[..length as usize]);
buf[i * Block::SIZE..i * Block::SIZE + length]
.copy_from_slice(&block.inner()[..length]);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fs/src/structs/dir_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl ShortFileName {
}
// Denotes the start of the file extension
b'.' => {
if idx >= 1 && idx <= 8 {
if (1..=8).contains(&idx) {
seen_dot = true;
idx = 8;
} else {
Expand All @@ -243,7 +243,7 @@ impl ShortFileName {
let ch = ch.to_ascii_uppercase();
trace!("char: '{}', at: {}", ch as char, idx);
if seen_dot {
if idx >= 8 && idx < 11 {
if (8..11).contains(&idx) {
sfn.ext[idx - 8] = ch;
} else {
return Err(FilenameError::NameTooLong);
Expand Down
8 changes: 1 addition & 7 deletions pkg/fs/src/structs/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@ impl MBRPartitions {
}
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Default)]
pub struct PartitionMetaData {
data: [u8; 16],
}

impl Default for PartitionMetaData {
fn default() -> Self {
Self { data: [0u8; 16] }
}
}

impl PartitionMetaData {
/// Attempt to parse a Boot Parameter Block from a 512 byte sector.
pub fn parse(data: &[u8; 16]) -> PartitionMetaData {
Expand Down
11 changes: 7 additions & 4 deletions pkg/kernel/src/drivers/ata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! reference: https://wiki.osdev.org/ATA_PIO_Mode
//! reference: https://github.com/xfoxfu/rust-xos/blob/main/kernel/src/drivers/ide.rs
use alloc::boxed::Box;
use alloc::{string::String, vec::Vec};
use bit_field::BitField;
use core::hint::spin_loop;
Expand Down Expand Up @@ -64,8 +65,8 @@ impl Bus {
status: PortReadOnly::new(io_base + 7),
command: PortWriteOnly::new(io_base + 7),

alternate_status: PortReadOnly::new(ctrl_base + 0),
control: PortWriteOnly::new(ctrl_base + 0),
alternate_status: PortReadOnly::new(ctrl_base),
control: PortWriteOnly::new(ctrl_base),
drive_blockess: PortReadOnly::new(ctrl_base + 1),
}
}
Expand Down Expand Up @@ -217,7 +218,9 @@ impl Bus {
}
}
match (self.cylinder_low(), self.cylinder_high()) {
(0x00, 0x00) => Ok(IdentifyResponse::Ata([(); 256].map(|_| self.read_data()))),
(0x00, 0x00) => Ok(IdentifyResponse::Ata(Box::new(
[(); 256].map(|_| self.read_data()),
))),
(0x14, 0xEB) => Ok(IdentifyResponse::Atapi),
(0x3C, 0x3C) => Ok(IdentifyResponse::Sata),
(_, _) => Err(()),
Expand Down Expand Up @@ -278,7 +281,7 @@ pub struct Drive {
}

enum IdentifyResponse {
Ata([u16; 256]),
Ata(Box<[u16; 256]>),
Atapi,
Sata,
None,
Expand Down
10 changes: 8 additions & 2 deletions pkg/kernel/src/drivers/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ impl Console {
}
}

impl Default for Console {
fn default() -> Self {
Self::new()
}
}

impl Console {
pub fn size(&self) -> (isize, isize) {
let size: Size = get_display_for_sure().size();
Expand Down Expand Up @@ -133,8 +139,8 @@ impl Console {
}

pub fn move_cursor(&mut self, dx: isize, dy: isize) {
self.x_pos = self.x_pos as isize + dx;
self.y_pos = self.y_pos as isize + dy;
self.x_pos += dx;
self.y_pos += dy;
}

pub fn draw_hint(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/src/drivers/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn resolve_path(root_path: &str) -> Option<Directory> {
path = path[pos + 1..].to_string();
trace!("Resolving path: {}", path);

if path.len() == 0 {
if path.is_empty() {
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/src/drivers/gop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<'a> GOPDisplay<'a> {
let buffer = unsafe {
core::slice::from_raw_parts_mut(
graphic.fb_addr as *mut u32,
graphic.mode.resolution().0 as usize * graphic.mode.resolution().1 as usize,
graphic.mode.resolution().0 * graphic.mode.resolution().1,
)
};

Expand Down
8 changes: 1 addition & 7 deletions pkg/kernel/src/drivers/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ pub fn push_key(key: DecodedKey) {
}

pub fn try_get_key() -> Option<DecodedKey> {
interrupts::without_interrupts(|| {
if let Some(key) = get_input_buf_for_sure().pop() {
Some(key)
} else {
None
}
})
interrupts::without_interrupts(|| get_input_buf_for_sure().pop())
}

pub fn get_key() -> DecodedKey {
Expand Down
6 changes: 3 additions & 3 deletions pkg/kernel/src/interrupt/syscall/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn sys_allocate(args: &SyscallArgs) -> usize {
let layout = unsafe { (args.arg0 as *const Layout).as_ref().unwrap() };
let ptr = crate::memory::user::USER_ALLOCATOR
.lock()
.allocate_first_fit(layout.clone())
.allocate_first_fit(*layout)
.unwrap()
.as_ptr();
ptr as usize
Expand All @@ -36,7 +36,7 @@ pub fn sys_deallocate(args: &SyscallArgs) {
unsafe {
crate::memory::user::USER_ALLOCATOR
.lock()
.deallocate(core::ptr::NonNull::new_unchecked(ptr), layout.clone());
.deallocate(core::ptr::NonNull::new_unchecked(ptr), *layout);
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn sys_open(args: &SyscallArgs) -> usize {

trace!("sys_open: opened: {} at fd={}", path, &fd);

u8::from(fd) as usize
fd as usize
}

pub fn sys_close(args: &SyscallArgs) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use frames::*;
pub use paging::*;

pub fn init(boot_info: &'static boot::BootInfo) {
let physical_memory_offset = x86_64::VirtAddr::new_truncate(PHYSICAL_OFFSET as u64);
let physical_memory_offset = x86_64::VirtAddr::new_truncate(PHYSICAL_OFFSET);
let memory_map = &boot_info.memory_map;

let mut mem_size = 0;
Expand Down
48 changes: 32 additions & 16 deletions pkg/kernel/src/process/manager.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::*;
use crate::memory::{
self,
allocator::{ALLOCATOR, HEAP_SIZE},
get_frame_alloc_for_sure,
user::{USER_ALLOCATOR, USER_HEAP_SIZE}, self, PAGE_SIZE,
user::{USER_ALLOCATOR, USER_HEAP_SIZE},
PAGE_SIZE,
};
use crate::utils::Registers;
use alloc::collections::BTreeMap;
Expand Down Expand Up @@ -160,14 +162,14 @@ impl ProcessManager {
proc_data: Option<ProcessData>,
) -> ProcessId {
let mut p = Process::new(
&mut *crate::memory::get_frame_alloc_for_sure(),
&mut crate::memory::get_frame_alloc_for_sure(),
name,
parent,
self.get_kernel_page_table(),
proc_data,
);
p.pause();
p.init_elf(&elf);
p.init_elf(elf);
p.init_stack_frame(
VirtAddr::new_truncate(elf.header.pt2.entry_point()),
VirtAddr::new_truncate(STACK_INIT_TOP),
Expand All @@ -187,7 +189,7 @@ impl ProcessManager {
proc_data: Option<ProcessData>,
) -> ProcessId {
let mut p = Process::new(
&mut *crate::memory::get_frame_alloc_for_sure(),
&mut crate::memory::get_frame_alloc_for_sure(),
name,
parent,
self.get_kernel_page_table(),
Expand All @@ -202,7 +204,8 @@ impl ProcessManager {
}

pub fn print_process_list(&self) {
let mut output = String::from(" PID | PPID | Process Name | Ticks | Memory | Status\n");
let mut output =
String::from(" PID | PPID | Process Name | Ticks | Memory | Status\n");
for p in self.processes.iter() {
output += format!("{}\n", p).as_str();
}
Expand All @@ -218,37 +221,50 @@ impl ProcessManager {
let frames_recycled = alloc.recycled_count();
let frames_total = alloc.frames_total();

let (sys_used, sys_used_unit) = memory::humanized_size(heap_used as u64);
let (sys_size, sys_size_unit) = memory::humanized_size(heap_size as u64);

output += format!(
"System : {:>7.*} KiB/ {:>7.*} KiB ({:>5.2}%)\n",
"System : {:>6.*} {} / {:>6.*} {} ({:>5.2}%)\n",
2,
heap_used as f64 / 1024f64,
sys_used,
sys_used_unit,
2,
heap_size as f64 / 1024f64,
sys_size,
sys_size_unit,
heap_used as f64 / heap_size as f64 * 100.0
)
.as_str();

let (user_used, user_used_unit) = memory::humanized_size(user_heap_used as u64);
let (user_size, user_size_unit) = memory::humanized_size(user_heap_size as u64);

output += format!(
"User : {:>7.*} KiB/ {:>7.*} KiB ({:>5.2}%)\n",
"User : {:>6.*} {} / {:>6.*} {} ({:>5.2}%)\n",
2,
user_heap_used as f64 / 1024f64,
user_used,
user_used_unit,
2,
user_heap_size as f64 / 1024f64,
user_size,
user_size_unit,
user_heap_used as f64 / user_heap_size as f64 * 100.0
)
.as_str();

// put used/total frames in MiB
let (used_size, used_unit) = memory::humanized_size(frames_used as u64 * PAGE_SIZE);
let (used_size, used_unit) =
memory::humanized_size((frames_used - frames_recycled) as u64 * PAGE_SIZE);
let (tot_size, tot_unit) = memory::humanized_size(frames_total as u64 * PAGE_SIZE);

output += format!(
"Memory : {:>7.*} {}/ {:>7.*} {} ({:>5.2}%) [{} recycled]\n",
"Memory : {:>6.*} {} / {:>6.*} {} ({:>5.2}%) [{} recycled]\n",
2,
used_size, used_unit,
used_size,
used_unit,
2,
tot_size, tot_unit,
frames_used as f64 / frames_total as f64 * 100.0,
tot_size,
tot_unit,
(frames_used - frames_recycled) as f64 / frames_total as f64 * 100.0,
frames_recycled
)
.as_str();
Expand Down
Loading

0 comments on commit 4fcba84

Please sign in to comment.