diff options
author | Christian Cunningham <cc@localhost> | 2022-08-19 23:13:53 -0700 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2022-08-19 23:13:53 -0700 |
commit | fe157268a376f5b945f1d64629e051340b06e6f2 (patch) | |
tree | 98a02388072436e2b3d6d58480071952eeb61033 | |
parent | 26ab71043d97c1b06bdd252378b64171cb95b1a9 (diff) |
New allocation
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | src/kernel.rs | 31 | ||||
-rw-r--r-- | src/mem.rs | 1 | ||||
-rw-r--r-- | src/mem/alloc.rs (renamed from src/alloc.rs) | 150 | ||||
-rw-r--r-- | src/panic_wait.rs | 26 |
5 files changed, 199 insertions, 10 deletions
@@ -1,2 +1,3 @@ /target Cargo.lock +*.swp diff --git a/src/kernel.rs b/src/kernel.rs index 3f10849..19c4e5f 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -12,10 +12,14 @@ #![feature(panic_info_message)] #![feature(trait_alias)] #![feature(exclusive_range_pattern)] +#![feature(default_alloc_error_handler)] #![no_main] #![no_std] -mod alloc; +extern crate alloc; +use alloc::boxed::Box; + +mod mem; mod console; mod cpu; mod panic_wait; @@ -23,7 +27,8 @@ mod print; mod sync; mod uart; use crate::console::console; -use crate::alloc::*; +use crate::mem::alloc::*; +//use crate::sync::interface::Mutex; /// # Initialization Code /// @@ -36,6 +41,10 @@ use crate::alloc::*; unsafe fn kernel_init() -> ! { console().init().unwrap(); U64_QUEUE_ALLOCATOR.init(); + ALLOCATOR.init(); + //ALLOCATOR.lock(|qa| { + // qa.init(); + //}); kernel_main() } @@ -43,16 +52,28 @@ unsafe fn kernel_init() -> ! { /// /// TODO: Figure out what to do here fn kernel_main() -> ! { - for idx in 0..30 { + for idx in 0..5 { if let Some(cell) = U64_QUEUE_ALLOCATOR.alloc() { let inner = cell.inner(); *inner = idx; - println!("SUCCESS: Allocated a char! {:?}", cell); + println!("SUCCESS: Allocated a char! {:?} {:?}", cell, U64_QUEUE_ALLOCATOR); U64_QUEUE_ALLOCATOR.free(cell); } else { - println!("ERROR: No more chars remaining!"); + println!("ERROR: No more chars remaining! {:?}", U64_QUEUE_ALLOCATOR); } } println!("I should be able to print {} here!", 5); + { + let a: Box<u8> = Box::new(5); + println!("{:?}", a); + let b: Box<u8> = Box::new(7); + println!("{:?}", b); + } + { + let a: Box<u8> = Box::new(8); + println!("{:?}", a); + let b: Box<u16> = Box::new(9); + println!("{:?}", b); + } loop { } } diff --git a/src/mem.rs b/src/mem.rs new file mode 100644 index 0000000..920965d --- /dev/null +++ b/src/mem.rs @@ -0,0 +1 @@ +pub mod alloc; diff --git a/src/alloc.rs b/src/mem/alloc.rs index 0c0df56..0b02fbd 100644 --- a/src/alloc.rs +++ b/src/mem/alloc.rs @@ -128,9 +128,7 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { } Some(entry) => { unsafe { - if (entry as u32) == (freed_item as *mut QueueItem<'a,T> as u32) { - (*entry).next = Some(freed_item as *mut QueueItem<'a,T>); - } + (*entry).next = Some(freed_item as *mut QueueItem<'a,T>); } } } @@ -155,3 +153,149 @@ impl<T: Debug,const COUNT: usize> Debug for QueueAllocator<'_,T,COUNT> { const U64_POOL_SIZE: usize = 2; init_queue!(U64_QUEUE_ALLOCATOR, U64_POOL_SIZE, 0, u64); + + + + + + +extern crate alloc; +use alloc::alloc::{GlobalAlloc,Layout}; + +pub struct GrandAllocator { } +const GRAND_ALLOC_SIZE: usize = 64; +init_queue!(U8_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u8); +init_queue!(U16_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u16); +init_queue!(U32_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u32); +init_queue!(U64_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u64); +init_queue!(U128_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u128); +impl GrandAllocator { + pub fn init(&self) { + U8_GRAND_ALLOC.init(); + U16_GRAND_ALLOC.init(); + U32_GRAND_ALLOC.init(); + U64_GRAND_ALLOC.init(); + U128_GRAND_ALLOC.init(); + } +} + +unsafe impl GlobalAlloc for GrandAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + match layout.size() { + 1 => { + match U8_GRAND_ALLOC.alloc() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).inner() as *mut u8; + } + } + } + /* + 2 => { + U16_GRAND_ALLOC.inner.lock(|pool| { + match pool.alloc() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + (*elem).inner() as *mut u8; + } + } + }) + } + 4 => { + U32_GRAND_ALLOC.inner.lock(|pool| { + match pool.alloc() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + (*elem).inner() as *mut u8; + } + } + }) + } + 8 => { + U64_GRAND_ALLOC.inner.lock(|pool| { + match pool.alloc() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + (*elem).inner() as *mut u8; + } + } + }) + } + */ + _ => { + panic!("No allocators for size {}!", layout.size()); + } + } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + match layout.size() { + 1 => { + U8_GRAND_ALLOC.inner.lock(|pool| { + for idx in 2..pool.len() { + if pool[idx].inner() as *mut u8 == ptr { + U8_GRAND_ALLOC.free(&mut pool[idx]); + return; + } + } + }); + } + _ => { + panic!("No deallocators for size {}!", layout.size()); + } + } + } +} + +#[global_allocator] +pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; +/* +unsafe impl<const COUNT: usize> GlobalAlloc for NullLock<QueueAllocator<'_,u8,COUNT>> { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + match layout.size() { + 1 => { + self.lock(|qa| { + match qa.alloc() { + None => { + panic!("No data to allocate!"); + } + Some(elem) => { + return (*elem).inner() as *mut u8; + } + } + }) + } + _ => { + panic!("No allocators for size {}!", layout.size()); + } + } + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { + self.lock(|qa| { + qa.inner.lock(|pool| { + for idx in 2..COUNT { + if pool[idx].inner() as *mut u8 == _ptr { + qa.free(&mut pool[idx]); + return; + } + } + }); + }); + } +} + +const GLOBAL_ALLOCATOR_SIZE: usize = 100; + +// TODO: Add other allocation sizes +#[global_allocator] +pub static ALLOCATOR: NullLock<QueueAllocator<'static,u8,{GLOBAL_ALLOCATOR_SIZE+2}>> = NullLock::new(QueueAllocator::<u8,{GLOBAL_ALLOCATOR_SIZE+2}>{inner: NullLock::new([QueueItem{data: 0, next: None}; {GLOBAL_ALLOCATOR_SIZE+2}])}); +*/ diff --git a/src/panic_wait.rs b/src/panic_wait.rs index 1136e54..2181869 100644 --- a/src/panic_wait.rs +++ b/src/panic_wait.rs @@ -2,10 +2,32 @@ //! //! A panic handler that infinitely waits +use crate::{cpu,println}; use core::panic::PanicInfo; +fn panic_prevent_reenter() { + use core::sync::atomic::{AtomicBool, Ordering}; + + static PANIC_IN_PROGRESS: AtomicBool = AtomicBool::new(false); + + if !PANIC_IN_PROGRESS.load(Ordering::Relaxed) { + PANIC_IN_PROGRESS.store(true, Ordering::Relaxed); + return; + } + + cpu::wait_forever() +} + /// # Panic handler #[panic_handler] -fn panic(_info: &PanicInfo) -> ! { - unimplemented!() +fn panic(info: &PanicInfo) -> ! { + panic_prevent_reenter(); + + let (location, line, column) = match info.location() { + Some(loc) => (loc.file(), loc.line(), loc.column()), + _ => ("???",0,0), + }; + + println!("Kernel panic!\n\nPanic Location:\n\tFile: '{}', line {}, column {}\n\n{}", location, line, column, info.message().unwrap_or(&format_args!("")),); + cpu::wait_forever() } |