3
3
#![ feature( naked_functions, asm_const) ]
4
4
#![ deny( warnings) ]
5
5
6
- #[ macro_use]
7
- mod console;
8
-
9
6
mod clint;
10
7
mod device_tree;
11
8
mod execute;
12
9
mod hart_csr_utils;
13
- mod ns16550a;
14
10
mod qemu_hsm;
15
11
mod qemu_test;
16
12
@@ -25,15 +21,20 @@ mod constants {
25
21
pub ( crate ) const LEN_STACK_SBI : usize = LEN_STACK_PER_HART * NUM_HART_MAX ;
26
22
}
27
23
24
+ #[ macro_use]
25
+ extern crate rcore_console;
26
+
28
27
use constants:: * ;
29
28
use core:: {
30
29
convert:: Infallible ,
30
+ mem:: MaybeUninit ,
31
31
sync:: atomic:: { AtomicBool , Ordering :: AcqRel } ,
32
32
} ;
33
33
use device_tree:: BoardInfo ;
34
34
use execute:: Operation ;
35
35
use rustsbi:: RustSBI ;
36
- use spin:: Once ;
36
+ use spin:: { Mutex , Once } ;
37
+ use uart_16550:: MmioSerialPort ;
37
38
38
39
/// 特权软件信息。
39
40
#[ derive( Debug ) ]
@@ -65,9 +66,9 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
65
66
///
66
67
/// 裸函数。
67
68
#[ naked]
69
+ #[ no_mangle]
68
70
#[ link_section = ".text.entry" ]
69
- #[ export_name = "_start" ]
70
- unsafe extern "C" fn entry ( ) -> ! {
71
+ unsafe extern "C" fn _start ( ) -> ! {
71
72
#[ link_section = ".bss.uninit" ]
72
73
static mut SBI_STACK : [ u8 ; LEN_STACK_SBI ] = [ 0 ; LEN_STACK_SBI ] ;
73
74
@@ -113,16 +114,19 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
113
114
static BOARD_INFO : Once < BoardInfo > = Once :: new ( ) ;
114
115
static CSR_PRINT : AtomicBool = AtomicBool :: new ( false ) ;
115
116
116
- // static RUSTSBI: Once<RustSBI<>> = Once::new();
117
-
118
117
// 全局初始化过程
119
118
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) } ;
122
124
// 解析设备树
123
125
let board_info = BOARD_INFO . call_once ( || device_tree:: parse ( opaque) ) ;
124
126
// 初始化外设
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" ) ) ;
126
130
clint:: init ( board_info. clint . start ) ;
127
131
qemu_test:: init ( board_info. test . start ) ;
128
132
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 {
148
152
mem = board_info. mem,
149
153
hartid = hart_id( ) ,
150
154
dtb = board_info. dtb,
151
- firmware = entry as usize ,
155
+ firmware = _start as usize ,
152
156
) ;
153
157
}
154
158
155
159
let hsm = HSM . wait ( ) ;
156
160
if let Some ( supervisor) = hsm. take_supervisor ( ) {
157
- use execute:: * ;
158
161
// 设置并打印 pmp
159
162
set_pmp ( BOARD_INFO . wait ( ) ) ;
160
163
if !CSR_PRINT . swap ( true , AcqRel ) {
@@ -167,7 +170,7 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
167
170
. with_reset ( qemu_test:: get ( ) )
168
171
. with_hsm ( hsm)
169
172
. build ( ) ;
170
- execute_supervisor ( sbi, hsm, supervisor)
173
+ execute :: execute_supervisor ( sbi, hsm, supervisor)
171
174
} else {
172
175
Operation :: Stop
173
176
}
@@ -190,7 +193,7 @@ unsafe extern "C" fn finalize(op: Operation) -> ! {
190
193
Operation :: SystemReset => {
191
194
// TODO 等待其他核关闭
192
195
// 直接回 entry
193
- entry ( )
196
+ _start ( )
194
197
}
195
198
}
196
199
}
@@ -200,20 +203,6 @@ fn hart_id() -> usize {
200
203
riscv:: register:: mhartid:: read ( )
201
204
}
202
205
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
-
217
206
/// 设置 PMP。
218
207
fn set_pmp ( board_info : & BoardInfo ) {
219
208
use riscv:: register:: {
@@ -237,3 +226,22 @@ fn set_pmp(board_info: &BoardInfo) {
237
226
pmpaddr4:: write ( 1 << ( usize:: BITS - 1 ) ) ;
238
227
}
239
228
}
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
+ }
0 commit comments