OS in Rust
4096_t.cno_stdbootimage
.cargo/config.toml#[test] attribute on some functions.cargo test or, if you see someone around, cargo t, will check the assertions.test flag in the Cargo.toml to true.Cargo.toml - mine no longer has an allusions to panic with the introduction of a JSON target.[[bin]]no_stdno_std.test library, which depends on std
$ cargo t
Compiling bootloader v0.9.34
error[E0463]: can't find crate for `core`
|
= note: the `x86_64-osirs` target may not be installed
= help: consider downloading the target with `rustup target add x86_64-osirs`
For more information about this error, try `rustc --explain E0463`.
error: could not compile `bootloader` (lib) due to 1 previous errorstd, we lose e.g. should_panic tests.
60)
52) andprintln! (likely 51)src/main.rs
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
mod vga;
#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn()]) {
println!("Running {} tests", tests.len());
for test in tests {
test();
}
}
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{}", info);
loop {}
}
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
println!("Hello world!");
loop {}
}&dyn?dynThe
dynkeyword is used to highlight that calls to methods on the associatedTraitare dynamically dispatched.
_start for now.fs.ppm.pngbase64 the PNG.
$ base64 fs.png | wc
25 25 1861
$ base64 fs.png
iVBORw0KGgoAAAANSUhEUgAAAtAAAAGQCAIAAAAIhcA6AAAFJ0lEQVR4nO3dyXaDIBQAUMzp//9y
ujAlVgw+p4TovasaFKEb8DGlBAAAAPDtuvv9nlLquq6/Hl3W9TdP3l9JakS9hLNVa7ZeANCg25aH
K41u++1xvYSvUnNHJP8BAMza1OEAAIj4qSePvuO3xy1WZFgOYQx/eZVhvmfF4I7oBQDs6xHhuP8Z
puU2u5c2t8TvzHB4uehd9Ska+ff2x4wAoB2PCMcoMDD07Z/7i3oGJoQCwBFmhlRSS63vlt5AO7UA
gAua73Awou8CAEstWKVSTvJ4p3bWo372/wAA32hm46/6opJhajBp3bKXcu7nbAkrZQuWMK9wmaya
OAcAAAA0pKuMDviIBwB2YadRAAA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AAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZ/ULzGetS97yp6YAAAAA
SUVORK5CYII=<div>
<p>Taken from wikpedia</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>base64 PNG data within.A values in the big block.
“They’re my bits, in my computer - let me use them!”
__start.src/main.rs
main too so you know what is running, mine says “I’m main”.+nightly is getting annoying.
you can add a rust-toolchain.toml file, for example
target for clarity._start.src/main.rs is a fan of the Black Eyed Peas.
_start function is still used as entry point.
_start function.main function that calls test_runner
main.Note: There is currently a bug in cargo that leads to “duplicate lang item” errors on cargo test in some cases. It occurs when you have set panic = "abort" for a profile in your Cargo.toml. Try removing it, then cargo test should work. Alternatively, if that doesn’t work, then add panic-abort-tests = true to the [unstable] section of your .cargo/config.toml file. See the cargo issue for more information on this.
test_runner from _start
run instead of test?$ cargo r
Compiling osirs v0.1.0 (/home/user/tmp/work)
error[E0423]: expected function, found built-in attribute `test_runner`
--> src/main.rs:26:5
|
26 | test_runner(&[]);
| ^^^^^^^^^^^ not a function
|
note: found an item that was configured out
--> src/main.rs:9:4
|
8 | #[cfg(test)]
| ---- the item is gated here
9 | fn test_runner(_tests: &[&dyn Fn()]) {
| ^^^^^^^^^^^
For more information about this error, try `rustc --explain E0423`.
error: could not compile `osirs` (bin "osirs") due to 1 previous error_start to either call test_runner or not.#[cfg(test)]run instead of test?_ prefix.truetrueassert! macro to the expressiontest_runner array of functions.test_runner_hi and _bye at this time.test_runnertest_runner returns to _start function_start contains an infinite loop
cargo t to exit after running all tests.isa-debug-exit.
-device argument to qemuCargo.tomlcargo t (and also cargo r) and verify the qemu command differs.$ cargo t
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.06s
Running unittests src/main.rs (target/x86_64-osirs/debug/deps/osirs-577ad3ee7fc7cc13)
Building bootloader
Compiling bootloader v0.9.34 (/home/calvin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bootloader-0.9.34)
Finished `release` profile [optimized + debuginfo] target(s) in 0.83s
Running: `qemu-system-x86_64 -drive format=raw,file=/home/calvin/tmp/work/target/x86_64-osirs/debug/deps/bootimage-osirs-577ad3ee7fc7cc13.bin -no-reboot -device isa-debug-exit,iobase=0xf4,iosize=0x04`$ cargo r
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
Running `bootimage runner target/x86_64-osirs/debug/osirs`
Building bootloader
Compiling bootloader v0.9.34 (/home/calvin/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bootloader-0.9.34)
Finished `release` profile [optimized + debuginfo] target(s) in 0.84s
Running: `qemu-system-x86_64 -drive format=raw,file=target/x86_64-osirs/debug/bootimage-osirs.bin`in and outisa-debug-exit device uses port-mapped I/O.
isa = instruction set architecturein or outiobase parameter specifies the port address of the (imaginary) device0xf4 is a generally unused portiosize specifies the port size (0x04 means four bytes).isa-debug-exit is simple.v is written to the I/O port specified by iobase, QEMU to exits with exit status (v << 1) | 1.0 to the port, QEMU will exit with (0 << 1) | 1 = 1,in and out
x86_64 crate.[dependencies] in Cargo.toml:0xA, you can use anything.
u32 because we said we would.test_runner and you should be good to go!$ cargo t
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.07s
Running unittests src/main.rs (target/x86_64-osirs/debug/deps/osirs-429ccf0d82ba9f9c)
Building bootloader
Finished `release` profile [optimized + debuginfo] target(s) in 0.07s
Running: `qemu-system-x86_64 -drive format=raw,file=/home/calvin/tmp/work/target/x86_64-osirs/debug/deps/bootimage-osirs-429ccf0d82ba9f9c.bin -no-reboot -device isa-debug-exit,iobase=0xf4,iosize=0x04`
error: test failed, to rerun pass `--bin osirs`
Caused by:
process didn't exit successfully: `bootimage runner /home/calvin/tmp/work/target/x86_64-osirs/debug/deps/osirs-429ccf0d82ba9f9c` (exit status: 21)
note: test exited abnormally; to see the full output pass --no-capture to the harness.
$ cargo t expects and being admonished.
Cargo.tomlCargo.toml
((0xA<<1)|1)==21