You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust编译优化移除riscv-rt中断/异常处理程序问题求助

RISC-V中断处理程序被优化移除问题及可行实现示例请求

我正在基于riscv-rt 0.14.0为riscv32imac目标开发简单应用,需要实现若干外部中断处理程序。目前riscv-rt的启动代码(包括寄存器初始化、栈指针设置、_setup_interrupts等)运行正常,甚至可以重载自定义的_setup_interrupts实现。

但遇到一个问题:无论采用哪种方式,自定义的中断/异常处理程序都会被编译器优化移除。编译生成的汇编中能看到InstructionMisaligned处理程序的代码,但通过cargo objdump -- -d > dump.txt生成的反汇编文件里却找不到该内容。向量模式和直接模式下都无法正常安装处理程序。

现请求提供可行的实现示例,以下是我当前的代码、汇编片段及memory.x配置:

当前Rust代码

#![no_std]
#![no_main]

use core::panic::PanicInfo;
use riscv::interrupt::Exception;

#[panic_handler]
unsafe fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

#[riscv_rt::exception(Exception::InstructionMisaligned)]
fn misaligned_handler(_trap_frame: &riscv_rt::TrapFrame) -> ! {
    loop {
        riscv::asm::wfi();
        riscv::asm::nop();
    }
}

#[riscv_rt::entry]
fn main() -> ! {
    loop {}
}

编译生成的汇编片段

.section    .text.InstructionMisaligned,"ax",@progbits
        .globl  InstructionMisaligned
        .p2align    1
        .type   InstructionMisaligned,@function
InstructionMisaligned:
.Lfunc_begin1:
        .cfi_startproc
.LBB1_1:
        .file   2 "/u/valeriyk/slowfs_home/.cargo/registry/src/index.crates.io-6f17d22bba15001f/riscv-0.13.0/src" "asm.rs"
        .loc    2 19 22 prologue_end
        #APP
        wfi
        #NO_APP
.Ltmp1:
        .loc    2 19 22 is_stmt 0
        #APP
        nop
        #NO_APP
.Ltmp2:
        .loc    1 16 9 is_stmt 1
        j   .LBB1_1
.Ltmp3:
.Lfunc_end1:
        .size   InstructionMisaligned, .Lfunc_end1-InstructionMisaligned
        .cfi_endproc

memory.x配置

/* Fully featured memory.x file */
MEMORY
{
  ICCM0 : ORIGIN = 0x0, LENGTH = 256K
  DCCM  : ORIGIN = 0x80000000, LENGTH = 248K
  DCCM_STACK : ORIGIN = 0x8003E000, LENGTH = 8K
}

REGION_ALIAS("REGION_TEXT", ICCM0);
REGION_ALIAS("REGION_RODATA", ICCM0);
REGION_ALIAS("REGION_DATA", DCCM);
REGION_ALIAS("REGION_BSS", DCCM);
REGION_ALIAS("REGION_HEAP", DCCM);
REGION_ALIAS("REGION_STACK", DCCM_STACK);

_max_hart_id = 1;                               /* Two harts present */
_hart_stack_size = 1K;                          /* Set stack size per hart to 1KB */
_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK);
_heap_size = 1K;
_vector_table = ORIGIN(REGION_TEXT) + 0x12340;
_start_trap = ORIGIN(REGION_TEXT) + 0x12340;

内容的提问来源于stack exchange,提问作者Valeriy Kazantsev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 21:33:18