arch: riscv: pass main thread to custom stack guard without multithreading - #115704
Open
hongquan-prog wants to merge 1 commit into
Open
arch: riscv: pass main thread to custom stack guard without multithreading#115704hongquan-prog wants to merge 1 commit into
hongquan-prog wants to merge 1 commit into
Conversation
…ading In !CONFIG_MULTITHREADING mode the inline assembly in z_riscv_switch_to_main_no_multithreading() calls z_riscv_custom_stack_guard_enable() without setting a0 to the k_thread * the callee's contract requires. The Andes implementation only survives because it ignores the argument; any implementation that dereferences thread faults. Pass a static k_thread whose stack_info describes z_main_stack (z_main_thread does not exist in this mode), bound to a0 with the fixed-register pattern. Also pin main_entry to callee-saved s1 so the jalr target survives the call. Fixes zephyrproject-rtos#113190 Signed-off-by: Hongquan Li <hongquan.li@processmission.com>
zephyrbot
requested review from
AFOliveira,
VynDragon,
dcpleung,
fkokosinski,
katsuster,
kgugala,
lstnl,
maass-hamburg,
meijemac,
mgielda,
npitre,
tgorochowik and
ycsin
August 8, 2026 13:53
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Problem
With
CONFIG_MULTITHREADING=nandCONFIG_CUSTOM_STACK_GUARD=y, the inlineassembly in
z_riscv_switch_to_main_no_multithreading()callsz_riscv_custom_stack_guard_enable(struct k_thread *thread)without settinga0to thethreadargument required by the function's contract(
arch/riscv/include/kernel_arch_func.h). All other call sites(
arch/riscv/core/isr.S,arch/riscv/core/switch.S) load a validk_thread *intoa0before the call.The in-tree Andes HSP implementation only survives because its
!CONFIG_MULTITHREADINGbranch ignoresthread; any vendor implementationthat follows the contract and dereferences
thread(e.g. readsthread->stack_info.start) faults with a Load access fault inno-multithreading mode.
Fix
Pass a static
k_threaddescriptor whosestack_infodescribesz_main_stack(z_main_threadis only defined withCONFIG_MULTITHREADING), bound toa0with the fixed-register pattern usedby
z_riscv_userspace_enter(). A zero-initialized descriptor behavescorrectly through every field the in-tree implementation inspects
(
thread_state,user_options,stack_info.start).Also pin
main_entryto the callee-saved registers1so thejalrtargetis guaranteed to survive the
call; previously this relied on the compilerhappening to pick a callee-saved register.
Fixes #113190