Skip to content

Commit

Permalink
feat: set up kernel stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Hqnnqh committed Dec 26, 2024
1 parent 20de18f commit 3335c3e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use graphics::{
FG_COLOR_ERROR, FG_COLOR_INFO, FG_COLOR_LOG, FG_COLOR_OK,
};
use log::{error, info};
use memory::KERNEL_STACK_SIZE;
use uefi::prelude::*;

mod error;
Expand All @@ -37,7 +38,7 @@ fn main() -> Status {

logln!(FG_COLOR_CAPTION, "{}", CAPTION);

log!(FG_COLOR_LOG, " [LOG ]: Initialize framebuffer ");
log!(FG_COLOR_LOG, " [LOG ]: Initializing framebuffer ");
logln!(FG_COLOR_OK, "OK");

// get kernel file from disk
Expand All @@ -59,6 +60,17 @@ fn main() -> Status {
kernel_elf.base(),
kernel_elf.num_pages()
);

let kernel_stack = validate!(
memory::allocate_kernel_stack(KERNEL_STACK_SIZE),
"Allocating memory for kernel stack"
);
loginfo!(
"Kernel stack top: {:#x}, bottom: {:#x}, pages: {:#x}",
kernel_stack.top(),
kernel_stack.bottom(),
kernel_stack.num_pages()
);
}
// this won't always be shown in the console, because stdout may not be available in some cases
Err(err) => error!("Bootloader: Failed to initialize framebuffer: {}", err),
Expand Down
46 changes: 45 additions & 1 deletion src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
use uefi::boot::MemoryType;
use hal::{PhysicalAddress, PAGE_SIZE};
use uefi::boot::{self, AllocateType, MemoryType};

// custom memory types of the NebulaLoader
pub(crate) const PSF_DATA: MemoryType = MemoryType::custom(0x8000_0000);
pub(crate) const KERNEL_CODE: MemoryType = MemoryType::custom(0x8000_0001);
pub(crate) const KERNEL_STACK: MemoryType = MemoryType::custom(0x8000_0002);

pub(crate) const KERNEL_STACK_SIZE: usize = 1024 * 1024; // 1 MB

#[derive(Copy, Clone, Debug)]
pub(crate) struct KernelStack {
/// Starting address of memory allocated for stack
///
/// > since uefi sets up identity-mapped paging, the virtual and physical addresses are equivalent
bottom: PhysicalAddress,
/// Address of stack top
///
/// > since uefi sets up identity-mapped paging, the virtual and physical addresses are equivalent
top: PhysicalAddress,
/// Number of stack pages
num_pages: usize,
}

impl KernelStack {
pub(crate) fn bottom(&self) -> PhysicalAddress {
self.bottom
}
pub(crate) fn top(&self) -> PhysicalAddress {
self.top
}
pub(crate) fn num_pages(&self) -> usize {
self.num_pages
}
}

/// Allocate kernel stack with the given size in bytes (aligned to upward page-size)
pub(crate) fn allocate_kernel_stack(bytes: usize) -> Result<KernelStack, uefi::Error> {
let num_pages = bytes.div_ceil(PAGE_SIZE);
let bottom = boot::allocate_pages(AllocateType::AnyPages, KERNEL_STACK, num_pages)?.as_ptr()
as PhysicalAddress;
let top = bottom + (PAGE_SIZE * num_pages) as u64;

Ok(KernelStack {
bottom,
top,
num_pages,
})
}

0 comments on commit 3335c3e

Please sign in to comment.