From fa148bc9b76934bae06e888a066fe2e0ec7fd7e6 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 27 Feb 2026 09:40:00 +0100 Subject: [PATCH] Add an example kernel that uses the built-in `x86_64-unknown-none` target This ensures that bootimage works even if the user doesn't enable the unstable `json-target-spec` feature. --- .github/workflows/build.yml | 4 ++ .../.cargo/config.toml | 2 + example-kernel-built-in-target/.gitignore | 1 + example-kernel-built-in-target/Cargo.lock | 53 +++++++++++++++++++ example-kernel-built-in-target/Cargo.toml | 8 +++ example-kernel-built-in-target/rust-toolchain | 1 + example-kernel-built-in-target/src/main.rs | 22 ++++++++ 7 files changed, 91 insertions(+) create mode 100644 example-kernel-built-in-target/.cargo/config.toml create mode 100644 example-kernel-built-in-target/.gitignore create mode 100644 example-kernel-built-in-target/Cargo.lock create mode 100644 example-kernel-built-in-target/Cargo.toml create mode 100644 example-kernel-built-in-target/rust-toolchain create mode 100644 example-kernel-built-in-target/src/main.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fcf00c2..727ab3e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -90,6 +90,10 @@ jobs: - name: "Print QEMU Version" run: qemu-system-x86_64 --version + - name: 'Build kernel with built-in target (no json-target-spec config)' + run: cargo bootimage --target x86_64-unknown-none + working-directory: example-kernel-built-in-target + - name: 'Build "basic" Kernel' run: cargo bootimage --target ../x86_64-bootimage-example-kernels.json working-directory: example-kernels/basic diff --git a/example-kernel-built-in-target/.cargo/config.toml b/example-kernel-built-in-target/.cargo/config.toml new file mode 100644 index 0000000..92cee48 --- /dev/null +++ b/example-kernel-built-in-target/.cargo/config.toml @@ -0,0 +1,2 @@ +[unstable] +build-std = ["core", "compiler_builtins"] diff --git a/example-kernel-built-in-target/.gitignore b/example-kernel-built-in-target/.gitignore new file mode 100644 index 0000000..b83d222 --- /dev/null +++ b/example-kernel-built-in-target/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/example-kernel-built-in-target/Cargo.lock b/example-kernel-built-in-target/Cargo.lock new file mode 100644 index 0000000..527c5e2 --- /dev/null +++ b/example-kernel-built-in-target/Cargo.lock @@ -0,0 +1,53 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bit_field" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4b40c7323adcfc0a41c4b88143ed58346ff65a288fc144329c5c45e05d70c6" + +[[package]] +name = "bitflags" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" + +[[package]] +name = "bootloader" +version = "0.9.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13f6a8a495d2f93fe3d6eb3a224f9aa749a63cfd746ed03eb5ddcbd00ade7d8f" + +[[package]] +name = "built-in-target" +version = "0.1.0" +dependencies = [ + "bootloader", + "x86_64", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "volatile" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" + +[[package]] +name = "x86_64" +version = "0.14.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c101112411baafbb4bf8d33e4c4a80ab5b02d74d2612331c61e8192fc9710491" +dependencies = [ + "bit_field", + "bitflags", + "rustversion", + "volatile", +] diff --git a/example-kernel-built-in-target/Cargo.toml b/example-kernel-built-in-target/Cargo.toml new file mode 100644 index 0000000..4664912 --- /dev/null +++ b/example-kernel-built-in-target/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "example-kernel-built-in-target" +version = "0.1.0" +edition = "2018" + +[dependencies] +bootloader = "0.9.7" +x86_64 = "0.14.1" diff --git a/example-kernel-built-in-target/rust-toolchain b/example-kernel-built-in-target/rust-toolchain new file mode 100644 index 0000000..bf867e0 --- /dev/null +++ b/example-kernel-built-in-target/rust-toolchain @@ -0,0 +1 @@ +nightly diff --git a/example-kernel-built-in-target/src/main.rs b/example-kernel-built-in-target/src/main.rs new file mode 100644 index 0000000..f3a34b9 --- /dev/null +++ b/example-kernel-built-in-target/src/main.rs @@ -0,0 +1,22 @@ +#![no_std] +#![no_main] + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(_info: &PanicInfo) -> ! { + loop {} +} + +#[no_mangle] +pub extern "C" fn _start() -> ! { + unsafe { exit_qemu(); } + loop {} +} + +unsafe fn exit_qemu() { + use x86_64::instructions::port::Port; + + let mut port = Port::::new(0xf4); + port.write(51); // exit code is (51 << 1) | 1 = 103 +}