Fix phantom exception 528 during Cortex-M tail-chaining/return - #45
Open
PAException wants to merge 1 commit into
Open
Fix phantom exception 528 during Cortex-M tail-chaining/return#45PAException wants to merge 1 commit into
PAException wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes renode/renode#995
Description:
This PR fixes a bug in the ARM Cortex-M
tlibemulation where exception returns from unaligned stacks could cause the emulator to complete a phantom, invalid exception (oftenHardwareIRQ#512).The Issue:
During exception entry, if the stack is not 8-byte aligned, the Cortex-M hardware (and
tlib) correctly sets bit 9 (0x200=512) in the stackedxPSR. However, during certain exception returns (such as tail-chaining into aPendSVcontext switch triggered by FreeRTOS'sxQueueSendFromISR),tlibreads the returning exception number from the poppedxPSRbut fails to isolate the 9-bitIPSRfield. The stack alignment bit (0x200) leaks into theenv->v7m.exceptionvariable, causing the emulator to pass a corrupted exception number (like528) back to the C#NVIC.cs, which throws aTrying to complete not active IRQerror.The Fix:
Updated
helper.cto properly apply the0x1ffbitmask when extracting the exception number from the poppedxPSR. This correctly strips out the stack alignment bit and restricts the exception number to its valid 9-bit range, matching the behavior in the rest oftlib.Testing:
Ran a FreeRTOS firmware on an S32K144 simulation. Previously, calling
xQueueSendFromISRfrom within an active CAN interrupt would reliably crash the simulation withTrying to complete not active IRQ HardwareIRQ#512 (528). With this fix, the tail-chaining completes successfully and FreeRTOS context switches perfectly without throwing errors.