Skip to content

Commit

Permalink
step 3
Browse files Browse the repository at this point in the history
  • Loading branch information
buhe committed Sep 29, 2021
1 parent 1dcbcf4 commit e451a27
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[build]
target = "riscv64gc-unknown-none-elf"


[target.riscv64gc-unknown-none-elf]
rustflags = [
"-Clink-arg=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes"
]
118 changes: 117 additions & 1 deletion docs/3 打印.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,119 @@
## 打印

退出看不出来,看来只好打印了,幸好 SBI 实现了打印,省去了自己实现驱动,调用系统调用就行了。
退出看不出来,看来只好打印了,幸好 SBI 实现了打印,省去了自己实现驱动,调用系统调用就行了。

实现系统调用,在 scall_sbi/mod.rs 中

```rust
#[inline(always)]
fn scall(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
}
const SBI_CONSOLE_PUTCHAR: usize = 1;
const SBI_SHUTDOWN: usize = 8;

pub fn shutdown() -> ! {
scall(SBI_SHUTDOWN, 0, 0, 0);
panic!("It should shutdown!");
}

pub fn put_char(c: usize) {
scall(SBI_CONSOLE_PUTCHAR, c, 0, 0);
}

```

建立 console.rs 调用刚才的系统调用,内容是

```rust
use core::fmt::{self, Write};

use crate::scall_sbi::put_char;

struct STDOUT;

impl Write for STDOUT {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for c in s.chars() {
put_char(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)+)?));
}
}
```

说明一下

- 利用 宏 + core::fmt::Write 来解析参数

main.rs 里就可以调用宏输出了,这时 main.rs 的内容

```rust
#![feature(llvm_asm)]
#![feature(global_asm)]
#![no_std]
#![no_main]

use scall_sbi::shutdown;

mod lang;
mod scall_sbi;
#[macro_use]
mod console;

global_asm!(include_str!("stack.asm"));

#[no_mangle]
extern "C" fn rust_main() ->! {
println!("hello OS");
shutdown();
}
```

编译一下,make build,编译过了,运行一下 make run 也成功了,输出

```
[rustsbi] RustSBI version 0.2.0-alpha.3
.______ __ __ _______.___________. _______..______ __
| _ \ | | | | / | | / || _ \ | |
| |_) | | | | | | (----`---| |----`| (----`| |_) || |
| / | | | | \ \ | | \ \ | _ < | |
| |\ \----.| `--' |.----) | | | .----) | | |_) || |
| _| `._____| \______/ |_______/ |__| |_______/ |______/ |__|
[rustsbi] Platform: K210 (Version 0.2.0)
[rustsbi] misa: RV64ACDFIMSU
[rustsbi] mideleg: 0x22
[rustsbi] medeleg: 0x1ab
[rustsbi] Kernel entry: 0x80020000
hello OS
[rustsbi] reset triggered! todo: shutdown all harts on k210; program halt. Type: 0, reason: 0
```

32 changes: 32 additions & 0 deletions os/src/console.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use core::fmt::{self, Write};

use crate::scall_sbi::put_char;

struct STDOUT;

impl Write for STDOUT {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for c in s.chars() {
put_char(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)+)?));
}
}
3 changes: 3 additions & 0 deletions os/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ use scall_sbi::shutdown;

mod lang;
mod scall_sbi;
#[macro_use]
mod console;

global_asm!(include_str!("stack.asm"));

#[no_mangle]
extern "C" fn rust_main() ->! {
println!("hello OS");
shutdown();
}
6 changes: 5 additions & 1 deletion os/src/scall_sbi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ fn scall(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
}
ret
}

const SBI_CONSOLE_PUTCHAR: usize = 1;
const SBI_SHUTDOWN: usize = 8;

pub fn shutdown() -> ! {
scall(SBI_SHUTDOWN, 0, 0, 0);
panic!("It should shutdown!");
}

pub fn put_char(c: usize) {
scall(SBI_CONSOLE_PUTCHAR, c, 0, 0);
}

0 comments on commit e451a27

Please sign in to comment.