Skip to content

Commit 60463e2

Browse files
committed
Move memory initialisation from kmain to kernel::init
Previously, we initialised the heap (and the rest of memory) in kmain, as memory::init used to return the PML4. Now that it doesn't, there's no reason not to move memory::init into kernel::init. Signed-off-by: SlyMarbo <[email protected]>
1 parent 9e1f78a commit 60463e2

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

kernel/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate alloc;
3131

3232
use alloc::vec;
3333
use alloc::vec::Vec;
34+
use bootloader::BootInfo;
3435
use core::panic::PanicInfo;
3536
use lazy_static::lazy_static;
3637
use raw_cpuid::CpuId;
@@ -50,11 +51,14 @@ lazy_static! {
5051

5152
/// init sets up critical core functions of the kernel.
5253
///
53-
pub fn init() {
54+
pub fn init(boot_info: &'static BootInfo) {
5455
gdt::init();
5556
interrupts::init();
5657
time::init();
5758
x86_64::instructions::interrupts::enable();
59+
60+
// Set up the heap allocator.
61+
unsafe { memory::init(boot_info) };
5862
}
5963

6064
#[alloc_error_handler]
@@ -287,16 +291,16 @@ pub fn shutdown_qemu() {
287291
}
288292

289293
#[cfg(test)]
290-
use bootloader::{entry_point, BootInfo};
294+
use bootloader::entry_point;
291295

292296
#[cfg(test)]
293297
entry_point!(test_kernel_main);
294298

295299
/// test_kernel_main is the entry point for `cargo xtest`.
296300
///
297301
#[cfg(test)]
298-
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
299-
init();
302+
fn test_kernel_main(boot_info: &'static BootInfo) -> ! {
303+
init(boot_info);
300304
test_main();
301305
halt_loop();
302306
}

kernel/src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,20 @@ entry_point!(kernel_main);
3535
#[allow(unused_variables)]
3636
fn kernel_main(boot_info: &'static BootInfo) -> ! {
3737
println!("Kernel booting...");
38-
kernel::init();
38+
kernel::init(boot_info);
3939

4040
#[cfg(test)]
4141
test_main();
4242

4343
#[cfg(not(test))]
44-
kmain(boot_info);
44+
kmain();
4545

4646
kernel::shutdown_qemu();
4747
kernel::halt_loop();
4848
}
4949

5050
#[cfg(not(test))]
51-
fn kmain(boot_info: &'static BootInfo) {
52-
// Set up the heap allocator.
53-
unsafe { memory::init(boot_info) };
54-
51+
fn kmain() {
5552
println!("Kernel ready!");
5653
println!("Kernel booted at {}.", time::boot_time());
5754
if let Some(branding) = CPU_ID.get_processor_brand_string() {

kernel/tests/heap_allocation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ use bootloader::bootinfo::{FrameRange, MemoryRegion, MemoryRegionType};
1111
use bootloader::{entry_point, BootInfo};
1212
use core::panic::PanicInfo;
1313
use kernel::memory::pmm::BitmapFrameAllocator;
14-
use kernel::memory::{self, KERNEL_HEAP};
14+
use kernel::memory::KERNEL_HEAP;
1515
use kernel::Bitmap;
1616
use x86_64::structures::paging::{FrameAllocator, FrameDeallocator, PhysFrame};
1717
use x86_64::PhysAddr;
1818

1919
entry_point!(main);
2020

2121
fn main(boot_info: &'static BootInfo) -> ! {
22-
kernel::init();
23-
unsafe { memory::init(boot_info) };
22+
kernel::init(boot_info);
2423

2524
test_main();
2625
loop {}

0 commit comments

Comments
 (0)