feat[libcpu][cortex-m4]: Detect interrupt context from IPSR on Cortex-M4`#11316
feat[libcpu][cortex-m4]: Detect interrupt context from IPSR on Cortex-M4`#11316wdfk-prog wants to merge 2 commits intoRT-Thread:masterfrom
Conversation
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
3c42e32 to
d6f326a
Compare
📌 Code Review Assignment🏷️ Tag: kernelReviewers: @GorrayLi @ReviewSun @hamburger-os @lianux-mm @wdfk-prog @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-04-06 09:11 CST)
📝 Review Instructions
|
There was a problem hiding this comment.
Pull request overview
This PR aims to make “interrupt context” detection on Cortex-M4 explicit (via IPSR), reduce direct scheduler coupling to rt_interrupt_nest, and complete Cortex-M4-side interrupt hook/IRQ-mask query support.
本 PR 目标是在 Cortex-M4 上显式化“中断上下文”识别(基于 IPSR),降低调度器对 rt_interrupt_nest 的直接耦合,并补齐 Cortex-M4 侧的中断 hook / 中断屏蔽状态查询能力。
Changes:
- Replace
src/scheduler_up.cdirect dependency onrt_interrupt_nestwithrt_interrupt_get_nest(). - Expose interrupt enter/leave hook function pointers for platform-side reuse.
- Add Cortex-M4 implementations for
rt_interrupt_enter/leave,rt_interrupt_get_nest(), andrt_hw_interrupt_is_disabled().
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
src/scheduler_up.c |
Switch interrupt-context checks/logging to rt_interrupt_get_nest() instead of rt_interrupt_nest. |
src/irq.c |
Make interrupt enter/leave hook pointers globally visible (non-static). |
libcpu/arm/cortex-m4/cpuport.c |
Add IPSR/PRIMASK helpers and Cortex-M4 overrides for IRQ enter/leave, nest query, and IRQ-disabled query. |
| "thread:%.*s(sp:0x%08x), " | ||
| "from thread:%.*s(sp: 0x%08x)", | ||
| rt_interrupt_nest, highest_ready_priority, | ||
| rt_interrupt_get_nest(), highest_ready_priority, |
There was a problem hiding this comment.
[custom/类别]: 🟡 [PR Title/PR 标题]: Missing or incorrect prefix format / 缺少或错误的前缀格式
English: PR title should follow format: [module][subsystem] Description in lowercase. Current title starts with feat and does not start with the bracketed prefix, which diverges from the repo’s PR title convention.
中文:PR 标题应遵循格式:小写的 [模块][子系统] 描述。当前标题以 feat 开头且未以方括号前缀开头,与仓库约定不一致。
| void (*rt_interrupt_enter_hook)(void); | ||
| void (*rt_interrupt_leave_hook)(void); |
There was a problem hiding this comment.
[maintainability/类别]: Exported hook function pointers lack a public declaration / 导出的 hook 函数指针缺少公开声明
English: rt_interrupt_enter_hook / rt_interrupt_leave_hook are now global symbols but are not declared in any header, forcing other translation units to redeclare them with extern (as done in cpuport.c). To avoid drift and keep types consistent, please add a single authoritative declaration in an appropriate internal header (guarded by RT_USING_HOOK && RT_HOOK_USING_FUNC_PTR), or alternatively keep the pointers private and expose a small rt_interrupt_enter_hook_call()/rt_interrupt_leave_hook_call() helper.
中文:rt_interrupt_enter_hook / rt_interrupt_leave_hook 现在变成了全局符号,但当前没有任何头文件声明它们,导致其他文件只能各自 extern 重复声明(如 cpuport.c)。为避免类型不一致与维护成本,建议在合适的内部头文件中提供唯一声明(并用 RT_USING_HOOK && RT_HOOK_USING_FUNC_PTR 保护),或保持指针私有并提供 rt_interrupt_enter_hook_call()/rt_interrupt_leave_hook_call() 之类的小封装接口。
| void (*rt_interrupt_enter_hook)(void); | |
| void (*rt_interrupt_leave_hook)(void); | |
| static void (*rt_interrupt_enter_hook)(void); | |
| static void (*rt_interrupt_leave_hook)(void); |
| rt_interrupt_get_nest(), highest_ready_priority, | ||
| RT_NAME_MAX, to_thread->parent.name, to_thread->sp, | ||
| RT_NAME_MAX, from_thread->parent.name, from_thread->sp); | ||
|
|
||
| RT_SCHEDULER_STACK_CHECK(to_thread); |
There was a problem hiding this comment.
[performance/类别]: Avoid calling rt_interrupt_get_nest() multiple times in rt_schedule / 避免在 rt_schedule 中重复调用 rt_interrupt_get_nest()
English: rt_interrupt_get_nest() can be relatively expensive on some ports (e.g., it may mask/restore IRQs to read an atomic counter). Here it’s called for logging; please cache the value in a local variable and reuse it for the subsequent ISR-context check.
中文:在部分架构上 rt_interrupt_get_nest() 可能会关/开中断以读取原子计数,代价不低。此处用于日志打印时已调用一次,建议缓存到局部变量并在后续 ISR 上下文判断时复用。
| void rt_interrupt_enter(void) | ||
| { | ||
| extern void (*rt_interrupt_enter_hook)(void); | ||
| RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,()); | ||
| LOG_D("irq has come..."); |
There was a problem hiding this comment.
[bug/类别]: rt_interrupt_enter() override does not increment interrupt nest counter / 覆盖实现的 rt_interrupt_enter() 未增加中断嵌套计数
English: The generic src/irq.c implementation increments rt_interrupt_nest before calling the enter hook. This Cortex-M4 override only calls the hook/log, so the nesting counter will never increase on Cortex-M4, breaking code/tests that rely on nesting depth. Please keep rt_interrupt_nest maintenance consistent (or avoid overriding if not needed).
中文:通用实现(src/irq.c)会在调用 enter hook 前递增 rt_interrupt_nest。当前 Cortex-M4 覆盖实现仅调用 hook/日志,导致 Cortex-M4 上嵌套计数不会增加,从而影响依赖嵌套深度的代码/测试。建议保持 rt_interrupt_nest 的维护语义一致(或如果无必要则不要覆盖该函数)。
| { | ||
| extern void (*rt_interrupt_leave_hook)(void); |
There was a problem hiding this comment.
[bug/类别]: rt_interrupt_leave() override does not decrement interrupt nest counter / 覆盖实现的 rt_interrupt_leave() 未减少中断嵌套计数
English: The generic src/irq.c implementation decrements rt_interrupt_nest on leave. This Cortex-M4 override doesn’t update the counter, so rt_interrupt_nest becomes inconsistent on Cortex-M4 builds. Please decrement the nest counter (and keep hook call order consistent with the generic implementation) or avoid overriding.
中文:通用实现(src/irq.c)在 leave 时会递减 rt_interrupt_nest。当前 Cortex-M4 覆盖实现未更新该计数,导致 Cortex-M4 上 rt_interrupt_nest 失真。建议补齐递减逻辑(并保持 hook 调用顺序与通用实现一致),或避免覆盖实现。
| { | |
| extern void (*rt_interrupt_leave_hook)(void); | |
| { | |
| extern rt_uint8_t rt_interrupt_nest; | |
| extern void (*rt_interrupt_leave_hook)(void); | |
| rt_interrupt_nest--; |
| rt_uint8_t rt_interrupt_get_nest(void) | ||
| { | ||
| return (__get_IPSR() != 0); | ||
| } |
There was a problem hiding this comment.
[bug/类别]: rt_interrupt_get_nest() changed from “nesting depth” to boolean / rt_interrupt_get_nest() 从“嵌套深度”变为布尔语义
English: The API contract is “the number of nested interrupts”, but this implementation returns only 0/1 based on IPSR. This can break callers that expect depth > 1 (e.g., IRQ nesting tests or logic distinguishing nested vs first-level ISR). Please keep rt_interrupt_get_nest() returning the real nesting count; add a separate fast helper for “in ISR context” if needed.
中文:该 API 约定是“中断嵌套次数”,但当前实现仅基于 IPSR 返回 0/1。这样会破坏依赖嵌套深度 > 1 的调用方(例如中断嵌套测试或需要区分是否发生嵌套的逻辑)。建议保持 rt_interrupt_get_nest() 返回真实嵌套计数;若需要快速判断 ISR 上下文,可新增独立接口。
| rt_inline rt_uint32_t __get_IPSR(void) | ||
| { | ||
| #if defined(__CC_ARM) | ||
| register uint32_t __regIPSR __asm("ipsr"); | ||
| return(__regIPSR); |
There was a problem hiding this comment.
[best_practices/类别]: Avoid defining __get_IPSR (reserved/CMSIS name) in RT-Thread code / 避免在 RT-Thread 代码中定义 __get_IPSR(保留标识符/CMSIS 重名)
English: __get_IPSR is commonly provided by CMSIS and the leading __ identifier is reserved by the C standard. Defining it here risks redefinition conflicts depending on BSP/CMSIS include order. Prefer using the CMSIS intrinsic (include the proper CMSIS header) or rename this helper to an RT-Thread-specific name.
中文:__get_IPSR 通常由 CMSIS 提供,且以 __ 开头的标识符属于 C 标准保留名称。在此处定义可能因 BSP/CMSIS 头文件包含顺序导致重定义冲突。建议直接使用 CMSIS 的实现(包含对应头文件),或将本地辅助函数改为 RT-Thread 风格命名。
| #if defined(__CC_ARM) | ||
| register uint32_t __regIPSR __asm("ipsr"); | ||
| return(__regIPSR); | ||
| #elif defined(__clang__) | ||
| uint32_t result; |
There was a problem hiding this comment.
[bug/类别]: __get_IPSR() lacks a final #else/#error or return path / __get_IPSR() 缺少最终 #else/#error 或返回路径
English: This #if/#elif chain has no final #else branch. If none of the toolchain macros match, the function may compile without a return (UB) or fail under Werror. Please add a final #else with #error (preferred) or a safe fallback return.
中文:该 #if/#elif 分支没有最终的 #else。若工具链宏都不匹配,可能出现“非 void 函数未返回”的未定义行为,或在 Werror 下编译失败。建议补充最终 #else:给出 #error(推荐)或安全兜底返回值。
| #if defined(__CC_ARM) | ||
| register uint32_t __regPRIMASK __asm("primask"); | ||
| return (__regPRIMASK); | ||
| #elif defined(__clang__) | ||
| uint32_t result; |
There was a problem hiding this comment.
[bug/类别]: rt_hw_get_primask_value() lacks a final #else/#error or return path / rt_hw_get_primask_value() 缺少最终 #else/#error 或返回路径
English: Like __get_IPSR(), this helper has no final #else branch. Please add a final #else with #error (preferred) or a safe fallback return to avoid undefined behavior / build failures on unexpected toolchains.
中文:与 __get_IPSR() 类似,该辅助函数也缺少最终 #else 分支。建议补充最终 #else:使用 #error(推荐)或安全兜底返回值,以避免在非预期工具链下出现未定义行为/编译失败。
拉取/合并请求描述:(PR description)
为什么提交这份 PR (why to submit this PR)
当前补丁的核心目标,是将 Cortex-M4 平台上的中断上下文识别与相关 hook 调用路径显式化,并减少调度器对外部中断嵌套计数变量的直接耦合。
现有实现中,
scheduler_up.c直接依赖rt_interrupt_nest判断当前是否处于中断上下文;而在 Cortex-M4 平台上,这类信息本身可以直接通过IPSR寄存器判断。与此同时,本补丁还补充了rt_interrupt_enter()/rt_interrupt_leave()的平台侧实现,以及中断屏蔽状态查询接口rt_hw_interrupt_is_disabled(),使 Cortex-M4 平台上的中断相关行为更完整、更清晰。因此提交本 PR,以完善 Cortex-M4 架构下的中断上下文识别能力、补齐中断 enter/leave hook 调用链,并将调度器中的中断态判断改为通过统一接口获取,降低对内部变量的直接依赖。
你的解决方案是什么 (what is your solution)
本 PR 主要做了以下修改:
补充 Cortex-M4 中断上下文识别能力
libcpu/arm/cortex-m4/cpuport.c中新增__get_IPSR();IPSR实现rt_interrupt_get_nest();补充 Cortex-M4 中断 enter/leave 路径
cpuport.c中新增rt_interrupt_enter();cpuport.c中新增rt_interrupt_leave();rt_interrupt_enter_hookrt_interrupt_leave_hook暴露中断 hook 指针供平台侧使用
src/irq.c中,将:rt_interrupt_enter_hookrt_interrupt_leave_hook从文件内
static变量改为全局可见符号;增加中断屏蔽状态查询接口
cpuport.c中新增rt_hw_get_primask_value();PRIMASK实现rt_hw_interrupt_is_disabled();调整调度器中的中断态判断方式
src/scheduler_up.c中移除对rt_interrupt_nest的直接 extern 依赖;rt_interrupt_get_nest();请提供验证的 BSP 和 config (provide the config and bsp)
BSP:
bsp/stm32/stm32f407-atk-explorer.config:
CONFIG_ARCH_ARM_CORTEX_M4CONFIG_RT_USING_HOOK验证方式:
rt_interrupt_get_nest()返回 0;rt_interrupt_get_nest()返回非 0;rt_interrupt_enter()/rt_interrupt_leave()能正常触发 hook;rt_schedule()在中断上下文下不会走线程态切换路径;rt_hw_interrupt_is_disabled()在关中断/开中断场景下返回值符合预期。action:
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up