-
Notifications
You must be signed in to change notification settings - Fork 7.7k
arch: Add SRAM based context for ISR #88883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Martinhoff-maker
wants to merge
2
commits into
zephyrproject-rtos:main
Choose a base branch
from
Martinhoff-maker:isr_vector_table_in_ram
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+399
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/application_development/ram_context_for_isr/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright (c) 2025 Silicon Laboratories Inc. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
|
||
project(ram_context_for_isr) | ||
|
||
FILE(GLOB app_sources src/*.c) | ||
target_sources(app PRIVATE ${app_sources}) | ||
|
||
zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) | ||
|
||
if(CONFIG_CPU_CORTEX_M_HAS_VTOR) | ||
zephyr_code_relocate(FILES ${ZEPHYR_BASE}/arch/arm/core/cortex_m/isr_wrapper.c LOCATION RAM) | ||
zephyr_code_relocate(FILES ${ZEPHYR_BASE}/arch/arm/core/cortex_m/exc_exit.c LOCATION RAM) | ||
endif() | ||
|
||
zephyr_code_relocate(FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c FILTER ".test_irq_callback" LOCATION RAM ) | ||
zephyr_code_relocate(FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/fake_driver.c FILTER ".fake_driver_isr" LOCATION RAM ) | ||
|
||
# Only needed because the fake driver is defined in tests folder | ||
zephyr_linker_sources(SECTIONS sections-rom.ld) |
94 changes: 94 additions & 0 deletions
94
tests/application_development/ram_context_for_isr/README.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
.. _vector_table_relocation: | ||
|
||
RAM Context for ISR | ||
################### | ||
|
||
Overview | ||
******** | ||
A test that verifies the ISR vector table relocation and | ||
full IRQ execution under RAM context. | ||
|
||
|
||
Test Description | ||
**************** | ||
|
||
This test verifies that interrupt service routines (ISRs), their callbacks, and the driver code | ||
can be fully relocated to RAM, ensuring that all interrupt handling occurs from SRAM rather than | ||
flash. This is important for real-time applications where minimizing interrupt latency is critical. | ||
|
||
The test uses a simple "fake driver" that registers an IRQ callback. The following aspects are | ||
validated: | ||
|
||
- The vector table and ISR wrapper are relocated to SRAM. | ||
- The fake driver's ISR and the registered callback are also located in SRAM. | ||
- When the interrupt is triggered, the callback and all relevant code execute from RAM, not flash. | ||
- The test asserts at runtime that the addresses of the callback, driver ISR, and ISR wrapper are | ||
within the SRAM address range. | ||
|
||
Test Steps | ||
********** | ||
|
||
1. The test registers a callback with the fake driver. | ||
2. It triggers the interrupt (IRQ 27). | ||
3. The callback checks (using assertions) that: | ||
- Its own address, | ||
- The driver's ISR address, | ||
- The ISR wrapper address, | ||
- And the device structure, | ||
are all within the SRAM region. | ||
4. The test passes if all assertions succeed and the callback is executed. | ||
|
||
Configuration | ||
************* | ||
|
||
The test is configured with the following options in prj.conf: | ||
|
||
- ``CONFIG_SRAM_VECTOR_TABLE=y``: Relocate the vector table to SRAM. | ||
- ``CONFIG_SRAM_SW_ISR_TABLE=y``: Relocate the software ISR table to SRAM. | ||
- ``CONFIG_CODE_DATA_RELOCATION=y``: Enable code/data relocation to SRAM. | ||
- ``CONFIG_DEVICE_MUTABLE=y``: Allow device structures to be mutable (in RAM). | ||
- ``CONFIG_ZTEST=y``: Enable Zephyr's test framework. | ||
|
||
The test is only supported on ARM Cortex-M platforms with VTOR (Vector Table Offset Register) support. | ||
|
||
Advanced Configuration and Limitations | ||
************************************** | ||
|
||
Configuration Options in testcase.yaml | ||
====================================== | ||
|
||
By default, the test disables several options in testcase.yaml: | ||
|
||
- ``CONFIG_TRACING_ISR=n``: Disables ISR tracing. | ||
**If enabled:** Enabling ISR tracing adds hooks to trace ISR entry/exit, which may introduce | ||
additional code paths not relocated to RAM. This can increase interrupt latency and may cause | ||
some tracing code to execute from flash, partially defeating the purpose of full RAM relocation. | ||
|
||
- ``CONFIG_STACK_SENTINEL=n``: Disables stack overflow detection sentinels. | ||
**If enabled:** Enabling stack sentinels adds extra checks to detect stack overflows. These | ||
checks may reside in flash and be called during interrupt handling, potentially increasing | ||
latency and causing some code to execute from flash. | ||
|
||
- ``CONFIG_PM=n``: Disables power management. | ||
**If enabled:** Enabling power management may cause the system to enter low-power states. Some | ||
wake-up interrupts may not use the relocated vector table in RAM, and the system may revert to | ||
using the flash-based vector table to exit idle states. This can result in some ISRs or their | ||
wrappers executing from flash, especially during wake-up from deep sleep. | ||
|
||
**Summary:** | ||
If you enable any of these options, ensure all code paths executed during interrupt handling are | ||
also relocated to RAM to maintain the lowest possible interrupt latency. Otherwise, some code may | ||
still execute from flash, increasing latency and partially defeating the test's purpose. | ||
|
||
Driver API Location and Limitations | ||
=================================== | ||
|
||
**Important Note:** | ||
While the test ensures ISR, callback, and device structures are relocated to RAM, the driver API | ||
structure (``struct fake_driver_api``)—containing function pointers for driver methods—is not | ||
relocated to RAM by default. This structure typically resides in flash (ROM) as device constant data. | ||
|
||
As long as you don't call the driver API during ISR execution, the current configuration is sufficient. | ||
However, if you need to call driver API functions from within an ISR, you must relocate the driver API | ||
structure to RAM. The simplest approach is to relocate the entire driver file to RAM rather than just | ||
the ISR symbol. |
17 changes: 17 additions & 0 deletions
17
tests/application_development/ram_context_for_isr/app.overlay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) 2025 Silicon Laboratories Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/ { | ||
#address-cells = <1>; | ||
#size-cells = <1>; | ||
|
||
fakedriver: fakedriver@E0000000 { | ||
compatible = "fakedriver"; | ||
reg = <0xE0000000 0x2000>; | ||
zephyr,mutable; | ||
status = "okay"; | ||
}; | ||
}; |
8 changes: 8 additions & 0 deletions
8
tests/application_development/ram_context_for_isr/dts/bindings/fakedriver.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright (c) 2025 Silicon Laboratories | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
description: Properties for fake driver. | ||
|
||
compatible: "fakedriver" | ||
|
||
include: [base.yaml, mutable.yaml] |
37 changes: 37 additions & 0 deletions
37
tests/application_development/ram_context_for_isr/include/fake_driver.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2025 Silicon Laboratories Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZEPHYR_INCLUDE_DRIVERS_FAKE_DRIVER_H_ | ||
#define ZEPHYR_INCLUDE_DRIVERS_FAKE_DRIVER_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef void (*fake_driver_irq_callback_t)(const struct device *dev, void *user_data); | ||
|
||
struct fake_driver_config { | ||
void (*irq_config_func)(void); | ||
uint8_t irq_num; | ||
uint8_t irq_priority; | ||
}; | ||
|
||
struct fake_driver_data { | ||
fake_driver_irq_callback_t irq_callback; | ||
void *user_data; | ||
}; | ||
|
||
__subsystem struct fake_driver_api { | ||
int (*configure)(const struct device *dev, int config); | ||
int (*register_irq_callback)(const struct device *dev, fake_driver_irq_callback_t cb, | ||
void *user_data); | ||
}; | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* ZEPHYR_INCLUDE_DRIVERS_FAKE_DRIVER_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG_COVERAGE=n | ||
CONFIG_ZTEST=y | ||
CONFIG_SRAM_VECTOR_TABLE=y | ||
CONFIG_SRAM_SW_ISR_TABLE=y | ||
CONFIG_CODE_DATA_RELOCATION=y | ||
CONFIG_DEVICE_MUTABLE=y |
10 changes: 10 additions & 0 deletions
10
tests/application_development/ram_context_for_isr/sections-rom.ld
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright (c) 2025 Silicon Laboratories Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/linker/iterable_sections.h> | ||
|
||
/* Only needed because the driver is defined in tests folder */ | ||
ITERABLE_SECTION_ROM(fake_driver_api, Z_LINK_ITERABLE_SUBALIGN) |
82 changes: 82 additions & 0 deletions
82
tests/application_development/ram_context_for_isr/src/fake_driver.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2025 Silicon Laboratories Inc. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/device.h> | ||
#include <zephyr/irq.h> | ||
#include "fake_driver.h" | ||
|
||
#define DT_DRV_COMPAT fakedriver | ||
|
||
#define TEST_IRQ_NUM 27 | ||
#define TEST_IRQ_PRIO 4 | ||
|
||
static void fake_driver_isr(const void *arg) | ||
{ | ||
const struct device *dev = (const struct device *)arg; | ||
struct fake_driver_data *data = dev->data; | ||
|
||
/* Store the address of fake_driver_isr in user_data because it will be optimized by the | ||
* compiler (even with __noinline__ attribute) | ||
*/ | ||
data->user_data = (void *)&fake_driver_isr; | ||
|
||
if (data->irq_callback != NULL) { | ||
data->irq_callback(dev, data->user_data); | ||
} | ||
} | ||
|
||
static int fake_driver_configure(const struct device *dev, int config) | ||
{ | ||
return 0; | ||
} | ||
|
||
static int fake_driver_register_irq_callback(const struct device *dev, | ||
fake_driver_irq_callback_t cb, void *user_data) | ||
{ | ||
struct fake_driver_data *data = dev->data; | ||
|
||
data->irq_callback = cb; | ||
data->user_data = user_data; | ||
|
||
return 0; | ||
} | ||
|
||
DEVICE_API(fake, fake_driver_func) = { | ||
.configure = fake_driver_configure, | ||
.register_irq_callback = fake_driver_register_irq_callback, | ||
}; | ||
|
||
static int fake_driver_init(const struct device *dev) | ||
{ | ||
const struct fake_driver_config *config = dev->config; | ||
struct fake_driver_data *data = dev->data; | ||
|
||
data->irq_callback = NULL; | ||
data->user_data = NULL; | ||
|
||
config->irq_config_func(); | ||
|
||
return 0; | ||
} | ||
|
||
#define FAKE_INIT(inst) \ | ||
static struct fake_driver_data fake_driver_data_##inst; \ | ||
static void fake_driver_irq_config_func_##inst(void) \ | ||
{ \ | ||
IRQ_CONNECT(TEST_IRQ_NUM, TEST_IRQ_PRIO, fake_driver_isr, \ | ||
DEVICE_DT_INST_GET(inst), 0); \ | ||
irq_enable(TEST_IRQ_NUM); \ | ||
} \ | ||
static struct fake_driver_config fake_driver_config_##inst = { \ | ||
.irq_config_func = fake_driver_irq_config_func_##inst, \ | ||
.irq_num = TEST_IRQ_NUM, \ | ||
.irq_priority = TEST_IRQ_PRIO, \ | ||
}; \ | ||
DEVICE_DT_INST_DEFINE(inst, &fake_driver_init, NULL, &fake_driver_data_##inst, \ | ||
&fake_driver_config_##inst, PRE_KERNEL_1, \ | ||
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &fake_driver_func); | ||
|
||
DT_INST_FOREACH_STATUS_OKAY(FAKE_INIT) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.