From ae70548cc0ba771eb4874145585d254305fe3f72 Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 7 May 2022 14:08:34 +0200 Subject: [PATCH] Moved allocator to the kernel --- Cargo.lock | 1 + ableos/src/arch/x86_64/drivers/allocator.rs | 4 ++-- ableos/src/experiments/kinfo.rs | 3 ++- ableos/src/lib.rs | 2 -- ableos/src/rhai_shell/mod.rs | 2 +- kernel/Cargo.toml | 3 ++- kernel/src/allocator.rs | 24 +++++++++++++++++++++ kernel/src/lib.rs | 10 ++++++--- 8 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 kernel/src/allocator.rs diff --git a/Cargo.lock b/Cargo.lock index 850b6b4b..32c5f2aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,6 +384,7 @@ dependencies = [ name = "kernel" version = "0.1.2" dependencies = [ + "linked_list_allocator", "log", "versioning", ] diff --git a/ableos/src/arch/x86_64/drivers/allocator.rs b/ableos/src/arch/x86_64/drivers/allocator.rs index b1057be9..14da4063 100644 --- a/ableos/src/arch/x86_64/drivers/allocator.rs +++ b/ableos/src/arch/x86_64/drivers/allocator.rs @@ -1,4 +1,4 @@ -use crate::allocator::{HEAP_SIZE, HEAP_START}; +use kernel::allocator::{HEAP_SIZE, HEAP_START}; use alloc::alloc::{GlobalAlloc, Layout}; use core::ptr::null_mut; use x86_64::{ @@ -39,7 +39,7 @@ pub fn init_heap( } unsafe { - crate::allocator::ALLOCATOR + kernel::allocator::ALLOCATOR .lock() .init(HEAP_START, HEAP_SIZE); } diff --git a/ableos/src/experiments/kinfo.rs b/ableos/src/experiments/kinfo.rs index 46646db5..fdacbb26 100644 --- a/ableos/src/experiments/kinfo.rs +++ b/ableos/src/experiments/kinfo.rs @@ -1,6 +1,7 @@ use super::systeminfo::SystemMemory; -use crate::{arch::drivers::sysinfo::master, ALLOCATOR}; +use crate::arch::drivers::sysinfo::master; use core::fmt::Display; +use kernel::allocator::ALLOCATOR; use versioning::Version; use x86_64::instructions::interrupts::{disable, enable}; diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index 911b3e33..98892da3 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -40,7 +40,6 @@ pub mod print; #[macro_use] pub mod serial_print; -pub mod allocator; pub mod boot_conf; pub mod devices; pub mod driver_traits; @@ -68,7 +67,6 @@ mod unicode_utils; #[prelude_import] pub use prelude::rust_2021::*; -pub use allocator::*; pub use driver_traits::*; pub use experiments::*; pub use graphics::*; diff --git a/ableos/src/rhai_shell/mod.rs b/ableos/src/rhai_shell/mod.rs index aae557bc..7886826a 100644 --- a/ableos/src/rhai_shell/mod.rs +++ b/ableos/src/rhai_shell/mod.rs @@ -2,13 +2,13 @@ use crate::arch::drivers::sysinfo::master; use crate::filesystem::FILE_SYSTEM; use crate::time::fetch_time; use crate::wasm_jumploader::interp; -use crate::ALLOCATOR; use crate::{ arch::{shutdown, sloop}, systeminfo::{KERNEL_VERSION, RELEASE_TYPE}, KERNEL_STATE, }; use genfs::{Fs, OpenOptions}; +use kernel::allocator::ALLOCATOR; use rhai::Engine; use spin::Lazy; use x86_64::instructions::interrupts::{disable, enable}; diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index b6b5df25..8a86d749 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -4,7 +4,8 @@ name = "kernel" version = "0.1.2" [dependencies] +linked_list_allocator = "0.9" log = "0.4.14" [dependencies.versioning] -git = "https://git.ablecorp.us/able/aos_userland" \ No newline at end of file +git = "https://git.ablecorp.us/able/aos_userland" diff --git a/kernel/src/allocator.rs b/kernel/src/allocator.rs new file mode 100644 index 00000000..3c177d48 --- /dev/null +++ b/kernel/src/allocator.rs @@ -0,0 +1,24 @@ +//! Memory allocator + +use linked_list_allocator::LockedHeap; + +/// +pub const HEAP_START: usize = 0x_4444_4444_0000; + +/// +pub const HEAP_MULTIPLIER: usize = 100000; + +/// +pub const HEAP_BASE: usize = 100; + +/// +pub const HEAP_SIZE: usize = HEAP_BASE * HEAP_MULTIPLIER; + +/// Global allocator +#[global_allocator] +pub static ALLOCATOR: LockedHeap = LockedHeap::empty(); + +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("allocation error: {:?}", layout) +} diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 42902107..53f1f734 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -1,10 +1,14 @@ //! The ableOS kernel. -#![deny(missing_docs)] -#![no_std] -#![feature(prelude_import)] +#![feature(alloc_error_handler)] #![feature(arbitrary_enum_discriminant)] +#![feature(prelude_import)] +#![no_std] +#![deny(missing_docs)] +extern crate alloc; + +pub mod allocator; pub mod device_interface; pub mod messaging; pub mod panic;