Repository navigation
libs/libc/machine/arm64: Add the ucontext functions. - #20724
Merged
Merged
Conversation
|
acassis
previously approved these changes
Oct 17, 2026
casaroli
force-pushed
the
arm64-ucontext
branch
from
October 17, 2026 19:49
9879a91 to
1a083c7
Compare
Programs that switch between their own stacks, such as coroutine libraries, use the ucontext functions, and NuttX had none. Add include/ucontext.h with ucontext_t, and an implementation for arm64, which selects the new ARCH_HAVE_UCONTEXT. A context holds the callee-saved registers, the stack pointer, the program counter, the arguments of makecontext(), the callee-saved FP registers when there is an FPU, and the signal mask. A context made by makecontext() continues with uc_link when its function returns, or the task exits if uc_link is NULL. mcontext_t is in arch/arm64/include/irq.h, next to struct xcptcontext. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
casaroli
force-pushed
the
arm64-ucontext
branch
from
October 18, 2026 12:29
1a083c7 to
c541a86
Compare
xiaoxiang781216
approved these changes
Oct 18, 2026
xiaoxiang781216
approved these changes
Oct 18, 2026
acassis
approved these changes
Oct 18, 2026
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.
Summary
Adds the ucontext functions (
getcontext(),setcontext(),makecontext(),swapcontext()) andinclude/ucontext.h, with an arm64 implementation that selects the newARCH_HAVE_UCONTEXT. Programs that switch between their own stacks, such as coroutine libraries, need them.NuttX has no ucontext today, and its
sigaltstack()does not support an alternate stack (SS_ONSTACKfails withEINVAL), so the usual fallback of coroutine libraries does not work either: a program has no way to switch to a stack of its own. POSIX.1-2008 removed these functions (POSIX.1-2001 marked them obsolescent), but glibc, musl and FreeBSD keep them because programs still use them;apps/testing/ltpexcludes the tests that includeucontext.hbecause NuttX lacks them.A context holds the callee-saved registers, the stack pointer, the program counter, the arguments of
makecontext(), the callee-saved FP registers when there is an FPU, and the signal mask. A context frommakecontext()continues withuc_linkwhen its function returns, or the task exits ifuc_linkis NULL.QEMU, for example, needs them for its coroutines.
Impact
New header and functions, arm64 only.
mcontext_tis inarch/arm64/include/irq.h, next tostruct xcptcontext. Nothing else changes.With
CONFIG_TLS_ALIGNED(the default of the kernel and protected builds), user code finds its TLS from the stack pointer. Code on amakecontext()stack that uses TLS, for exampleexit()when the function returns withuc_linkNULL, then reads a wrong address. #20723 removes this limit: user code reads the TLS pointer fromTPIDR_EL0.Testing
Builds:
qemu-armv8a:nsh,qemu-armv8a:knsh,lm3s6965-ek:qemu-flat.tools/checkpatch.sh -c -u -m -gpasses.A test program on
qemu-armv8a:knsh(QEMU 11 with HVF on an Apple M4) with 8 checks: agetcontext()/setcontext()loop, two arguments withuc_link, a function with 10 arguments (two on the stack, negative values), chained contexts, 100swapcontext()round trips,uc_sigmask, FP registers preserved acrossswapcontext(), and a task that exits when its context returns withuc_linkNULL.exit()because ofCONFIG_TLS_ALIGNED(see Impact).ostestonqemu-armv8a:knshandqemu-armv8a:nshends withostest_main: Exiting with status 0.