From 40aed4fa0f24c38118e9d34239bd6c41026f7ccd Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 12 Apr 2017 19:32:59 +0200 Subject: [PATCH] Create a static WRITER protected by a spinlock --- Cargo.toml | 1 + src/lib.rs | 7 ++++++- src/vga_buffer.rs | 21 +++++++-------------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 455d244..b6910f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ crate-type = ["staticlib"] [dependencies] rlibc = "1.0" volatile = "0.1.0" +spin = "0.4.5" diff --git a/src/lib.rs b/src/lib.rs index 2fa06b5..3dc67e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,12 @@ #![feature(lang_items)] #![feature(const_fn)] +#![feature(const_unique_new)] #![feature(unique)] #![no_std] extern crate rlibc; extern crate volatile; +extern crate spin; mod vga_buffer; @@ -12,7 +14,10 @@ mod vga_buffer; pub extern fn rust_main() { // ATTENTION: we have a very small stack and no guard page - vga_buffer::print_something(); + use core::fmt::Write; + + vga_buffer::WRITER.lock().write_str("Hello again"); + write!(vga_buffer::WRITER.lock(), ", some numbers: {} {}", 42, 1.337); loop{} } diff --git a/src/vga_buffer.rs b/src/vga_buffer.rs index cdfe7ab..ca9187d 100644 --- a/src/vga_buffer.rs +++ b/src/vga_buffer.rs @@ -1,7 +1,14 @@ use core::fmt; use core::ptr::Unique; +use spin::Mutex; use volatile::Volatile; +pub static WRITER: Mutex = Mutex::new(Writer { + column_position: 0, + color_code: ColorCode::new(Color::LightGreen, Color::Black), + buffer: unsafe { Unique::new_unchecked(0xb8000 as *mut _) }, +}); + #[allow(dead_code)] #[derive(Debug, Clone, Copy)] #[repr(u8)] @@ -116,17 +123,3 @@ impl fmt::Write for Writer { Ok(()) } } - - -pub fn print_something() { - use core::fmt::Write; - - let mut writer = Writer { - column_position: 0, - color_code: ColorCode::new(Color::LightGreen, Color::Black), - buffer: unsafe { Unique::new(0xb8000 as *mut _) }, - }; - - writeln!(writer, "Hello!"); - writeln!(writer, "The numbers are {} and {}", 42, 1.0/3.0); -}