Skip to content

Commit e41432e

Browse files
authored
Merge pull request #188 from DeathWish5/rv64
update toolchain
2 parents 06f1d5c + 453c58d commit e41432e

File tree

27 files changed

+614
-432
lines changed

27 files changed

+614
-432
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Cargo.lock
77
.idea
88
scripts/linux/test-result.txt
99
rusty-tags.vi
10+
*.img
11+
zCore/src/link_user.S

kernel-hal-bare/src/arch/riscv/interrupt.rs

Lines changed: 82 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
1+
use alloc::boxed::Box;
2+
use alloc::vec::Vec;
13
use riscv::register::{
2-
scause::{
3-
self,
4-
Trap,
5-
Exception,
6-
Interrupt,
7-
},
84
satp,
9-
sie,
10-
stval,
11-
sstatus,
5+
scause::{self, Exception, Interrupt, Trap},
6+
sie, sstatus, stval,
127
};
13-
use trapframe::{TrapFrame, UserContext};
14-
use alloc::boxed::Box;
15-
use alloc::vec::Vec;
168
use spin::Mutex;
9+
use trapframe::{TrapFrame, UserContext};
1710

1811
/*
1912
use crate::timer::{
20-
TICKS,
21-
clock_set_next_event,
13+
TICKS,
14+
clock_set_next_event,
2215
clock_close,
2316
};
2417
*/
2518

2619
//use crate::context::TrapFrame;
27-
use super::sbi;
2820
use super::plic;
21+
use super::sbi;
2922
use super::uart;
3023

31-
use crate::{putfmt, map_range, phys_to_virt};
3224
use super::consts::PHYSICAL_MEMORY_OFFSET;
3325
use super::timer_set_next;
26+
use crate::{map_range, phys_to_virt, putfmt};
3427

3528
//global_asm!(include_str!("trap.asm"));
3629

3730
/*
3831
#[repr(C)]
3932
pub struct TrapFrame{
40-
pub x: [usize; 32], //General registers
41-
pub sstatus: Sstatus,
42-
pub sepc: usize,
43-
pub stval: usize,
44-
pub scause: Scause,
33+
pub x: [usize; 32], //General registers
34+
pub sstatus: Sstatus,
35+
pub sepc: usize,
36+
pub stval: usize,
37+
pub scause: Scause,
4538
}
4639
*/
4740

@@ -52,27 +45,25 @@ lazy_static! {
5245
}
5346

5447
fn init_irq() {
55-
5648
init_irq_table();
5749
irq_add_handle(Timer, Box::new(super_timer)); //模拟参照了x86_64,把timer处理函数也放进去了
58-
//irq_add_handle(Keyboard, Box::new(keyboard));
50+
//irq_add_handle(Keyboard, Box::new(keyboard));
5951
irq_add_handle(S_PLIC, Box::new(plic::handle_interrupt));
6052
}
6153

