|
359 | 359 | #![no_std]
|
360 | 360 | #![deny(missing_docs)]
|
361 | 361 |
|
362 |
| -use riscv::register::{mcause, mhartid, scause}; |
| 362 | +#[cfg(feature = "s-mode")] |
| 363 | +use riscv::register::{scause as xcause, stvec as xtvec, stvec::TrapMode as xTrapMode}; |
| 364 | + |
| 365 | +#[cfg(not(feature = "s-mode"))] |
| 366 | +use riscv::register::{mcause as xcause, mhartid, mtvec as xtvec, mtvec::TrapMode as xTrapMode}; |
| 367 | + |
363 | 368 | pub use riscv_rt_macros::{entry, pre_init};
|
364 | 369 |
|
365 | 370 | #[export_name = "error: riscv-rt appears more than once in the dependency graph"]
|
@@ -400,12 +405,10 @@ pub unsafe extern "C" fn start_rust(a0: usize, a1: usize, a2: usize) -> ! {
|
400 | 405 | }
|
401 | 406 |
|
402 | 407 | // sbi passes hartid as first parameter (a0)
|
403 |
| - let hartid: usize; |
404 |
| - if cfg!(feature = "s-mode") { |
405 |
| - hartid = a0; |
406 |
| - } else { |
407 |
| - hartid = mhartid::read(); |
408 |
| - } |
| 408 | + #[cfg(feature = "s-mode")] |
| 409 | + let hartid = a0; |
| 410 | + #[cfg(not(feature = "s-mode"))] |
| 411 | + let hartid = mhartid::read(); |
409 | 412 |
|
410 | 413 | if _mp_hook(hartid) {
|
411 | 414 | __pre_init();
|
@@ -458,24 +461,13 @@ pub extern "C" fn start_trap_rust(trap_frame: *const TrapFrame) {
|
458 | 461 | }
|
459 | 462 |
|
460 | 463 | unsafe {
|
461 |
| - let code: usize; |
462 |
| - let is_exception: bool; |
| 464 | + let cause = xcause::read(); |
463 | 465 |
|
464 |
| - if cfg!(feature = "s-mode") { |
465 |
| - let cause = scause::read(); |
466 |
| - is_exception = cause.is_exception(); |
467 |
| - code = cause.code(); |
468 |
| - } else { |
469 |
| - let cause = mcause::read(); |
470 |
| - is_exception = cause.is_exception(); |
471 |
| - code = cause.code(); |
472 |
| - } |
473 |
| - |
474 |
| - if is_exception { |
| 466 | + if cause.is_exception() { |
475 | 467 | ExceptionHandler(&*trap_frame)
|
476 | 468 | } else {
|
477 |
| - if code < __INTERRUPTS.len() { |
478 |
| - let h = &__INTERRUPTS[code]; |
| 469 | + if cause.code() < __INTERRUPTS.len() { |
| 470 | + let h = &__INTERRUPTS[cause.code()]; |
479 | 471 | if h.reserved == 0 {
|
480 | 472 | DefaultHandler();
|
481 | 473 | } else {
|
@@ -601,11 +593,5 @@ pub unsafe extern "Rust" fn default_setup_interrupts() {
|
601 | 593 | fn _start_trap();
|
602 | 594 | }
|
603 | 595 |
|
604 |
| - if cfg!(feature = "s-mode") { |
605 |
| - use riscv::register::stvec::{self, TrapMode}; |
606 |
| - stvec::write(_start_trap as usize, TrapMode::Direct); |
607 |
| - } else { |
608 |
| - use riscv::register::mtvec::{self, TrapMode}; |
609 |
| - mtvec::write(_start_trap as usize, TrapMode::Direct); |
610 |
| - } |
| 596 | + xtvec::write(_start_trap as usize, xTrapMode::Direct); |
611 | 597 | }
|
0 commit comments