@@ -13,9 +13,9 @@ mod qemu_test;
13
13
mod constants {
14
14
/// 特权软件入口。
15
15
pub ( crate ) const SUPERVISOR_ENTRY : usize = 0x8020_0000 ;
16
- /// 每个核设置 16KiB 栈空间。
16
+ /// 每个硬件线程设置 16KiB 栈空间。
17
17
pub ( crate ) const LEN_STACK_PER_HART : usize = 16 * 1024 ;
18
- /// qemu-virt 最多 8 核 。
18
+ /// qemu-virt 最多 8 个硬件线程 。
19
19
pub ( crate ) const NUM_HART_MAX : usize = 8 ;
20
20
}
21
21
@@ -25,13 +25,15 @@ extern crate rcore_console;
25
25
use constants:: * ;
26
26
use core:: {
27
27
convert:: Infallible ,
28
- mem:: { forget, MaybeUninit } ,
28
+ mem:: { forget, size_of , MaybeUninit } ,
29
29
ptr:: NonNull ,
30
- sync:: atomic:: { AtomicBool , Ordering :: AcqRel } ,
30
+ sync:: atomic:: { AtomicBool , AtomicUsize , Ordering :: AcqRel } ,
31
31
} ;
32
32
use device_tree:: BoardInfo ;
33
33
use execute:: Operation ;
34
- use fast_trap:: { reuse_stack_for_trap, FastContext , FastResult , FreeTrapStack , TrapStackBlock } ;
34
+ use fast_trap:: {
35
+ reuse_stack_for_trap, FastContext , FastResult , FlowContext , FreeTrapStack , TrapStackBlock ,
36
+ } ;
35
37
use rustsbi:: RustSBI ;
36
38
use spin:: { Mutex , Once } ;
37
39
use uart_16550:: MmioSerialPort ;
@@ -178,7 +180,7 @@ unsafe extern "C" fn finalize(op: Operation) -> ! {
178
180
}
179
181
}
180
182
Operation :: SystemReset => {
181
- // TODO 等待其他核关闭
183
+ // TODO 等待其他硬件线程关闭
182
184
// 直接回 entry
183
185
_start ( )
184
186
}
@@ -241,23 +243,29 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
241
243
}
242
244
243
245
/// 类型化栈。
246
+ ///
247
+ /// 每个硬件线程拥有一个满足这样条件的内存块。
248
+ /// 这个内存块的底部放着硬件线程状态 [`HartContext`],顶部用于陷入处理,中间是这个硬件线程的栈空间。
249
+ /// 不需要 M 态线程,每个硬件线程只有这一个栈。
244
250
#[ repr( C , align( 128 ) ) ]
245
251
struct Stack ( [ u8 ; LEN_STACK_PER_HART ] ) ;
246
252
247
253
impl Stack {
248
254
/// 零初始化以避免加载。
249
255
const ZERO : Self = Self ( [ 0 ; LEN_STACK_PER_HART ] ) ;
250
256
257
+ /// 从栈上取出硬件线程状态。
258
+ #[ inline]
259
+ fn hart_context ( & mut self ) -> & mut HartContext {
260
+ unsafe { & mut * self . 0 . as_mut_ptr ( ) . cast ( ) }
261
+ }
262
+
251
263
fn load_as_stack ( & ' static mut self ) {
252
- let bottom = self . 0 . as_mut_ptr ( ) . cast ( ) ;
264
+ let ptr = unsafe { NonNull :: new_unchecked ( & mut self . hart_context ( ) . flow ) } ;
253
265
forget (
254
- FreeTrapStack :: new (
255
- StackRef ( self ) ,
256
- unsafe { NonNull :: new_unchecked ( bottom) } ,
257
- fast_handler,
258
- )
259
- . unwrap ( )
260
- . load ( ) ,
266
+ FreeTrapStack :: new ( StackRef ( self ) , ptr, fast_handler)
267
+ . unwrap ( )
268
+ . load ( ) ,
261
269
) ;
262
270
}
263
271
}
@@ -268,14 +276,14 @@ struct StackRef(&'static mut Stack);
268
276
impl AsRef < [ u8 ] > for StackRef {
269
277
#[ inline]
270
278
fn as_ref ( & self ) -> & [ u8 ] {
271
- & self . 0 . 0
279
+ & self . 0 . 0 [ size_of :: < HartContext > ( ) .. ]
272
280
}
273
281
}
274
282
275
283
impl AsMut < [ u8 ] > for StackRef {
276
284
#[ inline]
277
285
fn as_mut ( & mut self ) -> & mut [ u8 ] {
278
- & mut self . 0 . 0
286
+ & mut self . 0 . 0 [ size_of :: < HartContext > ( ) .. ]
279
287
}
280
288
}
281
289
@@ -287,6 +295,14 @@ impl Drop for StackRef {
287
295
}
288
296
}
289
297
298
+ #[ repr( C ) ]
299
+ struct HartContext {
300
+ flow : FlowContext ,
301
+ state : AtomicUsize ,
302
+ start_address : usize ,
303
+ opaque : usize ,
304
+ }
305
+
290
306
/// 特权软件信息。
291
307
#[ derive( Debug ) ]
292
308
struct Supervisor {
0 commit comments