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

Commit

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

NOTE: same as PR #292

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

Co-authored-by: Jorge Aparicio <jorge.aparicio@ferrous-systems.com>
  • Loading branch information
bors[bot] and japaric authored Sep 2, 2020
2 parents 283dc32 + 6020e4f commit 427705f
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions asm.s
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ FpuTrampoline:
# Hand execution over to `main`.
bl main
# Note: `main` must not return. `bl` is used only because it has a wider range than `b`.

# 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 @@ -923,9 +923,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 @@ -1030,6 +1038,9 @@ pub enum Exception {
pub use self::Exception as exception;

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

fn NonMaskableInt();

fn HardFaultTrampoline();
Expand Down

0 comments on commit 427705f

Please sign in to comment.