nuttx/sched
Marco Casaroli ac7b5f5218 sched: Run a module's SIGEV_THREAD notification with its data base.
A SIGEV_THREAD notification -- from mq_notify() or timer_create() -- runs its
callback on a shared signal-notification work queue, not in the registering
task.  That worker carries no FDPIC data base, so a module's callback reaches
it with the wrong base and cannot touch its own globals.  This is unlike every
other callback entry point, where the callback runs in a task that inherited
the module's data space and resolving the code address is enough.

The base is knowable exactly once, at registration, when the call is still in
the module's own context: capture it there with fdpic_base() into the
persisted work structure (mq's ntwork, the timer's pt_work).  At send or
expiry the descriptor is resolved to its code address -- a plain memory read
that needs no base -- and stored in work->func.  The worker, seeing a non-zero
base, installs it in the FDPIC register around the call and restores it after;
a zero base, which is every non-module callback, takes the direct path
unchanged.

fdpic_base() exposes the test fdpic_callback() already makes internally --
whether the caller is a module -- for a site that has to decide before it
stores a pointer somewhere the register will no longer be correct.

fdpic_invoke() is the install-call-restore, in the same ARM-thumb inline asm
as the rest of fdpic.h.  It saves the register on the stack and keeps the push
8-byte aligned, and pins the argument in r0, so it asks the allocator for only
two free registers -- enough on builds that also reserve a frame pointer.  It
is safe against preemption: the FDPIC register is REG_PIC in the saved
context, preserved across a context switch, and base firmware reserves it so
no interrupt handler disturbs it.  A context switch or interrupt while the
callback runs therefore keeps the module's base.

Verified with CONFIG_SIG_EVTHREAD on two ARM cores: an RP2350 (Cortex-M33,
armv8-m) on hardware and mps2-an500 (Cortex-M7, armv7e-m) under QEMU.  On each
a module's mq_notify and timer_create SIGEV_THREAD callbacks run on the worker
and write a distinctive value into a module global, proving the base was
installed.  Removing just the register install makes the same callback
HardFault the board, confirming it is load-bearing.

Assisted-by: Claude Code:claude-opus-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-01 12:23:27 +02:00
..
addrenv sched/addrenv: do not dereference a NULL address environment 2026-07-28 10:14:44 +08:00
clock drivers/timers, sched/clock: use file_get/file_put 2026-07-11 17:22:07 -04:00
environ libc/environ: add interrupt context check to getenv 2026-01-21 10:37:55 +08:00
event sched/event: Fix uninitialized need_switch flag issue in event_post() 2025-11-04 21:43:59 +08:00
group sched/group: add getresuid, getresgid, setreuid, and setregid 2026-07-30 09:48:10 +08:00
hrtimer sched/hrtimer: Simplify the hrtimer. 2026-02-02 13:26:22 +08:00
init arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y 2026-06-16 17:07:32 +08:00
instrument gprof: move gprof function from sched to libbuiltin/libgcc 2024-11-13 02:42:31 +08:00
irq !include/fcntl.h: align open flags with Linux values 2026-06-30 13:43:44 +08:00
misc sched: avoid dumping raw memory at address 0 for tasks without kstack 2026-07-20 20:57:45 -03:00
module !include/fcntl.h: align open flags with Linux values 2026-06-30 13:43:44 +08:00
mqueue sched: Run a module's SIGEV_THREAD notification with its data base. 2026-08-01 12:23:27 +02:00
paging include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
pthread !sys/types.h: change time_t and clock_t to int64_t to align with other OSes 2026-05-19 16:21:28 +08:00
sched sched/sched: Fix uninitialized sporadic params returned by nxsched_get_param() 2026-08-01 11:19:50 +08:00
semaphore arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y 2026-06-16 17:07:32 +08:00
signal sched: Run a module's SIGEV_THREAD notification with its data base. 2026-08-01 12:23:27 +02:00
task libc, sched: Resolve FDPIC descriptors at module callback entry points. 2026-08-01 12:23:27 +02:00
timer sched: Run a module's SIGEV_THREAD notification with its data base. 2026-08-01 12:23:27 +02:00
tls include/fcntl.h: remove O_RDOK/O_WROK aliases 2026-06-28 09:10:11 -03:00
wdog !sys/types.h: change time_t and clock_t to int64_t to align with other OSes 2026-05-19 16:21:28 +08:00
wqueue include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
CMakeLists.txt sched: migrate to SPDX identifier 2024-09-12 01:10:14 +08:00
Kconfig arch, sched/signal: Fix compilation with ENABLE_PARTIAL_SIGNALS=y 2026-06-16 17:07:32 +08:00
Makefile tools: fix stale archive members surviving a Kconfig-driven CSRCS change 2026-07-28 21:26:03 -03:00