From 60c2a77ac1411e18107970b33c1307c21f85d519 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 16 Jul 2018 09:48:58 +0800 Subject: [PATCH] Unify IO in arch --- kernel/src/arch/riscv32/io.rs | 21 +++++++++++++ kernel/src/arch/riscv32/mod.rs | 2 +- kernel/src/arch/riscv32/serial.rs | 13 --------- kernel/src/arch/x86_64/driver/serial.rs | 11 +++---- kernel/src/arch/x86_64/io.rs | 12 ++++++++ kernel/src/arch/x86_64/mod.rs | 1 + kernel/src/logging.rs | 39 ++++--------------------- 7 files changed, 44 insertions(+), 55 deletions(-) create mode 100644 kernel/src/arch/riscv32/io.rs delete mode 100644 kernel/src/arch/riscv32/serial.rs create mode 100644 kernel/src/arch/x86_64/io.rs diff --git a/kernel/src/arch/riscv32/io.rs b/kernel/src/arch/riscv32/io.rs new file mode 100644 index 0000000..f6d35bf --- /dev/null +++ b/kernel/src/arch/riscv32/io.rs @@ -0,0 +1,21 @@ +use super::bbl::sbi; +use core::fmt::{Write, Result, Arguments}; + +struct SerialPort; + +impl Write for SerialPort { + fn write_str(&mut self, s: &str) -> Result { + for c in s.bytes() { + sbi::console_putchar(c as usize); + } + Ok(()) + } +} + +pub fn getchar() -> char { + sbi::console_getchar() as u8 as char +} + +pub fn putfmt(fmt: Arguments) { + SerialPort.write_fmt(fmt).unwrap(); +} diff --git a/kernel/src/arch/riscv32/mod.rs b/kernel/src/arch/riscv32/mod.rs index 96f804a..7f5b32e 100644 --- a/kernel/src/arch/riscv32/mod.rs +++ b/kernel/src/arch/riscv32/mod.rs @@ -1,7 +1,7 @@ extern crate bbl; extern crate riscv; -pub mod serial; +pub mod io; pub mod interrupt; pub mod timer; pub mod paging; diff --git a/kernel/src/arch/riscv32/serial.rs b/kernel/src/arch/riscv32/serial.rs deleted file mode 100644 index 5b0372e..0000000 --- a/kernel/src/arch/riscv32/serial.rs +++ /dev/null @@ -1,13 +0,0 @@ -use super::bbl::sbi; -use core::fmt; - -pub struct SerialPort; - -impl fmt::Write for SerialPort { - fn write_str(&mut self, s: &str) -> fmt::Result { - for c in s.bytes() { - sbi::console_putchar(c as usize); - } - Ok(()) - } -} \ No newline at end of file diff --git a/kernel/src/arch/x86_64/driver/serial.rs b/kernel/src/arch/x86_64/driver/serial.rs index 28c9173..182e733 100644 --- a/kernel/src/arch/x86_64/driver/serial.rs +++ b/kernel/src/arch/x86_64/driver/serial.rs @@ -20,20 +20,17 @@ pub fn init() { } pub trait SerialRead { - fn receive(&mut self); + fn receive(&mut self) -> u8; } impl SerialRead for SerialPort { - fn receive(&mut self) { + fn receive(&mut self) -> u8 { unsafe { let ports = self as *mut _ as *mut [Pio; 6]; let line_sts = &(*ports)[5]; let data = &(*ports)[0]; - while line_sts.read() & 1 == 1 { - let data = data.read(); - writeln!(self, "serial receive {}", data).unwrap(); - // TODO handle received data - } + while line_sts.read() & 1 != 1 {} + data.read() } } } diff --git a/kernel/src/arch/x86_64/io.rs b/kernel/src/arch/x86_64/io.rs new file mode 100644 index 0000000..cf054e1 --- /dev/null +++ b/kernel/src/arch/x86_64/io.rs @@ -0,0 +1,12 @@ +use super::driver::serial::*; +use core::fmt::{Arguments, Write}; + +pub fn getchar() -> char { + unsafe { COM1.force_unlock(); } + COM1.lock().receive() as char +} + +pub fn putfmt(fmt: Arguments) { + unsafe { COM1.force_unlock(); } + COM1.lock().write_fmt(fmt).unwrap() +} \ No newline at end of file diff --git a/kernel/src/arch/x86_64/mod.rs b/kernel/src/arch/x86_64/mod.rs index 291f660..8ae4873 100644 --- a/kernel/src/arch/x86_64/mod.rs +++ b/kernel/src/arch/x86_64/mod.rs @@ -10,6 +10,7 @@ pub mod gdt; pub mod idt; pub mod smp; pub mod memory; +pub mod io; pub fn init() { idt::init(); diff --git a/kernel/src/logging.rs b/kernel/src/logging.rs index f59c435..6307499 100644 --- a/kernel/src/logging.rs +++ b/kernel/src/logging.rs @@ -1,6 +1,5 @@ use core::fmt; -use log; -use log::{Level, LevelFilter, Log, Metadata, Record}; +use log::{self, Level, LevelFilter, Log, Metadata, Record}; pub fn init() { static LOGGER: SimpleLogger = SimpleLogger; @@ -34,42 +33,14 @@ macro_rules! with_color { }}; } -#[cfg(target_arch = "x86_64")] fn print_in_color(args: fmt::Arguments, color: Color) { - use core::fmt::Write; - use arch::driver::serial::COM1; -// use arch::driver::vga::*; -// { -// let mut writer = vga_writer::VGA_WRITER.lock(); -// writer.set_color(color); -// writer.write_fmt(args).unwrap(); -// } - // TODO: 解决死锁问题 - // 若进程在持有锁时被中断,中断处理程序请求输出,就会死锁 - unsafe{ COM1.force_unlock(); } - COM1.lock().write_fmt(with_color!(args, color)).unwrap(); + use arch::io; + io::putfmt(with_color!(args, color)); } -#[cfg(target_arch = "riscv")] -fn print_in_color(args: fmt::Arguments, color: Color) { - use arch::serial::SerialPort; - use core::fmt::Write; - SerialPort.write_fmt(with_color!(args, color)).unwrap(); -} - -#[cfg(target_arch = "x86_64")] -pub fn print(args: fmt::Arguments) { - use core::fmt::Write; - use arch::driver::serial::COM1; - unsafe { COM1.force_unlock(); } - COM1.lock().write_fmt(args).unwrap(); -} - -#[cfg(target_arch = "riscv")] pub fn print(args: fmt::Arguments) { - use arch::serial::SerialPort; - use core::fmt::Write; - SerialPort.write_fmt(args).unwrap(); + use arch::io; + io::putfmt(args); } struct SimpleLogger;