From fe1c2f460e0690f5ae9474245e7d8e3b3e34ed34 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 10 Sep 2025 16:26:32 +0200 Subject: [PATCH] Fix a bunch of errors and warnings after moving to the 2024 edition --- src/arch/x86_64/interrupt/exception.rs | 68 ++++++++++++++------------ src/arch/x86_64/interrupt/handler.rs | 8 +-- src/arch/x86_64/interrupt/irq.rs | 4 +- src/arch/x86_shared/idt.rs | 30 +++++------- src/main.rs | 1 + src/syscall/usercopy.rs | 24 +++++---- 6 files changed, 68 insertions(+), 67 deletions(-) diff --git a/src/arch/x86_64/interrupt/exception.rs b/src/arch/x86_64/interrupt/exception.rs index 3da86b8a8f..575fd997df 100644 --- a/src/arch/x86_64/interrupt/exception.rs +++ b/src/arch/x86_64/interrupt/exception.rs @@ -9,7 +9,7 @@ use crate::{ interrupt_stack!(divide_by_zero, |stack| { println!("Divide by zero"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 0, ..Default::default() @@ -81,7 +81,7 @@ interrupt_stack!(breakpoint, |stack| { interrupt_stack!(overflow, |stack| { println!("Overflow trap"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 4, ..Default::default() @@ -91,7 +91,7 @@ interrupt_stack!(overflow, |stack| { interrupt_stack!(bound_range, |stack| { println!("Bound range exceeded fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 5, ..Default::default() @@ -101,7 +101,7 @@ interrupt_stack!(bound_range, |stack| { interrupt_stack!(invalid_opcode, |stack| { println!("Invalid opcode fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 6, ..Default::default() @@ -111,7 +111,7 @@ interrupt_stack!(invalid_opcode, |stack| { interrupt_stack!(device_not_available, |stack| { println!("Device not available fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 7, ..Default::default() @@ -121,17 +121,19 @@ interrupt_stack!(device_not_available, |stack| { interrupt_error!(double_fault, |stack, _code| { println!("Double fault"); stack.dump(); - stack_trace(); - loop { - interrupt::disable(); - interrupt::halt(); + unsafe { + stack_trace(); + loop { + interrupt::disable(); + interrupt::halt(); + } } }); interrupt_error!(invalid_tss, |stack, code| { println!("Invalid TSS fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 10, code, @@ -142,7 +144,7 @@ interrupt_error!(invalid_tss, |stack, code| { interrupt_error!(segment_not_present, |stack, code| { println!("Segment not present fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 11, code, @@ -153,7 +155,7 @@ interrupt_error!(segment_not_present, |stack, code| { interrupt_error!(stack_segment, |stack, code| { println!("Stack segment fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 12, code, @@ -164,7 +166,7 @@ interrupt_error!(stack_segment, |stack, code| { interrupt_error!(protection, |stack, code| { println!("Protection fault code={:#0x}", code); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 13, code, @@ -201,7 +203,7 @@ interrupt_error!(page, |stack, code| { if crate::memory::page_fault_handler(stack, generic_flags, cr2).is_err() { println!("Page fault: {:>016X} {:#?}", cr2.data(), arch_flags); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 14, code, @@ -213,7 +215,7 @@ interrupt_error!(page, |stack, code| { interrupt_stack!(fpu_fault, |stack| { println!("FPU floating point fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 16, ..Default::default() @@ -223,7 +225,7 @@ interrupt_stack!(fpu_fault, |stack| { interrupt_error!(alignment_check, |stack, code| { println!("Alignment check fault"); stack.dump(); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 17, code, @@ -234,10 +236,12 @@ interrupt_error!(alignment_check, |stack, code| { interrupt_stack!(machine_check, @paranoid, |stack| { println!("Machine check fault"); stack.dump(); - stack_trace(); - loop { - interrupt::disable(); - interrupt::halt(); + unsafe { + stack_trace(); + loop { + interrupt::disable(); + interrupt::halt(); + } } }); @@ -245,9 +249,9 @@ interrupt_stack!(simd, |stack| { println!("SIMD floating point fault"); stack.dump(); let mut mxcsr = 0_usize; - core::arch::asm!("stmxcsr [{}]", in(reg) core::ptr::addr_of_mut!(mxcsr)); + unsafe { core::arch::asm!("stmxcsr [{}]", in(reg) core::ptr::addr_of_mut!(mxcsr)) }; println!("MXCSR {:#0x}", mxcsr); - stack_trace(); + unsafe { stack_trace() }; excp_handler(Exception { kind: 19, ..Default::default() @@ -257,19 +261,23 @@ interrupt_stack!(simd, |stack| { interrupt_stack!(virtualization, |stack| { println!("Virtualization fault"); stack.dump(); - stack_trace(); - loop { - interrupt::disable(); - interrupt::halt(); + unsafe { + stack_trace(); + loop { + interrupt::disable(); + interrupt::halt(); + } } }); interrupt_error!(security, |stack, _code| { println!("Security exception"); stack.dump(); - stack_trace(); - loop { - interrupt::disable(); - interrupt::halt(); + unsafe { + stack_trace(); + loop { + interrupt::disable(); + interrupt::halt(); + } } }); diff --git a/src/arch/x86_64/interrupt/handler.rs b/src/arch/x86_64/interrupt/handler.rs index 7597de3261..226aaa708f 100644 --- a/src/arch/x86_64/interrupt/handler.rs +++ b/src/arch/x86_64/interrupt/handler.rs @@ -363,9 +363,9 @@ macro_rules! interrupt_stack { ($name:ident, $save1:ident!, $save2:ident!, $rstor2:ident!, $rstor1:ident!, is_paranoid: $is_paranoid:expr_2021, |$stack:ident| $code:block) => { #[naked] pub unsafe extern "C" fn $name() { unsafe { - unsafe extern "C" fn inner($stack: &mut $crate::arch::x86_64::interrupt::InterruptStack) { unsafe { + unsafe extern "C" fn inner($stack: &mut $crate::arch::x86_64::interrupt::InterruptStack) { $code - }} + } core::arch::naked_asm!(concat!( // Clear direction flag, required by ABI when running any Rust code in the kernel. "cld;", @@ -456,9 +456,9 @@ macro_rules! interrupt_error { ($name:ident, |$stack:ident, $error_code:ident| $code:block) => { #[naked] pub unsafe extern "C" fn $name() { unsafe { - unsafe extern "C" fn inner($stack: &mut $crate::arch::x86_64::interrupt::handler::InterruptStack, $error_code: usize) { unsafe { + unsafe extern "C" fn inner($stack: &mut $crate::arch::x86_64::interrupt::handler::InterruptStack, $error_code: usize) { $code - }} + } core::arch::naked_asm!(concat!( // Clear direction flag, required by ABI when running any Rust code in the kernel. diff --git a/src/arch/x86_64/interrupt/irq.rs b/src/arch/x86_64/interrupt/irq.rs index 4ceeacd991..ee44f081b1 100644 --- a/src/arch/x86_64/interrupt/irq.rs +++ b/src/arch/x86_64/interrupt/irq.rs @@ -174,7 +174,7 @@ interrupt_stack!(pit_stack, |_stack| { *time::OFFSET.lock() += pit::RATE; } - eoi(0); + unsafe { eoi(0) }; // Wake up other CPUs ipi(IpiKind::Pit, IpiTarget::Other); @@ -309,7 +309,7 @@ interrupt_error!(generic_irq, |_stack, code| { // (containing lots of useless NOPs). irq_trigger((code as i32).wrapping_add(128) as u8); - lapic_eoi(); + unsafe { lapic_eoi() }; }); core::arch::global_asm!(" diff --git a/src/arch/x86_shared/idt.rs b/src/arch/x86_shared/idt.rs index e9d7a7148f..63e9485768 100644 --- a/src/arch/x86_shared/idt.rs +++ b/src/arch/x86_shared/idt.rs @@ -55,15 +55,12 @@ pub fn is_reserved(cpu_id: LogicalCpuId, index: u8) -> bool { let byte_index = index / 32; let bit = index % 32; - { - &IDTS - .read() - .as_ref() - .unwrap() - .get(&cpu_id) - .unwrap() - .reservations[usize::from(byte_index)] - } + IDTS.read() + .as_ref() + .unwrap() + .get(&cpu_id) + .unwrap() + .reservations[usize::from(byte_index)] .load(Ordering::Acquire) & (1 << bit) != 0 @@ -74,15 +71,12 @@ pub fn set_reserved(cpu_id: LogicalCpuId, index: u8, reserved: bool) { let byte_index = index / 32; let bit = index % 32; - { - &IDTS - .read() - .as_ref() - .unwrap() - .get(&cpu_id) - .unwrap() - .reservations[usize::from(byte_index)] - } + IDTS.read() + .as_ref() + .unwrap() + .get(&cpu_id) + .unwrap() + .reservations[usize::from(byte_index)] .fetch_or(u32::from(reserved) << bit, Ordering::AcqRel); } diff --git a/src/main.rs b/src/main.rs index d890ec28b4..e20a6c88dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,7 @@ #![deny(unreachable_patterns)] // Ensure that all must_use results are used #![deny(unused_must_use)] +#![warn(static_mut_refs)] // FIXME deny once all occurences are fixed #![feature(allocator_api)] #![feature(int_roundings)] #![feature(iter_next_chunk)] diff --git a/src/syscall/usercopy.rs b/src/syscall/usercopy.rs index e49f0a6167..bce8907114 100644 --- a/src/syscall/usercopy.rs +++ b/src/syscall/usercopy.rs @@ -108,21 +108,19 @@ impl UserSlice { } } pub unsafe fn read_exact(self) -> Result { - unsafe { - let mut t: T = core::mem::zeroed(); - let slice = unsafe { - core::slice::from_raw_parts_mut( - (&mut t as *mut T).cast::(), - core::mem::size_of::(), - ) - }; + let mut t: T = unsafe { core::mem::zeroed() }; + let slice = unsafe { + core::slice::from_raw_parts_mut( + (&mut t as *mut T).cast::(), + core::mem::size_of::(), + ) + }; - self.limit(core::mem::size_of::()) - .ok_or(Error::new(EINVAL))? - .copy_to_slice(slice)?; + self.limit(core::mem::size_of::()) + .ok_or(Error::new(EINVAL))? + .copy_to_slice(slice)?; - Ok(t) - } + Ok(t) } pub fn copy_common_bytes_to_slice(self, slice: &mut [u8]) -> Result { let min = core::cmp::min(self.len(), slice.len());