Skip to content

Commit

Permalink
New test to check usable memory is writable
Browse files Browse the repository at this point in the history
split out the write check from the lower_memory_free test. Those 2
aren't really related
  • Loading branch information
Wasabi375 committed Jun 22, 2024
1 parent 4446278 commit f3d383e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"tests/test_kernels/ramdisk",
"tests/test_kernels/min_stack",
"tests/test_kernels/lower_memory_free",
"tests/test_kernels/write_usable_memory",
]
exclude = ["examples/basic", "examples/test_framework"]

Expand Down Expand Up @@ -69,6 +70,7 @@ test_kernel_ramdisk = { path = "tests/test_kernels/ramdisk", artifact = "bin", t
test_kernel_config_file = { path = "tests/test_kernels/config_file", artifact = "bin", target = "x86_64-unknown-none" }
test_kernel_min_stack = { path = "tests/test_kernels/min_stack", artifact = "bin", target = "x86_64-unknown-none" }
test_kernel_lower_memory_free = { path = "tests/test_kernels/lower_memory_free", artifact = "bin", target = "x86_64-unknown-none" }
test_kernel_write_usable_memory = { path = "tests/test_kernels/write_usable_memory", artifact = "bin", target = "x86_64-unknown-none" }

[profile.dev]
panic = "abort"
Expand Down
12 changes: 0 additions & 12 deletions tests/test_kernels/lower_memory_free/src/bin/lower_memory_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use bootloader_api::{
use test_kernel_lower_memory_free::{exit_qemu, QemuExitCode};

const LOWER_MEMORY_END_PAGE: u64 = 0x0010_0000;
const WRITE_TEST_UNTIL: u64 = 0x4000_0000;

pub const BOOTLOADER_CONFIG: BootloaderConfig = {
let mut config = BootloaderConfig::new_default();
Expand All @@ -21,8 +20,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
use core::fmt::Write;
use test_kernel_lower_memory_free::serial;

let phys_mem_offset = boot_info.physical_memory_offset.into_option().unwrap();

let mut count = 0;
for region in boot_info.memory_regions.iter() {
writeln!(
Expand All @@ -38,15 +35,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
let pages = (end - region.start) / 4096;
count += pages;
}
if region.kind == MemoryRegionKind::Usable && region.start < WRITE_TEST_UNTIL {
let end = core::cmp::min(region.end, WRITE_TEST_UNTIL);
// ensure region is actually writable
let addr = phys_mem_offset + region.start;
let size = end - region.start;
unsafe {
core::ptr::write_bytes(addr as *mut u8, 0xff, size as usize);
}
}
}

writeln!(serial(), "Free lower memory page count: {}", count).unwrap();
Expand Down
12 changes: 12 additions & 0 deletions tests/test_kernels/write_usable_memory/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "test_kernel_write_usable_memory"
version = "0.1.0"
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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#![no_std] // don't link the Rust standard library
#![no_main] // disable all Rust-level entry points

use bootloader_api::{
config::Mapping, entry_point, info::MemoryRegionKind, BootInfo, BootloaderConfig,
};
use test_kernel_write_usable_memory::{exit_qemu, QemuExitCode};

pub const BOOTLOADER_CONFIG: BootloaderConfig = {
let mut config = BootloaderConfig::new_default();
config.mappings.physical_memory = Some(Mapping::FixedAddress(0x0000_4000_0000_0000));
config
};

entry_point!(kernel_main, config = &BOOTLOADER_CONFIG);

fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
let phys_mem_offset = boot_info.physical_memory_offset.into_option().unwrap();

for region in boot_info.memory_regions.iter() {
if region.kind == MemoryRegionKind::Usable {
// ensure region is actually writable
let addr = phys_mem_offset + region.start;
let size = region.end - region.start;
unsafe {
core::ptr::write_bytes(addr as *mut u8, 0xff, size as usize);
}
}
}

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_write_usable_memory::serial(), "PANIC: {}", info);
exit_qemu(QemuExitCode::Failed);
}
27 changes: 27 additions & 0 deletions tests/test_kernels/write_usable_memory/src/lib.rs
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
}
7 changes: 7 additions & 0 deletions tests/write_usable_memory.rs
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_WRITE_USABLE_MEMORY_write_usable_memory"
));
}

0 comments on commit f3d383e

Please sign in to comment.