Permalink
Browse files

Implemented kprint! and kprintln! macros for debug logging via serial…

… port
  • Loading branch information...
1 parent 7be7fd2 commit 0af76208346af047288851b1e1f11db0db5c2155 @diodesign committed Nov 30, 2015
Showing with 41 additions and 5 deletions.
  1. +1 −0 Cargo.toml
  2. +33 −3 src/debug.rs
  3. +7 −2 src/lib.rs
View
@@ -8,4 +8,5 @@ crate-type = ["staticlib"]
[dependencies]
rlibc = "0.1.4"
+spin = "0.3.4"
View
@@ -7,17 +7,47 @@
*
*/
+use core::fmt;
+use spin::Mutex; /* can't use the std lib's atomic ops */
+
/* hook us up with the platform-specific serial port */
extern
{
fn serial_write_byte(byte: u8);
}
-pub fn write_str(s: &str)
+/* create an object representing the debug serial port.
+ * this is needed so we can wrap a spinlock around it
+ * for thread safety. */
+pub static SERIALPORT: Mutex<SerialWriter> = Mutex::new(SerialWriter);
+
+/* create macros for kernel-only kprintln and kprint */
+macro_rules! kprintln
+{
+ ($fmt:expr) => (kprint!(concat!($fmt, "\n")));
+ ($fmt:expr, $($arg:tt)*) => (kprint!(concat!($fmt, "\n"), $($arg)*));
+}
+
+macro_rules! kprint
+{
+ ($($arg:tt)*) =>
+ ({
+ use core::fmt::Write;
+ $crate::debug::SERIALPORT.lock().write_fmt(format_args!($($arg)*)).unwrap();
+ });
+}
+
+pub struct SerialWriter; /* no state to store per serial port */
+
+impl fmt::Write for SerialWriter
{
- for byte in s.bytes()
+ fn write_str(&mut self, s: &str) -> ::core::fmt::Result
{
- unsafe{ serial_write_byte(byte) }
+ for byte in s.bytes()
+ {
+ unsafe{ serial_write_byte(byte); }
+ }
+ Ok(())
}
}
View
@@ -7,7 +7,7 @@
*
*/
-#![feature(no_std, lang_items, core_str_ext)]
+#![feature(no_std, lang_items, core_str_ext, const_fn)]
#![no_std]
/* bring in the kernel debugging features, provides kprintln! and kprint! */
@@ -18,11 +18,16 @@ mod debug;
* things like memcpy - see: https://crates.io/crates/rlibc */
extern crate rlibc;
+/* bare-metal atomic operations because we can't use the std lib.
+ * see: https://crates.io/crates/spin */
+extern crate spin;
+
/* entry point for our kernel */
#[no_mangle]
pub extern fn kmain()
{
- debug::write_str("hello world!\n");
+ /* display boot banner */
+ kprintln!("diosix {} 'menchi' now running", env!("CARGO_PKG_VERSION"));
}
#[lang = "eh_personality"] extern fn eh_personality() {}

0 comments on commit 0af7620

Please sign in to comment.