-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |