-
Notifications
You must be signed in to change notification settings - Fork 8.4k
riscv: pmp: enable stackguard without multithreading #80546
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
Merged
nashif
merged 2 commits into
zephyrproject-rtos:main
from
f0rget-the-sad:stack-guard-no-threading
Nov 20, 2024
Merged
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| cmake_minimum_required(VERSION 3.20.0) | ||
| find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
| project(riscv_pmp) | ||
|
|
||
| FILE(GLOB app_sources src/*.c) | ||
| target_sources(app PRIVATE ${app_sources}) | ||
|
|
||
| target_include_directories(app PRIVATE | ||
| ${ZEPHYR_BASE}/kernel/include | ||
| ${ZEPHYR_BASE}/arch/${ARCH}/include | ||
| ) |
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,2 @@ | ||
| CONFIG_ZTEST=y | ||
| CONFIG_MULTITHREADING=n |
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,79 @@ | ||
| /* | ||
| * Copyright (c) 2024 Marvell. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <kernel_internal.h> | ||
| #include <zephyr/tc_util.h> | ||
| #include <zephyr/ztest.h> | ||
|
|
||
| static volatile ZTEST_BMEM bool valid_fault; | ||
|
|
||
| void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf) | ||
| { | ||
| int rv = TC_PASS; | ||
|
|
||
| TC_PRINT("Caught system error -- reason %d %d\n", reason, valid_fault); | ||
| if (!valid_fault) { | ||
| TC_PRINT("Fatal error was unexpected, aborting...\n"); | ||
| rv = TC_FAIL; | ||
| } | ||
| TC_END_RESULT_CUSTOM(rv, "test_pmp"); | ||
| TC_END_REPORT(rv); | ||
| arch_system_halt(reason); | ||
| } | ||
|
|
||
| #ifdef CONFIG_PMP_STACK_GUARD | ||
| static void check_isr_stack_guard(void) | ||
| { | ||
| char *isr_stack = (char *)z_interrupt_stacks; | ||
|
|
||
| valid_fault = true; | ||
| *isr_stack = 42; | ||
| } | ||
|
|
||
| static void check_main_stack_guard(void) | ||
| { | ||
| char *main_stack = (char *)z_main_stack; | ||
|
|
||
| valid_fault = true; | ||
| *main_stack = 42; | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| static void check_isr_stack_guard(void) | ||
| { | ||
| ztest_test_skip(); | ||
| } | ||
|
|
||
| static void check_main_stack_guard(void) | ||
| { | ||
| ztest_test_skip(); | ||
| } | ||
|
|
||
| #endif /* CONFIG_PMP_STACK_GUARD */ | ||
|
|
||
| typedef void (*pmp_test_func_t)(void); | ||
|
|
||
| static const pmp_test_func_t pmp_test_func[] = { | ||
| check_isr_stack_guard, | ||
| check_main_stack_guard, | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Verify RISC-V specific PMP stack guard regions. | ||
| * @details Manually write to the protected stack region to trigger fatal error. | ||
| */ | ||
| ZTEST(riscv_pmp_no_mt, test_pmp) | ||
| { | ||
| #ifndef PMP_TEST_FUNC_IDX | ||
| #define PMP_TEST_FUNC_IDX 0 | ||
| #endif | ||
| pmp_test_func[PMP_TEST_FUNC_IDX](); | ||
|
|
||
| zassert_unreachable("Write to stack guard did not fault"); | ||
| TC_END_REPORT(TC_FAIL); | ||
| } | ||
|
|
||
| ZTEST_SUITE(riscv_pmp_no_mt, NULL, NULL, NULL, NULL, NULL); |
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,13 @@ | ||
| common: | ||
| platform_allow: | ||
| - qemu_riscv32 | ||
| - qemu_riscv32e | ||
| - qemu_riscv64 | ||
| filter: CONFIG_RISCV_PMP | ||
| ignore_faults: true | ||
|
|
||
| tests: | ||
| arch.riscv.pmp.no-mt.isr-stack-guard: | ||
| extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=0 | ||
| arch.riscv.pmp.no-mt.main-stack-guard: | ||
| extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=1 | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could have used Kconfig instead? i.e.
CONFIG_TEST_ISR_STACK_GUARD/CONFIG_TEST_MAIN_STACK_GUARD, and enable it hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Kconfig option usually meant to allow user to tweak something.
Here it's already possible to select different tests by name, so I don't see benefits in using Kconfig.