diff --git a/Cargo.lock b/Cargo.lock index b7cbc841..2f6d906c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,12 +35,6 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitflags" version = "2.11.0" @@ -226,7 +220,7 @@ version = "0.5.0" dependencies = [ "aarch64-cpu", "atomic_refcell", - "bitflags 2.11.0", + "bitflags", "chrono", "dirs", "fdt", @@ -283,7 +277,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags 2.11.0", + "bitflags", "libc", ] @@ -465,22 +459,13 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c8d0fd677905edcbeedbf2edb6494d676f0e98d54d5cf9bda0b061cb8fb8aba" -[[package]] -name = "raw-cpuid" -version = "10.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332" -dependencies = [ - "bitflags 1.3.2", -] - [[package]] name = "redox_syscall" version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" dependencies = [ - "bitflags 2.11.0", + "bitflags", ] [[package]] @@ -500,7 +485,7 @@ version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190" dependencies = [ - "bitflags 2.11.0", + "bitflags", "errno", "libc", "linux-raw-sys", @@ -579,7 +564,7 @@ version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f84d13b3b8a0d4e91a2629911e951db1bb8671512f5c09d7d4ba34500ba68c8" dependencies = [ - "bitflags 2.11.0", + "bitflags", "libc", "libssh2-sys", "parking_lot", @@ -643,13 +628,11 @@ checksum = "8d2d250f87fb3fb6f225c907cf54381509f47b40b74b1d1f12d2dccbc915bdfe" [[package]] name = "uart_16550" -version = "0.4.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94d293f51425981fdb1b766beae254dbb711a17e8c4b549dc69b9b7ee0d478d5" +checksum = "22ee77075ecbc5e1c9b1236d03ff013dd77c7bb06da181c5c44f1f39b6cf3ff2" dependencies = [ - "bitflags 2.11.0", - "rustversion", - "x86", + "bitflags", ] [[package]] @@ -728,7 +711,7 @@ version = "0.244.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47b807c72e1bac69382b3a6fb3dbe8ea4c0ed87ff5629b8685ae6b9a611028fe" dependencies = [ - "bitflags 2.11.0", + "bitflags", "hashbrown 0.15.5", "indexmap", "semver", @@ -865,7 +848,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d66ea20e9553b30172b5e831994e35fbde2d165325bec84fc43dbf6f4eb9cb2" dependencies = [ "anyhow", - "bitflags 2.11.0", + "bitflags", "indexmap", "log", "serde", @@ -895,17 +878,6 @@ dependencies = [ "wasmparser", ] -[[package]] -name = "x86" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2781db97787217ad2a2845c396a5efe286f87467a5810836db6d74926e94a385" -dependencies = [ - "bit_field", - "bitflags 1.3.2", - "raw-cpuid", -] - [[package]] name = "x86_64" version = "0.15.4" @@ -913,7 +885,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7841fa0098ceb15c567d93d3fae292c49e10a7662b4936d5f6a9728594555ba" dependencies = [ "bit_field", - "bitflags 2.11.0", + "bitflags", "const_fn", "rustversion", "volatile", diff --git a/Cargo.toml b/Cargo.toml index 53a7f140..81b5c6b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ fdt = "0.1.5" chrono = { version = "0.4", default-features = false } [target.'cfg(target_arch = "x86_64")'.dependencies] -uart_16550 = "0.4.0" +uart_16550 = "0.6.0" x86_64 = { version = "0.15.4", default-features = false, features = [ "instructions", ] } diff --git a/src/main.rs b/src/main.rs index 5928863d..a53b4f33 100644 --- a/src/main.rs +++ b/src/main.rs @@ -180,7 +180,7 @@ fn boot_from_device( #[cfg(target_arch = "x86_64")] #[no_mangle] pub extern "C" fn rust64_start(#[cfg(not(feature = "coreboot"))] pvh_info: &pvh::StartInfo) -> ! { - serial::PORT.borrow_mut().init(); + serial::init(); logger::init(); arch::x86_64::sse::enable_sse(); @@ -202,7 +202,7 @@ pub extern "C" fn rust64_start(x0: *const u8) -> ! { arch::aarch64::paging::setup(); // Use atomic operation before MMU enabled may cause exception, see https://www.ipshop.xyz/5909.html - serial::PORT.borrow_mut().init(); + serial::init(); logger::init(); let info = fdt::StartInfo::new( @@ -225,7 +225,7 @@ pub extern "C" fn rust64_start(x0: *const u8) -> ! { pub extern "C" fn rust64_start(a0: u64, a1: *const u8) -> ! { use crate::bootinfo::{EntryType, Info, MemoryEntry}; - serial::PORT.borrow_mut().init(); + serial::init(); logger::init(); info!("Starting on RV64 0x{:x} 0x{:x}", a0, a1 as u64,); diff --git a/src/serial.rs b/src/serial.rs index c3fd2ec2..abb4b52c 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -12,14 +12,14 @@ use atomic_refcell::AtomicRefCell; use crate::{arch::aarch64::layout::map, uart_pl011::Pl011 as UartPl011}; #[cfg(target_arch = "x86_64")] -use uart_16550::SerialPort as Uart16550; +use uart_16550::{backend::PioBackend, Config, Uart16550Tty}; #[cfg(target_arch = "riscv64")] use crate::uart_mmio::UartMmio; // We use COM1 as it is the standard first serial port. #[cfg(target_arch = "x86_64")] -pub static PORT: AtomicRefCell = AtomicRefCell::new(unsafe { Uart16550::new(0x3f8) }); +pub static PORT: AtomicRefCell>> = AtomicRefCell::new(None); #[cfg(target_arch = "aarch64")] pub static PORT: AtomicRefCell = @@ -31,10 +31,41 @@ const SERIAL_PORT_ADDRESS: u64 = 0x1000_0000; #[cfg(target_arch = "riscv64")] pub static PORT: AtomicRefCell = AtomicRefCell::new(UartMmio::new(SERIAL_PORT_ADDRESS)); +#[cfg(target_arch = "x86_64")] +pub fn init() { + let mut port = PORT.borrow_mut(); + + if port.is_none() { + *port = Some(unsafe { + Uart16550Tty::new_port(0x3f8, Config::default()) + .expect("Failed to initialize UART16550") + }); + } +} + +#[cfg(not(target_arch = "x86_64"))] +pub fn init() { + PORT.borrow_mut().init(); +} + pub struct Serial; impl fmt::Write for Serial { fn write_str(&mut self, s: &str) -> fmt::Result { - PORT.borrow_mut().write_str(s) + #[cfg(target_arch = "x86_64")] + { + let mut port = PORT.borrow_mut(); + + if let Some(port) = port.as_mut() { + return fmt::Write::write_str(port, s); + } + + Ok(()) + } + + #[cfg(not(target_arch = "x86_64"))] + { + PORT.borrow_mut().write_str(s) + } } }