forked from rust-osdev/bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit fixes the review changes in rust-osdev#317. Most of the credit should go to Jason Couture <[email protected]>
- Loading branch information
Showing
7 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use bootloader_test_runner::run_test_kernel; | ||
#[test] | ||
fn lower_memory_free() { | ||
run_test_kernel(env!( | ||
"CARGO_BIN_FILE_TEST_KERNEL_LOWER_MEMORY_FREE_lower_memory_free" | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "test_kernel_lower_memory_free" | ||
version = "0.1.0" | ||
authors = ["Philipp Oppermann <[email protected]>"] | ||
edition = "2021" | ||
|
||
[dependencies] | ||
bootloader_api = { path = "../../../api" } | ||
x86_64 = { version = "0.14.7", default-features = false, features = [ | ||
"instructions", | ||
"inline_asm", | ||
] } | ||
uart_16550 = "0.2.10" |
45 changes: 45 additions & 0 deletions
45
tests/test_kernels/lower_memory_free/src/bin/lower_memory_free.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![no_std] // don't link the Rust standard library | ||
#![no_main] // disable all Rust-level entry points | ||
|
||
use bootloader_api::{entry_point, info::MemoryRegionKind, BootInfo}; | ||
use test_kernel_lower_memory_free::{exit_qemu, QemuExitCode}; | ||
|
||
const LOWER_MEMORY_END_PAGE: u64 = 0x0010_0000; | ||
|
||
entry_point!(kernel_main); | ||
|
||
fn kernel_main(boot_info: &'static mut BootInfo) -> ! { | ||
use core::fmt::Write; | ||
use test_kernel_lower_memory_free::serial; | ||
|
||
let mut count = 0; | ||
for region in boot_info.memory_regions.iter() { | ||
writeln!( | ||
serial(), | ||
"Region: {:016x}-{:016x} - {:?}", | ||
region.start, | ||
region.end, | ||
region.kind | ||
) | ||
.unwrap(); | ||
if region.kind == MemoryRegionKind::Usable && region.start < LOWER_MEMORY_END_PAGE { | ||
let end = core::cmp::min(region.end, LOWER_MEMORY_END_PAGE); | ||
let pages = (end - region.start) / 4096; | ||
count += pages; | ||
} | ||
} | ||
|
||
writeln!(serial(), "Free lower memory page count: {}", count).unwrap(); | ||
assert!(count > 0x10); // 0x20 chosen arbirarily, we need _some_ free conventional memory, but not all of it. Some, especially on BIOS, may be reserved for hardware. | ||
exit_qemu(QemuExitCode::Success); | ||
} | ||
|
||
/// This function is called on panic. | ||
#[panic_handler] | ||
#[cfg(not(test))] | ||
fn panic(info: &core::panic::PanicInfo) -> ! { | ||
use core::fmt::Write; | ||
|
||
let _ = writeln!(test_kernel_lower_memory_free::serial(), "PANIC: {}", info); | ||
exit_qemu(QemuExitCode::Failed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#![no_std] | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
#[repr(u32)] | ||
pub enum QemuExitCode { | ||
Success = 0x10, | ||
Failed = 0x11, | ||
} | ||
|
||
pub fn exit_qemu(exit_code: QemuExitCode) -> ! { | ||
use x86_64::instructions::{nop, port::Port}; | ||
|
||
unsafe { | ||
let mut port = Port::new(0xf4); | ||
port.write(exit_code as u32); | ||
} | ||
|
||
loop { | ||
nop(); | ||
} | ||
} | ||
|
||
pub fn serial() -> uart_16550::SerialPort { | ||
let mut port = unsafe { uart_16550::SerialPort::new(0x3F8) }; | ||
port.init(); | ||
port | ||
} |