</>EmbeddedInterview

What does the Linux kernel do from head.S to starting the init process?

HardEmbedded Linux & Boot

Short answer: head.S sets up the early page tables, enables the MMU and jumps to start_kernel() in init/main.c. start_kernel() initialises the architecture (setup_arch), memory management, the scheduler, interrupts, timers and the console, then calls rest_init(), which creates kernel_init (it becomes PID 1) and kthreadd (PID 2) and turns the boot thread into the idle task (PID 0). kernel_init runs all driver initcalls, mounts the root filesystem and execs /sbin/init.

1. Early assembly (arch/arm64/kernel/head.S)

  • Record the boot arguments (DTB address in x0).
  • Check the exception level; set up EL2 for KVM if entered there.
  • Create identity-mapped and kernel page tables, enable the MMU.
  • Set up the initial stack and jump to start_kernel() (via __primary_switched).

On 32-bit ARM with a zImage, a decompressor runs first and then jumps to the real kernel head.S.

2. start_kernel() (key calls, in roughly this order)

Call What it does
setup_arch(&command_line) parse the device tree, find memory, early page tables, CPU features
setup_command_line, parse_args process bootargs
mm_core_init() (older: mm_init()) buddy allocator, slab, vmalloc
sched_init() runqueues, the idle task
early_irq_init(), init_IRQ() interrupt descriptors, the GIC driver
rcu_init() read-copy-update
time_init(), timers, timekeeping_init() clocksource and clockevents (arch timer)
console_init() early console so printk output appears
calibrate_delay() BogoMIPS for delay loops
rest_init() hand over to the threads below

3. rest_init()

  • user_mode_thread(kernel_init, ...) creates PID 1 (it starts as a kernel thread and later becomes the user-space init).
  • kernel_thread(kthreadd, ...) creates PID 2, the parent of all other kernel threads.
  • The boot CPU then calls cpu_startup_entry() and becomes the idle task (PID 0), which runs when nothing else is runnable.

4. kernel_init()

  • kernel_init_freeable(): brings up the other CPUs (SMP), runs do_basic_setup() → do_initcalls(): every driver and subsystem *_initcall() in level order, which is when platform drivers probe against device tree nodes.
  • Unpacks the initramfs (if any); if it has /init, runs it. Otherwise prepare_namespace() mounts the root device from root=.
  • Frees the __init memory ("Freeing unused kernel memory").
  • Tries init= from the command line, then /sbin/init, /etc/init, /bin/init, /bin/sh. If none work: "Kernel panic - not syncing: No working init found."

Follow-up: "Is PID 1 a kernel thread?" It begins as one and becomes a normal user process when kernel_execve() loads the init binary, so it keeps PID 1.