Skip to content

Commit 2b2eede

Browse files
committed
Add example kernel that directly exits QEMU for testing on CI
1 parent 6543906 commit 2b2eede

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

example-kernel/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
**/*.rs.bk

example-kernel/Cargo.lock

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-kernel/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "example-kernel"
3+
version = "0.1.0"
4+
authors = ["Philipp Oppermann <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
x86_64 = "0.3.4"

example-kernel/src/main.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![no_std] // don't link the Rust standard library
2+
#![no_main] // disable all Rust-level entry points
3+
4+
use core::panic::PanicInfo;
5+
6+
/// This function is called on panic.
7+
#[panic_handler]
8+
fn panic(_info: &PanicInfo) -> ! {
9+
loop {}
10+
}
11+
12+
#[no_mangle] // don't mangle the name of this function
13+
pub extern "C" fn _start() -> ! {
14+
// this function is the entry point, since the linker looks for a function
15+
// named `_start` by default
16+
17+
// exit QEMU (see https://os.phil-opp.com/integration-tests/#shutting-down-qemu)
18+
unsafe { exit_qemu(); }
19+
20+
loop {}
21+
}
22+
23+
pub unsafe fn exit_qemu() {
24+
use x86_64::instructions::port::Port;
25+
26+
let mut port = Port::<u32>::new(0xf4);
27+
port.write(61); // exit code is (61 << 1) | 1 = 123
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"llvm-target": "x86_64-unknown-none",
3+
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
4+
"arch": "x86_64",
5+
"target-endian": "little",
6+
"target-pointer-width": "64",
7+
"target-c-int-width": "32",
8+
"os": "none",
9+
"executables": true,
10+
"linker-flavor": "ld.lld",
11+
"linker": "rust-lld",
12+
"panic-strategy": "abort",
13+
"disable-redzone": true,
14+
"features": "-mmx,-sse,+soft-float"
15+
}

0 commit comments

Comments
 (0)