Clean up unused imports and put IPC logging behind a feature

This commit is contained in:
pjht 2024-11-16 12:08:25 -06:00
parent 0fcdbbd363
commit e4782341b0
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
6 changed files with 140 additions and 149 deletions

View File

@ -29,3 +29,6 @@ humansize = "2.1.3"
cast = "0.3.0" cast = "0.3.0"
az = "1.2.1" az = "1.2.1"
unsigned-varint = "0.8.0" unsigned-varint = "0.8.0"
[features]
log-rpc = []

View File

@ -1,7 +1,6 @@
use crate::{ use crate::{
bootinfo::BOOTINFO, bootinfo::BOOTINFO,
print, println, print, println,
serial::SECOND_PORT,
tasking::SleepReason, tasking::SleepReason,
virtual_memory::{ASpaceMutex, AddressSpace, ACTIVE_SPACE, KERNEL_SPACE}, virtual_memory::{ASpaceMutex, AddressSpace, ACTIVE_SPACE, KERNEL_SPACE},
TASKING, TASKING,
@ -11,16 +10,12 @@ use az::WrappingCast;
use cast::{u64, usize}; use cast::{u64, usize};
use core::{ use core::{
arch::{asm, naked_asm}, arch::{asm, naked_asm},
ffi::CStr,
fmt::Write,
ptr, slice, ptr, slice,
}; };
use hashbrown::HashMap; use hashbrown::HashMap;
use pic8259::ChainedPics; use pic8259::ChainedPics;
use saturating_cast::SaturatingCast;
use spin::{Lazy, Mutex, RwLock}; use spin::{Lazy, Mutex, RwLock};
use x86_64::{ use x86_64::{
instructions::port::{Port, PortReadOnly},
registers::control::Cr2, registers::control::Cr2,
set_general_handler, set_general_handler,
structures::{ structures::{
@ -32,6 +27,11 @@ use x86_64::{
PhysAddr, PrivilegeLevel, VirtAddr, PhysAddr, PrivilegeLevel, VirtAddr,
}; };
#[cfg(feature = "log-rpc")]
use crate::serial::SECOND_PORT;
#[cfg(feature = "log-rpc")]
use saturating_cast::SaturatingCast;
const IRQ_BASE: u8 = 32; const IRQ_BASE: u8 = 32;
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| { static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
@ -110,7 +110,6 @@ fn general_handler(stack_frame: InterruptStackFrame, index: u8, _error_code: Opt
println!("Other interrupt {index}\n{stack_frame:#?}"); println!("Other interrupt {index}\n{stack_frame:#?}");
} }
#[expect(clippy::needless_pass_by_value, reason = "Signature dictated by external crate")]
fn exc_handler(_stack_frame: InterruptStackFrame, _index: u8, _error_code: Option<u64>) { fn exc_handler(_stack_frame: InterruptStackFrame, _index: u8, _error_code: Option<u64>) {
if let Some(current_pid) = TASKING.current_pid() { if let Some(current_pid) = TASKING.current_pid() {
print!("PID {current_pid} "); print!("PID {current_pid} ");
@ -132,44 +131,47 @@ impl Drop for EoiGuard {
} }
pub fn send_ipc_to(pid: usize, buffer: Box<[u8], &'static ASpaceMutex>, len: usize) { pub fn send_ipc_to(pid: usize, buffer: Box<[u8], &'static ASpaceMutex>, len: usize) {
//#[expect( #[cfg(feature = "log-rpc")]
// clippy::unwrap_used, {
// reason = "The min call guarantees that the value is in the range of a u32 before the cast" #[expect(
//)] clippy::unwrap_used,
//let trunc_len: u32 = usize::min(len, 4096).try_into().unwrap(); reason = "The min call guarantees that the value is in the range of a u32 before the cast"
//#[expect( )]
// clippy::arithmetic_side_effects, let trunc_len: u32 = usize::min(len, 4096).try_into().unwrap();
// reason = "Can't underflow, as x % 4 < 4 no matter the x" #[expect(
//)] clippy::arithmetic_side_effects,
//let padding = if (trunc_len % 4) != 0 { 4 - (trunc_len % 4) } else { 0 }; reason = "Can't underflow, as x % 4 < 4 no matter the x"
//#[expect( )]
// clippy::arithmetic_side_effects, let padding = if (trunc_len % 4) != 0 { 4 - (trunc_len % 4) } else { 0 };
// reason = "Can't overflow, as padding is no more than 4 and trunc_len is no more than 4096." #[expect(
//)] clippy::arithmetic_side_effects,
//let padded_len = trunc_len + padding; reason = "Can't overflow, as padding is no more than 4 and trunc_len is no more than 4096."
//#[expect( )]
// clippy::arithmetic_side_effects, let padded_len = trunc_len + padding;
// reason = "Can't overflow, as padded_len is no more than 4096 and 4096+24 < u32::MAX" #[expect(
//)] clippy::arithmetic_side_effects,
//let total_len = padded_len + 8 + (4 * 4); reason = "Can't overflow, as padded_len is no more than 4096 and 4096+24 < u32::MAX"
//SECOND_PORT.write_u32s(&[ )]
// 0x3, // SPB type let total_len = padded_len + 8 + (4 * 4);
// total_len, // Total block length SECOND_PORT.write_u32s(&[
// len.saturating_cast::<u32>().saturating_add(8), // Packet length 0x3, // SPB type
//]); total_len, // Total block length
//SECOND_PORT.write_bytes(&pid.to_ne_bytes()); len.saturating_cast::<u32>().saturating_add(8), // Packet length
//#[expect( ]);
// clippy::indexing_slicing, SECOND_PORT.write_bytes(&pid.to_ne_bytes());
// reason = "The truncated length is always <= the buffer's length" #[expect(
//)] clippy::indexing_slicing,
//SECOND_PORT.write_bytes(&buffer[0..usize(trunc_len)]); reason = "The truncated length is always <= the buffer's length"
//for _ in 0..padding { )]
// SECOND_PORT.write_bytes(&[0]); SECOND_PORT.write_bytes(&buffer[0..usize(trunc_len)]);
//} for _ in 0..padding {
//SECOND_PORT.write_u32s(&[ SECOND_PORT.write_bytes(&[0]);
// total_len, // Total block length }
//]); SECOND_PORT.write_u32s(&[
//assert!(len <= buffer.len()); total_len, // Total block length
]);
}
assert!(len <= buffer.len());
if TASKING.message_queue_mut(pid, |_| ()).is_ok() { if TASKING.message_queue_mut(pid, |_| ()).is_ok() {
let buf_num_pages = buffer.len() / 4096; let buf_num_pages = buffer.len() / 4096;
let buffer = Box::into_raw(buffer); let buffer = Box::into_raw(buffer);
@ -209,7 +211,6 @@ pub fn send_ipc_to(pid: usize, buffer: Box<[u8], &'static ASpaceMutex>, len: usi
} }
} }
#[expect(clippy::needless_pass_by_value, reason = "Signature dictated by external crate")]
fn irq_handler(_stack_frame: InterruptStackFrame, index: u8, _error_code: Option<u64>) { fn irq_handler(_stack_frame: InterruptStackFrame, index: u8, _error_code: Option<u64>) {
#[expect( #[expect(
clippy::arithmetic_side_effects, clippy::arithmetic_side_effects,
@ -500,43 +501,46 @@ extern "C" fn syscall_handler() {
let len = usize(regs.rsi); let len = usize(regs.rsi);
assert!(len <= buffer.len()); assert!(len <= buffer.len());
if TASKING.message_queue_mut(pid, |_| ()).is_ok() { if TASKING.message_queue_mut(pid, |_| ()).is_ok() {
//#[expect( #[cfg(feature = "log-rpc")]
// clippy::unwrap_used, {
// reason = "The min call guarantees that the value is in the range of a u32 before the cast" #[expect(
//)] clippy::unwrap_used,
//let trunc_len: u32 = usize::min(len, 4096).try_into().unwrap(); reason = "The min call guarantees that the value is in the range of a u32 before the cast"
//#[expect( )]
// clippy::arithmetic_side_effects, let trunc_len: u32 = usize::min(len, 4096).try_into().unwrap();
// reason = "Can't underflow, as x % 4 < 4 no matter the x" #[expect(
//)] clippy::arithmetic_side_effects,
//let padding = if (trunc_len % 4) != 0 { 4 - (trunc_len % 4) } else { 0 }; reason = "Can't underflow, as x % 4 < 4 no matter the x"
//#[expect( )]
// clippy::arithmetic_side_effects, let padding = if (trunc_len % 4) != 0 { 4 - (trunc_len % 4) } else { 0 };
// reason = "Can't overflow, as padding is no more than 4 and trunc_len is no more than 4096." #[expect(
//)] clippy::arithmetic_side_effects,
//let padded_len = trunc_len + padding; reason = "Can't overflow, as padding is no more than 4 and trunc_len is no more than 4096."
//#[expect( )]
// clippy::arithmetic_side_effects, let padded_len = trunc_len + padding;
// reason = "Can't overflow, as padded_len is no more than 4096 and 4096+24 < u32::MAX" #[expect(
//)] clippy::arithmetic_side_effects,
//let total_len = padded_len + 8 + (4 * 4); reason = "Can't overflow, as padded_len is no more than 4096 and 4096+24 < u32::MAX"
//SECOND_PORT.write_u32s(&[ )]
// 0x3, // SPB type let total_len = padded_len + 8 + (4 * 4);
// total_len, // Total block length SECOND_PORT.write_u32s(&[
// len.saturating_cast::<u32>().saturating_add(8), // Packet length 0x3, // SPB type
//]); total_len, // Total block length
//SECOND_PORT.write_bytes(&pid.to_ne_bytes()); len.saturating_cast::<u32>().saturating_add(8), // Packet length
//#[expect( ]);
// clippy::indexing_slicing, SECOND_PORT.write_bytes(&pid.to_ne_bytes());
// reason = "The truncated length is always <= the buffer's length" #[expect(
//)] clippy::indexing_slicing,
//SECOND_PORT.write_bytes(&buffer[0..usize(trunc_len)]); reason = "The truncated length is always <= the buffer's length"
//for _ in 0..padding { )]
// SECOND_PORT.write_bytes(&[0]); SECOND_PORT.write_bytes(&buffer[0..usize(trunc_len)]);
//} for _ in 0..padding {
//SECOND_PORT.write_u32s(&[ SECOND_PORT.write_bytes(&[0]);
// total_len, // Total block length }
//]); SECOND_PORT.write_u32s(&[
total_len, // Total block length
]);
}
let buf_num_pages = buffer.len() / 4096; let buf_num_pages = buffer.len() / 4096;
let buffer = Box::into_raw(buffer); let buffer = Box::into_raw(buffer);
let buf_start_page = let buf_start_page =
@ -574,10 +578,6 @@ extern "C" fn syscall_handler() {
clippy::unwrap_used, clippy::unwrap_used,
reason = "The PID is known valid due to using it in message_queue_mut in the if-let condition" reason = "The PID is known valid due to using it in message_queue_mut in the if-let condition"
)] )]
#[expect(
clippy::unwrap_used,
reason = "The PID is known valid due to using it in message_queue_mut in the if-let condition"
)]
TASKING.wake(pid, SleepReason::WaitingForIPC).unwrap(); TASKING.wake(pid, SleepReason::WaitingForIPC).unwrap();
retval = 0; retval = 0;
} else { } else {

View File

@ -5,7 +5,6 @@
#![feature(allocator_api)] #![feature(allocator_api)]
#![feature(naked_functions)] #![feature(naked_functions)]
#![feature(stmt_expr_attributes)] #![feature(stmt_expr_attributes)]
#![feature(exposed_provenance)]
// #![feature(strict_provenance)] // #![feature(strict_provenance)]
#![deny( #![deny(
unsafe_op_in_unsafe_fn, unsafe_op_in_unsafe_fn,
@ -87,7 +86,6 @@ mod virtual_memory;
use core::{ptr, slice}; use core::{ptr, slice};
use alloc::ffi::CString;
use bootinfo::BOOTINFO; use bootinfo::BOOTINFO;
use cast::usize; use cast::usize;
use elf::{ use elf::{
@ -99,7 +97,6 @@ use elf::{
ElfBytes, ElfBytes,
}; };
use physical_memory::PHYSICAL_MEMORY; use physical_memory::PHYSICAL_MEMORY;
use serial::SECOND_PORT;
use spin::lazy::Lazy; use spin::lazy::Lazy;
use tar_no_std::TarArchiveRef; use tar_no_std::TarArchiveRef;
use tasking::{SleepReason, TASKING}; use tasking::{SleepReason, TASKING};
@ -112,6 +109,9 @@ use x86_64::{
use crate::virtual_memory::AddressSpace; use crate::virtual_memory::AddressSpace;
#[cfg(feature = "log-rpc")]
use serial::SECOND_PORT;
// pub static INITRD: &[u8] = include_bytes!("../initrd.tar"); // pub static INITRD: &[u8] = include_bytes!("../initrd.tar");
#[expect( #[expect(
@ -270,28 +270,25 @@ pub fn main() {
} }
} }
//// Before starting init, write the pcapng section header + interface description to the second serial port #[cfg(feature = "log-rpc")]
//SECOND_PORT.write_u32s(&[ // Before starting init, write the pcapng section header + interface description to the second serial port
// 0x0A0D_0D0A, // SHB type SECOND_PORT.write_u32s(&[
// 7 * 4, // Total block length 0x0A0D_0D0A, // SHB type
// 0x1A2B_3C4D, // Byte order magic 7 * 4, // Total block length
// 0x0000_0001, // Version (1.0) 0x1A2B_3C4D, // Byte order magic
// 0xFFFF_FFFF, // Length upper (-1) across both 0x0000_0001, // Version (1.0)
// 0xFFFF_FFFF, // Length lower 0xFFFF_FFFF, // Length upper (-1) across both
// 7 * 4, // Total block length 0xFFFF_FFFF, // Length lower
// 0x1, // IDB type 7 * 4, // Total block length
// 5 * 4, // Total block length 0x1, // IDB type
// 147, // Link type 5 * 4, // Total block length
// 4096 + 8, // Packet length limit, 147, // Link type
// 5 * 4, // Total block length 4096 + 8, // Packet length limit,
//]); 5 * 4, // Total block length
]);
#[expect(
clippy::unwrap_used,
reason = "Argument does not contain a null byte, thus this cannot panic"
)]
let init_pid = TASKING let init_pid = TASKING
.new_process(ptr::with_exposed_provenance(usize(init.ehdr.e_entry)), init_addr_space) .new_process(ptr::with_exposed_provenance(usize(init.ehdr.e_entry)), init_addr_space)
.expect("Failed to create init process"); .expect("Failed to create init process");
TASKING.wake(init_pid, SleepReason::NewProcess).unwrap(); TASKING.wake(init_pid, SleepReason::NewProcess).expect("Failed to wake new init process");
} }

