Skip to content

Commit 0e1fd09

Browse files
committed
Test booting with a minimal kernel stack size
1 parent 78c35e4 commit 0e1fd09

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

Cargo.lock

Lines 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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ members = [
2424
"tests/test_kernels/pie",
2525
"tests/test_kernels/lto",
2626
"tests/test_kernels/ramdisk",
27+
"tests/test_kernels/min_stack",
2728
]
2829
exclude = ["examples/basic", "examples/test_framework"]
2930

@@ -60,6 +61,7 @@ test_kernel_higher_half = { path = "tests/test_kernels/higher_half", artifact =
6061
test_kernel_map_phys_mem = { path = "tests/test_kernels/map_phys_mem", artifact = "bin", target = "x86_64-unknown-none" }
6162
test_kernel_pie = { path = "tests/test_kernels/pie", artifact = "bin", target = "x86_64-unknown-none" }
6263
test_kernel_ramdisk = { path = "tests/test_kernels/ramdisk", artifact = "bin", target = "x86_64-unknown-none" }
64+
test_kernel_min_stack = { path = "tests/test_kernels/min_stack", artifact = "bin", target = "x86_64-unknown-none" }
6365

6466
[profile.dev]
6567
panic = "abort"
@@ -113,6 +115,9 @@ rustflags = [
113115
"code-model=large",
114116
]
115117

118+
[profile.test.package.test_kernel_min_stack]
119+
opt-level = 2
120+
116121
[build-dependencies]
117122
llvm-tools = "0.1.1"
118123
async-process = "1.6.0"

tests/min_stack.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use bootloader_test_runner::run_test_kernel;
2+
3+
#[test]
4+
fn basic_boot() {
5+
run_test_kernel(env!("CARGO_BIN_FILE_TEST_KERNEL_MIN_STACK_basic_boot"));
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "test_kernel_min_stack"
3+
version = "0.1.0"
4+
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
bootloader_api = { path = "../../../api" }
9+
x86_64 = { version = "0.14.7", default-features = false, features = [
10+
"instructions",
11+
"inline_asm",
12+
] }
13+
uart_16550 = "0.2.10"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![no_std] // don't link the Rust standard library
2+
#![no_main] // disable all Rust-level entry points
3+
4+
use bootloader_api::{entry_point, BootInfo, BootloaderConfig};
5+
use core::fmt::Write;
6+
use test_kernel_min_stack::{exit_qemu, serial, QemuExitCode};
7+
8+
const BOOTLOADER_CONFIG: BootloaderConfig = {
9+
let mut config = BootloaderConfig::new_default();
10+
config.kernel_stack_size = 3000;
11+
config
12+
};
13+
entry_point!(kernel_main, config = &BOOTLOADER_CONFIG);
14+
15+
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
16+
writeln!(serial(), "Entered kernel with boot info: {boot_info:?}").unwrap();
17+
exit_qemu(QemuExitCode::Success);
18+
}
19+
20+
/// This function is called on panic.
21+
#[panic_handler]
22+
#[cfg(not(test))]
23+
fn panic(info: &core::panic::PanicInfo) -> ! {
24+
let _ = writeln!(serial(), "PANIC: {info}");
25+
exit_qemu(QemuExitCode::Failed);
26+
}
Lines 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+
}

0 commit comments

Comments
 (0)