nuttx/include
Marco Casaroli a229e8b87e sched/arch/libc: give fork(), vfork() and task_fork() separate semantics
NuttX implemented fork() and vfork() as the same function.  Both were libc
wrappers around a single up_fork() syscall; vfork() differed only by a
trailing waitpid().  Underneath, the child joined the parent's address
environment -- the same addrenv_join() that pthread_create() uses -- and got
a private copy of the stack.  So the child shared .data, .bss and the heap
with its parent and ran concurrently with it.

That is not fork().  It is vfork()-with-a-private-stack under fork()'s name,
and the history says so: today's fork() is NuttX's old vfork(), renamed in
c33d1c9c97 (2023) without any change of behaviour.  The failure was silent --
a program written against POSIX fork() compiled, ran, and had its child's
writes land in the parent's variables.

Separate them into three primitives, chosen by which function the caller
called rather than by what the hardware happens to be:

  fork()       child gets its own copy of the parent's memory at the same
               virtual addresses; runs concurrently.  Only where an address
               environment can be duplicated -- elsewhere it is not declared
               at all, so calling it is a build error naming the function.
  vfork()      child shares the parent's memory; parent suspended until the
               child _exit()s or exec()s.  Implementable everywhere.
  task_fork()  the historical behaviour under an honest name: shares memory,
               private stack copy, both running.  Non-POSIX, in sched.h.

Below libc there are now three syscalls -- up_task_fork(), up_vfork() and
up_fork().  The per-arch register snapshot is common to all three; each
architecture's entry points share one sequence and differ only in a
FORK_TYPE_* selector (include/nuttx/fork.h) handed to nxtask_setup_fork(),
which is the single place the memory semantics are decided.

The vfork() parent suspension moves out of libc into nxtask_start_vfork(),
released from nxsched_release_tcb().  Two things follow: the parent is
resumed at exec(), since exec_swap() has already handed the child's pid to
the loaded program by the time the vfork stub exits, and vfork() no longer
depends on CONFIG_SCHED_WAITPID.

Releasing there requires one fix in nxtask_exit().  It raises rtcb->lockcount
directly rather than through sched_lock() while it tears the TCB down, so the
nxsem_post() that wakes the vfork() parent queues it on g_pendingtasks -- and
the matching raw lockcount-- does not merge that list the way sched_unlock()
would, leaving the parent stranded with nothing left to move it off.  A
nxsched_merge_pending() after the decrement publishes it.  The call is a
no-op while pre-emption is still disabled, and up_exit() re-reads this_task()
afterwards, so a change of the ready-to-run head is honoured.  Without it
vfork() deadlocks on any configuration where no other task happens to call
sched_unlock() afterwards -- rv-virt:nsh64 and rv-virt:pnsh64, for instance,
where NSH is blocked in waitpid() holding the lock.

fork() is built on a new addrenv_fork(), backed by an up_addrenv_fork() hook
that duplicates an address environment into freshly allocated pages mapped at
the same virtual addresses -- unlike up_addrenv_clone(), which copies only
the representation and leaves both pointing at the same page tables.  The
child then adopts the parent's stack geometry rather than being given a
relocated copy: a pointer to a stack local taken before fork() must name the
same object in the child that it named in the parent, and the parent's stack
is already in the duplicate, with its contents, at the parent's address.

No architecture implements up_addrenv_fork() yet, so this commit leaves
fork() unavailable everywhere.  That is the intended state.  It withdraws
fork() from ARCH_ARM, flat ARCH_ARM64, ARCH_RISCV, ARCH_SIM and ARCH_X86_64,
where until now it named the sharing primitive; per-architecture patches
restore it, with POSIX semantics, as up_addrenv_fork() lands.  Nothing is
lost in the meantime: task_fork() is that same sharing primitive under its
own name, and CONFIG_FORK_IS_TASK_FORK (default n) aliases fork() back to it
for legacy code, on exactly the configurations that had fork() before.

Kconfig: ARCH_HAVE_TASK_FORK and ARCH_HAVE_VFORK inherit ARCH_HAVE_FORK's
select lines, conditions included, so no configuration gains machinery;
ARCH_HAVE_FORK is redefined to mean "can provide POSIX fork() semantics" and
derives from the new ARCH_HAVE_ADDRENV_FORK.

