From f930bb2fd0b4a81797a186fe8224fb21f0485d76 Mon Sep 17 00:00:00 2001 From: Mathieu Strypsteen Date: Fri, 27 Dec 2024 08:27:09 +0100 Subject: [PATCH] Fix locking heap --- Cargo.lock | 26 +++++++------------------- kernel/Cargo.toml | 12 ++++++------ kernel/src/main.rs | 10 +++++----- kernel/src/misc/alloc.rs | 25 +++++++++++++++++++++++++ kernel/src/misc/mod.rs | 1 + loader/Cargo.toml | 4 ++-- 6 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 kernel/src/misc/alloc.rs diff --git a/Cargo.lock b/Cargo.lock index dd00ba8..09e6ae2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,7 +64,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.91", + "syn 2.0.92", ] [[package]] @@ -102,9 +102,6 @@ name = "buddy_system_allocator" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1a0108968a3a2dab95b089c0fc3f1afa7759aa5ebe6f1d86d206d6f7ba726eb" -dependencies = [ - "spin", -] [[package]] name = "byteorder" @@ -320,7 +317,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", - "syn 2.0.91", + "syn 2.0.92", ] [[package]] @@ -354,9 +351,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.37" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" dependencies = [ "proc-macro2", ] @@ -423,15 +420,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" -[[package]] -name = "spin" -version = "0.9.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" -dependencies = [ - "lock_api", -] - [[package]] name = "static-alloc" version = "0.2.5" @@ -454,9 +442,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.91" +version = "2.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035" +checksum = "70ae51629bf965c5c098cc9e87908a3df5301051a9e087d6f9bef5c9771ed126" dependencies = [ "proc-macro2", "quote", @@ -502,7 +490,7 @@ checksum = "9b24e77d3fc1e617051e630f99da24bcae6328abab37b8f9216bb68d06804f9a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.92", ] [[package]] diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 9c97378..ecfa6df 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -5,14 +5,14 @@ edition = "2021" license = "Unlicense" [dependencies] -acpi = {version = "5.1.0", default-features = false} -acpica-rs = {path = "../lib/acpica-rs"} +acpi = { version = "5.1.0", default-features = false } +acpica-rs = { path = "../lib/acpica-rs" } bitfield = "0.17.0" -bitvec = {version = "1.0.1", default-features = false, features = ["alloc", "atomic"]} -buddy_system_allocator = "0.11.0" -elf = {version = "0.7.4", default-features = false} +bitvec = { version = "1.0.1", default-features = false, features = ["alloc", "atomic"] } +buddy_system_allocator = { version = "0.11.0", default-features = false } +elf = { version = "0.7.4", default-features = false } embedded-graphics = "0.8.1" -kernel-common = {path = "../lib/kernel-common"} +kernel-common = { path = "../lib/kernel-common" } lock_api = "0.4.12" log = "0.4.22" raw-cpuid = "11.2.0" diff --git a/kernel/src/main.rs b/kernel/src/main.rs index 72e3d04..ad210e7 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -14,7 +14,6 @@ use core::{ use acpi::AcpiTables; use acpica_rs::{AcpiEnableSubsystem, AcpiInitializeObjects, AcpiInitializeSubsystem, AcpiInitializeTables, AcpiLoadTables, ACPI_FULL_INITIALIZATION}; use alloc::format; -use buddy_system_allocator::LockedHeap; use cpu::{cpu::set_cpu_flags, gdt::setup_gdt, idt::setup_idt, paging::setup_paging}; use kernel_common::{ instructions::{cli, hlt}, @@ -23,7 +22,10 @@ use kernel_common::{ paging::{KERNEL_HEAP_INITIAL_SIZE, KERNEL_HEAP_START}, }; use log::{error, info}; -use misc::display::{setup_display, PANIC_DISPLAY}; +use misc::{ + alloc::ALLOCATOR, + display::{setup_display, PANIC_DISPLAY}, +}; use sys::{ acpica_osl::AE_OK, early_acpi::EarlyACPIHandler, @@ -42,8 +44,6 @@ mod cpu; mod misc; mod sys; -#[global_allocator] -static ALLOC: LockedHeap<32> = LockedHeap::empty(); pub static RSDP_ADDRESS: AtomicU64 = AtomicU64::new(0); static LOADER_STRUCT: Spinlock = Spinlock::new(LoaderStruct { magic: 0, @@ -74,7 +74,7 @@ const INIT_BINARY: &[u8] = include_bytes!("../../target/x86_64-unknown-os/releas #[no_mangle] extern "C" fn early_main(temp_loader_struct: *const LoaderStruct) -> ! { unsafe { - ALLOC.lock().init(KERNEL_HEAP_START as usize, KERNEL_HEAP_INITIAL_SIZE); + ALLOCATOR.heap.lock().init(KERNEL_HEAP_START as usize, KERNEL_HEAP_INITIAL_SIZE); } let mut loader_struct = LOADER_STRUCT.lock(); unsafe { diff --git a/kernel/src/misc/alloc.rs b/kernel/src/misc/alloc.rs new file mode 100644 index 0000000..11b4e60 --- /dev/null +++ b/kernel/src/misc/alloc.rs @@ -0,0 +1,25 @@ +use core::{ + alloc::{GlobalAlloc, Layout}, + ptr::NonNull, +}; + +use buddy_system_allocator::Heap; + +use crate::sys::locks::Spinlock; + +pub struct KernelAllocator { + pub heap: Spinlock>, +} + +unsafe impl GlobalAlloc for KernelAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + self.heap.lock().alloc(layout).unwrap().as_ptr() + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + self.heap.lock().dealloc(NonNull::new(ptr).unwrap(), layout); + } +} + +#[global_allocator] +pub static ALLOCATOR: KernelAllocator = KernelAllocator { heap: Spinlock::new(Heap::new()) }; diff --git a/kernel/src/misc/mod.rs b/kernel/src/misc/mod.rs index 7e7f578..b51165d 100644 --- a/kernel/src/misc/mod.rs +++ b/kernel/src/misc/mod.rs @@ -1,3 +1,4 @@ +pub mod alloc; pub mod display; mod draw_target; pub mod elf; diff --git a/loader/Cargo.toml b/loader/Cargo.toml index 33baddc..bdddc21 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" license = "Unlicense" [dependencies] -elf = {version = "0.7.4", default-features = false} -kernel-common = {path = "../lib/kernel-common"} +elf = { version = "0.7.4", default-features = false } +kernel-common = { path = "../lib/kernel-common" } log = "0.4.22" raw-cpuid = "11.2.0" static-alloc = "0.2.5"