Skip to content
This repository was archived by the owner on Jul 17, 2025. It is now read-only.

Commit 5b3003a

Browse files
committed
Add function to pre-populate large heap objects PML4.
This becomes necessary once we made dispatcher allocation lazy. Since we didn't have this PT slot initialized before we spawned an ELF binary.
1 parent c3dfd46 commit 5b3003a

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kernel/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bit_field = "0.10"
4646
crossbeam-queue = { version = "0.3", default-features = false, features = ["alloc"] }
4747
arrayvec = { version = "0.7.0", default-features = false }
4848
memoffset = { version = "0.6.5", features = ["unstable_const"] }
49-
fallible_collections = { git = "https://github.com/gz/fallible_collections.git", branch = "allocator_api2", features = ["unstable", "rust_1_57"] }
49+
fallible_collections = { git = "https://github.com/gz/fallible_collections.git", branch = "allocator_api3", features = ["unstable", "rust_1_57"] }
5050
# Should be optional (but currently aren't, TODO)
5151
gimli = { version = "0.26", default-features = false, features = ["read", "endian-reader"] }
5252
gdbstub = { git = "https://github.com/daniel5151/gdbstub.git", branch = "dev/0.6", default-features = false, features = ["alloc"] }

kernel/src/arch/x86_64/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ fn _start(argc: isize, _argv: *const *const u8) -> isize {
372372
dyn_mem.set_global_pmem(&global_memory_static);
373373
core::mem::forget(dyn_mem);
374374

375+
unsafe { vspace::init_large_objects_pml4() };
376+
375377
// Set-up interrupt routing drivers (I/O APIC controllers)
376378
irq::ioapic_initialize();
377379

kernel/src/arch/x86_64/vspace/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::ops::Bound::*;
77
use fallible_collections::btree::BTreeMap;
88
use lazy_static::lazy_static;
99
use spin::Mutex;
10-
use x86::current::paging::{PDFlags, PDPTFlags, PTFlags};
10+
use x86::current::paging::{PDFlags, PDPTFlags, PTFlags, PML4Flags, PML4Entry};
1111

1212
mod debug;
1313
pub mod page_table; /* TODO(encapsulation): This should be a private module but we break encapsulation in a few places */
@@ -18,7 +18,7 @@ use crate::error::KError;
1818
use crate::memory::{detmem::DA, vspace::*};
1919
use crate::memory::{Frame, PAddr, VAddr};
2020

21-
use page_table::PageTable;
21+
use page_table::{PT_LAYOUT, PageTable};
2222

2323
lazy_static! {
2424
/// A handle to the initial kernel address space (created for us by the
@@ -83,6 +83,27 @@ lazy_static! {
8383
};
8484
}
8585

86+
/// We insert a pml4 slot for the big kernel allocated objects, this ensures
87+
/// that the PML4 entry is already there when we create a ELF process and
88+
/// patch-in the kernel mappings. Otherwise we don't have a mapping and lose
89+
/// access to big malloc'd objects in the kernel once we switch to a user-space
90+
/// page-table.
91+
///
92+
/// # TODO
93+
/// - This clearly needs a better solution. See also the part where we patch
94+
/// this into the process page-table.
95+
pub(crate) unsafe fn init_large_objects_pml4() {
96+
log::info!("init_large_objects_pml4()");
97+
let mut vspace = INITIAL_VSPACE.lock();
98+
let frame_ptr = alloc::alloc::alloc(PT_LAYOUT);
99+
100+
let vaddr = VAddr::from(frame_ptr as *const u8 as u64);
101+
let paddr = crate::arch::memory::kernel_vaddr_to_paddr(vaddr);
102+
let mut frame = Frame::new(paddr, PT_LAYOUT.size(), 0);
103+
frame.zero();
104+
(*vspace.pml4)[132] = PML4Entry::new(frame.base, PML4Flags::P | PML4Flags::RW);
105+
}
106+
86107
pub(crate) struct VSpace {
87108
pub mappings: BTreeMap<VAddr, MappingInfo>,
88109
pub page_table: PageTable,

kernel/src/arch/x86_64/vspace/page_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::memory::vspace::*;
1717
use crate::memory::{kernel_vaddr_to_paddr, paddr_to_kernel_vaddr, Frame, PAddr, VAddr};
1818

1919
/// Describes a potential modification operation on existing page tables.
20-
const PT_LAYOUT: Layout =
20+
pub(super) const PT_LAYOUT: Layout =
2121
unsafe { Layout::from_size_align_unchecked(BASE_PAGE_SIZE, BASE_PAGE_SIZE) };
2222
// Safety (size not overflowing when rounding up is given with size == align):
2323
static_assertions::const_assert!(BASE_PAGE_SIZE > 0); // align must not be zero

0 commit comments

Comments
 (0)