This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What is the meaning of a 64 bit aligned stack pointer address?

According to ARM Architecture Procedure Call Standard (AAPCS) on the ARMv6-M, and ARMv7-M architecture in  it says:

"Although the processor hardware allows SP to be at any word aligned address at function boundaries, standard programming practice requires C program code to ensure that the SP is at a 64-bit (doubleword) aligned address."

What does it mean that the Stack pointer has to be at a 64 bit aligned address?

Parents
  • Hello,

    as far as my memory is correct, the 8-byte alignment of the exception stack frame would be required only for Cortex-M7.

    The other Cortex-M core would be enough with 4-byte alignment.
    The reason of 8-byte alignment of Cortex-M7 would be guessed as that the internal AXI bus width is 64 bit.

    According to "ARM®v7-M Architecture Reference Manual [ARM DDI 0403E.b (ID120114)]", B1.5.7 describes as the following.

    B1.5.7 Stack alignment on exception entry

    The ARMv7-M architecture guarantees that stack pointer values are at least 4-byte aligned.

    However, some software standards require the stack pointer to be 8-byte aligned, and the architecture can enforce this alignment.
    The CCR.STKALIGN bit indicates whether, as part of an exception entry, the processor aligns the SP to 4 bytes, or
    to 8 bytes. It is IMPLEMENTATION DEFINED whether this bit is:
    - RW, in which case its reset value is IMPLEMENTATION DEFINED.
    - RO, in which case it is RAO, indicating 8-byte SP alignment

    Also, according to the Cortex-M4 TRM, CCR.STKALIGN is R/W although the initial value is 1 (i.e. 8-byte align).
    According to the Coetex-M7 TRM, CCR.STKALIGN is R/O and the initial value is 1 (i.e. 8-byte align).
    As for Cortex-M0+ and Cortex-M3 TRMs, it is uncertain whether CCR.STKALIGN can be modified or not, but the initial value is 1. I think they can be modified.

    Best regards,

    Yasuhiko Koumoto.

Reply
  • Hello,

    as far as my memory is correct, the 8-byte alignment of the exception stack frame would be required only for Cortex-M7.

    The other Cortex-M core would be enough with 4-byte alignment.
    The reason of 8-byte alignment of Cortex-M7 would be guessed as that the internal AXI bus width is 64 bit.

    According to "ARM®v7-M Architecture Reference Manual [ARM DDI 0403E.b (ID120114)]", B1.5.7 describes as the following.

    B1.5.7 Stack alignment on exception entry

    The ARMv7-M architecture guarantees that stack pointer values are at least 4-byte aligned.

    However, some software standards require the stack pointer to be 8-byte aligned, and the architecture can enforce this alignment.
    The CCR.STKALIGN bit indicates whether, as part of an exception entry, the processor aligns the SP to 4 bytes, or
    to 8 bytes. It is IMPLEMENTATION DEFINED whether this bit is:
    - RW, in which case its reset value is IMPLEMENTATION DEFINED.
    - RO, in which case it is RAO, indicating 8-byte SP alignment

    Also, according to the Cortex-M4 TRM, CCR.STKALIGN is R/W although the initial value is 1 (i.e. 8-byte align).
    According to the Coetex-M7 TRM, CCR.STKALIGN is R/O and the initial value is 1 (i.e. 8-byte align).
    As for Cortex-M0+ and Cortex-M3 TRMs, it is uncertain whether CCR.STKALIGN can be modified or not, but the initial value is 1. I think they can be modified.

    Best regards,

    Yasuhiko Koumoto.

Children
  • Hi Yasuhiko,

    In Cortex-M0 and Cortex-M0+ processors, CCR.STKALIGN is fixed to 1. So it always enforce 8 byte stack alignment for exception stack frames.

    When setting up the initial SP values, the values should be 8 bytes aligned.

    When having function calls, the SP value at function call boundaries should be 8 byte aligned. It is okay to have 4 byte alignment in the middle of a function, as long as the SP value is adjust back to 8 bytes aligned before the function is ended or a function call is made.

    If a function is coded in assembly language, and inside this function it calls another assembly function which you knew that it does not require 8 byte stack alignment, you can have 4 byte stack alignment at function call in such case. But if you are calling a C function, then 8 byte stack alignment is required, as the C compiler might make assumption of SP value in pointer handling.

    Hope this helps.

    regards,

    Joseph

  • Hello Joseph Yiu,

    thank you for your detailed explanation.

    I have been misunderstood.

    My understanding had been that the case of CCR.STKALIGN=1 made the stack pointer at interrupt or exceptions aligned to 8 byte boundary.

    Best regards,

    Yasuhiko Koumoto.

  • That's correct. CCR.STKALIGN only affect exception stack frames.  In Cortex-M0, Cortex-M0+ and Cortex-M7 processors, this bit is fixed to 1 which always ensure that the exception stack frames are double word aligned.

    However, software still have to implemented correctly to be AAPCS compliant.

    regards,

    Joseph