Skip to content

Commit 7e177b3

Browse files
committed
fix: 正确实现 timer interrupt delegate
Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent 4284643 commit 7e177b3

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

rustsbi-qemu/src/clint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ impl Timer for Clint {
5555
#[inline]
5656
fn set_timer(&self, time_value: u64) {
5757
unsafe {
58+
riscv::register::mip::clear_stimer();
59+
riscv::register::mie::set_mtimer();
5860
((self.base as *mut u8).offset(0x4000) as *mut u64)
5961
.add(hart_id())
6062
.write_volatile(time_value);

test-kernel/src/main.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use core::arch::asm;
88
use riscv::register::{
9-
scause::Trap,
9+
scause::{Interrupt, Trap},
1010
sepc,
1111
stvec::{self, TrapMode},
1212
};
@@ -84,7 +84,10 @@ extern "C" fn primary_rust_main(hartid: usize, dtb_pa: usize) -> ! {
8484
test::sbi_ins_emulation();
8585

8686
unsafe { stvec::write(start_trap as usize, TrapMode::Direct) };
87-
test::trap_delegate(hartid);
87+
test::trap_execption_delegate(hartid);
88+
89+
unsafe { riscv::register::sstatus::set_sie() };
90+
test::trap_interrupt_delegate(hartid);
8891

8992
test::hsm(hartid, smp);
9093

@@ -97,9 +100,16 @@ extern "C" fn rust_trap_exception(trap_frame: &mut TrapFrame) {
97100

98101
let cause = scause::read().cause();
99102
let expected = unsafe { core::mem::take(&mut EXPECTED[trap_frame.tp]) };
100-
101103
if Some(cause) == expected {
102-
sepc::write(sepc::read().wrapping_add(4));
104+
match cause {
105+
Trap::Exception(_) => {
106+
sepc::write(sepc::read().wrapping_add(4));
107+
}
108+
Trap::Interrupt(Interrupt::SupervisorTimer) => {
109+
sbi_rt::set_timer(u64::MAX);
110+
}
111+
_ => {}
112+
}
103113
} else {
104114
panic!("[test-kernel] SBI test FAILED due to unexpected trap {cause:?}");
105115
}

test-kernel/src/test.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
pub(crate) fn base_extension() {
1+
use crate::EXPECTED;
2+
3+
pub(crate) fn base_extension() {
24
println!(
35
"
46
[test-kernel] Testing base extension"
@@ -55,8 +57,7 @@ pub(crate) fn sbi_ins_emulation() {
5557
}
5658
}
5759

58-
pub(crate) fn trap_delegate(hartid: usize) {
59-
use crate::EXPECTED;
60+
pub(crate) fn trap_execption_delegate(hartid: usize) {
6061
use core::arch::asm;
6162
use riscv::register::scause::{Exception, Trap};
6263

@@ -78,6 +79,31 @@ pub(crate) fn trap_delegate(hartid: usize) {
7879
);
7980
}
8081

82+
pub(crate) fn trap_interrupt_delegate(hartid: usize) {
83+
use core::arch::asm;
84+
use riscv::register::{
85+
scause::{Interrupt, Trap},
86+
sie, time,
87+
};
88+
89+
println!(
90+
"
91+
[test-kernel] Testing trap delegate
92+
[test-kernel] Set timer +1s"
93+
);
94+
unsafe {
95+
sie::set_stimer();
96+
EXPECTED[hartid] = Some(Trap::Interrupt(Interrupt::SupervisorTimer));
97+
}
98+
sbi_rt::set_timer(time::read64() + (10 << 20));
99+
100+
unsafe { riscv::asm::wfi() };
101+
println!(
102+
"\
103+
[test-kernel] Timer interrupt delegate success"
104+
);
105+
}
106+
81107
/// 所有副核:启动 -> 不可恢复休眠 -> 唤醒 -> 可恢复休眠 -> 唤醒 -> 关闭。
82108
pub(crate) fn hsm(hartid: usize, smp: usize) {
83109
use sbi_rt::SbiRet;

0 commit comments

Comments
 (0)