Skip to content

Commit 4aeadce

Browse files
committed
refactor(rustsbi-qemu): 整理,换装 rcore-console
Signed-off-by: YdrMaster <[email protected]>
1 parent da6ec2d commit 4aeadce

File tree

8 files changed

+45
-93
lines changed

8 files changed

+45
-93
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1515

1616
- Use instance based RustSBI interface, with separate functions for legacy stdio
1717
- Update sbi-testing to version 0.0.1
18+
- Use crate rcore-console version 0.0.0 in rustsbi-qemu and test-kernel for `print!` and `println!`
1819
- Use crate os-xtask-utils version 0.0.0 in xtask builder
1920

2021
### Fixed

Cargo.lock

Lines changed: 1 addition & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ riscv = "0.9.0"
2323
spin = "0.9"
2424
r0 = "1"
2525
uart_16550 = "0.2"
26+
rcore-console = "0.0.0"
2627
dtb-walker = "=0.2.0-alpha.3"
2728
qemu-exit = "3.0"

rustsbi-qemu/src/console.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

rustsbi-qemu/src/execute.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ impl<'a> Environment<'a> {
5959
use rustsbi::spec::{binary::*, hsm::*, srst::*};
6060
if self.ctx.sbi_extension() == sbi_spec::legacy::LEGACY_CONSOLE_PUTCHAR {
6161
let ch = self.ctx.a(0);
62-
crate::console::STDOUT.wait().putchar((ch & 0xFF) as u8);
62+
print!("{:}", ch as u8 as char);
6363
self.ctx.mepc = self.ctx.mepc.wrapping_add(4);
6464
return None;
6565
} else if self.ctx.sbi_extension() == sbi_spec::legacy::LEGACY_CONSOLE_GETCHAR {
66-
let ch = crate::console::STDOUT.wait().getchar();
67-
*self.ctx.a_mut(0) = ch as usize;
66+
*self.ctx.a_mut(0) = unsafe { crate::UART.lock().assume_init_mut().receive() } as usize;
6867
self.ctx.mepc = self.ctx.mepc.wrapping_add(4);
6968
return None;
7069
}

rustsbi-qemu/src/main.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
#![feature(naked_functions, asm_const)]
44
#![deny(warnings)]
55

6-
#[macro_use]
7-
mod console;
8-
96
mod clint;
107
mod device_tree;
118
mod execute;
129
mod hart_csr_utils;
13-
mod ns16550a;
1410
mod qemu_hsm;
1511
mod qemu_test;
1612

@@ -25,15 +21,20 @@ mod constants {
2521
pub(crate) const LEN_STACK_SBI: usize = LEN_STACK_PER_HART * NUM_HART_MAX;
2622
}
2723

24+
#[macro_use]
25+
extern crate rcore_console;
26+
2827
use constants::*;
2928
use core::{
3029
convert::Infallible,
30+
mem::MaybeUninit,
3131
sync::atomic::{AtomicBool, Ordering::AcqRel},
3232
};
3333
use device_tree::BoardInfo;
3434
use execute::Operation;
3535
use rustsbi::RustSBI;
36-
use spin::Once;
36+
use spin::{Mutex, Once};
37+
use uart_16550::MmioSerialPort;
3738

3839
/// 特权软件信息。
3940
#[derive(Debug)]
@@ -65,9 +66,9 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
6566
///
6667
/// 裸函数。
6768
#[naked]
69+
#[no_mangle]
6870
#[link_section = ".text.entry"]
69-
#[export_name = "_start"]
70-
unsafe extern "C" fn entry() -> ! {
71+
unsafe extern "C" fn _start() -> ! {
7172
#[link_section = ".bss.uninit"]
7273
static mut SBI_STACK: [u8; LEN_STACK_SBI] = [0; LEN_STACK_SBI];
7374

@@ -113,16 +114,19 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
113114
static BOARD_INFO: Once<BoardInfo> = Once::new();
114115
static CSR_PRINT: AtomicBool = AtomicBool::new(false);
115116

116-
// static RUSTSBI: Once<RustSBI<>> = Once::new();
117-
118117
// 全局初始化过程
119118
if GENESIS.swap(false, AcqRel) {
120-
// 清零 bss 段
121-
zero_bss();
119+
extern "C" {
120+
static mut sbss: u64;
121+
static mut ebss: u64;
122+
}
123+
unsafe { r0::zero_bss(&mut sbss, &mut ebss) };
122124
// 解析设备树
123125
let board_info = BOARD_INFO.call_once(|| device_tree::parse(opaque));
124126
// 初始化外设
125-
console::init(unsafe { ns16550a::Ns16550a::new(board_info.uart.start) });
127+
*UART.lock() = MaybeUninit::new(unsafe { MmioSerialPort::new(board_info.uart.start) });
128+
rcore_console::init_console(&Console);
129+
rcore_console::set_log_level(option_env!("LOG"));
126130
clint::init(board_info.clint.start);
127131
qemu_test::init(board_info.test.start);
128132
HSM.call_once(|| qemu_hsm::QemuHsm::new(NUM_HART_MAX, opaque));
@@ -148,13 +152,12 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
148152
mem = board_info.mem,
149153
hartid = hart_id(),
150154
dtb = board_info.dtb,
151-
firmware = entry as usize,
155+
firmware = _start as usize,
152156
);
153157
}
154158

155159
let hsm = HSM.wait();
156160
if let Some(supervisor) = hsm.take_supervisor() {
157-
use execute::*;
158161
// 设置并打印 pmp
159162
set_pmp(BOARD_INFO.wait());
160163
if !CSR_PRINT.swap(true, AcqRel) {
@@ -167,7 +170,7 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
167170
.with_reset(qemu_test::get())
168171
.with_hsm(hsm)
169172
.build();
170-
execute_supervisor(sbi, hsm, supervisor)
173+
execute::execute_supervisor(sbi, hsm, supervisor)
171174
} else {
172175
Operation::Stop
173176
}
@@ -190,7 +193,7 @@ unsafe extern "C" fn finalize(op: Operation) -> ! {
190193
Operation::SystemReset => {
191194
// TODO 等待其他核关闭
192195
// 直接回 entry
193-
entry()
196+
_start()
194197
}
195198
}
196199
}
@@ -200,20 +203,6 @@ fn hart_id() -> usize {
200203
riscv::register::mhartid::read()
201204
}
202205

203-
/// 清零 bss 段。
204-
#[inline(always)]
205-
fn zero_bss() {
206-
#[cfg(target_pointer_width = "32")]
207-
type Word = u32;
208-
#[cfg(target_pointer_width = "64")]
209-
type Word = u64;
210-
extern "C" {
211-
static mut sbss: Word;
212-
static mut ebss: Word;
213-
}
214-
unsafe { r0::zero_bss(&mut sbss, &mut ebss) };
215-
}
216-
217206
/// 设置 PMP。
218207
fn set_pmp(board_info: &BoardInfo) {
219208
use riscv::register::{
@@ -237,3 +226,22 @@ fn set_pmp(board_info: &BoardInfo) {
237226
pmpaddr4::write(1 << (usize::BITS - 1));
238227
}
239228
}
229+
230+
struct Console;
231+
static UART: Mutex<MaybeUninit<MmioSerialPort>> = Mutex::new(MaybeUninit::uninit());
232+
233+
impl rcore_console::Console for Console {
234+
#[inline]
235+
fn put_char(&self, c: u8) {
236+
unsafe { UART.lock().assume_init_mut() }.send(c);
237+
}
238+
239+
#[inline]
240+
fn put_str(&self, s: &str) {
241+
let mut uart = UART.lock();
242+
let uart = unsafe { uart.assume_init_mut() };
243+
for c in s.bytes() {
244+
uart.send(c);
245+
}
246+
}
247+
}

rustsbi-qemu/src/ns16550a.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

rustsbi-qemu/src/qemu_hsm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Hart state monitor designed for QEMU
22
3-
use crate::{clint, entry, hart_id, Supervisor, NUM_HART_MAX, SUPERVISOR_ENTRY};
3+
use crate::{_start, clint, hart_id, Supervisor, NUM_HART_MAX, SUPERVISOR_ENTRY};
44
use core::{mem::MaybeUninit, sync::atomic::AtomicU8};
55
use riscv::register::*;
66
use rustsbi::spec::{binary::SbiRet, hsm as spec};
@@ -126,7 +126,7 @@ impl QemuHsm {
126126
// 通过软件中断重启
127127
unsafe {
128128
mie::set_msoft();
129-
mtvec::write(entry as _, mtvec::TrapMode::Direct);
129+
mtvec::write(_start as _, mtvec::TrapMode::Direct);
130130
};
131131
// 转移状态
132132
if let Err(unexpected) = state.compare_exchange(current, new, AcqRel, Acquire) {

0 commit comments

Comments
 (0)