From 7f872901ce9564ffece6afef02e48c4c1ddee81e Mon Sep 17 00:00:00 2001 From: WangRunji Date: Tue, 10 Apr 2018 20:46:24 +0800 Subject: [PATCH] Serial stdio --- Makefile | 2 +- src/arch/x86_64/driver/mod.rs | 3 +- src/arch/x86_64/driver/serial.rs | 49 ++++++++++++++++++++++++++++++++ src/io/mod.rs | 2 ++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/arch/x86_64/driver/serial.rs diff --git a/Makefile b/Makefile index 8f31918..bf0abd5 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ grub_cfg := $(boot_src)/grub.cfg assembly_source_files := $(wildcard $(boot_src)/*.asm) assembly_object_files := $(patsubst $(boot_src)/%.asm, \ build/arch/$(arch)/boot/%.o, $(assembly_source_files)) -qemu_opts := -cdrom $(iso) -smp 2 +qemu_opts := -cdrom $(iso) -smp 2 -serial stdio ifdef travis test := 1 diff --git a/src/arch/x86_64/driver/mod.rs b/src/arch/x86_64/driver/mod.rs index 1f1f8fb..626481e 100644 --- a/src/arch/x86_64/driver/mod.rs +++ b/src/arch/x86_64/driver/mod.rs @@ -1,4 +1,5 @@ pub mod vga; pub mod acpi; pub mod ioapic; -pub mod mp; \ No newline at end of file +pub mod mp; +pub mod serial; \ No newline at end of file diff --git a/src/arch/x86_64/driver/serial.rs b/src/arch/x86_64/driver/serial.rs new file mode 100644 index 0000000..d6da11a --- /dev/null +++ b/src/arch/x86_64/driver/serial.rs @@ -0,0 +1,49 @@ +/* + * Rust BareBones OS + * - By John Hodge (Mutabah/thePowersGang) + * + * arch/x86/debug.rs + * - Debug output channel + * + * Writes debug to the standard PC serial port (0x3F8 .. 0x3FF) + * + * == LICENCE == + * This code has been put into the public domain, there are no restrictions on + * its use, and the author takes no liability. + */ + +use core::fmt; +use spin::Mutex; +use x86_64::instructions::port::{inb, outb}; + +pub static SERIAL: Mutex = Mutex::new(Serial{}); + +pub struct Serial; + +impl Serial { + /// Write a single byte to the output channel + pub fn write(&mut self, byte: u8) { + unsafe { + self.wait(); + + // Send the byte out the serial port + outb(0x3F8, byte); + + // Also send to the bochs 0xe9 hack + outb(0xE9, byte); + } + } + /// Wait for the serial port's fifo to not be empty + unsafe fn wait(&self) { + while (inb(0x3F8+5) & 0x20) == 0 {} + } +} + +impl fmt::Write for Serial { + fn write_str(&mut self, s: &str) -> fmt::Result { + for byte in s.bytes() { + self.write(byte) + } + Ok(()) + } +} \ No newline at end of file diff --git a/src/io/mod.rs b/src/io/mod.rs index ba05279..4f95262 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,4 +1,5 @@ use core::fmt; +use arch::driver::serial::SERIAL; mod vga_writer; @@ -32,6 +33,7 @@ fn print_in_color(args: fmt::Arguments, color: Color) { let mut writer = vga_writer::VGA_WRITER.lock(); writer.set_color(color); writer.write_fmt(args).unwrap(); + SERIAL.lock().write_fmt(args).unwrap(); } pub fn print(args: fmt::Arguments) {