gpt4 book ai didi

stack - ISR(中断服务程序)是否有单独的堆栈?

转载 作者:行者123 更新时间:2023-12-04 19:48:36 25 4
gpt4 key购买 nike

使用 RTOS(前 FreeRTOS)时,我们为每个线程提供单独的堆栈空间。那么ISR(中断服务程序)呢,它们在内存中是否有单独的堆栈?或者这是可配置的?

如果他们没有一个堆栈来存储 ISR 中声明的局部变量?

最佳答案

我有完全相同的问题,大量搜索使我得出这个结论:答案取决于您的芯片以及您使用的操作系统如何配置该芯片。

因此,看看我最喜欢的 ARM Cortex-M3 芯片之一(中断是一种异常形式),各个位置的文档如下:

Operating Modes

The Cortex-M3 supports Privileged and User (non-privileged) execution. Code run as Privileged has full access rights whereas code executed as User has limited access rights. The limitations include restrictions on instruction use such as MSR fields, access to memory and peripherals based on system design, and restrictions imposed by the MPU configuration.

The processor supports two operation modes, Thread mode and Handler mode. Thread mode is entered on reset and normally on return from an exception. When in Thread mode, code can be executed as either Privileged or Unprivileged.

Handler mode will be entered as a result of an exception. Code in Handler mode is always executed as Privileged, therefore the core will automatically switch to Privileged mode when exceptions occur. You can change between Privileged Thread mode and User Thread mode when returning from an exception by modifying the EXC_RETURN value in the link register (R14). You can also change from Privileged Thread to User Thread mode by clearing CONTROL[0] using an MSR instruction. However, you cannot directly change to privileged mode from unprivileged mode without going through an exception, for example an SVC.

Main and Process Stacks

The Cortex-M3 supports two different stacks, a main stack and a process stack. To support this the Cortex-M3 has two stack pointers (R13). One of these is banked out depending on the stack in use. This means that only one stack pointer at a time is visible as R13. However, both stack pointers can be accessed using the MRS and MSR instructions. The main stack is used at reset, and is always used in Handler mode (when entering an exception handler). The process stack pointer is only available as the current stack pointer when in Thread mode. You can select which stack pointer (main or process) is used in Thread mode in one of two ways, either by using the EXC_RETURN value when exiting from Handler Mode or while in Thread Mode by writing to CONTROL[1] using an MSR instruction.



和...

When the processor takes an exception, unless the exception is a tail-chained or a late-arriving exception, the processor pushes information onto the current stack. This operation is referred to as stacking and the structure of eight data words is referred as the stack frame. ...

Immediately after stacking, the stack pointer indicates the lowest address in the stack frame



来自“ARM Cortex-M3权威指南”一书:

The MSP, also called SP_main in ARM documentation, is the default SP after power-up; it is used by kernel code and exception handlers. The PSP, or SP_process in ARM documentation, is typically used by thread processes in system with embedded OS running.

Because exception handlers always use the Main Stack Pointer, the main stack memory should contain enough space for the largest number of nesting interrupts.

When an exception takes place, the registers R0–R3, R12, LR, PC, and Program Status (PSR) are pushed to the stack. If the code that is running uses the Process Stack Pointer (PSP), the process stack will be used; if the code that is running uses the Main Stack Pointer (MSP), the main stack will be used. Afterward, the main stack will always be used during the handler, so all nested interrupts will use the main stack.



2017 年 6 月更新:

我之前的答案是不正确的,我分析了皮质处理器的 FreeRTOS 并将我的答案改写为:

Cortex-M3 的标准 FreeRTOS 版本实际上配置和使用 MSP 和 PSP。当第一个任务运行时,它修改 MSP 以指向向量表中指定的第一个地址(0x00000000),这往往是 SRAM 中的最后一个字,然后它触发系统调用,在它设置的系统调用异常处理程序中PSP 到下一个任务堆栈位置,然后它修改异常 LR 值,以便“返回线程模式并在返回时使用进程堆栈”。

这意味着中断服务程序(AKA 异常处理程序)堆栈从向量表中指定的地址开始向下增长。

您可以配置您的链接器和启动代码以在您喜欢的任何位置定位异常处理程序堆栈,确保您的堆或其他内存区域不与异常处理程序区域重叠并确保该区域足够大。

其他芯片和操作系统的答案可能完全不同!

关于stack - ISR(中断服务程序)是否有单独的堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35179883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com