Skip to content

libs/libc/machine/arm64: Add the ucontext functions. - #20724

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:arm64-ucontext
Oct 18, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
casaroli:arm64-ucontext

Conversation

@casaroli

@casaroli casaroli commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the ucontext functions (getcontext(), setcontext(), makecontext(), swapcontext()) and include/ucontext.h, with an arm64 implementation that selects the new ARCH_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_ONSTACK fails with EINVAL), 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/ltp excludes the tests that include ucontext.h because 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 from makecontext() continues with uc_link when its function returns, or the task exits if uc_link is NULL.

QEMU, for example, needs them for its coroutines.

Impact

New header and functions, arm64 only. mcontext_t is in arch/arm64/include/irq.h, next to struct 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 a makecontext() stack that uses TLS, for example exit() when the function returns with uc_link NULL, then reads a wrong address. #20723 removes this limit: user code reads the TLS pointer from TPIDR_EL0.

Testing

Builds: qemu-armv8a:nsh, qemu-armv8a:knsh, lm3s6965-ek:qemu-flat.

tools/checkpatch.sh -c -u -m -g passes.

A test program on qemu-armv8a:knsh (QEMU 11 with HVF on an Apple M4) with 8 checks: a getcontext()/setcontext() loop, two arguments with uc_link, a function with 10 arguments (two on the stack, negative values), chained contexts, 100 swapcontext() round trips, uc_sigmask, FP registers preserved across swapcontext(), and a task that exits when its context returns with uc_link NULL.

ostest on qemu-armv8a:knsh and qemu-armv8a:nsh ends with ostest_main: Exiting with status 0.

@github-actions github-actions Bot added Arch: arm64 Issues related to ARM64 (64-bit) architecture Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Oct 17, 2026
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

esp32-devkitc

  • ROM: .flash.text +80 B (+0.1%, 126,280 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +80 B (+0.1%, 90,476 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +4 B (+0.0%, 84,024 B / 4,194,304 B, total: 2% used)

qemu-armv8a

  • Code: .rodata +56 B, .text.cp_handler +32 B, .text.get_primes_thread +80 B, .text.rr_test +116 B (+0.3%, 349,512 B)
  • Data: -3 B (-0.0%, 77,687 B)

qemu-intel64

  • Code: .text +565 B (+0.0%, 8,684,187 B)

rx65n-rsk2mb

acassis
acassis previously approved these changes Oct 17, 2026
Comment thread Kconfig Outdated
Comment thread arch/arm64/include/ucontext.h Outdated
Comment thread libs/libc/machine/arm64/arch_makecontext.c Outdated
Comment thread libs/libc/machine/arm64/arch_makecontext.c Outdated
Comment thread libs/libc/machine/arm64/arch_makecontext.c Outdated
Comment thread libs/libc/machine/arm64/arch_makecontext.c Outdated
Comment thread libs/libc/machine/arm64/arch_makecontext.c Outdated
Comment thread arch/arm64/include/ucontext.h Outdated
Comment thread libs/libc/machine/arm64/arch_makecontext.c Outdated
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>
@xiaoxiang781216
xiaoxiang781216 merged commit 6d0ae23 into apache:master Oct 18, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm64 Issues related to ARM64 (64-bit) architecture Area: OS Components OS Components issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants