From a8e251947a90d7b9328eceec1165c7c91a1ec0f7 Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Thu, 14 Apr 2022 23:42:24 +0200 Subject: [PATCH 1/2] GlobalAlloc: implement GlobalAlloc --- .cargo/config.toml | 2 +- src/elsos.rs | 4 +++- src/memory/allocator.rs | 35 +++++++++++++++++++++++++++++++++++ src/memory/mod.rs | 1 + src/memory/pageframe.rs | 3 +++ 5 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 src/memory/allocator.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 718cfa8..7317206 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,2 @@ [unstable] -build-std = ["core"] +build-std = ["core", "alloc"] diff --git a/src/elsos.rs b/src/elsos.rs index 861fa11..a4d1803 100644 --- a/src/elsos.rs +++ b/src/elsos.rs @@ -4,6 +4,9 @@ #![allow(dead_code)] #![no_std] #![no_main] +#![feature(alloc_error_handler)] + +extern crate alloc; use core::panic::PanicInfo; use crate::multiboot::{MULTIBOOT_MMAP, MULTIBOOT_MMAP_ENTRIES}; @@ -255,4 +258,3 @@ macro_rules! logln () => ($crate::log!("\n")); ($($arg:tt)*) => ($crate::log!("{}\n", format_args!($($arg)*))); } - diff --git a/src/memory/allocator.rs b/src/memory/allocator.rs new file mode 100644 index 0000000..d4a74e5 --- /dev/null +++ b/src/memory/allocator.rs @@ -0,0 +1,35 @@ +use alloc::alloc::{GlobalAlloc, Layout}; +use core::ffi::c_void; +use super::*; + +pub struct Allocator; + +unsafe impl GlobalAlloc for Allocator +{ + unsafe fn alloc(&self, _layout: Layout) -> *mut u8 + { + crate::serial_println!("trying to allocate {} ({:#x}) bytes...", _layout.size(), _layout.size()); + let address = vmalloc(_layout.size()); + crate::serial_println!("allocated {} bytes at {:p}", _layout.size(), address); + address as *mut u8 + } + + unsafe fn alloc_zeroed(&self, _layout: Layout) -> *mut u8 + { + kzalloc(_layout.size()) as *mut u8 + } + + unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) + { + crate::serial_println!("deallocating {:p}", _ptr); + vfree(_ptr as *mut c_void); + } +} + +#[global_allocator] +static ALLOCATOR: Allocator = Allocator; + +#[alloc_error_handler] +fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! { + panic!("allocation error: {:?}", layout) +} diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 55dba0e..0d0b8a0 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -4,6 +4,7 @@ use crate::multiboot::MultibootTagMmap; use pagetable::flags::*; pub use malloc::*; +pub mod allocator; pub mod malloc; mod page; mod pageframe; diff --git a/src/memory/pageframe.rs b/src/memory/pageframe.rs index 1f3f906..305ad8a 100644 --- a/src/memory/pageframe.rs +++ b/src/memory/pageframe.rs @@ -57,11 +57,14 @@ impl Allocator kernel_start = &_kernel_start as *const _ as usize; kernel_end = &_kernel_end as *const _ as usize; } + crate::logln!("KERNEL START {:#08x} END {:#08x}", kernel_start, kernel_end); + self.reserved_mem = crate::memory::get_mem_size(mmap, mmap_size); crate::logln!("[INFO] found {}KiB of memory", self.reserved_mem / 1024); // initialise the bitmap according to mem size, and set every page as reserved self.init_bitmap(ferramenta::align(kernel_end, 0x1000)); crate::logln!("[INFO] assigned {} pages to bitmap", self.bitmap.size); + crate::logln!("[INFO] bitmap end : {:#08x}", &self.bitmap as *const _ as usize + self.bitmap.buffer.len()); unsafe { for entry in (*mmap).entries(mmap_size) From 27cbe795974dcb934aea3315f790d01fd2646a4e Mon Sep 17 00:00:00 2001 From: Edgar Fouillet Date: Fri, 4 Nov 2022 16:26:48 +0100 Subject: [PATCH 2/2] commands: add a str test command --- src/tty/basic_commands.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tty/basic_commands.rs b/src/tty/basic_commands.rs index 5fd0ce2..523ea4f 100644 --- a/src/tty/basic_commands.rs +++ b/src/tty/basic_commands.rs @@ -63,6 +63,13 @@ pub fn execute(command: &str) { loadkeys(arg); }, + "str" => + { + let a = alloc::string::String::from(arg); + crate::logln!("Allocated String containing \"{}\"", a); + crate::logln!(" heap content address {:p}", a.as_ptr()); + crate::logln!("stack pointer address {:p}", &a); + }, _ => crate::println!("{}: unknown or invalid command. Use help for more", command) } }