From 7b4cc6d772d72636a4eebe817c409efce87a45b3 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Thu, 12 Jul 2018 20:45:22 +0800 Subject: [PATCH] Fix `log` crate --- Cargo.toml | 9 +++++++-- src/io/mod.rs | 19 ++++++++++++++++++- src/io/riscv_io.rs | 45 --------------------------------------------- src/lang.rs | 2 +- src/lib.rs | 9 +-------- 5 files changed, 27 insertions(+), 57 deletions(-) delete mode 100644 src/io/riscv_io.rs diff --git a/Cargo.toml b/Cargo.toml index 1104c73..0148c14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,13 +13,19 @@ qemu_auto_exit = [] link_user_program = [] [profile.dev] -# enable RVO to avoid stack overflow +# MUST >= 1 : Enable RVO to avoid stack overflow +# MUST >= 2 : Avoid (u8, u8) LLVM error in RV32 (at crate 'log') +# Error: Assertion `isSimple() && "Expected a SimpleValueType!"' failed. +# BUT! +# MUST <= 1 : Avoid double fault at -O2 T_T opt-level = 1 [profile.release] debug = true [dependencies] +# Fixed version for RV32 +log = { git = "https://github.com/riscv-and-rust-and-decaf/log.git" } rlibc = "1.0" spin = "0.4.8" once = "0.3.3" @@ -37,7 +43,6 @@ multiboot2 = "0.6" x86_64 = "0.2.6" linked_list_allocator = "0.6" redox_syscall = "0.1" -log = "0.4" uart_16550 = "0.1" simple-filesystem = { git = "https://github.com/wangrunji0408/SimpleFileSystem-Rust" } diff --git a/src/io/mod.rs b/src/io/mod.rs index 952d725..96b4277 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,4 +1,3 @@ -use arch::driver::serial::COM1; use core::fmt; use log; use log::{Level, LevelFilter, Log, Metadata, Record}; @@ -35,8 +34,10 @@ 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(); @@ -49,12 +50,28 @@ fn print_in_color(args: fmt::Arguments, color: Color) { COM1.lock().write_fmt(with_color!(args, color)).unwrap(); } +#[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(); +} + struct SimpleLogger; impl Log for SimpleLogger { diff --git a/src/io/riscv_io.rs b/src/io/riscv_io.rs deleted file mode 100644 index bb2e756..0000000 --- a/src/io/riscv_io.rs +++ /dev/null @@ -1,45 +0,0 @@ -// FIXME: merge to x86_64 io - -use core::fmt; - -macro_rules! print { - ($($arg:tt)*) => ({ - $crate::io::print(format_args!($($arg)*)); - }); -} - -macro_rules! println { - ($fmt:expr) => (print!(concat!($fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); -} - -macro_rules! trace { - ($fmt:expr) => (print!(concat!("[trace] ", $fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!("[trace] ", $fmt, "\n"), $($arg)*)); -} - -macro_rules! debug { - ($fmt:expr) => (print!(concat!("[debug] ", $fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!("[debug] ", $fmt, "\n"), $($arg)*)); -} - -macro_rules! info { - ($fmt:expr) => (print!(concat!("[ info] ", $fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!("[ info] ", $fmt, "\n"), $($arg)*)); -} - -macro_rules! warn { - ($fmt:expr) => (print!(concat!("[ warn] ", $fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!("[ warn] ", $fmt, "\n"), $($arg)*)); -} - -macro_rules! error { - ($fmt:expr) => (print!(concat!("[error] ", $fmt, "\n"))); - ($fmt:expr, $($arg:tt)*) => (print!(concat!("[error] ", $fmt, "\n"), $($arg)*)); -} - -pub fn print(args: fmt::Arguments) { - use arch::serial::SerialPort; - use core::fmt::Write; - SerialPort.write_fmt(args).unwrap(); -} \ No newline at end of file diff --git a/src/lang.rs b/src/lang.rs index 95914f1..5359cb6 100644 --- a/src/lang.rs +++ b/src/lang.rs @@ -25,7 +25,7 @@ pub fn panic(info: &PanicInfo) -> ! { #[lang = "panic_fmt"] #[no_mangle] pub fn panic_fmt(fmt: ::core::fmt::Arguments, file: &'static str, line: u32, col: u32) -> ! { - println!("\n\nPANIC in {} at {}:{}\n {}", file, line, col, fmt); + error!("\n\nPANIC in {} at {}:{}\n {}", file, line, col, fmt); loop {} } diff --git a/src/lib.rs b/src/lib.rs index 348c2a6..497454c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,6 @@ extern crate bitflags; extern crate lazy_static; extern crate linked_list_allocator; #[macro_use] -#[cfg(target_arch = "x86_64")] extern crate log; #[cfg(target_arch = "x86_64")] extern crate multiboot2; @@ -59,14 +58,7 @@ pub use arch::other_main; use linked_list_allocator::LockedHeap; #[macro_use] // print! -#[cfg(target_arch = "x86_64")] -mod io; - -#[macro_use] // print! -#[cfg(target_arch = "riscv")] -#[path = "io/riscv_io.rs"] mod io; - mod memory; mod lang; mod util; @@ -97,6 +89,7 @@ fn timer_interrupt() { #[no_mangle] #[cfg(target_arch = "riscv")] pub extern fn rust_main() -> ! { + io::init(); arch::init(); process::init(); info!("RISCV init end");