From e70a0dffe5bdab220c1ab9c7e5bbdedb7847a56c Mon Sep 17 00:00:00 2001 From: killerboss Date: Sun, 24 Nov 2024 22:28:46 +0100 Subject: [PATCH] Added and fixed some tests --- Cargo.lock | 33 +++++++++++++++++++++++++++++++-- Cargo.toml | 9 +++++++-- src/main.rs | 19 +++++++++++++++---- src/serial.rs | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 src/serial.rs diff --git a/Cargo.lock b/Cargo.lock index 3298937..3e726f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,12 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.6.0" @@ -52,8 +58,9 @@ dependencies = [ "bootloader", "lazy_static", "spin", + "uart_16550", "volatile 0.2.7", - "x86_64", + "x86_64 0.15.1", ] [[package]] @@ -77,6 +84,17 @@ dependencies = [ "lock_api", ] +[[package]] +name = "uart_16550" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "614ff2a87880d4bd4374722268598a970bbad05ced8bf630439417347254ab2e" +dependencies = [ + "bitflags 1.3.2", + "rustversion", + "x86_64 0.14.12", +] + [[package]] name = "volatile" version = "0.2.7" @@ -96,7 +114,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96cb6fd45bfeab6a5055c5bffdb08768bd0c069f1d946debe585bbb380a7c062" dependencies = [ "bit_field", - "bitflags", + "bitflags 2.6.0", + "rustversion", + "volatile 0.4.6", +] + +[[package]] +name = "x86_64" +version = "0.15.1" +source = "git+https://github.com/rust-osdev/x86_64?branch=fix%2Fstep-nightly-breakage#b4b66637b7306254447bde805789f1a2cf92b6e5" +dependencies = [ + "bit_field", + "bitflags 2.6.0", "rustversion", "volatile 0.4.6", ] diff --git a/Cargo.toml b/Cargo.toml index 734093a..5d8f40e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,8 @@ bootloader = "0.9" lazy_static = { version = "1.5.0", features = ["spin_no_std"] } spin = "0.9.8" volatile = "0.2.6" -x86_64 = "0.14.2" +x86_64 = { git = "https://github.com/rust-osdev/x86_64", branch = "fix/step-nightly-breakage" } +uart_16550 = "0.2.0" # the profile used for `cargo build` [profile.dev] @@ -19,5 +20,9 @@ panic = "abort" # disable stack unwinding on panic panic = "abort" # disable stack unwinding on panic [package.metadata.bootimage] -test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04"] +test-args = [ + "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio", + "-display", "none" +] test-success-exit-code = 33 +test-timeout = 300 # (in seconds) \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4b42e80..89ccf54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod vga_buffer; +mod serial; use core::panic::PanicInfo; @@ -18,26 +19,36 @@ pub extern "C" fn _start() -> ! { loop {} } +#[cfg(not(test))] #[panic_handler] fn panic(info: &PanicInfo) -> ! { println!("{}", info); loop {} } +#[cfg(test)] +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + serial_println!("[failed]\n"); + serial_println!("Error: {}\n", info); + exit_qemu(QemuExitCode::Failed); + loop {} +} + fn test_runner(tests: &[&dyn Fn()]) { - println!("Running {} tests", tests.len()); + serial_println!("Running {} tests", tests.len()); for test in tests { test(); } - /// new + exit_qemu(QemuExitCode::Success); } #[test_case] fn trivial_assertion() { - print!("trivial assertion... "); + serial_print!("trivial assertion... "); assert_eq!(1, 1); - println!("[ok]"); + serial_println!("[ok]"); } #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/src/serial.rs b/src/serial.rs new file mode 100644 index 0000000..07129bb --- /dev/null +++ b/src/serial.rs @@ -0,0 +1,34 @@ +use uart_16550::SerialPort; +use spin::Mutex; +use lazy_static::lazy_static; + +lazy_static! { + pub static ref SERIAL1: Mutex = { + let mut serial_port = unsafe { SerialPort::new(0x3F8) }; + serial_port.init(); + Mutex::new(serial_port) + }; +} + +#[doc(hidden)] +pub fn _print(args: ::core::fmt::Arguments) { + use core::fmt::Write; + SERIAL1.lock().write_fmt(args).expect("Printing to serial failed"); +} + +/// Prints to the host through the serial interface. +#[macro_export] +macro_rules! serial_print { + ($($arg:tt)*) => { + $crate::serial::_print(format_args!($($arg)*)); + }; +} + +/// Prints to the host through the serial interface, appending a newline. +#[macro_export] +macro_rules! serial_println { + () => ($crate::serial_print!("\n")); + ($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => ($crate::serial_print!( + concat!($fmt, "\n"), $($arg)*)); +} \ No newline at end of file