diff --git a/os/src/console.rs b/os/src/console.rs new file mode 100644 index 00000000..2bd55930 --- /dev/null +++ b/os/src/console.rs @@ -0,0 +1,33 @@ +use core::fmt::{self, Write}; +use crate::sbi::console_putchar; + +struct Stdout; + +impl Write for Stdout { + fn write_str(&mut self, s: &str) -> fmt::Result { + for c in s.chars() { + console_putchar(c as usize); + } + Ok(()) + } +} + +pub fn print(args: fmt::Arguments) { + Stdout.write_fmt(args).unwrap(); +} + +#[macro_export] +macro_rules! print { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::console::print(format_args!($fmt $(, $($arg)+)?)); + } +} + +#[macro_export] +macro_rules! println { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?)); + } +} + + diff --git a/os/src/linker.ld b/os/src/linker.ld index f2487f31..8a24ad14 100644 --- a/os/src/linker.ld +++ b/os/src/linker.ld @@ -10,36 +10,33 @@ SECTIONS stext = .; .text : { *(.text.entry) - *(.text) + *(.text .text.*) } . = ALIGN(4K); etext = .; srodata = .; .rodata : { - *(.rodata) + *(.rodata .rodata.*) } . = ALIGN(4K); erodata = .; sdata = .; .data : { - *(.data) + *(.data .data.*) } . = ALIGN(4K); edata = .; - sbss = .; .bss : { - *(.bss) + *(.bss.stack) + sbss = .; + *(.bss .bss.*) } . = ALIGN(4K); ebss = .; - .stack : { - *(.bss.stack) - } - ekernel = .; /DISCARD/ : { diff --git a/os/src/main.rs b/os/src/main.rs index 5f4c11c9..02c67547 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -1,12 +1,34 @@ #![no_std] #![no_main] #![feature(global_asm)] +#![feature(llvm_asm)] mod lang_items; +mod sbi; +#[macro_use] +mod console; global_asm!(include_str!("entry.asm")); #[no_mangle] pub fn rust_main() -> ! { + println!("Hello, world!"); + extern "C" { + fn stext(); + fn etext(); + fn srodata(); + fn erodata(); + fn sdata(); + fn edata(); + fn sbss(); + fn ebss(); + fn boot_stack(); + fn boot_stack_top(); + }; + println!(".text [{:#x}, {:#x})", stext as usize, etext as usize); + println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize); + println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize); + println!("boot_stack [{:#x}, {:#x})", boot_stack as usize, boot_stack_top as usize); + println!(".bss [{:#x}, {:#x})", sbss as usize, ebss as usize); loop {} } \ No newline at end of file diff --git a/os/src/sbi.rs b/os/src/sbi.rs new file mode 100644 index 00000000..9fc74a97 --- /dev/null +++ b/os/src/sbi.rs @@ -0,0 +1,39 @@ +#![allow(unused)] + +const SBI_SET_TIMER: usize = 0; +const SBI_CONSOLE_PUTCHAR: usize = 1; +const SBI_CONSOLE_GETCHAR: usize = 2; +const SBI_CLEAR_IPI: usize = 3; +const SBI_SEND_IPI: usize = 4; +const SBI_REMOTE_FENCE_I: usize = 5; +const SBI_REMOTE_SFENCE_VMA: usize = 6; +const SBI_REMOTE_SFENCE_VMA_ASID: usize = 7; +const SBI_SHUTDOWN: usize = 8; + +#[inline(always)] +fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize { + let mut ret; + unsafe { + llvm_asm!("ecall" + : "={x10}" (ret) + : "{x10}" (arg0), "{x11}" (arg1), "{x12}" (arg2), "{x17}" (which) + : "memory" + : "volatile" + ); + } + ret +} + +pub fn console_putchar(c: usize) { + sbi_call(SBI_CONSOLE_PUTCHAR, c, 0, 0); +} + +pub fn console_getchar() -> usize { + sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0) +} + +pub fn shutdown() -> ! { + sbi_call(SBI_SHUTDOWN, 0, 0, 0); + panic!("It should shutdown!"); +} +