Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f3d383e

Browse filesBrowse files
committed
New test to check usable memory is writable
split out the write check from the lower_memory_free test. Those 2 aren't really related
1 parent 4446278 commit f3d383e
Copy full SHA for f3d383e

File tree

Expand file treeCollapse file tree

7 files changed

+100
-12
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+100
-12
lines changed

‎Cargo.lock

Copy file name to clipboardExpand all lines: Cargo.lock
+10Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Copy file name to clipboardExpand all lines: Cargo.toml
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ members = [
2727
"tests/test_kernels/ramdisk",
2828
"tests/test_kernels/min_stack",
2929
"tests/test_kernels/lower_memory_free",
30+
"tests/test_kernels/write_usable_memory",
3031
]
3132
exclude = ["examples/basic", "examples/test_framework"]
3233

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

7375
[profile.dev]
7476
panic = "abort"

‎tests/test_kernels/lower_memory_free/src/bin/lower_memory_free.rs

Copy file name to clipboardExpand all lines: tests/test_kernels/lower_memory_free/src/bin/lower_memory_free.rs
-12Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use bootloader_api::{
77
use test_kernel_lower_memory_free::{exit_qemu, QemuExitCode};
88

99
const LOWER_MEMORY_END_PAGE: u64 = 0x0010_0000;
10-
const WRITE_TEST_UNTIL: u64 = 0x4000_0000;
1110

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

24-
let phys_mem_offset = boot_info.physical_memory_offset.into_option().unwrap();
25-
2623
let mut count = 0;
2724
for region in boot_info.memory_regions.iter() {
2825
writeln!(
@@ -38,15 +35,6 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
3835
let pages = (end - region.start) / 4096;
3936
count += pages;
4037
}
41-
if region.kind == MemoryRegionKind::Usable && region.start < WRITE_TEST_UNTIL {
42-
let end = core::cmp::min(region.end, WRITE_TEST_UNTIL);
43-
// ensure region is actually writable
44-
let addr = phys_mem_offset + region.start;
45-
let size = end - region.start;
46-
unsafe {
47-
core::ptr::write_bytes(addr as *mut u8, 0xff, size as usize);
48-
}
49-
}
5038
}
5139

5240
writeln!(serial(), "Free lower memory page count: {}", count).unwrap();
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "test_kernel_write_usable_memory"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
bootloader_api = { path = "../../../api" }
8+
x86_64 = { version = "0.14.7", default-features = false, features = [
9+
"instructions",
10+
"inline_asm",
11+
] }
12+
uart_16550 = "0.2.10"
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![no_std] // don't link the Rust standard library
2+
#![no_main] // disable all Rust-level entry points
3+
4+
use bootloader_api::{
5+
config::Mapping, entry_point, info::MemoryRegionKind, BootInfo, BootloaderConfig,
6+
};
7+
use test_kernel_write_usable_memory::{exit_qemu, QemuExitCode};
8+
9+
pub const BOOTLOADER_CONFIG: BootloaderConfig = {
10+
let mut config = BootloaderConfig::new_default();
11+
config.mappings.physical_memory = Some(Mapping::FixedAddress(0x0000_4000_0000_0000));
12+
config
13+
};
14+
15+
entry_point!(kernel_main, config = &BOOTLOADER_CONFIG);
16+
17+
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
18+
let phys_mem_offset = boot_info.physical_memory_offset.into_option().unwrap();
19+
20+
for region in boot_info.memory_regions.iter() {
21+
if region.kind == MemoryRegionKind::Usable {
22+
// ensure region is actually writable
23+
let addr = phys_mem_offset + region.start;
24+
let size = region.end - region.start;
25+
unsafe {
26+
core::ptr::write_bytes(addr as *mut u8, 0xff, size as usize);
27+
}
28+
}
29+
}
30+
31+
exit_qemu(QemuExitCode::Success);
32+
}
33+
34+
/// This function is called on panic.
35+
#[panic_handler]
36+
#[cfg(not(test))]
37+
fn panic(info: &core::panic::PanicInfo) -> ! {
38+
use core::fmt::Write;
39+
40+
let _ = writeln!(test_kernel_write_usable_memory::serial(), "PANIC: {}", info);
41+
exit_qemu(QemuExitCode::Failed);
42+
}
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![no_std]
2+
3+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4+
#[repr(u32)]
5+
pub enum QemuExitCode {
6+
Success = 0x10,
7+
Failed = 0x11,
8+
}
9+
10+
pub fn exit_qemu(exit_code: QemuExitCode) -> ! {
11+
use x86_64::instructions::{nop, port::Port};
12+
13+
unsafe {
14+
let mut port = Port::new(0xf4);
15+
port.write(exit_code as u32);
16+
}
17+
18+
loop {
19+
nop();
20+
}
21+
}
22+
23+
pub fn serial() -> uart_16550::SerialPort {
24+
let mut port = unsafe { uart_16550::SerialPort::new(0x3F8) };
25+
port.init();
26+
port
27+
}

‎tests/write_usable_memory.rs

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use bootloader_test_runner::run_test_kernel;
2+
#[test]
3+
fn lower_memory_free() {
4+
run_test_kernel(env!(
5+
"CARGO_BIN_FILE_TEST_KERNEL_WRITE_USABLE_MEMORY_write_usable_memory"
6+
));
7+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.