The Two-Phase Birth of Every Process
The commit runs on privileged ISA machinery: the new world is an Sv39 page table of PTEs with R/W/X permission bits, the stack keeps 16-byte alignment because the RISC-V calling convention requires it, and the trapframe's epc and sp fields are the registers sret will resume from. The article names PTE_R and PTE_U, the uvmalloc guarantee, and the two register fields that main(argc, argv) falls out of.
No processor internals are measured or reasoned about: the build-then-commit discipline, the four ELF sanity checks, and the three-assignment swap are supervisor-mode code identical on any hart running xv6, so the mechanism is judged at the ISA and kernel layers.
Machine mode never appears: the private page table is built and committed entirely in supervisor mode, with no firmware behavior in the path.
The entire mechanism is xv6 kernel code: kexec and loadseg in kernel/exec.c, proc_pagetable and uvmclear in kernel/vm.c, all read firsthand from the xv6-getscount checkout. The two phases exist so a failed exec can never leave a process half-born.
No algorithm is presented: the article walks a syscall implementation with a fixed order of checks and a single commit block, not a procedure with an invariant to prove.
Every process on xv6 is born the same way. init forks, the child calls exec with a path, and 176 lines of kernel/exec.c take over. The first thing exec does is not touch the running process at all. It opens the executable under begin_op, finds the inode with namei, and reads the ELF header, checking the magic 0x464C457F, the little-endian spelling of the four bytes 0x7F, E, L, F [1].
Then it builds a brand-new page table, private and uncommitted, and walks the program headers. Only ELF_PROG_LOAD segments get loaded, and each one must pass four sanity checks: memsz is not smaller than filesz, because the zero-filled remainder is what bss becomes; vaddr plus memsz must not wrap around; and vaddr must be page-aligned. Permissions come from flags2perm, which reads only two ELF bits, bit 0 for execute and bit 1 for write, because uvmalloc always adds PTE_R and PTE_U on its own, so a page can never be born kernel-only or unreadable by accident [1].
Photo: grendelkhan, CC BY-SA 2.0, via Wikimedia Commons
loadseg fills each page with readi from the inode, but before a single byte is copied it looks the page up with walkaddr and panics with loadseg: address should exist if the mapping is missing. The mapping exists first, the bytes second [1]. The stack gets its own rite: the size is rounded up, one extra page is allocated beyond the user stack pages and cleared by uvmclear into an inaccessible guard page, and the argument strings are copied down from the top of the stack with 16-byte alignment, because the RISC-V calling convention requires it [2].
Only when the new world is complete comes the commit. The source comment names the phase: Commit to the user image. Three assignments swap the page tables, the trapframe's epc is set to elf.entry and its sp to the new stack top, the old table is freed, and argc returns as the syscall value, landing in a0 with argv in a1, so main(argc, argv) is not a convention anyone agreed on, it is just what falls out of two registers [1].
The commit, in six lines:
// Commit to the user image.
oldpagetable = p->pagetable;
p->pagetable = pagetable;
p->sz = sz;
p->trapframe->epc = elf.entry;
p->trapframe->sp = sp;
The running process is never touched while its replacement is built. The file could be corrupt, the disk could fail, memory could run dry, and the old process would still be standing there, mid-instruction, untouched. The new world is complete before it is committed, or it never exists at all.
Sources
- 01dillingerstaffing/xv6-getscount kernel/exec.cChris Dillinger
kexec (kernel/exec.c): the ELF magic check, the program-header walk with four sanity checks, flags2perm reading two ELF bits, loadseg's walkaddr-first discipline and 'address should exist' panic, the stack rite with the uvmclear guard page, the 'Commit to the user image' block, and the bad: cleanup path. Read firsthand.
- 02RISC-V ISA Manual, privileged architectureRISC-V International
Sv39 page-table entries with the V/R/W/X permission bits, sret resuming through sepc keyed off sstatus SPP and SPIE, and the 16-byte stack alignment of the RISC-V calling convention.