Skip to content

Commit 0c82b09

Browse files
committed
feat: 设计每个硬件线程的栈布局
Signed-off-by: YdrMaster <[email protected]>
1 parent 25d45c2 commit 0c82b09

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

rustsbi-qemu/src/main.rs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ mod qemu_test;
1313
mod constants {
1414
/// 特权软件入口。
1515
pub(crate) const SUPERVISOR_ENTRY: usize = 0x8020_0000;
16-
/// 每个核设置 16KiB 栈空间。
16+
/// 每个硬件线程设置 16KiB 栈空间。
1717
pub(crate) const LEN_STACK_PER_HART: usize = 16 * 1024;
18-
/// qemu-virt 最多 8
18+
/// qemu-virt 最多 8 个硬件线程
1919
pub(crate) const NUM_HART_MAX: usize = 8;
2020
}
2121

@@ -25,13 +25,15 @@ extern crate rcore_console;
2525
use constants::*;
2626
use core::{
2727
convert::Infallible,
28-
mem::{forget, MaybeUninit},
28+
mem::{forget, size_of, MaybeUninit},
2929
ptr::NonNull,
30-
sync::atomic::{AtomicBool, Ordering::AcqRel},
30+
sync::atomic::{AtomicBool, AtomicUsize, Ordering::AcqRel},
3131
};
3232
use device_tree::BoardInfo;
3333
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+
};
3537
use rustsbi::RustSBI;
3638
use spin::{Mutex, Once};
3739
use uart_16550::MmioSerialPort;
@@ -178,7 +180,7 @@ unsafe extern "C" fn finalize(op: Operation) -> ! {
178180
}
179181
}
180182
Operation::SystemReset => {
181-
// TODO 等待其他核关闭
183+
// TODO 等待其他硬件线程关闭
182184
// 直接回 entry
183185
_start()
184186
}
@@ -241,23 +243,29 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
241243
}
242244

243245
/// 类型化栈。
246+
///
247+
/// 每个硬件线程拥有一个满足这样条件的内存块。
248+
/// 这个内存块的底部放着硬件线程状态 [`HartContext`],顶部用于陷入处理,中间是这个硬件线程的栈空间。
249+
/// 不需要 M 态线程,每个硬件线程只有这一个栈。
244250
#[repr(C, align(128))]
245251
struct Stack([u8; LEN_STACK_PER_HART]);
246252

247253
impl Stack {
248254
/// 零初始化以避免加载。
249255
const ZERO: Self = Self([0; LEN_STACK_PER_HART]);
250256

257+
/// 从栈上取出硬件线程状态。
258+
#[inline]
259+
fn hart_context(&mut self) -> &mut HartContext {
260+
unsafe { &mut *self.0.as_mut_ptr().cast() }
261+
}
262+
251263
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) };
253265
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(),
261269
);
262270
}
263271
}
@@ -268,14 +276,14 @@ struct StackRef(&'static mut Stack);
268276
impl AsRef<[u8]> for StackRef {
269277
#[inline]
270278
fn as_ref(&self) -> &[u8] {
271-
&self.0 .0
279+
&self.0 .0[size_of::<HartContext>()..]
272280
}
273281
}
274282

275283
impl AsMut<[u8]> for StackRef {
276284
#[inline]
277285
fn as_mut(&mut self) -> &mut [u8] {
278-
&mut self.0 .0
286+
&mut self.0 .0[size_of::<HartContext>()..]
279287
}
280288
}
281289

@@ -287,6 +295,14 @@ impl Drop for StackRef {
287295
}
288296
}
289297

298+
#[repr(C)]
299+
struct HartContext {
300+
flow: FlowContext,
301+
state: AtomicUsize,
302+
start_address: usize,
303+
opaque: usize,
304+
}
305+
290306
/// 特权软件信息。
291307
#[derive(Debug)]
292308
struct Supervisor {

0 commit comments

Comments
 (0)