Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Merge #292
Browse files Browse the repository at this point in the history
292: [ARMv6-M] initialize the LR register r=therealprof a=japaric

the ARMv6-M Architecture Reference Manual (ARM DDI 0419D) indicates in section
B1.5.5 "Reset behavior" that the LR (Link Register) starts in an unknown state
when the Reset handler is taken and that its "Value must be initialised by
software"

So this PR does that: it initializes the LR register to 0xFFFF_FFFF (-1) first
thing in the Reset handler (only for v6). The manual doesn't say which value to
use so I decided to use the value used by the ARMv7-M (v7 sets LR to 0xFFFF_FFFF
before invoking the Reset handler; see its Architecture Manual for details).

The values of LR (these are pushed onto the stack in function preludes) are used
to unwind the stack (e.g. GDB's `backtrace` or a future `cortex_m_panic_unwind`
handler). Having the initial stack frame use a known value on all Cortex-M
variants makes it easier to implement `panic_unwind` and avoids virtual
unwinders like GDB `backtrace` trying to unwind beyond the `Reset` handler

Note that this implementation uses a trampoline that runs before `Reset` to set
LR on v6. This is required because the prelude of the `Reset` routine will push
LR onto the stack; we want that LR value to be -1. Calling `register::lr::write`
from `Reset` would perform the write after LR has been pushed onto the stack and
that's too late

*NOTE* this is a PR against v0.6.x. I'm using that branch as 0.6.x is the latest minor release. I can open a PR against master after this one has been approved.

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
  • Loading branch information
bors[bot] and japaric committed Sep 4, 2020
2 parents 17ea2ee + c9c9aa0 commit 2907763
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions asm.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ HardFaultTrampoline:
0:
mrs r0, PSP
b HardFault

# ARMv6-M leaves LR in an unknown state on Reset
# this trampoline sets LR before it's pushed onto the stack by Reset
.section .PreResetTrampoline, "ax"
.global PreResetTrampoline
# .type and .thumb_func are both required; otherwise its Thumb bit does not
# get set and an invalid vector table is generated
.type PreResetTrampoline,%function
.thumb_func
PreResetTrampoline:
# set LR to the initial value used by the ARMv7-M (0xFFFF_FFFF)
ldr r0,=0xffffffff
mov lr,r0
b Reset
Binary file modified bin/thumbv6m-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv7em-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv7em-none-eabihf.a
Binary file not shown.
Binary file modified bin/thumbv7m-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv8m.base-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv8m.main-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv8m.main-none-eabihf.a
Binary file not shown.
4 changes: 4 additions & 0 deletions link.x.in
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ SECTIONS
/* ### .text */
.text _stext :
{
/* place these 2 close to each other or the `b` instruction will fail to link */
*(.PreResetTrampoline);
*(.Reset);

*(.text .text.*);
*(.HardFaultTrampoline);
*(.HardFault.*);
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,17 @@ pub fn heap_start() -> *mut u32 {
#[doc(hidden)]
#[link_section = ".vector_table.reset_vector"]
#[no_mangle]
#[cfg(not(armv6m))]
pub static __RESET_VECTOR: unsafe extern "C" fn() -> ! = Reset;

#[doc(hidden)]
#[link_section = ".vector_table.reset_vector"]
#[no_mangle]
#[cfg(armv6m)]
pub static __RESET_VECTOR: unsafe extern "C" fn() -> ! = PreResetTrampoline;

#[doc(hidden)]
#[link_section = ".Reset"]
#[no_mangle]
pub unsafe extern "C" fn Reset() -> ! {
extern "C" {
Expand Down Expand Up @@ -600,6 +608,9 @@ pub enum Exception {
}

extern "C" {
#[cfg(armv6m)]
fn PreResetTrampoline() -> !;

fn NonMaskableInt();

fn HardFaultTrampoline();
Expand Down
2 changes: 1 addition & 1 deletion tests/compile-fail/interrupt-invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ enum interrupt {
// NOTE this looks a bit better when using a device crate:
// "no variant named `foo` found for type `stm32f30x::Interrupt` in the current scope"
#[interrupt]
fn foo() {} //~ ERROR no variant or associated item named `foo` found for type `interrupt` in the current scope
fn foo() {} //~ ERROR no variant or associated item named `foo` found for enum `interrupt` in the current scope

0 comments on commit 2907763

Please sign in to comment.