ARMv7 Exception Handling: Preferred Return Address and Link Register (LR) Calculation

When dealing with exception handling in ARMv7-A and ARMv7-R architectures, one of the most critical aspects is determining the correct return address after an exception has been serviced. The return address is the location in memory where the processor should resume execution once the exception handler has completed its task. This address is crucial for ensuring that the program continues to execute correctly after an interrupt or other exception.

In ARM architectures, the return address is typically stored in the Link Register (LR). However, calculating the correct value for the LR can be confusing, especially when considering the differences between the ARM and Thumb instruction sets, as well as the specific behavior of the processor during exception entry and exit.

The ARM Architecture Reference Manual for ARMv7-A and ARMv7-R provides detailed pseudocode and descriptions for calculating the return address. However, discrepancies can arise when interpreting this information, particularly when comparing the preferred return address method with the pseudocode provided in the manual. This guide will delve into the intricacies of these calculations, clarify common misunderstandings, and provide a comprehensive approach to determining the correct return address.

Preferred Return Address vs. Link Register (LR) Calculation Discrepancies

The primary confusion arises from the two different methods described in the ARM Architecture Reference Manual for calculating the return address. The first method involves determining the preferred return address, which is the address of the next instruction to execute after the exception. The second method involves using pseudocode to calculate the LR based on the Program Counter (PC) value at the time of the exception.

Preferred Return Address Calculation

The preferred return address is the address of the next instruction that would have been executed had the exception not occurred. For example, if the processor is executing an instruction at address 0x00001000 and an IRQ is taken, the preferred return address would be:

  • Thumb Instruction Set State: 0x00001002 (since Thumb instructions are 2 bytes long).
  • ARM Instruction Set State: 0x00001004 (since ARM instructions are 4 bytes long).

This preferred return address is then used to calculate the value of the LR, which is typically set to the preferred return address plus an offset. The offset depends on the instruction set state when the exception was taken:

  • Thumb Instruction Set State: LR = Preferred Return Address + 4 = 0x00001002 + 4 = 0x00001006.
  • ARM Instruction Set State: LR = Preferred Return Address + 4 = 0x00001004 + 4 = 0x00001008.

Pseudocode-Based LR Calculation

The ARM Architecture Reference Manual also provides pseudocode for calculating the LR based on the PC value at the time of the exception. According to the pseudocode:

  • Thumb Instruction Set State: LR = PC – 0 = 0x00001004 - 0 = 0x00001004.
  • ARM Instruction Set State: LR = PC – 4 = 0x00001008 - 4 = 0x00001004.

Here, the PC value used in the pseudocode is the address of the instruction that was being executed when the exception was taken, adjusted by the pipeline offset. This adjustment is necessary because the PC value in ARM architectures typically points to the instruction being fetched, not the one being executed.

Discrepancy Analysis

The discrepancy between the two methods arises from the different contexts in which they are used. The preferred return address method is used to determine the address where execution should resume after the exception, while the pseudocode-based LR calculation reflects the internal behavior of the processor during exception entry.

The key takeaway is that the preferred return address method is what software should use to determine the correct return address, while the pseudocode-based LR calculation is more relevant to understanding the internal workings of the processor. In practice, software should use the preferred return address method and adjust the LR accordingly.

Correct Approach to Calculating the Final Return Address

To correctly calculate the final return address after an exception, follow these steps:

  1. Determine the Preferred Return Address: Identify the address of the next instruction that would have been executed had the exception not occurred. This is the preferred return address.

    • Thumb Instruction Set State: Preferred Return Address = Current Instruction Address + 2.
    • ARM Instruction Set State: Preferred Return Address = Current Instruction Address + 4.
  2. Calculate the Link Register (LR) Value: The LR should be set to the preferred return address plus an offset. The offset depends on the instruction set state and the type of exception.

    • IRQ or FIQ Exception:

      • Thumb Instruction Set State: LR = Preferred Return Address + 4.
      • ARM Instruction Set State: LR = Preferred Return Address + 4.
    • Data Abort or Prefetch Abort Exception:

      • Thumb Instruction Set State: LR = Preferred Return Address + 8.
      • ARM Instruction Set State: LR = Preferred Return Address + 8.
  3. Adjust the LR for Exception Return: When returning from the exception handler, subtract the appropriate offset from the LR to obtain the correct return address.

    • IRQ or FIQ Exception:

      • Thumb Instruction Set State: Return Address = LR – 4.
      • ARM Instruction Set State: Return Address = LR – 4.
    • Data Abort or Prefetch Abort Exception:

      • Thumb Instruction Set State: Return Address = LR – 8.
      • ARM Instruction Set State: Return Address = LR – 8.

Example Calculation

Consider a scenario where the processor is executing an instruction at address 0x00001000 in Thumb state, and an IRQ exception is taken.

  1. Preferred Return Address: 0x00001000 + 2 = 0x00001002.
  2. LR Calculation: 0x00001002 + 4 = 0x00001006.
  3. Exception Return: 0x00001006 - 4 = 0x00001002.

The processor will resume execution at address 0x00001002 after the IRQ handler completes.

Summary Table

Instruction Set State Exception Type Preferred Return Address LR Calculation Return Address Calculation
Thumb IRQ/FIQ Current Address + 2 Preferred + 4 LR – 4
Thumb Data/Prefetch Abort Current Address + 2 Preferred + 8 LR – 8
ARM IRQ/FIQ Current Address + 4 Preferred + 4 LR – 4
ARM Data/Prefetch Abort Current Address + 4 Preferred + 8 LR – 8

Conclusion

Understanding how to calculate the return address after an exception in ARMv7-A and ARMv7-R architectures is essential for writing reliable exception handlers. By following the preferred return address method and adjusting the LR accordingly, software can ensure that execution resumes correctly after an exception. The pseudocode provided in the ARM Architecture Reference Manual is useful for understanding the internal behavior of the processor, but the preferred return address method should be used in practice to determine the correct return address.

By carefully considering the instruction set state and the type of exception, developers can avoid common pitfalls and ensure that their exception handlers function as intended. This guide provides a comprehensive approach to calculating the return address, helping to clarify the discrepancies between different methods and offering a clear path to correct implementation.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *