From bf2ad7c6a5cc69133b66babd98994417f63ae8b7 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 6 Jul 2018 22:33:28 +0800 Subject: [PATCH] Change target arch to RISCV32IMA. Recover some dependencies. --- Cargo.toml | 20 ++++--- crate/bit-allocator/src/lib.rs | 30 +++++++---- crate/memory/src/lib.rs | 2 + src/arch/riscv32/mod.rs | 4 +- src/arch/riscv32/serial.rs | 13 +++++ src/arch/x86_64/driver/vga.rs | 98 ++++++++++++++++++++++++---------- src/io/mod.rs | 25 +++++++-- src/io/vga_writer.rs | 73 ------------------------- src/lib.rs | 8 ++- 9 files changed, 146 insertions(+), 127 deletions(-) create mode 100644 src/arch/riscv32/serial.rs delete mode 100644 src/io/vga_writer.rs diff --git a/Cargo.toml b/Cargo.toml index 070ca7e..733a5aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,26 +19,30 @@ opt-level = 1 [profile.release] debug = true -[target.x86_64-blog_os.dependencies] -bit_field = "0.9.0" +[dependencies] rlibc = "1.0" -volatile = "0.1.0" spin = "0.4.8" -multiboot2 = "0.6" +once = "0.3.3" +xmas-elf = "0.6" bitflags = "1.0" +bit_field = "0.9.0" +volatile = "0.1.0" +lazy_static = { version = "1.0.0", features = ["spin_no_std"] } +bit-allocator = { path = "crate/bit-allocator" } + + +[target.x86_64-blog_os.dependencies] +multiboot2 = "0.6" x86_64 = "0.2.6" -once = "0.3.3" linked_list_allocator = "0.6" redox_syscall = "0.1" -xmas-elf = "0.6" log = "0.4" uart_16550 = "0.1" -lazy_static = { version = "1.0.0", features = ["spin_no_std"] } simple-filesystem = { git = "https://github.com/wangrunji0408/SimpleFileSystem-Rust" } -bit-allocator = { path = "crate/bit-allocator" } ucore-memory = { path = "crate/memory" } [target.riscv32-blog_os.dependencies] +linked_list_allocator = { version = "0.5", default-features = false } # due to rust version riscv = { path = "crate/riscv" } bbl = { path = "crate/bbl" } diff --git a/crate/bit-allocator/src/lib.rs b/crate/bit-allocator/src/lib.rs index 016065b..b84de2f 100644 --- a/crate/bit-allocator/src/lib.rs +++ b/crate/bit-allocator/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] #![feature(asm)] +#![feature(universal_impl_trait)] extern crate bit_field; @@ -108,6 +109,7 @@ impl BitAlloc for BitAlloc16 { } #[inline(always)] +#[cfg(target_arch = "x86_64")] fn log2(x: u16) -> usize { assert_ne!(x, 0); let pos: u16; @@ -115,21 +117,27 @@ fn log2(x: u16) -> usize { pos as usize } +#[inline(always)] +#[cfg(not(target_arch = "x86_64"))] +fn log2(x: u16) -> usize { + log2_naive(x) +} + +#[inline(always)] +fn log2_naive(mut x: u16) -> usize { + assert_ne!(x, 0); + let mut pos = -1; + while x != 0 { + pos += 1; + x >>= 1; + } + pos as usize +} + #[cfg(test)] mod tests { use super::*; - #[inline(always)] - fn log2_naive(mut x: u16) -> usize { - assert_ne!(x, 0); - let mut pos = -1; - while x != 0 { - pos += 1; - x >>= 1; - } - pos as usize - } - #[test] fn log2_() { for x in 1..=0xffff { diff --git a/crate/memory/src/lib.rs b/crate/memory/src/lib.rs index 89fae2d..480d8e3 100644 --- a/crate/memory/src/lib.rs +++ b/crate/memory/src/lib.rs @@ -1,5 +1,7 @@ #![no_std] #![feature(alloc)] +#![feature(universal_impl_trait)] +#![feature(match_default_bindings)] extern crate alloc; diff --git a/src/arch/riscv32/mod.rs b/src/arch/riscv32/mod.rs index de9e012..ea5d074 100644 --- a/src/arch/riscv32/mod.rs +++ b/src/arch/riscv32/mod.rs @@ -1,6 +1,8 @@ extern crate riscv; extern crate bbl; +pub mod serial; + pub fn test() { - bbl::sbi::console_putchar(b'g' as u8 as u32); + println!("Hello RISCV! {}", 123); } \ No newline at end of file diff --git a/src/arch/riscv32/serial.rs b/src/arch/riscv32/serial.rs new file mode 100644 index 0000000..d09032b --- /dev/null +++ b/src/arch/riscv32/serial.rs @@ -0,0 +1,13 @@ +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 u32); + } + Ok(()) + } +} \ No newline at end of file diff --git a/src/arch/x86_64/driver/vga.rs b/src/arch/x86_64/driver/vga.rs index 45584cd..1f5139e 100644 --- a/src/arch/x86_64/driver/vga.rs +++ b/src/arch/x86_64/driver/vga.rs @@ -1,35 +1,15 @@ use consts::KERNEL_OFFSET; use core::ptr::Unique; +use core::fmt; use spin::Mutex; use volatile::Volatile; use x86_64::instructions::port::Port; +use io::Color; pub const VGA_BUFFER: Unique = unsafe { Unique::new_unchecked((KERNEL_OFFSET + 0xb8000) as *mut _) }; -#[allow(dead_code)] -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -#[repr(u8)] -pub enum Color { - Black = 0, - Blue = 1, - Green = 2, - Cyan = 3, - Red = 4, - Magenta = 5, - Brown = 6, - LightGray = 7, - DarkGray = 8, - LightBlue = 9, - LightGreen = 10, - LightCyan = 11, - LightRed = 12, - Pink = 13, - Yellow = 14, - White = 15, -} - #[derive(Debug, Clone, Copy)] struct ColorCode(u8); @@ -90,12 +70,72 @@ impl VgaBuffer { } } -#[cfg(test)] -mod test { - #[test] - fn print_something() { - let vga = unsafe {&mut *VGA_BUFFER}; - vga.clear(); - vga.write(0, 0, ScreenChar::new('h', Color::LightGray, Color::Black)); +lazy_static! { + pub static ref VGA_WRITER: Mutex = Mutex::new( + // It is the only user of VGA_BUFFER. So it's safe. + VgaWriter::new(unsafe{ &mut *VGA_BUFFER.as_ptr() }) + ); +} + +pub struct VgaWriter { + column_position: usize, + color: Color, + buffer: &'static mut VgaBuffer, +} + +impl VgaWriter { + fn new(buffer: &'static mut VgaBuffer) -> Self { + buffer.clear(); + VgaWriter { + column_position: 0, + color: Color::LightGray, + buffer, + } + } + + pub fn set_color(&mut self, color: Color) { + self.color = color; + } + + pub fn write_byte(&mut self, byte: u8) { + match byte { + b'\n' => self.new_line(), + byte => { + if self.column_position >= BUFFER_WIDTH { + self.new_line(); + } + + let row = BUFFER_HEIGHT - 1; + let col = self.column_position; + + self.buffer.write(row, col, ScreenChar::new(byte, self.color, Color::Black)); + self.column_position += 1; + self.buffer.set_cursor_at(row, col); + } + } + } + + fn new_line(&mut self) { + for row in 1..BUFFER_HEIGHT { + for col in 0..BUFFER_WIDTH { + let screen_char = self.buffer.read(row, col); + self.buffer.write(row - 1, col, screen_char); + } + } + let blank = ScreenChar::new(b' ', self.color, Color::Black); + for col in 0..BUFFER_WIDTH { + self.buffer.write(BUFFER_HEIGHT - 1, col, blank); + } + self.column_position = 0; + self.buffer.set_cursor_at(BUFFER_HEIGHT - 1, 0); + } +} + +impl fmt::Write for VgaWriter { + fn write_str(&mut self, s: &str) -> fmt::Result { + for byte in s.bytes() { + self.write_byte(byte) + } + Ok(()) } } \ No newline at end of file diff --git a/src/io/mod.rs b/src/io/mod.rs index 416951f..952d725 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,11 +1,8 @@ use arch::driver::serial::COM1; -use arch::driver::vga::Color; use core::fmt; use log; use log::{Level, LevelFilter, Log, Metadata, Record}; -mod vga_writer; - pub fn init() { static LOGGER: SimpleLogger = SimpleLogger; log::set_logger(&LOGGER).unwrap(); @@ -107,4 +104,26 @@ fn color_to_console_code(color: Color) -> (u8, u8) { Color::Yellow => (1, 3), Color::White => (1, 0), } +} + +#[allow(dead_code)] +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[repr(u8)] +pub enum Color { + Black = 0, + Blue = 1, + Green = 2, + Cyan = 3, + Red = 4, + Magenta = 5, + Brown = 6, + LightGray = 7, + DarkGray = 8, + LightBlue = 9, + LightGreen = 10, + LightCyan = 11, + LightRed = 12, + Pink = 13, + Yellow = 14, + White = 15, } \ No newline at end of file diff --git a/src/io/vga_writer.rs b/src/io/vga_writer.rs deleted file mode 100644 index 23b966e..0000000 --- a/src/io/vga_writer.rs +++ /dev/null @@ -1,73 +0,0 @@ -use spin::Mutex; -use core::fmt; -use arch::driver::vga::*; - -lazy_static! { - pub static ref VGA_WRITER: Mutex = Mutex::new( - // It is the only user of VGA_BUFFER. So it's safe. - VgaWriter::new(unsafe{ &mut *VGA_BUFFER.as_ptr() }) - ); -} - -pub struct VgaWriter { - column_position: usize, - color: Color, - buffer: &'static mut VgaBuffer -} - -impl VgaWriter { - fn new(buffer: &'static mut VgaBuffer) -> Self { - buffer.clear(); - VgaWriter { - column_position: 0, - color: Color::LightGray, - buffer, - } - } - - pub fn set_color(&mut self, color: Color) { - self.color = color; - } - - pub fn write_byte(&mut self, byte: u8) { - match byte { - b'\n' => self.new_line(), - byte => { - if self.column_position >= BUFFER_WIDTH { - self.new_line(); - } - - let row = BUFFER_HEIGHT - 1; - let col = self.column_position; - - self.buffer.write(row, col, ScreenChar::new(byte, self.color, Color::Black)); - self.column_position += 1; - self.buffer.set_cursor_at(row, col); - } - } - } - - fn new_line(&mut self) { - for row in 1..BUFFER_HEIGHT { - for col in 0..BUFFER_WIDTH { - let screen_char = self.buffer.read(row, col); - self.buffer.write(row-1, col, screen_char); - } - } - let blank = ScreenChar::new(b' ', self.color, Color::Black); - for col in 0..BUFFER_WIDTH { - self.buffer.write(BUFFER_HEIGHT-1, col, blank); - } - self.column_position = 0; - self.buffer.set_cursor_at(BUFFER_HEIGHT-1, 0); - } -} - -impl fmt::Write for VgaWriter { - fn write_str(&mut self, s: &str) -> fmt::Result { - for byte in s.bytes() { - self.write_byte(byte) - } - Ok(()) - } -} diff --git a/src/lib.rs b/src/lib.rs index 29e30c3..e3acb72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,11 +42,9 @@ extern crate multiboot2; #[macro_use] #[cfg(target_arch = "x86_64")] extern crate once; -#[cfg(target_arch = "x86_64")] extern crate rlibc; #[cfg(target_arch = "x86_64")] extern crate simple_filesystem; -#[cfg(target_arch = "x86_64")] extern crate spin; #[cfg(target_arch = "x86_64")] extern crate syscall as redox_syscall; @@ -70,6 +68,12 @@ 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; + #[cfg(target_arch = "x86_64")] mod memory; mod lang;