Skip to main content

kernel/mm/vmm/
boot_mapping.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! A datatype which describes a region of memory, used when memory only gets mapped in paging early
3//! on and later recieves VMAs
4//!
5//! Authors: MarioS271
6
7use crate::lib::addr::{PhysAddr, VirtAddr};
8use crate::lib::types::boot_info::KernelSectionInfo;
9use crate::mm::layout;
10use crate::mm::vmm::vma::VmaFlags;
11
12/// A data structure to define a memory mapping for when its definition needs to be remembered briefly
13/// (like when VMAs and pages get build at different times)
14pub struct BootMapping {
15    pub virt: VirtAddr,
16    pub phys: PhysAddr,
17    pub size: u64,
18    pub flags: VmaFlags,
19    pub can_overmap: bool
20}
21
22/// A type to avoid repeating `[BootMapping; 4]` everywhere
23pub type BootMappings = [BootMapping; 4];
24
25pub fn build_boot_mappings(sections: &KernelSectionInfo, total_mem: u64) -> BootMappings {
26    let image_offset = sections.kernel_phys_start.wrapping_sub(layout::KERNEL_IMAGE_BASE.as_u64());
27
28    [
29        // HHDM
30        BootMapping {
31            virt: layout::KERNEL_HHDM_BASE,
32            phys: PhysAddr::new(0),
33            size: total_mem,
34            flags: VmaFlags::READ | VmaFlags::WRITE,
35            can_overmap: true
36        },
37        // .text
38        BootMapping {
39            virt: VirtAddr::new(sections.kernel_start),
40            phys: PhysAddr::new(sections.kernel_start.wrapping_add(image_offset)),
41            size: sections.kernel_text_end - sections.kernel_start,
42            flags: VmaFlags::READ | VmaFlags::EXEC,
43            can_overmap: false
44        },
45        // .rodata
46        BootMapping {
47            virt: VirtAddr::new(sections.kernel_text_end),
48            phys: PhysAddr::new(sections.kernel_text_end.wrapping_add(image_offset)),
49            size: sections.kernel_rodata_end - sections.kernel_text_end,
50            flags: VmaFlags::READ,
51            can_overmap: false
52        },
53        // .data / .bss
54        BootMapping {
55            virt: VirtAddr::new(sections.kernel_rodata_end),
56            phys: PhysAddr::new(sections.kernel_rodata_end.wrapping_add(image_offset)),
57            size: sections.kernel_end - sections.kernel_rodata_end,
58            flags: VmaFlags::READ | VmaFlags::WRITE,
59            can_overmap: false
60        }
61    ]
62}