Skip to content

Commit 986e9a8

Browse files
tests: vector_table_relocation: add test for vector table relocation
Add a basic test for vector table relocation to SRAM. This test verifies that the vector table can be relocated to SRAM and that the VTOR register correctly points to the SRAM address range. Signed-off-by: Martin Hoff <[email protected]>
1 parent 7d7a873 commit 986e9a8

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2025 Silicon Laboratories Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
8+
project(vector_table_relocation)
9+
10+
FILE(GLOB app_sources src/*.c)
11+
target_sources(app PRIVATE ${app_sources})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. _vector_table_relocation:
2+
3+
Vector table relocation
4+
#################
5+
6+
Overview
7+
********
8+
A simple test that demonstrates the vector table relocation feature.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_COVERAGE=n
2+
CONFIG_ZTEST=y
3+
CONFIG_SRAM_VECTOR_TABLE=y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2025 Silicon Laboratories Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#if !defined(CONFIG_CPU_CORTEX_M)
8+
#error project can only run on Cortex-M
9+
#endif
10+
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/sys/printk.h>
13+
#include <zephyr/ztest.h>
14+
15+
#ifdef SCB_VTOR_TBLBASE_Msk
16+
#define VTOR_MASK (SCB_VTOR_TBLBASE_Msk | SCB_VTOR_TBLOFF_Msk)
17+
#else
18+
#define VTOR_MASK SCB_VTOR_TBLOFF_Msk
19+
#endif
20+
21+
/* This function will allow execute from sram region.
22+
* This is needed only for this sample because by default all soc will
23+
* disable the execute from SRAM.
24+
* An application that requires that the code be executed from SRAM will
25+
* have to configure the region appropriately in arm_mpu_regions.c.
26+
*/
27+
28+
#if (defined(CONFIG_ARM_MPU) && !defined(CONFIG_CPU_HAS_NXP_SYSMPU))
29+
#include <cmsis_core.h>
30+
void disable_mpu_rasr_xn(void)
31+
{
32+
uint32_t index;
33+
/* Kept the max index as 8(irrespective of soc) because the sram
34+
* would most likely be set at index 2.
35+
*/
36+
for (index = 0U; index < 8; index++) {
37+
MPU->RNR = index;
38+
#if defined(CONFIG_ARMV8_M_BASELINE) || defined(CONFIG_ARMV8_M_MAINLINE)
39+
if (MPU->RBAR & MPU_RBAR_XN_Msk) {
40+
MPU->RBAR ^= MPU_RBAR_XN_Msk;
41+
}
42+
#else
43+
if (MPU->RASR & MPU_RASR_XN_Msk) {
44+
MPU->RASR ^= MPU_RASR_XN_Msk;
45+
}
46+
#endif /* CONFIG_ARMV8_M_BASELINE || CONFIG_ARMV8_M_MAINLINE */
47+
}
48+
}
49+
#endif /* CONFIG_ARM_MPU */
50+
51+
ZTEST(vector_table_relocation, test_vector_table_in_ram)
52+
{
53+
/* Check that VTOR register effectively point to a RAM based location */
54+
volatile uint32_t vtor_address = SCB->VTOR & VTOR_MASK;
55+
56+
printf("VTOR address: 0x%x\n", vtor_address);
57+
zassert_true(vtor_address >= CONFIG_SRAM_BASE_ADDRESS &&
58+
vtor_address <= CONFIG_SRAM_BASE_ADDRESS + CONFIG_SRAM_SIZE,
59+
"Vector table is not in RAM! Address: 0x%x", vtor_address);
60+
}
61+
62+
void *relocate_code_setup(void)
63+
{
64+
#if (defined(CONFIG_ARM_MPU) && !defined(CONFIG_CPU_HAS_NXP_SYSMPU))
65+
disable_mpu_rasr_xn();
66+
#endif /* CONFIG_ARM_MPU */
67+
return NULL;
68+
}
69+
70+
ZTEST_SUITE(vector_table_relocation, NULL, relocate_code_setup, NULL, NULL, NULL);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
common:
2+
tags: linker
3+
tests:
4+
application_development.vector_table_relocation.arm:
5+
arch_allow: arm
6+
filter: CONFIG_CPU_CORTEX_M_HAS_VTOR
7+
# Exclude mps3/corstone310 because it uses another mechanism to support relocation
8+
# of the vector table (CONFIG_ROMSTART_RELOCATION_ROM).
9+
platform_exclude: mps3/corstone310

0 commit comments

Comments
 (0)