From 2b147549ee8ec158b23b41a9bb4072e9391ca7d4 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Fri, 27 Dec 2024 16:04:43 +0100 Subject: [PATCH] Fix clippy warnings --- kernel/src/cpu/instructions.rs | 12 ++++++++++++ kernel/src/cpu/mod.rs | 3 ++- kernel/src/cpu/paging.rs | 8 ++++---- kernel/src/cpu/{cpu.rs => startup.rs} | 12 +----------- kernel/src/main.rs | 2 +- kernel/src/misc/elf.rs | 2 +- kernel/src/sys/smp.rs | 7 +++---- kernel/src/sys/syscall.rs | 7 +++---- kernel/src/sys/task.rs | 2 +- 9 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 kernel/src/cpu/instructions.rs rename kernel/src/cpu/{cpu.rs => startup.rs} (91%) diff --git a/kernel/src/cpu/instructions.rs b/kernel/src/cpu/instructions.rs new file mode 100644 index 0000000..468599f --- /dev/null +++ b/kernel/src/cpu/instructions.rs @@ -0,0 +1,12 @@ +use core::arch::asm; + +pub fn enable_user_memory_access() { + unsafe { + asm!("stac"); + } +} +pub fn disable_user_memory_access() { + unsafe { + asm!("clac"); + } +} diff --git a/kernel/src/cpu/mod.rs b/kernel/src/cpu/mod.rs index 4852df1..932012e 100644 --- a/kernel/src/cpu/mod.rs +++ b/kernel/src/cpu/mod.rs @@ -1,5 +1,6 @@ -pub mod cpu; pub mod gdt; pub mod idt; +pub mod instructions; pub mod isr; pub mod paging; +pub mod startup; diff --git a/kernel/src/cpu/paging.rs b/kernel/src/cpu/paging.rs index 631fe6c..a28dc63 100644 --- a/kernel/src/cpu/paging.rs +++ b/kernel/src/cpu/paging.rs @@ -250,10 +250,10 @@ impl AddressSpace { } pub fn create_page_table() -> Box { - return Box::new(PageTable { + Box::new(PageTable { entries_phys: [PageEntry(0); 512], entries_virt: [const { None }; 512], - }); + }) } fn get_free_frame() -> u64 { let frames_vec = PHYSICAL_FRAMES.lock(); @@ -321,9 +321,9 @@ pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64, heap_start: u address_space.pml4.set(pml4).unwrap_or_else(|_| panic!()); for i in 256..512 { if i == 511 { - address_space.create_table_entry(i as i32, -1, -1, false, true, false); + address_space.create_table_entry(i, -1, -1, false, true, false); } else { - address_space.create_table_entry(i as i32, -1, -1, false, false, false); + address_space.create_table_entry(i, -1, -1, false, false, false); } } for i in text_start..text_end { diff --git a/kernel/src/cpu/cpu.rs b/kernel/src/cpu/startup.rs similarity index 91% rename from kernel/src/cpu/cpu.rs rename to kernel/src/cpu/startup.rs index cb9f56a..64ab02d 100644 --- a/kernel/src/cpu/cpu.rs +++ b/kernel/src/cpu/startup.rs @@ -50,7 +50,7 @@ pub fn set_cpu_flags() { // System Call Extensions asm!("rdmsr; bts rax, 0; wrmsr", in("rcx") 0xc0000080_u64, out("rax") _, out("rdx") _); asm!("wrmsr", in("rcx") 0xc0000081_u64, in("rax") 0, in("rdx") 8 | 16 << 16); - asm!("wrmsr", in("rcx") 0xc0000082_u64, in("rax") syscall, in("rdx") syscall as u64 >> 32); + asm!("wrmsr", in("rcx") 0xc0000082_u64, in("rax") syscall, in("rdx") syscall as usize as u64 >> 32); // Clear IF and DF asm!("wrmsr", in("rcx") 0xc0000084_u64, in("rax") 1 << 9 | 1 << 10, in("rdx") 0); } @@ -59,13 +59,3 @@ pub fn set_cpu_flags() { asm!("wrmsr", in("rcx") 0xc0000102_u64, in("rax") cpudata_address, in("rdx") cpudata_address >> 32); } } -pub fn enable_user_memory_access() { - unsafe { - asm!("stac"); - } -} -pub fn disable_user_memory_access() { - unsafe { - asm!("clac"); - } -} diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 32332c0..5e3a691 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -14,7 +14,7 @@ use core::{ use acpi::AcpiTables; use acpica_rs::{AcpiEnableSubsystem, AcpiInitializeObjects, AcpiInitializeSubsystem, AcpiInitializeTables, AcpiLoadTables, ACPI_FULL_INITIALIZATION}; use alloc::format; -use cpu::{cpu::set_cpu_flags, gdt::setup_gdt, idt::setup_idt, paging::setup_paging}; +use cpu::{gdt::setup_gdt, idt::setup_idt, paging::setup_paging, startup::set_cpu_flags}; use kernel_common::{ instructions::{cli, hlt}, loader_struct::{FramebufferInfo, LoaderStruct, LoaderStructMemoryRegion, LOADER_STRUCT_MAGIC}, diff --git a/kernel/src/misc/elf.rs b/kernel/src/misc/elf.rs index f3c9cd0..376d95e 100644 --- a/kernel/src/misc/elf.rs +++ b/kernel/src/misc/elf.rs @@ -5,7 +5,7 @@ use log::info; use crate::{ cpu::{ - cpu::{disable_user_memory_access, enable_user_memory_access}, + instructions::{disable_user_memory_access, enable_user_memory_access}, paging::USER_END, }, sys::{lapic::get_current_lapic_id, task::CURRENT_TASKS}, diff --git a/kernel/src/sys/smp.rs b/kernel/src/sys/smp.rs index d6f46eb..b8f2155 100644 --- a/kernel/src/sys/smp.rs +++ b/kernel/src/sys/smp.rs @@ -1,6 +1,5 @@ use core::{ptr::copy, sync::atomic::Ordering}; -use alloc::{vec, vec::Vec}; use kernel_common::{instructions::pause, paging::PageTable}; use crate::{cpu::isr::ISR_INVALIDATE_TLB, BROADCASTED_PANIC}; @@ -10,7 +9,7 @@ use super::{ lapic::{get_current_lapic_id, send_ipi, BSP_LAPIC_ID, LAPICS, NEXT_LAPIC_ID}, process::get_kernel_process, sync::RawSpinlock, - task::{ALL_APS_STARTED, STACK_SIZE, STARTING_AP_ID}, + task::{allocate_stack, ALL_APS_STARTED, STARTING_AP_ID}, }; extern "C" { @@ -26,7 +25,7 @@ const IPI_STARTUP: u32 = 0x600; static INVALIDATE_TLB_LOCK: RawSpinlock = RawSpinlock::new(); pub fn start_aps() { - let stack: Vec = vec![0; STACK_SIZE]; + let stack = allocate_stack(); let pml4_phys_addr; { let kernel_proc = get_kernel_process(); @@ -45,7 +44,7 @@ pub fn start_aps() { *pml4_addr = pml4_phys_addr; let stack_offset = (&raw const trampoline_stack).offset_from(src_ptr); let stack_addr = (0x1000 + stack_offset) as *mut u64; - *stack_addr = stack.as_ptr() as u64 + STACK_SIZE as u64; + *stack_addr = stack.address; address_space.map_range(0x1000, 0x1000, 0x1000, false, true, true, false); } } diff --git a/kernel/src/sys/syscall.rs b/kernel/src/sys/syscall.rs index 4353aeb..4e5656d 100644 --- a/kernel/src/sys/syscall.rs +++ b/kernel/src/sys/syscall.rs @@ -1,10 +1,10 @@ use core::{error::Error, ptr::copy}; -use alloc::{boxed::Box, string::String, vec::Vec}; +use alloc::{boxed::Box, string::String, vec, vec::Vec}; use log::{debug, trace}; use crate::cpu::{ - cpu::{disable_user_memory_access, enable_user_memory_access}, + instructions::{disable_user_memory_access, enable_user_memory_access}, paging::USER_END, }; @@ -43,8 +43,7 @@ fn copy_from_user(start: u64, size: usize) -> Result, Box> { return Err("Invalid copy requested".into()); } } - let mut buffer = Vec::with_capacity(size); - buffer.resize(size, 0); + let mut buffer: Vec = vec![0; size]; enable_user_memory_access(); unsafe { copy(start as *const u8, buffer.as_mut_ptr(), size); diff --git a/kernel/src/sys/task.rs b/kernel/src/sys/task.rs index 7e70b04..faadb15 100644 --- a/kernel/src/sys/task.rs +++ b/kernel/src/sys/task.rs @@ -59,7 +59,7 @@ pub struct CPUData { pub user_stack: u64, } -pub const STACK_SIZE: usize = 64 * 1024; +const STACK_SIZE: usize = 64 * 1024; static NEXT_TASK_ID: AtomicUsize = AtomicUsize::new(2); pub static CURRENT_TASKS: Spinlock<[Option; 256]> = Spinlock::new([const { None }; 256]); static RFLAGS: AtomicU64 = AtomicU64::new(0);