Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[unstable]
build-std = ["core"]
build-std = ["core", "alloc"]
4 changes: 3 additions & 1 deletion src/elsos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -255,4 +258,3 @@ macro_rules! logln
() => ($crate::log!("\n"));
($($arg:tt)*) => ($crate::log!("{}\n", format_args!($($arg)*)));
}

35 changes: 35 additions & 0 deletions src/memory/allocator.rs
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::multiboot::MultibootTagMmap;
use pagetable::flags::*;
pub use malloc::*;

pub mod allocator;
pub mod malloc;
mod page;
mod pageframe;
Expand Down
3 changes: 3 additions & 0 deletions src/memory/pageframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/tty/basic_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down