Page 1 of 1

[PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Thu Jun 11, 2020 6:33 pm
by stapelberg
I first wanted to send this as a pull request to https://github.com/ChibiOS/ChibiOS, but then saw an older pull request that was closed with a message to “post to the forums”, so here comes a patch that I would like to ask you to review and merge.

I tried attaching the file, but .patch is not an allowed file extension in this forum.

I hope including it like so in this post is okay (otherwise, please advise how to send this):

Code: Select all

From d942e1fae2f3f9df6ece5752d3345bbddff999ac Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <[email protected]>
Date: Thu, 11 Jun 2020 19:17:45 +0200
Subject: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

See the comment in the commit for more details.

Alignment generally doesn’t matter when the .vectors section is the first one
placed on the flash, ending up at address 0.

The Teensy 4.1, however, requires flashconfig to come first (more on that in
another commit to ChibiOS-Contrib), and isn’t happy with un-aligned vector
tables. In my case, it rebooted after enabling the SysTick_Handler interrupt.
---
 .../ARMCMx/compilers/GCC/ld/rules_code.ld     | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/os/common/startup/ARMCMx/compilers/GCC/ld/rules_code.ld b/os/common/startup/ARMCMx/compilers/GCC/ld/rules_code.ld
index 888cc712e..a175986f6 100644
--- a/os/common/startup/ARMCMx/compilers/GCC/ld/rules_code.ld
+++ b/os/common/startup/ARMCMx/compilers/GCC/ld/rules_code.ld
@@ -18,7 +18,24 @@ ENTRY(Reset_Handler)
 
 SECTIONS
 {
-    .vectors : ALIGN(16)
+/*
+
+The Teensy 4.1 has an ARM Cortex M7 with 160+16 interrupts, so we need to align
+256, the nearest power of two, as per:
+http://infocenter.arm.com/help/topic/com.arm.doc.dui0646c/DUI0646C_cortex_m7_dgug.pdf section 4.3.4:
+
+When setting TBLOFF, you must align the offset to the number of exception
+entries in the vector table. The minimum alignment is 32 words, enough for up to
+16 interrupts. For more interrupts, adjust the alignment by rounding up to the
+next power of two. For example, if you require 21 interrupts, the alignment must
+be on a 64-word boundary because the required table size is 37 words, and the
+next power of two is 64.
+
+Note: Table alignment requirements mean that bits[6:0] of the table offset are
+always zero.
+
+*/
+    .vectors : ALIGN(256)
     {
         KEEP(*(.vectors))
     } > VECTORS_FLASH AT > VECTORS_FLASH_LMA
--
2.27.0



Thank you,

Re: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Thu Jun 11, 2020 8:21 pm
by stapelberg
Hm, actually it seems like I need to align to 512 after fixing

Code: Select all

#define CORTEX_NUM_VECTORS      160

in cmparams.h, otherwise the device doesn’t work. Not 100% sure why right now.

Re: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Thu Jun 11, 2020 8:50 pm
by Giovanni
Hi,

The simplest solution would be to align for the worst case which is 240 vectors if I remember well, that would mean an 1kB alignment.

Moving this in "bug reports".

Giovanni

Re: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Thu Jun 11, 2020 9:33 pm
by stapelberg
Aligning to 1024 sounds good to me.

Do you want me to update my patch or are you going to make the change?

Re: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Thu Jun 11, 2020 9:37 pm
by Giovanni
I can do it, no problem :)

Giovanni

Re: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Fri Jun 26, 2020 9:12 am
by Giovanni
Hi,

Fixed as bug #1107.

Giovanni

Re: [PATCH] ARMCMx: rules_code.ld: align .vectors to 256 (for e.g. Teensy 4.1)

Posted: Sat Mar 13, 2021 3:33 pm
by stapelberg
stapelberg wrote:Hm, actually it seems like I need to align to 512 after fixing

Code: Select all

#define CORTEX_NUM_VECTORS      160

in cmparams.h, otherwise the device doesn’t work. Not 100% sure why right now.


I figured out my misunderstanding: I previously missed that the alignment needs to happen on words (each vector table entry is an address occupying one word), not bytes.

I.e., we have 160 vectors + 16 base vectors = 176 vectors total. The nearest power of 2 is 256 words, which results in 1024 bytes alignment.

The change you made already aligns to 1024 bytes, so everything is correct. Just wanted to add to the reasoning in case anyone comes across this post :)