From eb48a0a73c11c2a9cd4c3c91864ca4e0cf52dae8 Mon Sep 17 00:00:00 2001 From: Xiaorui Hu Date: Fri, 23 Jun 2017 11:02:50 +0800 Subject: [PATCH] arm: armv6-m: Support relocating vector table An abnormal crash was encountered in ARMv6-M SoCs that don't have flash starting at 0. With Zephyr OS the reason for this crash is that, on ARMv6-M the system requires an exception vector table at the 0 address. We implement the relocate_vector_table function to move the vector table code to address 0 on systems which don't have the start of code already at 0. [kumar.gala: reworderd commit message, tweaked how we check if we need to copy vector table] Signed-off-by: Xiaorui Hu Signed-off-by: Kumar Gala --- arch/arm/core/cortex_m/prep_c.c | 13 ++++++++++++- include/arch/arm/cortex_m/scripts/linker.ld | 4 ++-- include/linker/linker-defs.h | 3 +++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/arch/arm/core/cortex_m/prep_c.c b/arch/arm/core/cortex_m/prep_c.c index d23dd8bee99..138237937a3 100644 --- a/arch/arm/core/cortex_m/prep_c.c +++ b/arch/arm/core/cortex_m/prep_c.c @@ -22,9 +22,20 @@ #include #include #include +#include #ifdef CONFIG_ARMV6_M -static inline void relocate_vector_table(void) { /* do nothing */ } + +#define VECTOR_ADDRESS 0 +static inline void relocate_vector_table(void) +{ +#if defined(CONFIG_XIP) && (CONFIG_FLASH_BASE_ADDRESS != 0) || \ + !defined(CONFIG_XIP) && (CONFIG_SRAM_BASE_ADDRESS != 0) + size_t vector_size = (size_t)_vector_end - (size_t)_vector_start; + memcpy(VECTOR_ADDRESS, _vector_start, vector_size); +#endif +} + #elif defined(CONFIG_ARMV7_M) #ifdef CONFIG_XIP #define VECTOR_ADDRESS ((uintptr_t)&_image_rom_start + \ diff --git a/include/arch/arm/cortex_m/scripts/linker.ld b/include/arch/arm/cortex_m/scripts/linker.ld index 52f3a18ea7c..bfd6bee72bc 100644 --- a/include/arch/arm/cortex_m/scripts/linker.ld +++ b/include/arch/arm/cortex_m/scripts/linker.ld @@ -94,7 +94,7 @@ SECTIONS KEEP(*(.dbghdr)) KEEP(*(".dbghdr.*")) #endif - + _vector_start = .; . = CONFIG_TEXT_SECTION_OFFSET; KEEP(*(.exc_vector_table)) KEEP(*(".exc_vector_table.*")) @@ -112,7 +112,7 @@ SECTIONS #ifdef CONFIG_GEN_SW_ISR_TABLE KEEP(*(SW_ISR_TABLE)) #endif - + _vector_end = .; _image_text_start = .; *(.text) *(".text.*") diff --git a/include/linker/linker-defs.h b/include/linker/linker-defs.h index 49a3a635e4f..0d8c860919b 100644 --- a/include/linker/linker-defs.h +++ b/include/linker/linker-defs.h @@ -208,6 +208,9 @@ extern char _image_text_end[]; extern char _image_rodata_start[]; extern char _image_rodata_end[]; +extern char _vector_start[]; +extern char _vector_end[]; + /* end address of image, used by newlib for the heap */ extern char _end[];