Skip to content

Commit

Permalink
allocation working
Browse files Browse the repository at this point in the history
  • Loading branch information
svelterust committed Jan 29, 2024
1 parent 8590d9f commit 7566b0b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 11 deletions.
2 changes: 1 addition & 1 deletion os/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[unstable]
build-std = ["core", "compiler_builtins"]
build-std = ["core", "compiler_builtins", "alloc"]
build-std-features = ["compiler-builtins-mem"]

[build]
Expand Down
19 changes: 19 additions & 0 deletions os/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ volatile = "0.2.6"
uart_16550 = "0.2.0"
pic8259 = "0.10.4"
pc-keyboard = "0.7.0"
linked_list_allocator = "0.9.0"
lazy_static = { version = "1.0", features = ["spin_no_std"] }
bootloader = { version = "0.9.23", features = ["map_physical_memory"]}

Expand Down
43 changes: 43 additions & 0 deletions os/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use x86_64::{
structures::paging::{
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
},
VirtAddr,
};
use linked_list_allocator::LockedHeap;

#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();

pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
let page_range = {
let heap_start = VirtAddr::new(HEAP_START as u64);
let heap_end = heap_start + HEAP_SIZE - 1u64;
let heap_start_page = Page::containing_address(heap_start);
let heap_end_page = Page::containing_address(heap_end);
Page::range_inclusive(heap_start_page, heap_end_page)
};

for page in page_range {
let frame = frame_allocator
.allocate_frame()
.ok_or(MapToError::FrameAllocationFailed)?;
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe {
mapper.map_to(page, frame, flags, frame_allocator)?.flush()
};
}

// Initialize the allocator with the correct bounds
unsafe {
ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
}

Ok(())
}

pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
3 changes: 3 additions & 0 deletions os/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]

extern crate alloc;

#[cfg(test)]
use bootloader::{entry_point, BootInfo};

Expand All @@ -13,6 +15,7 @@ pub mod interrupts;
pub mod serial;
pub mod vga;
pub mod memory;
pub mod allocator;

pub fn init() {
gdt::init();
Expand Down
35 changes: 25 additions & 10 deletions os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,49 @@
#![test_runner(knarkos::test_runner)]
#![reexport_test_harness_main = "test_main"]

extern crate alloc;

use knarkos::println;
use bootloader::{BootInfo, entry_point};
use alloc::{boxed::Box, vec, vec::Vec, rc::Rc};

entry_point!(kernel_main);

fn kernel_main(boot_info: &'static BootInfo) -> ! {
use knarkos::memory;
use knarkos::{memory, allocator};
use knarkos::memory::BootInfoFrameAllocator;
use x86_64::{structures::paging::Page, VirtAddr}; // new import
use x86_64::VirtAddr;

// Initialize operating system
knarkos::init();
println!("Hello, world!");

// Read page tables
// Setup heap
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
let mut mapper = unsafe { memory::init(phys_mem_offset) };
let mut frame_allocator = unsafe {
BootInfoFrameAllocator::init(&boot_info.memory_map)
};

// map an unused page
let page = Page::containing_address(VirtAddr::new(0));
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");

// Heap allocation
// allocate a number on the heap
let heap_value = Box::new(41);
println!("heap_value at {:p}", heap_value);

// create a dynamically sized vector
let mut vec = Vec::new();
for i in 0..500 {
vec.push(i);
}
println!("vec at {:p}", vec.as_slice());

// write the string `New!` to the screen through the new mapping
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e)};
// create a reference counted vector -> will be freed when count reaches 0
let reference_counted = Rc::new(vec![1, 2, 3]);
let cloned_reference = reference_counted.clone();
println!("current reference count is {}", Rc::strong_count(&cloned_reference));
core::mem::drop(reference_counted);
println!("reference count is {} now", Rc::strong_count(&cloned_reference));

#[cfg(test)]
test_main();
Expand Down

0 comments on commit 7566b0b

Please sign in to comment.