@@ -7,6 +7,7 @@ mod clint;
7
7
mod device_tree;
8
8
mod execute;
9
9
mod hart_csr_utils;
10
+ mod hsm_cell;
10
11
mod qemu_hsm;
11
12
mod qemu_test;
12
13
@@ -24,16 +25,18 @@ extern crate rcore_console;
24
25
25
26
use constants:: * ;
26
27
use core:: {
28
+ arch:: asm,
27
29
convert:: Infallible ,
28
30
mem:: { forget, size_of, MaybeUninit } ,
29
31
ptr:: NonNull ,
30
- sync:: atomic:: { AtomicBool , AtomicUsize , Ordering :: AcqRel } ,
32
+ sync:: atomic:: { AtomicBool , Ordering } ,
31
33
} ;
32
34
use device_tree:: BoardInfo ;
33
35
use execute:: Operation ;
34
36
use fast_trap:: {
35
37
reuse_stack_for_trap, FastContext , FastResult , FlowContext , FreeTrapStack , TrapStackBlock ,
36
38
} ;
39
+ use hsm_cell:: HsmCell ;
37
40
use riscv:: register:: * ;
38
41
use rustsbi:: RustSBI ;
39
42
use spin:: { Mutex , Once } ;
@@ -56,7 +59,7 @@ static mut ROOT_STACK: [Stack; NUM_HART_MAX] = [Stack::ZERO; NUM_HART_MAX];
56
59
#[ no_mangle]
57
60
#[ link_section = ".text.entry" ]
58
61
unsafe extern "C" fn _start ( ) -> ! {
59
- core :: arch :: asm!(
62
+ asm ! (
60
63
// 关中断
61
64
" csrw mie, zero" ,
62
65
// 设置栈
@@ -85,6 +88,11 @@ unsafe extern "C" fn _start() -> ! {
85
88
)
86
89
}
87
90
91
+ #[ naked]
92
+ unsafe extern "C" fn _stop ( ) -> ! {
93
+ asm ! ( "wfi" , options( noreturn) )
94
+ }
95
+
88
96
static HSM : Once < qemu_hsm:: QemuHsm > = Once :: new ( ) ;
89
97
90
98
type FixedRustSBI < ' a > = RustSBI <
@@ -100,10 +108,9 @@ type FixedRustSBI<'a> = RustSBI<
100
108
extern "C" fn rust_main ( _hartid : usize , opaque : usize ) -> Operation {
101
109
static GENESIS : AtomicBool = AtomicBool :: new ( true ) ;
102
110
static BOARD_INFO : Once < BoardInfo > = Once :: new ( ) ;
103
- static CSR_PRINT : AtomicBool = AtomicBool :: new ( false ) ;
104
111
105
112
// 全局初始化过程
106
- if GENESIS . swap ( false , AcqRel ) {
113
+ if GENESIS . swap ( false , Ordering :: AcqRel ) {
107
114
extern "C" {
108
115
static mut sbss: u64 ;
109
116
static mut ebss: u64 ;
@@ -142,17 +149,17 @@ extern "C" fn rust_main(_hartid: usize, opaque: usize) -> Operation {
142
149
dtb = board_info. dtb,
143
150
firmware = _start as usize ,
144
151
) ;
152
+ // 设置并打印 pmp
153
+ set_pmp ( board_info) ;
154
+ hart_csr_utils:: print_pmps ( ) ;
155
+ } else {
156
+ set_pmp ( BOARD_INFO . wait ( ) ) ;
145
157
}
146
158
147
159
unsafe { ROOT_STACK [ hart_id ( ) ] . load_as_stack ( ) } ;
148
160
149
161
let hsm = HSM . wait ( ) ;
150
162
if let Some ( supervisor) = hsm. take_supervisor ( ) {
151
- // 设置并打印 pmp
152
- set_pmp ( BOARD_INFO . wait ( ) ) ;
153
- if !CSR_PRINT . swap ( true , AcqRel ) {
154
- hart_csr_utils:: print_pmps ( ) ;
155
- }
156
163
// 初始化 SBI 服务
157
164
let sbi = rustsbi:: Builder :: new_machine ( )
158
165
. with_ipi ( & clint:: Clint )
@@ -234,7 +241,19 @@ extern "C" fn fast_handler(
234
241
match cause. cause ( ) {
235
242
T :: Exception ( E :: Unknown ) => match cause. bits ( ) {
236
243
cause:: BOOT => {
237
- // TODO 检查状态,设置启动参数
244
+ let hart_id = hart_id ( ) ;
245
+ let hart_ctx = unsafe { ROOT_STACK [ hart_id] . hart_context ( ) } ;
246
+ match hart_ctx. hsm . take ( ) {
247
+ Ok ( supervisor) => {
248
+ hart_ctx. trap . a [ 0 ] = hart_id;
249
+ hart_ctx. trap . a [ 1 ] = supervisor. opaque ;
250
+ hart_ctx. trap . pc = supervisor. start_addr ;
251
+ }
252
+ // TODO 检查状态,设置启动参数
253
+ Err ( _state) => {
254
+ hart_ctx. trap . pc = _stop as usize ;
255
+ }
256
+ }
238
257
ctx. call ( 2 )
239
258
}
240
259
_ => todo ! ( ) ,
@@ -275,7 +294,7 @@ impl Stack {
275
294
}
276
295
277
296
fn load_as_stack ( & ' static mut self ) {
278
- let ptr = unsafe { NonNull :: new_unchecked ( & mut self . hart_context ( ) . flow ) } ;
297
+ let ptr = unsafe { NonNull :: new_unchecked ( & mut self . hart_context ( ) . trap ) } ;
279
298
forget (
280
299
FreeTrapStack :: new ( StackRef ( self ) , ptr, fast_handler)
281
300
. unwrap ( )
@@ -309,12 +328,12 @@ impl Drop for StackRef {
309
328
}
310
329
}
311
330
331
+ /// 硬件线程上下文。
312
332
#[ repr( C ) ]
313
333
struct HartContext {
314
- flow : FlowContext ,
315
- state : AtomicUsize ,
316
- start_address : usize ,
317
- opaque : usize ,
334
+ /// 陷入上下文。
335
+ trap : FlowContext ,
336
+ hsm : HsmCell < Supervisor > ,
318
337
}
319
338
320
339
/// 特权软件信息。
0 commit comments