Starting alone
A boot loader has copied the kernel into memory and jumped to it. The CPU is in kernel mode, running one thing: the kernel's main(). No free-memory list, trap table or process exists yet. Each line of main() builds one.
Building the parts
kinit() cuts the free RAM into pages of 4096 bytes and puts every page the kernel is not using onto a free list. From now on, every page the kernel or a program needs is taken from this list.
kvminit() takes a page from the free list and builds the kernel's page table: the map from the addresses code uses to real places in RAM. Then it writes the table's address into the CPU register satp, which turns paging on.
procinit() sets up the process table: 64 slots in xv6, one per possible process, each marked UNUSED. A process will be a filled-in slot plus its own memory, so this table must exist before any process can.
trapinithart() writes the address of the kernel's trap handler into the CPU register stvec. System calls, faults and interrupts will all enter the kernel at that one place. A program cannot change it: that needs kernel mode.
plicinit() sets up the interrupt controller, which passes device signals to the CPU. The timer, armed just before main(), fires about a tenth of a second in xv6. So the kernel can always take the CPU back.
virtio_disk_init() starts the disk driver. The lines just before it set up the buffer cache, the inode table and the file table. Now the kernel can read files from the disk, such as the first program.
The first process
No process exists to call fork(), so userinit() makes the first one by hand. It fills an empty slot with process ID 1, takes free pages for its page table and saved registers, and marks it RUNNABLE.
scheduler() never returns. It loops forever, choosing a RUNNABLE process and switching the CPU to it. Right now there is only one: process 1. The kernel's start-up code is finished.
Still in kernel mode, process 1 first loads the program /init from the disk into its own pages. Then it returns from a trap: the CPU drops to user mode, and /init starts the shell. Linux does the same with /init or /sbin/init.
Each part needs ones built before it. A process needs free pages and a table slot. Running it safely needs the trap table, interrupts and the disk. Only when all exist does the kernel hand the CPU to a program.
What happens
;; step 1/11 · power-on xv6's main() on the first CPU (abridged): 1 kinit(); // free page list 2 kvminit(); // kernel page table 3 procinit(); // process table 4 trapinithart(); // trap table 5 plicinit(); // interrupts 6 virtio_disk_init(); // disk driver 7 userinit(); // process 1 8 scheduler(); // run it mode kernel (the machine starts in kernel mode) exists only the kernel code itself
The card is xv6's main(), the small teaching kernel used in MIT 6.1810, cut to one line per part. Each line stands for one or two real calls (kvminit and kvminithart, for example), kept in their real order; the console is set up first and is not shown. The figure draws four pages and three process-table slots. A real machine has many thousands of pages, and xv6 has 64 slots. The disk driver and other parts take pages from the list too; the drawing shows only the kernel page table and process 1.