Skip to main content

kernel/arch/x86_64/mm/vmm/paging/
helpers.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! VMM Paging (x86_64) helpers
3//!
4//! Authors: MarioS271
5
6use super::super::page_type::PageType;
7use crate::lib::panic_codes::PanicCode;
8use crate::lib::panic::kernel_panic;
9use crate::state::kstate::KSTATE;
10use x86_64::structures::paging::page_table::PageTableEntry;
11use x86_64::structures::paging::{PageTable, PageTableFlags};
12
13/// Panic when a given address is not aligned properly
14pub fn misaligned_address_panic(align: PageType) -> ! {
15    kernel_panic(
16        PanicCode::InvalidPageOperation,
17        match align {
18            PageType::Normal => "Physical and/or virtual address is not aligned to 4 KiB",
19            PageType::HugePage2MiB => "Physical and/or virtual address is not aligned to 2 MiB",
20            PageType::HugePage1GiB => "Physical and/or virtual address is not aligned to 1 GiB"
21        }
22    );
23}
24
25/// Check whether the given [`PageTableEntry`] has the `PRESENT` property
26pub fn is_present(entry: &PageTableEntry) -> bool {
27    entry.flags().contains(PageTableFlags::PRESENT)
28}
29
30/// Check whether the given [`PageTableEntry`] has the `HUGE_PAGE` property
31pub fn is_huge_page(entry: &PageTableEntry) -> bool {
32    entry.flags().contains(PageTableFlags::HUGE_PAGE)
33}
34
35/// Returns a mutable pointer to the next page table
36pub fn advance_current_pagetable(entry: &PageTableEntry) -> *mut PageTable {
37    (entry.frame().unwrap().start_address().as_u64() + KSTATE.mm.hhdm_offset()) as *mut PageTable
38}