Skip to content

Processes, threads and user programs (kernel/proc/)

Source: kernel/proc/

(Writing a program: see user/README.md. This page is the kernel's side.)

How a file on the disk becomes a program running in ring 3: in its own address space, unable to touch the kernel, talking to it only through system calls, and ended by the kernel when it faults. A program is a process (what it owns) with one or more threads (what runs).

atlOS:/> run /disk0/BIN/HELLO.ELF
Hello from user mode!
running at 0x00000080000000d0
[pid 1 HELLO.ELF exited with 0]
atlOS:/> run /disk0/BIN/ASK.ELF
What is your name? Ada
Hello, Ada! I am process 2.
[pid 2 ASK.ELF exited with 0]
atlOS:/> run /disk0/BIN/THREADS.ELF
threads: counter 8
threads: thread ids 0x0000000100000003 0x0000000100000004
threads: all as expected
[pid 1 THREADS.ELF exited with 0]

What is it for?

So far everything ran in ring 0: any bug could overwrite any memory, any loop could stop the machine. A user program runs with the CPU's privilege level 3, where:

  • pages without the user bit fault (the whole kernel half, the kernel's identity map),
  • privileged instructions (cli, hlt, mov cr3, in/out) raise #GP,
  • interrupt gates with DPL 0 can not be entered with int.

The kernel gets control back on every interrupt, exception and system call: the CPU switches to the kernel stack named in the TSS (rsp0) and to ring 0 code through the IDT. A fault in a program ends that program only.

How could it be designed?

Question Options
Executable format ELF (Unix, Linux) · flat binary (objcopy -O binary) · PE (Windows)
Address spaces one per program, kernel half shared · one for all, programs at different addresses (no isolation between them)
Entering ring 3 iretq with a made up frame · sysret
System calls int 0x80 (a software interrupt: simple, slower) · syscall/sysret (MSRs, fast; Linux x86_64) · sysenter (32-bit)
Process and thread a process owns the resources, threads run in it (Windows, Mach, FreeBSD) · every thread is a task that shares resources with others by reference counts, clone(flags) decides what (Linux)
Who runs a thread a kernel thread that drops to ring 3 (1:1: the scheduler sees every thread) · user level threads on one kernel thread (N:1, "green threads") · M user threads on N kernel threads (M:N, tried and dropped by Solaris and FreeBSD)
Ending a program exitProcess, the end of the main thread, or a fault; every thread ends, the memory is freed by someone else
Files of a program a small table of open files per process, the index is the file descriptor (Unix) · handles to kernel objects (Windows) · no state: every call names the path and the offset
Keyboard input raw bytes to whoever reads, the program echoes · a "cooked" line mode in the kernel (the Unix terminal: echo, Backspace, the line on Enter)

How atlOS does it

Building a program (user/)

Static ELF64 executables in C++17, built with the same cross compiler, no libc: user/lib/crt0.S (_start: hand the stack to atlos_start, which calls main and passes its result to exitProcess), user/lib/atlos.hpp (the system call wrappers), user/user.ld. The system call numbers, errors and auxv types are one header both sides include: common/abi/abi.hpp (Abi::). The linker script puts the code at userBase = 0x0000008000000000 (PML4[1]) and the data in the next 2 MB page, with -z max-page-size=0x200000: protection is per 2 MB page, so code (read + execute) and data (read + write) must not share one. The disk image gets them as /BIN/HELLO.ELF, /BIN/FAULT.ELF, /BIN/BADPTR.ELF.

$ readelf -l build/default/user/hello.elf
  LOAD  offset 0x000  vaddr 0x0000008000000000  filesz 0x1b8  memsz 0x1b8  R E
  LOAD  offset 0x1c0  vaddr 0x00000080002001c0  filesz 0x017  memsz 0x01c  RW

Loading (elf.cpp, process.cpp)

parseElf only reads numbers out of the file and checks them: the ELF magic, 64-bit little endian, an executable for x86_64, program headers inside the file, every PT_LOAD segment inside the file and inside the user range, no two segments in one 2 MB page, the entry point inside an executable segment. The file is input from outside; nothing is trusted.

Runtime::spawn(path, argc, argv, heapSize) then:

  1. creates an AddressSpace(pmm, kernel): the kernel half and the identity map shared, the user range empty;
  2. for every segment allocates its 2 MB pages (zeroed: a program must never see what a page held before; the zeros are also its .bss), user and, for data, writable; copies the file bytes in through the physmap, because the new address space is not active;
  3. maps one writable page for the stack, its top at 0x00007FFFFFFFFFF0, and writes argc, argv and the strings onto it (pushArguments, see below);
  4. maps the heap: heapSize rounded up to 2 MB pages (2 MB by default, the shell's run -heap MB for more, at most 64 MB), zeroed and writable, right behind the last segment. The program's malloc divides it up; how, is in common/heap/README.md;
  5. starts the main thread (number 0, see below): a kernel Thread whose saved cr3 is the new address space and whose owner is the process.

The pid is the lowest free one (1, 2, ...; 0 is the kernel). A finished program's number comes back for the next one. Linux and Windows hand out numbers further on instead, so a stale pid does not name a new process at once; atlOS keeps the simplest rule.

Processes and threads (process.hpp)

A process is a container: it owns what its threads share. A thread is what runs: registers, a stack, a state. The process itself never runs; the scheduler only ever sees threads, all of them in one pool, and does not look at which process a thread belongs to (no priorities, no groups: a program with ten threads gets about ten times the CPU of one with one).

            Scheduler: one list of every thread, round robin
            +-----------------------------------------------------+
            |  boot  idle  t3  T(1.0)  T(1.1)  T(2.0)  T(1.2) ... |
            +-----------------------------------------------------+
                               | TCB::owner
             +-----------------+------------------+
      Process 1 (THREADS.ELF)              Process 2 (ASK.ELF)
      - address space (cr3)                - address space
      - files[], heap, exit code           - ...
      - threads: 1.0 (main), 1.1, 1.2      - threads: 2.0 (main)
Process (Process::Process) Thread (Process::UserThread + its kernel Thread)
Holds the address space, open files, heap, pid, thread list, exit code registers, its user stack page, its kernel stack, state, exit code
Runs? no yes
Shared nothing with other processes everything of its process: memory, files

How real systems split it:

  • Linux has no process structure: every thread is a task_struct, and the resources hang off it (mm_struct the address space, files_struct the files), shared by reference counts. clone(flags) decides what a new task shares: fork shares nothing, pthread_create passes CLONE_VM | CLONE_FILES | CLONE_THREAD | .... A "process" is a thread group; its pid is the group's id (getpid), each thread has its own (gettid).
  • Windows splits it like atlOS: EPROCESS (handle table, address space, token) never runs, ETHREAD does. CreateProcess makes a process and its primary thread, CreateThread more threads.
  • Mach / macOS (task and thread) and FreeBSD (proc and thread) do the same.

Process 0 is the kernel. The kernel threads (the boot flow, idle, the shell's helpers, test threads) belong to it: their TCB::owner is nullptr. Runtime::kernelProcess() is its record (pid 0, the kernel address space); the scheduler keeps its threads, so it has no thread list of its own, and it never ends. Windows has the same idea (the System process, pid 4); Linux's kernel threads are tasks without an mm.

What a process holds (struct Process): pid, name, space, the heap, threads (every thread not yet waited for, an intrusive list), mainThread, threadCount (threads that have not ended), nextThreadNumber, stackPages, openFileCount, files[], and its end: dying, exited, exitCode, done.

Thread ids

A thread id (Abi::ThreadId) is 64 bits: the pid above, the thread's number in its process below.

 63          32 31                          0
+--------------+----------------------------+
|     pid      |  number in the process     |    0x00000001_00000003: thread 3 of pid 1
+--------------+----------------------------+

The main thread is number 0, so its id is pid << 32. Numbers count up and are never reused while the process lives: an old id never names a new thread of the same process. Kernel threads are 0.1, 0.2, ... (tasks prints ids as pid.number). Bit 63 stays 0, so an id is never mistaken for a negative error. Linux takes thread ids from the same pool as pids and Windows its thread and process ids from one pool too; neither encodes the process in the thread id.

Thread stacks

Each thread has two stacks: a kernel stack (its kernel Thread's 2 MB page, where interrupts and system calls run) and a user stack in the process's address space. User stacks are slots below the main stack, each a 2 MB page with an unmapped 2 MB guard below it:

0x00007FFFFFE00000  slot 0: the main thread's stack (stackTop at its top)
0x00007FFFFFC00000  guard (unmapped)
0x00007FFFFFA00000  slot 1
0x00007FFFFF800000  guard
0x00007FFFFF600000  slot 2
                    ...

A stack grows down. A thread that runs off its page lands in the guard, a page fault, and the kernel names it: (stack overflow: thread 1 of pid 3 ran into its guard page). Without the guard it would silently write into the next thread's stack. The guard costs only addresses, no memory.

Nothing is taken in advance: a new thread gets the lowest free slot, its user stack page and its kernel stack when it starts, and gives them back when it is waited for (or with the process). There is no limit but memory: when the physical memory manager has fewer than three free 2 MB blocks, startThread returns -ENOMEM (the kernel stack's allocation would panic, so the check comes first).

Starting a thread

startThread(trampoline, entry, arg) (system call 9) starts a thread at trampoline(entry, arg): the kernel puts entry and arg in rdi and rsi (enterUserMode's third and fourth arguments) and rsp at the top of the slot minus 8, where the page's zero is the return address. The trampoline (user/lib/thread.cpp) calls entry(arg) and passes what it returns to exitThread, so a thread may simply return. Windows starts every thread in ntdll!RtlUserThreadStart, glibc in start_thread, macOS in _pthread_start, for the same reason; the kernel does not know the function, the program hands its address in.

sequenceDiagram
    participant M as main thread (ring 3)
    participant K as Runtime (kernel)
    participant S as Scheduler
    participant T as new thread (ring 3)
    M->>K: int 0x80: startThread(trampoline, worker, arg)
    K->>K: lowest free slot, map its stack page, kernel Thread
    K->>S: addTask (Ready)
    K-->>M: rax = tid (pid << 32 | number)
    M->>S: yield / sleep / wait ...
    S->>T: threadEntry: enterUserMode(trampoline, rsp, worker, arg)
    T->>T: worker(arg) returns 30
    T->>K: int 0x80: exitThread(30)
    K->>S: Threading::exitThread(30): waiters wake, the thread ends

Waiting for a thread

waitThread(tid) (system call 11) sleeps until the thread has ended and returns its exit code. It is built on the kernel Thread::wait() (kernel/sched/README.md): the ended thread wakes whoever waits for it.

A thread that has ended keeps a small record, its exit code, until someone waits for it: a zombie, like a Unix child process nobody has waited for. Its stacks are freed when the wait returns (or with the process). The User::Threading::Thread object waits in its destructor if nobody has (std::jthread does the same), so a program that keeps to the class leaves no zombies; there is no "detach".

waitThread on Result
itself -EDEADLK
a thread of another process, or an unknown one -ESRCH
the main thread, or a thread another one already waits for -EINVAL
two threads waiting for each other both sleep for good: a deadlock (Linux does not catch it either)

Ending a thread and a program

What What ends
a thread returns, or calls exitThread(code) that thread; its code is what waitThread returns
the main thread returns from main, or calls exitThread the program: atlos_start passes main's result to exitProcess, and the end of the main thread ends the process
any thread calls exitProcess(code) the program
a fault in any thread the program, with 128 + vector
Ctrl+C while a thread reads the keyboard the program, with 130

(Linux and Windows let a process live on after its main thread ended with pthread_exit/ExitThread; in atlOS the main thread's end is the program's end, which is easier to follow.)

A program does not stop all its threads at once: it is marked dying (startDying), and each thread ends itself the next time it would go back to ring 3. With one CPU and a cooperative scheduler, a thread of the program that is not running is always in one of two places: inside a system call that waits (yield, sleep, read, waitThread), or not started yet. So two checks are enough: at the end of systemCall, and in threadEntry before entering ring 3. Linux checks for pending signals on every return to user space (preemption means a thread can be anywhere); Windows delivers an APC that ends the thread.

sequenceDiagram
    participant A as thread A (calls exitProcess)
    participant R as Runtime
    participant B as thread B (sleeping)
    A->>R: exitProcess(5)
    R->>R: dying = true, exitCode = 5; B's sleep ends at once
    R->>A: endCurrentThread: threadCount 2 -> 1, exitThread
    Note over B: B runs next: sleep returns
    B->>R: end of systemCall: dying? yes
    R->>B: endCurrentThread: threadCount 1 -> 0
    R->>R: exited, print "[pid .. exited with 5]", give done

A sleeping thread is woken at once (its wake tick is set to 0). A thread waiting for another thread wakes when that one ends. A thread waiting for the keyboard ends at the next key, and two threads waiting for each other never end: there is no cancellable wait yet, so such a program can not be fully ended (see "Deliberately not done").

The last thread to end marks the process exited, prints [pid 1 HELLO.ELF exited with 0] and gives done. Each thread switches CR3 back to the kernel's tables before it ends, since the program's tables may be freed before it is switched away from for the last time.

Entering ring 3 (arch/x86_64/cpu/usermode.S)

A program thread's first code is Runtime::threadEntry, which calls enterUserMode(entry, stackPointer, rdi, rsi): it pushes the frame iretq expects, as if an interrupt had come from ring 3, clears every register but the two arguments (0 for a main thread) and returns into it:

push 0x23          ss      user data segment (GDT 4) | RPL 3
push stackPointer  rsp     it points at argc
push 0x202         rflags  IF set: interrupts stay on
push 0x1b          cs      user code segment (GDT 3) | RPL 3
push entry         rip
iretq

argc and argv: the start stack

A program's main(argc, argv) gets its arguments the way every Unix hands them over, the System V ABI start stack. pushArguments writes it at the top of the stack page (through the physmap, the address space is not active yet), and rsp starts at argc:

0x00007FFFFFFFFFF0  stackTop
                    "two words\0"                   the strings, argv[0] lowest
                    "one\0"
0x00007FFFFFFFFFCE  "/disk0/BIN/ARGS.ELF\0"
                    (0 .. 15 bytes: rsp must be 16 byte aligned)
                    0, 0                    auxv: AT_NULL, the end
                    0x1001, 0x200000        AT_ATLOS_HEAP_SIZE: 2 MB
                    0x1000, 0x8000400000    AT_ATLOS_HEAP_BASE: behind the data
                    6, 0x200000             AT_PAGESZ: 2 MB pages
                    0            envp[0]: NULL (no environment)
                    0            argv[3]: NULL
                    0x...FFFE6   argv[2]
                    0x...FFFE2   argv[1]
                    0x...FFFCE   argv[0]
rsp  ->             3            argc

Each auxv entry is a pair (type, value). crt0.S hands rsp to atlos_start (user/lib/start.cpp), which reads argc and argv, walks past envp to the auxv, sets up the heap from it and calls main(argc, argv). The pointers in argv[] are addresses in the program's address space, the strings are copies: the program may change them.

  • The shell's run P.ELF a "b c" passes argv = { "/disk0/.../P.ELF", "a", "b c" }: the resolved path, then the words (quotes keep spaces).
  • At most 16 arguments and 1024 bytes of strings (maxArguments, maxArgumentBytes); more is refused before the program starts.
  • The environment is there but empty. The auxiliary vector has the page size (AT_PAGESZ, the System V number) and the heap (atlOS's own numbers, far above Linux's); Linux also passes e.g. AT_ENTRY and AT_PHDR, and the variables (PATH=...) in envp.

Back into the kernel

When an interrupt, an exception or int 0x80 arrives in ring 3, the CPU loads rsp from TSS.rsp0 before pushing anything: the top of the program thread's kernel stack. The scheduler sets rsp0 on every switch (GDT::setKernelStack), so each program thread lands on its own stack.

sequenceDiagram
    participant P as program (ring 3)
    participant CPU
    participant S as isr.S stub
    participant I as intHandler
    participant R as Runtime
    P->>CPU: int $0x80 (rax = 1 write, rdi = 1, rsi = buffer, rdx = size)
    CPU->>CPU: gate 0x80 has DPL 3: allowed; rsp = TSS.rsp0
    CPU->>S: push ss, rsp, rflags, cs, rip
    S->>I: push registers, call intHandler(frame)
    I->>R: systemCall(frame)
    R->>R: buffer in the user range and mapped User? print it
    R-->>I: frame.rax = size (or -EFAULT ...)
    I-->>S: return
    S->>P: pop registers (rax = the result), iretq to ring 3

System calls (int 0x80)

The number in rax, the arguments in rdi, rsi, rdx, the result in rax. A negative result is an error, with the Linux numbers (-2 is -ENOENT).

rax Call Result
0 exitProcess(code) does not return; every thread of the program ends
1 write(fd, buffer, size) bytes written; -EBADF, -EINVAL (more than 4096 bytes), -EFAULT, -EIO
2 read(fd, buffer, size) bytes read, 0 at the end of a file; -EBADF, -EINVAL, -EFAULT, -EIO
3 open(path, flags) a file descriptor (3 and up); -ENOENT, -EISDIR, -EMFILE, -EFAULT, -ENAMETOOLONG, -EINVAL
4 close(fd) 0; -EBADF
5 yield() 0, after the other runnable threads had their turn
6 sleep(ms) 0, at least ms milliseconds later
7 currentProcess() the process id
8 uptime() milliseconds since boot (the scheduler's ticks)
9 startThread(trampoline, entry, arg) the new thread's id; -EFAULT (trampoline not in the user range), -ENOMEM
10 exitThread(code) does not return; the main thread's ends the program
11 waitThread(tid) its exit code once it has ended; -ESRCH, -EDEADLK, -EINVAL
12 currentThread() the calling thread's id

An unknown number returns -ENOSYS. The gate is an interrupt gate with DPL 3; every other gate has DPL 0, so int $0x21 from a program is a #GP.

Runtime::systemCall is one switch over rax, then the dying check. A call that waits (read from the keyboard, sleep, yield, waitThread) waits on the calling thread's own kernel stack, inside the interrupt handler: the scheduler switches to another thread and back later, then the handler returns to ring 3 as usual. The interrupt flag is part of each thread's saved state, so the other threads run with interrupts on while this one waits with them off.

File descriptors

fd What
0 standard input: the keyboard and the serial terminal
1, 2 standard output and error: the screen and the serial terminal
3 .. 10 files from open: Process::files[fd - 3], shared by every thread of the program

An open file (OpenFile) is its path, its offset and how it was opened. The VFS reads and writes by path and offset (Vfs::read(path, offset, ...)), so read and write pass both and move the offset by what was transferred.

  • flags: one of O_RDONLY, O_WRONLY, O_RDWR, plus O_CREAT (create it if missing), O_TRUNC (start empty: the FAT16 code has no truncate, so the file is removed and created again), O_APPEND (each write goes to the current end).
  • Paths start at the root (/disk0/...): a program has no current directory. A relative path is -ENOENT.
  • The path is copied out of the program byte by byte, each byte checked with isUserAccessible, up to 128 bytes with the terminating 0.
  • At exit nothing has to be flushed (every write went to the disk), so the table goes away with the process.

The keyboard: read(0, ...)

The shell and a program read the same CharacterQueue (the keyboard and COM1 feed it). run waits for the program (Runtime::wait), so while it runs the shell does not read and the keys go to the program.

read(0, buffer, size) sleeps until a key is there, then returns it and the keys already queued behind it, at most size. The bytes are raw: nothing is echoed, Backspace arrives as 0x7f (DEL) or 0x08, and a program that wants a line editor writes one (user/programs/ask.cpp). Two exceptions: Enter arrives as \n (a terminal sends \r), and Ctrl+C (0x03) ends the program with 130, like a Unix shell's 128 + SIGINT.

Waiting for a program

Only the kernel starts and waits for programs (the shell's run); a program can not start or wait for another one. Runtime::wait(pid) takes the process's done semaphore, which its last thread gives, then frees the program (reap) and returns its exit code. The waiting thread sleeps meanwhile, so the CPU halts when the program waits too (e.g. for a key).

User pointers. A program can pass any number as a pointer. Before write reads the buffer, AddressSpace::isUserAccessible checks that every byte is in the user range and mapped with the user bit at every table level. Without that check a program could make the kernel print (or later, write) kernel memory for it. (A real kernel also has to care that another thread could unmap the memory while the kernel reads it; atlOS is cooperative.)

The heap and threads. The threads of a program share one heap, and malloc has no lock. It needs none here: malloc makes no system call, so with a cooperative scheduler no other thread can run in the middle of it. A preemptive kernel would interrupt it anywhere, and the heap would need a lock (glibc's malloc has per-thread arenas and locks).

Faults and freeing

  • A breakpoint (int3) in ring 3 reports the registers and the program goes on.
  • A fault in ring 3 (frame.cs & 3 == 3): the exception is reported as usual (Page fault (in user mode), the decoded error code; a stack overflow names the thread) and the program ends with 128 + vector, e.g. 142 for a page fault, every thread of it: memory the threads share may be broken. The kernel does not panic.
  • A thread can not free its own kernel stack while running on it, so the exited program (threads, address space with all its pages and tables) is freed by another thread: Runtime::wait or Runtime::reap.

The boundary global

The int 0x80 handler and the ring 3 fault path are reached from the assembly stubs, which can not be handed an object. kernelMain binds the runtime once with Process::setRuntime, like Handlers::setInterruptController (see docs/style.md).

Deliberately not done

  • No syscall/sysret: int 0x80 goes the way every interrupt goes.
  • No preemption: a program that never makes a system call keeps the CPU for good (the timer only counts ticks). Only yield, sleep and a read that waits let other threads run.
  • No lseek, stat, directory listing, unlink or rename calls.
  • A program can not start or wait for another program (no fork/exec, spawn/wait system calls); only the kernel's shell does. No environment variables.
  • No cancellable wait: a dying program's thread that waits for the keyboard ends only at the next key (which it takes), and two threads waiting for each other never end; such a program can not be fully ended, and run waits for it for good. A wait that can be cancelled ("why did I wake up") is the next step in kernel/sync.
  • No user mode mutex (Linux's futex): with a cooperative scheduler a program's threads only switch where they call the kernel.
  • No thread detach, no thread local storage, no per-thread signal masks (there are no signals).
  • At most 64 programs at once (maxProcesses), not counting the kernel.
  • The heap is fixed at the start: no brk/sbrk, no mmap, and its pages are mapped whether the program uses them or not.
  • No NX bit: data pages are executable too.
  • Each thread's kernel stack is a whole 2 MB page: with its user stack, a thread costs 4 MB of memory.

Tests

  • ELF64 parsing: made up headers: a good two segment file, then each thing the loader must refuse.
  • Address spaces: a second address space, user pages, flags, activate.
  • User programs (ring 3, test disk): runs HELLO.ELF (output, exit code 0), FAULT.ELF (writes to address 0: a protection violation, exit code 142, no kernel panic), BADPTR.ELF (kernel, kernel only, unmapped and overlong pointers, a wrong fd and an unknown call are all refused), ASK.ELF with keys pushed into the queue first (a Backspace in them, then Ctrl+C: exit code 130), CLOCK.ELF (uptime, sleep, yield), FILES.ELF (open/read/write/close, O_TRUNC, O_APPEND, and the errors: a missing file, a relative path, a directory, a kernel pointer, the wrong access mode, closing twice, every descriptor in use) with the file it wrote read back through the VFS, files that are not programs, two programs at once at the same addresses, and that every page and table comes back. scripts/check-test-disk.sh reads USER.TXT with mtools too. ARGS.ELF with three arguments (one with a space; the strings at the top of the stack page, argv[argc] NULL, exit code 3), CAT.ELF on two paths (one missing) and without any, 16 arguments, and 17, a too long one and none refused. MALLOC.ELF with a 4 MB heap (mapped behind the data, two pages and not three, its output), without a heap (malloc returns NULL), and a heap above 64 MB refused. THREADS.ELF (threads share a counter, arguments and exit codes, ids with the pid above, separate stacks, exitThread, new in a thread, the destructor that waits, a global constructor, and what waitThread refuses), the next pid is the lowest free one, THREXIT.ELF main (main returns while a thread spins and one sleeps: the program ends with 3), THREXIT.ELF worker (a worker's exitProcess(5) ends main as it waits), OVERFLOW.ELF (a thread's endless recursion hits its guard page: 142, the thread named).
  • The shell test (scripts/run-shelltest.py) runs HELLO.ELF, FAULT.ELF (the shell still answers afterwards), types a name into ASK.ELF, ends it once with Ctrl+C, runs CLOCK.ELF and FILES.ELF and cats the file, ARGS.ELF one "two words" and CAT.ELF /disk0/HELLO.TXT, THREADS.ELF, THREXIT.ELF main, OVERFLOW.ELF and ps.

References

  • Intel SDM Vol. 3A: "Privilege Levels", "Calls to Interrupt or Exception Handlers" (stack switch, the TSS), "IRET", "Paging" (the U/S bit)
  • System V ABI, x86-64 supplement: process initialization (the stack at entry); ELF-64 Object File Format
  • OSDev Wiki: Getting to Ring 3, ELF, System Calls
  • xv6 book (MIT 6.1810): chapters "Traps and system calls", "Page tables" (exec)
  • Operating Systems: Three Easy Pieces: "Limited Direct Execution", "Concurrency: An Introduction" (threads), "Thread API"
  • Linux: clone(2), pthreads(7) (the thread group, CLONE_THREAD)
  • Windows Internals, Part 1: "Processes and jobs", "Threads" (EPROCESS, ETHREAD, RtlUserThreadStart)