There is one deliberate departure from "verbatim".  ARCH_ARM selected the
fork family unconditionally, BUILD_KERNEL included, and that has never
worked:  on a kernel build the architecture's fork entry point sees the
kernel's return address and stack pointer rather than the caller's, so the
child resumes at a kernel address.  On qemu-armv7a:knsh master faults in
ostest's task_fork case with "Child did not run" and then a data abort;
without the condition this change faults the same way through vfork().
ARCH_ARM64 and ARCH_X86_64 already carried "if !BUILD_KERNEL" for exactly
this reason -- ARM was the outlier.  Conditioning it turns a runtime fault
into an honest absence, which is the whole point of the change; arch/arm
takes the condition off again in the patch that adds its saved-syscall-frame
path.  Only the MMU-capable ARM ports are affected, since Cortex-M cannot
build BUILD_KERNEL at all.

Also fixes two latent syntax errors found on the way: a missing comma in
riscv_fork.c and mips_fork.c, both in *_FRAMEPOINTER && !SAVE_GP branches
that are never compiled today.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-29 16:39:07 +02:00
..
android
arpa
crypto crypto: add CRYPTO_AES_CTR_SSH variant (128-bit big-endian counter) 2026-07-16 15:42:10 +08:00
cxx include/cxx/ctime: Add localtime to std namespace. 2026-06-09 11:33:40 -03:00
net forward: Add IFF_NOSRC_FORWARD and IFF_NODST_FORWARD flags. 2026-01-31 02:37:58 +08:00
netinet netinet/in.h: Rename imr_interface to imr_address in struct ip_mreqn. 2026-01-16 09:42:12 +08:00
netpacket net/packet: add PACKET_<ADD|DROP>_MEMBERSHIP support 2025-12-25 10:01:43 +08:00
nuttx sched/arch/libc: give fork(), vfork() and task_fork() separate semantics 2026-07-29 16:39:07 +02:00
ssp
sys sched/arch/libc: give fork(), vfork() and task_fork() separate semantics 2026-07-29 16:39:07 +02:00
.gitignore ci: add stdbit.h test 2026-06-29 14:44:17 +02:00
aio.h
alloca.h
assert.h
byteswap.h
ctype.h
debug.h style: fix checkpatch issues after debug.h move 2026-04-07 07:50:06 -03:00
dirent.h fs/dirent: add d_ino member to struct dirent 2026-06-26 10:45:33 -04:00
dlfcn.h
dsp.h libs/libdsp: Add Matrix operations 2026-07-11 14:55:59 -03:00
dspb16.h
elf.h
elf32.h
elf64.h
endian.h
err.h
errno.h include/errno.h: skip set_errno in interrupt context 2026-05-03 17:23:40 -03:00
execinfo.h
fcntl.h !include/fcntl.h: align open flags with Linux values 2026-06-30 13:43:44 +08:00
fixedmath.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
fnmatch.h
ftw.h
gcov.h
getopt.h
glob.h
grp.h libc/grp: add getgrouplist() 2026-06-23 23:09:22 +08:00
hex2bin.h
iconv.h
ifaddrs.h
imx_container.h
inttypes.h !sched/clock: remove CONFIG_SYSTEM_TIME64 and always use 64-bit time 2026-05-19 16:21:28 +08:00
iso646.h
langinfo.h
libgen.h
libintl.h
limits.h !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
locale.h
lzf.h lzf: prevent lzf header struct optimization 2026-02-13 11:58:50 +01:00
malloc.h
mqueue.h
netdb.h
nl_types.h
nxflat.h
obstack.h
poll.h
pthread.h include/pthread : initialize wait_count in PTHREAD_COND_INITIALIZER 2026-06-16 19:12:30 -03:00
pty.h
pwd.h
regex.h
resolv.h
sched.h sched/arch/libc: give fork(), vfork() and task_fork() separate semantics 2026-07-29 16:39:07 +02:00
search.h LICENSE: update NuttX-PublicDomain SPDX identifier 2025-12-26 19:46:12 +08:00
semaphore.h
shadow.h
signal.h sched/timer: Fix MISRA Rule 10.4 2026-01-22 22:14:00 +08:00
spawn.h sched/spawn: Fix MISRA C 2012 Rule 10.4 violations 2026-02-02 20:39:36 +08:00
stdbool.h
stddef.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
stdint.h arm64_head.S: fix the asm code build error 2026-01-13 21:20:52 +08:00
stdio.h stdio.h: Update TMP_MAX definition to match Linux. 2026-01-14 17:28:30 -08:00
stdlib.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
stdnoreturn.h
string.h include/string.h: mark memset and memcpy as used_code 2026-02-12 13:19:06 -05:00
strings.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
syscall.h
syslog.h
termios.h
threads.h
time.h
ulimit.h ulimit: add ulimit implementation 2026-01-16 10:03:53 +08:00
unistd.h sched/arch/libc: give fork(), vfork() and task_fork() separate semantics 2026-07-29 16:39:07 +02:00
utime.h
uuid.h
wait.h
wchar.h
wctype.h