62-
pub fn init(){
63-
unsafe{
64-
65-
sstatus::set_sie();
54+
pub fn init() {
55+
unsafe {
56+
sstatus::set_sie();
6657

6758
init_uart();
6859

6960
sie::set_sext();
7061
init_ext();
71-
}
62+
}
7263

7364
init_irq();
7465

75-
bare_println!("+++ setup interrupt +++");
66+
bare_println!("+++ setup interrupt +++");
7667
}
7768

7869
#[no_mangle]
@@ -83,20 +74,26 @@ pub extern "C" fn trap_handler(tf: &mut TrapFrame) {
8374
let is_int = scause.bits() >> 63;
8475
let code = scause.bits() & !(1 << 63);
8576

86-
match scause.cause() {
87-
Trap::Exception(Exception::Breakpoint) => breakpoint(&mut tf.sepc),
88-
Trap::Exception(Exception::IllegalInstruction) => panic!("IllegalInstruction: {:#x}->{:#x}", sepc, stval),
89-
Trap::Exception(Exception::LoadFault) => panic!("Load access fault: {:#x}->{:#x}", sepc, stval),
90-
Trap::Exception(Exception::StoreFault) => panic!("Store access fault: {:#x}->{:#x}", sepc, stval),
77+
match scause.cause() {
78+
Trap::Exception(Exception::Breakpoint) => breakpoint(&mut tf.sepc),
79+
Trap::Exception(Exception::IllegalInstruction) => {
80+
panic!("IllegalInstruction: {:#x}->{:#x}", sepc, stval)
81+
}
82+
Trap::Exception(Exception::LoadFault) => {
83+
panic!("Load access fault: {:#x}->{:#x}", sepc, stval)
84+
}
85+
Trap::Exception(Exception::StoreFault) => {
86+
panic!("Store access fault: {:#x}->{:#x}", sepc, stval)
87+
}
9188
Trap::Exception(Exception::LoadPageFault) => page_fault(stval, tf),
9289
Trap::Exception(Exception::StorePageFault) => page_fault(stval, tf),
9390
Trap::Exception(Exception::InstructionPageFault) => page_fault(stval, tf),
94-
Trap::Interrupt(Interrupt::SupervisorTimer) => super_timer(),
95-
Trap::Interrupt(Interrupt::SupervisorSoft) => super_soft(),
91+
Trap::Interrupt(Interrupt::SupervisorTimer) => super_timer(),
92+
Trap::Interrupt(Interrupt::SupervisorSoft) => super_soft(),
9693
Trap::Interrupt(Interrupt::SupervisorExternal) => plic::handle_interrupt(),
97-
//Trap::Interrupt(Interrupt::SupervisorExternal) => irq_handle(code as u8),
98-
_ => panic!("Undefined Trap: {:#x} {:#x}", is_int, code)
99-
}
94+
//Trap::Interrupt(Interrupt::SupervisorExternal) => irq_handle(code as u8),
95+
_ => panic!("Undefined Trap: {:#x} {:#x}", is_int, code),
96+
}
10097
}
10198

10299
fn init_irq_table() {
@@ -202,44 +199,47 @@ pub fn overwrite_handler(msi_id: u32, handle: Box<dyn Fn() + Send + Sync>) -> bo
202199
set
203200
}
204201

205-
fn breakpoint(sepc: &mut usize){
206-
bare_println!("Exception::Breakpoint: A breakpoint set @0x{:x} ", sepc);
202+
fn breakpoint(sepc: &mut usize) {
203+
bare_println!("Exception::Breakpoint: A breakpoint set @0x{:x} ", sepc);
207204

208-
//sepc为触发中断指令ebreak的地址
209-
//防止无限循环中断,让sret返回时跳转到sepc的下一条指令地址
210-
*sepc +=2
205+
//sepc为触发中断指令ebreak的地址
206+
//防止无限循环中断,让sret返回时跳转到sepc的下一条指令地址
207+
*sepc += 2
211208
}
212209

213-
fn page_fault(stval: usize, tf: &mut TrapFrame){
210+
fn page_fault(stval: usize, tf: &mut TrapFrame) {
214211
let this_scause = scause::read();
215-
info!("EXCEPTION Page Fault: {:?} @ {:#x}->{:#x}", this_scause.cause(), tf.sepc, stval);
212+
info!(
213+
"EXCEPTION Page Fault: {:?} @ {:#x}->{:#x}",
214+
this_scause.cause(),
215+
tf.sepc,
216+
stval
217+
);
216218
let vaddr = stval;
217219

218-
use riscv::paging::{Rv39PageTable, PageTableFlags as PTF, *};
219-
use riscv::addr::{Page, PhysAddr, VirtAddr};
220220
use crate::PageTableImpl;
221-
use kernel_hal::{PageTableTrait, MMUFlags};
221+
use kernel_hal::{MMUFlags, PageTableTrait};
222+
use riscv::addr::{Page, PhysAddr, VirtAddr};
223+
use riscv::paging::{PageTableFlags as PTF, Rv39PageTable, *};
222224

223225
//let mut flags = PTF::VALID;
224226
let code = this_scause.code();
225-
let mut flags =
226-
if code == 15 {
227-
//MMUFlags::WRITE ???
228-
MMUFlags::READ | MMUFlags::WRITE
229-
}else if code == 12 {
230-
MMUFlags::EXECUTE
231-
}else {
232-
MMUFlags::READ
233-
};
234-
235-
let linear_offset =
236-
if stval >= PHYSICAL_MEMORY_OFFSET {
237-
// Kernel
238-
PHYSICAL_MEMORY_OFFSET
239-
}else{
240-
// User
241-
0
242-
};
227+
let mut flags = if code == 15 {
228+
//MMUFlags::WRITE ???
229+
MMUFlags::READ | MMUFlags::WRITE
230+
} else if code == 12 {
231+
MMUFlags::EXECUTE
232+
} else {
233+
MMUFlags::READ
234+
};
235+
236+
let linear_offset = if stval >= PHYSICAL_MEMORY_OFFSET {
237+
// Kernel
238+
PHYSICAL_MEMORY_OFFSET
239+
} else {
240+
// User
241+
0
242+
};
243243

244244
/*
245245
let current =
@@ -248,16 +248,19 @@ fn page_fault(stval: usize, tf: &mut TrapFrame){
248248
map_range(&mut pt, vaddr, vaddr, linear_offset, flags);
249249
*/
250250

251-
let mut pti =
252-
PageTableImpl {
253-
root_paddr: satp::read().frame().start_address().as_usize(),
254-
};
251+
let mut pti = PageTableImpl {
252+
root_paddr: satp::read().frame().start_address().as_usize(),
253+
};
255254

256255
let page = Page::of_addr(VirtAddr::new(vaddr));
257256
if let Ok(pte) = pti.get().ref_entry(page) {
258257
let pte = unsafe { &mut *(pte as *mut PageTableEntry) };
259258
if !pte.is_unused() {
260-
debug!("PageAlreadyMapped -> {:#x?}, {:?}", pte.addr().as_usize(), pte.flags());
259+
debug!(
260+
"PageAlreadyMapped -> {:#x?}, {:?}",
261+
pte.addr().as_usize(),
262+
pte.flags()
263+
);
261264
//TODO update flags
262265

263266
pti.unmap(vaddr).unwrap();
@@ -266,16 +269,16 @@ fn page_fault(stval: usize, tf: &mut TrapFrame){
266269
pti.map(vaddr, vaddr - linear_offset, flags).unwrap();
267270
}
268271

269-
fn super_timer(){
272+
fn super_timer() {
270273
timer_set_next();
271274
super::timer_tick();
272275

273276
//bare_print!(".");
274277

275-
//发生外界中断时,epc的指令还没有执行,故无需修改epc到下一条
278+
//发生外界中断时,epc的指令还没有执行,故无需修改epc到下一条
276279
}
277280

278-
fn init_uart(){
281+
fn init_uart() {
279282
uart::Uart::new(0x1000_0000 + PHYSICAL_MEMORY_OFFSET).simple_init();
280283

281284
//但当没有SBI_CONSOLE_PUTCHAR时,却为什么不行?
@@ -295,7 +298,7 @@ pub fn try_process_serial() -> bool {
295298
}
296299
}
297300

298-
pub fn init_ext(){
301+
pub fn init_ext() {
299302
// Qemu virt
300303
// UART0 = 10
301304
plic::set_priority(10, 7);
@@ -305,24 +308,23 @@ pub fn init_ext(){
305308
bare_println!("+++ Setting up PLIC +++");
306309
}
307310

308-
fn super_soft(){
311+
fn super_soft() {
309312
sbi::clear_ipi();
310313
bare_println!("Interrupt::SupervisorSoft!");
311314
}
312315

313-
pub fn init_soft(){
316+
pub fn init_soft() {
314317
unsafe {
315318
sie::set_ssoft();
316319
}
317-
bare_println!("+++ setup soft int! +++");
320+
bare_println!("+++ setup soft int! +++");
318321
}
319322

320323
#[export_name = "fetch_trap_num"]
321324
pub fn fetch_trap_num(_context: &UserContext) -> usize {
322325
scause::read().bits()
323326
}
324327

325-
326328
pub fn wait_for_interrupt() {
327329
unsafe {
328330
// enable interrupt and disable

0 commit comments

Comments
 (0)