View File

@ -11,6 +11,7 @@ pub static FIRST_PORT: Lazy<Wrapper> = Lazy::new(|| {
Wrapper(Mutex::new(port)) Wrapper(Mutex::new(port))
}); });
#[cfg(feature = "log-rpc")]
pub static SECOND_PORT: Lazy<Wrapper> = Lazy::new(|| { pub static SECOND_PORT: Lazy<Wrapper> = Lazy::new(|| {
// SAFETY: // SAFETY:
// 0x2f8 is the defined address for the second serial port on x86_64, // 0x2f8 is the defined address for the second serial port on x86_64,
@ -45,13 +46,17 @@ impl Wrapper {
#[macro_export] #[macro_export]
macro_rules! print { macro_rules! print {
($($arg:tt)*) => ($crate::serial::_print(format_args!($($arg)*))); ($($arg:tt)*) => ({
#[expect(clippy::used_underscore_items, reason="_print is meant for this macro")]
$crate::serial::_print(format_args!($($arg)*))
})
} }
#[macro_export] #[macro_export]
macro_rules! println { macro_rules! println {
() => ($crate::print!("\r\n")); () => ($crate::print!("\r\n"));
($($arg:tt)*) => ({ ($($arg:tt)*) => ({
#[expect(clippy::used_underscore_items, reason="_print is meant for this macro")]
$crate::serial::_print(format_args!($($arg)*)); $crate::serial::_print(format_args!($($arg)*));
$crate::print!("\r\n"); $crate::print!("\r\n");
}) })

View File

@ -1,19 +1,18 @@
use crate::{ use crate::{
gdt, gdt,
interrupts::{send_ipc_to, REGISTERD_PIDS}, interrupts::{send_ipc_to, REGISTERD_PIDS},
println, qemu_exit, println,
virtual_memory::{ASpaceMutex, AddressSpace, PagingError, ACTIVE_SPACE, KERNEL_SPACE}, virtual_memory::{ASpaceMutex, AddressSpace, PagingError, ACTIVE_SPACE, KERNEL_SPACE},
}; };
use alloc::{boxed::Box, collections::VecDeque, string::ToString, vec::Vec}; use alloc::{boxed::Box, collections::VecDeque, vec::Vec};
use cast::{u64, usize}; use cast::{u64, usize};
use core::{ use core::{
arch::naked_asm, arch::naked_asm,
ffi::CStr,
ptr::{addr_of, addr_of_mut}, ptr::{addr_of, addr_of_mut},
sync::atomic::{AtomicBool, Ordering}, sync::atomic::{AtomicBool, Ordering},
}; };
use crossbeam_queue::SegQueue; use crossbeam_queue::SegQueue;
use humansize::{SizeFormatter, BINARY}; //use humansize::{SizeFormatter, BINARY};
use slab::Slab; use slab::Slab;
use spin::{Lazy, Mutex, RwLock}; use spin::{Lazy, Mutex, RwLock};
use x86_64::{ use x86_64::{
@ -258,15 +257,9 @@ impl Tasking {
core::mem::drop(processes); core::mem::drop(processes);
switch_to_asm(curr_stack, kernel_esp); switch_to_asm(curr_stack, kernel_esp);
break; break;
} else if { } else if #[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")] self.processes.read()[current_pid].sleeping.read().is_some()
let res = self.processes.read()[current_pid].sleeping.read().is_some(); {
res
} {
//println!("All processes sleeping, exiting QEMU");
//self.print_stats();
//qemu_exit::exit_qemu();
//println!("All processes sleeping, waiting for interrupt");
self.wfi_loop.store(true, Ordering::Relaxed); self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt(); x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable(); x86_64::instructions::interrupts::disable();
@ -318,7 +311,6 @@ impl Tasking {
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")] #[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
let mut processes = self.processes.write(); let mut processes = self.processes.write();
if let Some(current_pid) = *self.current_pid.read() { if let Some(current_pid) = *self.current_pid.read() {
//self.freeable_kstacks.lock().push(processes.remove(current_pid).kernel_stack);
*processes[current_pid].sleeping.write() = Some(SleepReason::Exited); *processes[current_pid].sleeping.write() = Some(SleepReason::Exited);
} }
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")] #[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
@ -339,11 +331,9 @@ impl Tasking {
switch_to_asm_exit(kernel_esp); switch_to_asm_exit(kernel_esp);
unreachable!() unreachable!()
} else { } else {
//println!("Last non-sleeping process exited, exiting QEMU");
self.wfi_loop.store(true, Ordering::Relaxed); self.wfi_loop.store(true, Ordering::Relaxed);
x86_64::instructions::interrupts::enable_and_hlt(); x86_64::instructions::interrupts::enable_and_hlt();
x86_64::instructions::interrupts::disable(); x86_64::instructions::interrupts::disable();
//self.exit();
} }
} }
} }
@ -418,10 +408,6 @@ impl Tasking {
Ok(func(aspace)) Ok(func(aspace))
} }
pub fn proc_sleeping(&self, pid: usize) -> Result<Option<SleepReason>, InvalidPid> {
Ok(*(self.processes.read().get(pid).ok_or(InvalidPid)?.sleeping.read()))
}
pub fn sleep(&self, reason: SleepReason) { pub fn sleep(&self, reason: SleepReason) {
#[warn(clippy::unwrap_used, reason = "FIXME")] #[warn(clippy::unwrap_used, reason = "FIXME")]
#[warn(clippy::indexing_slicing, reason = "FIXME(?)")] #[warn(clippy::indexing_slicing, reason = "FIXME(?)")]
@ -442,20 +428,20 @@ impl Tasking {
Ok(()) Ok(())
} }
pub fn print_stats(&self) { //pub fn print_stats(&self) {
let mut total = KERNEL_SPACE.lock().get_bytes_allocated(); // let mut total = KERNEL_SPACE.lock().get_bytes_allocated();
println!( // println!(
"[TASKING] Kernel used {}", // "[TASKING] Kernel used {}",
SizeFormatter::new(KERNEL_SPACE.lock().get_bytes_allocated(), BINARY) // SizeFormatter::new(KERNEL_SPACE.lock().get_bytes_allocated(), BINARY)
); // );
for (i, process) in self.processes.read().iter() { // for (i, process) in self.processes.read().iter() {
let bytes_used = process.address_space.as_ref().map_or_else( // let bytes_used = process.address_space.as_ref().map_or_else(
|| ACTIVE_SPACE.lock().get_bytes_allocated(), // || ACTIVE_SPACE.lock().get_bytes_allocated(),
|space| space.get_bytes_allocated(), // |space| space.get_bytes_allocated(),
); // );
total += bytes_used; // total += bytes_used;
println!("[TASKING] PID {} used {}", i, SizeFormatter::new(bytes_used, BINARY),); // println!("[TASKING] PID {} used {}", i, SizeFormatter::new(bytes_used, BINARY),);
} // }
println!("[TASKING] Total used {} ({})", SizeFormatter::new(total, BINARY), total / 4096); // println!("[TASKING] Total used {} ({})", SizeFormatter::new(total, BINARY), total / 4096);
} //}
} }

View File

@ -792,9 +792,9 @@ impl AddressSpace {
self.bytes_allocated.fetch_sub(size, Ordering::Relaxed); self.bytes_allocated.fetch_sub(size, Ordering::Relaxed);
} }
pub fn get_bytes_allocated(&self) -> usize { //pub fn get_bytes_allocated(&self) -> usize {
self.bytes_allocated.load(Ordering::Relaxed) // self.bytes_allocated.load(Ordering::Relaxed)
} //}
} }
impl Drop for AddressSpace { impl Drop for AddressSpace {