Skip to main content

kernel/mm/
kernel.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! High-Level kernel memory management functions
3//!
4//! Authors: MarioS271
5
6use crate::lib::addr::VirtAddr;
7use crate::mm::mm_error::MmResult;
8use crate::mm::pmm::{FRAME_SIZE, Pmm};
9use crate::mm::vmm::Vmm;
10use crate::mm::vmm::address_space::AddressSpace;
11use crate::mm::vmm::vma::VmaFlags;
12use crate::state::kstate::KSTATE;
13
14/// Eagerly allocate a fixed-size 16 KiB kernel stack
15///
16/// Returns a virtual pointer to the top of the newly allocated stack
17///
18/// # Safety
19/// The caller must guarantee that boot stage 2 has already been completed
20pub unsafe fn alloc_stack(
21    pmm: &mut Pmm,
22    addr_space: &mut AddressSpace
23) -> MmResult<VirtAddr> {
24    let slot = KSTATE.mm.alloc_kernel_stack_slot();
25
26    let stack_bottom = slot + FRAME_SIZE;
27    let stack_top = stack_bottom + super::layout::KERNEL_STACK_SIZE;
28
29    Vmm::map_region(
30        pmm,
31        addr_space,
32        stack_bottom,
33        super::layout::KERNEL_STACK_SIZE,
34        VmaFlags::READ | VmaFlags::WRITE
35    )?;
36
37    Ok(stack_top)
38}