CPU_LWP_FORK(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’… CPU_LWP_FORK(9) β€” the kernel dev girlies' manual

okay so bestie, welcome to the Kernel Developer's Manual page for CPU_LWP_FORK(9) β€” yes that's the real section number, we're in the big leagues now πŸŽ€


✨ NAME ✨

cpu_lwp_fork, child_return, lwp_trampoline β€” finish a fork operation. like, the actual finishing touches, not just the vibe πŸ”₯


🌸 SYNOPSIS 🌸

#include <sys/proc.h>

void
cpu_lwp_fork(struct lwp *l1, struct lwp *l2, void *stack,
    size_t stacksize, void (*func)(void *), void *arg);

void
child_return(void *arg);

copy that #include exactly, bestie, no cap 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so cpu_lwp_fork() is the machine-dependent portion of fork1() that finishes a fork operation, with the child lwp l2 nearly set up β€” like, it's at 99% and we just need to hit send πŸ’… It copies and updates the PCB and the trap frame from the parent l1, making the child ready to run. literally that's the whole glow-up.

cpu_lwp_fork() rigs the child's kernel stack so that it will start in lwp_trampoline(). and lwp_trampoline() does not have a normal calling sequence β€” it's entered by cpu_switchto(), so don't come at it expecting a regular call, it's built different 😭

If an alternate user-level stack is requested β€” and that means non-zero values in both the stack and stacksize arguments, both of them, no half-sending β€” the user stack pointer is set up accordingly.

After being entered by cpu_switchto() and while running in user context (within the kernel), lwp_trampoline() will invoke the function func with the argument arg. iconic behavior honestly ✨

  • If a kernel thread is being created, the return path and argument are specified with func and arg.
  • If a user process is being created, fork1() will pass child_return() and l2 to cpu_lwp_fork() as func and arg respectively.

This causes the newly-created child process to go directly to user level with an apparent return value of 0 from fork(2), while the parent process returns normally. so the child's like "hiii i returned 0" and the parent's like "i returned your pid" β€” both slaying, different paths πŸ‘‘


🌈 SEE ALSO 🌈

read them, they're the supporting cast and they ate πŸ“


ngl this page is short but it's dense β€” every pointer matters. don't skip the "both non-zero" bit on stack and stacksize, that's the trap πŸ™ˆ

and that's the page, bestie!! this one came from Debian, dated April 26, 2018 β€” vintage but still serving πŸ’– k byeee, go fork something responsibly πŸ§šβ€β™€οΈβ­

πŸ“¦ where this page comes from