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

Report here problems in any of ChibiOS components. This forum is NOT for support.
stapelberg
Posts: 10
Joined: Wed Jun 03, 2020 9:17 pm
Has thanked: 4 times
Been thanked: 3 times

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

Postby stapelberg » Thu Jun 11, 2020 6:33 pm

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 <stapelberg@google.com>
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,

stapelberg
Posts: 10
Joined: Wed Jun 03, 2020 9:17 pm
Has thanked: 4 times
Been thanked: 3 times

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

Postby stapelberg » Thu Jun 11, 2020 8:21 pm

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.

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

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

Postby Giovanni » Thu Jun 11, 2020 8:50 pm

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

stapelberg
Posts: 10
Joined: Wed Jun 03, 2020 9:17 pm
Has thanked: 4 times
Been thanked: 3 times

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

Postby stapelberg » Thu Jun 11, 2020 9:33 pm

Aligning to 1024 sounds good to me.

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

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

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

Postby Giovanni » Thu Jun 11, 2020 9:37 pm

I can do it, no problem :)

Giovanni

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

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

Postby Giovanni » Fri Jun 26, 2020 9:12 am

Hi,

Fixed as bug #1107.

Giovanni

stapelberg
Posts: 10
Joined: Wed Jun 03, 2020 9:17 pm
Has thanked: 4 times
Been thanked: 3 times

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

Postby stapelberg » Sat Mar 13, 2021 3:33 pm

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 :)


Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 8 guests