ch1: Print and shutdown based on sbi
This commit is contained in:
parent
43fd85405b
commit
3681c27870
6 changed files with 94 additions and 3 deletions
18
Cargo.lock
generated
18
Cargo.lock
generated
|
@ -5,3 +5,21 @@ version = 3
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "os"
|
name = "os"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"sbi-rt",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sbi-rt"
|
||||||
|
version = "0.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7fbaa69be1eedc61c426e6d489b2260482e928b465360576900d52d496a58bd0"
|
||||||
|
dependencies = [
|
||||||
|
"sbi-spec",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sbi-spec"
|
||||||
|
version = "0.0.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6e36312fb5ddc10d08ecdc65187402baba4ac34585cb9d1b78522ae2358d890"
|
||||||
|
|
|
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
sbi-rt = "0.0.3"
|
||||||
|
|
29
src/console.rs
Normal file
29
src/console.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use core::fmt::{self, Write};
|
||||||
|
use crate::sbi;
|
||||||
|
|
||||||
|
struct Stdout;
|
||||||
|
|
||||||
|
impl Write for Stdout {
|
||||||
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
|
sbi::console_write(s.as_bytes());
|
||||||
|
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)+)?));
|
||||||
|
};
|
||||||
|
}
|
|
@ -1,6 +1,17 @@
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
|
use crate::{sbi, println};
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(_info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
loop {}
|
if let Some(location) = info.location() {
|
||||||
|
println!(
|
||||||
|
"Panicked at {}:{} {}",
|
||||||
|
location.file(),
|
||||||
|
location.line(),
|
||||||
|
info.message().as_str().unwrap()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
println!("Panicked: {}", info.message().as_str().unwrap());
|
||||||
|
}
|
||||||
|
sbi::shutdown(true)
|
||||||
}
|
}
|
15
src/main.rs
15
src/main.rs
|
@ -2,6 +2,10 @@
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
mod lang_items;
|
mod lang_items;
|
||||||
|
mod sbi;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod console;
|
||||||
|
|
||||||
use core::arch::global_asm;
|
use core::arch::global_asm;
|
||||||
global_asm!(include_str!("entry.asm"));
|
global_asm!(include_str!("entry.asm"));
|
||||||
|
@ -9,7 +13,16 @@ global_asm!(include_str!("entry.asm"));
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub fn rust_main() -> ! {
|
pub fn rust_main() -> ! {
|
||||||
clear_bss();
|
clear_bss();
|
||||||
loop {}
|
|
||||||
|
sbi::console_write_byte('O' as u8);
|
||||||
|
sbi::console_write_byte('K' as u8);
|
||||||
|
sbi::console_write_byte('\n' as u8);
|
||||||
|
|
||||||
|
sbi::console_write("Hello World.\n".as_bytes());
|
||||||
|
|
||||||
|
println!("hello world from macro");
|
||||||
|
|
||||||
|
panic!("Shutdown machine!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear_bss() {
|
fn clear_bss() {
|
||||||
|
|
19
src/sbi.rs
Normal file
19
src/sbi.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
pub fn console_write_byte(c: u8) {
|
||||||
|
sbi_rt::console_write_byte(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn console_write(bytes: &[u8]) {
|
||||||
|
let range = bytes.as_ptr_range();
|
||||||
|
let bytes = sbi_rt::Physical::new(bytes.len(), range.start as usize, range.end as usize);
|
||||||
|
sbi_rt::console_write(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn shutdown(failure: bool) -> ! {
|
||||||
|
use sbi_rt::{system_reset, NoReason, Shutdown, SystemFailure};
|
||||||
|
if !failure {
|
||||||
|
system_reset(Shutdown, NoReason);
|
||||||
|
} else {
|
||||||
|
system_reset(Shutdown, SystemFailure);
|
||||||
|
}
|
||||||
|
unreachable!()
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue