Skip to content

Commit 63b738e

Browse files
committed
feat: 实现 DBCN
Signed-off-by: YdrMaster <[email protected]>
1 parent cddd08b commit 63b738e

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rustsbi-qemu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ edition = "2021"
1717
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1818

1919
[dependencies]
20-
rustsbi = { git = "https://github.com/rustsbi/rustsbi", rev = "7118c8c" }
20+
rustsbi = { version = "0.3.2-rc.2", features = ["sbi_2_0"] }
2121
sbi-spec = { version = "0.0.5", features = ["legacy"] }
2222
riscv = "0.10.1"
2323
spin = "0.9"

rustsbi-qemu/src/dbcn.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use crate::UART;
2+
use core::ops::Range;
3+
use rustsbi::{spec::binary::SbiRet, Console, Physical};
4+
use spin::Once;
5+
6+
pub(crate) struct DBCN(Range<usize>);
7+
8+
static INSTANCE: Once<DBCN> = Once::new();
9+
10+
pub(crate) fn init(memory: Range<usize>) {
11+
INSTANCE.call_once(|| DBCN(memory));
12+
}
13+
14+
pub(crate) fn get() -> &'static DBCN {
15+
INSTANCE.wait()
16+
}
17+
18+
impl Console for DBCN {
19+
fn write(&self, bytes: Physical<&[u8]>) -> SbiRet {
20+
let start = bytes.phys_addr_lo();
21+
let end = start + bytes.num_bytes();
22+
if self.0.contains(&start) && self.0.contains(&(end - 1)) {
23+
let mut uart = UART.lock();
24+
let uart = unsafe { uart.assume_init_mut() };
25+
for ptr in start..end {
26+
let c = ptr as *const u8;
27+
unsafe { uart.send(c.read_volatile()) };
28+
}
29+
SbiRet::success(bytes.num_bytes())
30+
} else {
31+
SbiRet::invalid_param()
32+
}
33+
}
34+
35+
fn read(&self, bytes: Physical<&mut [u8]>) -> SbiRet {
36+
let start = bytes.phys_addr_lo();
37+
let end = start + bytes.num_bytes();
38+
if self.0.contains(&start) && self.0.contains(&(end - 1)) {
39+
let mut uart = UART.lock();
40+
let uart = unsafe { uart.assume_init_mut() };
41+
for ptr in start..end {
42+
let c = ptr as *mut u8;
43+
unsafe { c.write_volatile(uart.receive()) };
44+
}
45+
SbiRet::success(bytes.num_bytes())
46+
} else {
47+
SbiRet::invalid_param()
48+
}
49+
}
50+
51+
#[inline]
52+
fn write_byte(&self, byte: u8) -> SbiRet {
53+
let mut uart = UART.lock();
54+
let uart = unsafe { uart.assume_init_mut() };
55+
uart.send(byte);
56+
SbiRet::success(0)
57+
}
58+
}

rustsbi-qemu/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![deny(warnings)]
55

66
mod clint;
7+
mod dbcn;
78
mod device_tree;
89
mod hart_csr_utils;
910
mod qemu_test;
@@ -87,6 +88,7 @@ extern "C" fn rust_main(hartid: usize, opaque: usize) {
8788
rcore_console::set_log_level(option_env!("LOG"));
8889
clint::init(board_info.clint.start);
8990
qemu_test::init(board_info.test.start);
91+
dbcn::init(SUPERVISOR_ENTRY..board_info.mem.end);
9092
// 打印启动信息
9193
print!(
9294
"\
@@ -118,6 +120,7 @@ extern "C" fn rust_main(hartid: usize, opaque: usize) {
118120
.with_timer(&clint::Clint)
119121
.with_hsm(Hsm)
120122
.with_reset(qemu_test::get())
123+
.with_console(dbcn::get())
121124
.build(),
122125
);
123126
}
@@ -340,6 +343,7 @@ type FixedRustSBI<'a> = RustSBI<
340343
Hsm,
341344
&'a qemu_test::QemuTest,
342345
Infallible,
346+
&'a dbcn::DBCN,
343347
>;
344348

345349
struct Hsm;

0 commit comments

Comments
 (0)