Skip to content

Conversation

@Tang-Haojin
Copy link
Contributor

@Tang-Haojin Tang-Haojin commented Sep 2, 2025

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

观察到开启编译优化时,中断只能触发一次。定位到是由于对 plic 的 claim/complete 寄存器的操作没有被标记为 volatile,导致编译器优化掉了对 mmio 寄存器的写。

  • 问题代码序列
void plic_handle_irq(void)
{
    int plic_irq = plic_claim();
    plic_complete(plic_irq);
    irq_desc[plic_irq].handler(plic_irq, irq_desc[plic_irq].param);
}
  • 反汇编
00000000802264e6 <plic_handle_irq>:
    802264e6:	0001e797          	auipc	a5,0x1e
    802264ea:	b627a783          	lw	a5,-1182(a5) # 80244048 <__text_end>
    802264ee:	8181b683          	ld	a3,-2024(gp) # 80245720 <plic_base>
    802264f2:	00201737          	lui	a4,0x201
    802264f6:	00d7979b          	slliw	a5,a5,0xd
    802264fa:	97b6                	add	a5,a5,a3
    802264fc:	97ba                	add	a5,a5,a4
    802264fe:	43c8                	lw	a0,4(a5)
    80226500:	00037797          	auipc	a5,0x37
    80226504:	69078793          	addi	a5,a5,1680 # 8025db90 <irq_desc>
    80226508:	00451713          	slli	a4,a0,0x4
    8022650c:	97ba                	add	a5,a5,a4
    8022650e:	6398                	ld	a4,0(a5)
    80226510:	678c                	ld	a1,8(a5)
    80226512:	8702                	jr	a4

可以看到,对寄存器的写(sw)操作被优化掉了。

你的解决方案是什么 (what is your solution)

使用 readl()writel() 对 mmio 寄存器进行操作。

  • 修改后的反汇编
00000000802264e6 <plic_handle_irq>:
    802264e6:	0001e697          	auipc	a3,0x1e
    802264ea:	b7a68693          	addi	a3,a3,-1158 # 80244060 <__text_end>
    802264ee:	81818613          	addi	a2,gp,-2024 # 80245738 <plic_base>
    802264f2:	4288                	lw	a0,0(a3)
    802264f4:	621c                	ld	a5,0(a2)
    802264f6:	00201737          	lui	a4,0x201
    802264fa:	0711                	addi	a4,a4,4 # 201004 <__text_size+0x1bcfa4>
    802264fc:	97ba                	add	a5,a5,a4
    802264fe:	00d5151b          	slliw	a0,a0,0xd
    80226502:	953e                	add	a0,a0,a5
    80226504:	4108                	lw	a0,0(a0)
    80226506:	2501                	sext.w	a0,a0
    80226508:	0820000f          	fence	i,r
    8022650c:	4294                	lw	a3,0(a3)
    8022650e:	0140000f          	fence	w,o
    80226512:	621c                	ld	a5,0(a2)
    80226514:	00d6969b          	slliw	a3,a3,0xd
    80226518:	97ba                	add	a5,a5,a4
    8022651a:	97b6                	add	a5,a5,a3
    8022651c:	c388                	sw	a0,0(a5)
    8022651e:	00037797          	auipc	a5,0x37
    80226522:	6b278793          	addi	a5,a5,1714 # 8025dbd0 <irq_desc>
    80226526:	00451713          	slli	a4,a0,0x4
    8022652a:	97ba                	add	a5,a5,a4
    8022652c:	6398                	ld	a4,0(a5)
    8022652e:	678c                	ld	a1,8(a5)
    80226530:	8702                	jr	a4

请提供验证的bsp和config (provide the config and bsp)

  • BSP: bsp/qemu-virt64-riscv
  • .config: 无变化
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions github-actions bot added BSP Arch: RISC-V BSP related with risc-v libcpu labels Sep 2, 2025
@github-actions
Copy link

github-actions bot commented Sep 2, 2025

📌 Code Review Assignment

🏷️ Tag: libcpu_riscv

Reviewers: @Yaochenger

Changed Files (Click to expand)
  • libcpu/risc-v/virt64/plic.c

📊 Current Review Status (Last Updated: 2025-09-02 22:16 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@Rbb666 Rbb666 merged commit 893ae7d into RT-Thread:master Sep 3, 2025
61 checks passed
@Rbb666 Rbb666 added this to the v5.2.2 milestone Sep 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: RISC-V BSP related with risc-v BSP libcpu

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants