Skip to content

Commit b1e25cf

Browse files
josephlrrbradford
authored andcommitted
serial: Use external uart_16550 crate
When trying to run this on various hypervisors (QEMU, GCE, etc...), I noticed that our serial implementation is not very robust. We can start in the wrong mode or drop bytes, as we do not properly observe the status bits or setup the configuration registers. Instead of doing this ourselevs, we just use an external crate. Note that this create is made by the same people as x86_64, and only has that crate as a dependancy. Signed-off-by: Joe Richey <[email protected]>
1 parent 871f6b1 commit b1e25cf

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ bitflags = "1.2"
2828
x86_64 = "0.9"
2929
atomic_refcell = "0.1"
3030
r-efi = "2.1.0"
31-
31+
uart_16550 = "0.2.5"

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice, info: &dyn boot::Info
140140

141141
#[no_mangle]
142142
pub extern "C" fn rust64_start(rdi: Option<&pvh::StartInfo>, rsi: Option<&boot::Params>) -> ! {
143+
serial::PORT.borrow_mut().init();
144+
143145
if let Some(start_info) = rdi {
144146
log!("\nBooting via PVH Boot Protocol");
145147
run(start_info)

src/serial.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@
1818
use core::fmt;
1919

2020
use atomic_refcell::AtomicRefCell;
21-
use x86_64::instructions::port::PortWriteOnly;
21+
use uart_16550::SerialPort;
2222

2323
// We use COM1 as it is the standard first serial port.
24-
static PORT: AtomicRefCell<PortWriteOnly<u8>> = AtomicRefCell::new(PortWriteOnly::new(0x3f8));
24+
pub static PORT: AtomicRefCell<SerialPort> = AtomicRefCell::new(unsafe { SerialPort::new(0x3f8) });
2525

2626
pub struct Serial;
27-
2827
impl fmt::Write for Serial {
2928
fn write_str(&mut self, s: &str) -> fmt::Result {
30-
let mut port = PORT.borrow_mut();
31-
for b in s.bytes() {
32-
unsafe { port.write(b) }
33-
}
34-
Ok(())
29+
PORT.borrow_mut().write_str(s)
3530
}
3631
}
3732

0 commit comments

Comments
 (0)