How Linux Kernel Boots: Decompression, Startup Phases, and Initialization Explained
This article explains the Linux kernel boot sequence on ARM platforms, covering how the compressed kernel is decompressed, the role of the stext entry point, the preparation steps performed by __switch_data, and the detailed initialization performed by start_kernel and its key sub‑functions.
Linux kernel self‑decompression
After U‑Boot finishes, the bootm command loads the Linux kernel into memory and calls do_bootm. If the kernel image is compressed, the entry code located at /kernel/arch/arm/boot/compressed/head.S invokes decompress_kernel(), prints “Uncompressing Linux…done, booting the kernel”, and then calls gunzip() (or unlz4(), bunzip2(), unlz()) to place the uncompressed kernel at its execution address.
Kernel startup preparation phase
The kernel link script /kernel/arch/arm/kernel/vmlinux.lds shows that the entry point after decompression is the stext symbol defined in /kernel/arch/arm/kernel/head.S. The following assembly snippet illustrates the early steps performed before stext is reached:
ENTRY(stext)
setmode PSR_F_BIT | PSR_I_BIT | SVC_MODE, r9 @ disable IRQ/FIQ, enter SVC mode
mrc p15, 0, r9, c0, c0 @ read processor ID
bl __lookup_processor_type @ get processor descriptor (r5)
... (additional checks for processor, machine, ATAGS) ...
bl __create_page_tables @ build coarse page tables
ldrr13, __switch_data @ jump to post‑decompression codeThe preparation steps are:
Disable IRQ/FIQ and switch to SVC mode.
Validate the processor ID; abort if unsupported.
Validate the machine code (board type); abort if unsupported.
Verify the ATAGS format passed from U‑Boot.
Create a coarse page table for early memory mapping.
Jump to __switch_data for final setup.
Final preparation in __switch_data
The __switch_data routine (and its helper __mmap_switched) copies the kernel’s data segment, clears the BSS, saves processor and machine identifiers, and stores the ATAGS pointer. It then branches to start_kernel:
__switch_data:
.long __mmap_switched
...
__mmap_switched:
ldr r3, __switch_data + 4
... @ copy data segment if needed
mov fp, #0 @ clear BSS
str r9, [r4] @ save processor ID
str r1, [r5] @ save machine type
str r2, [r6] @ save ATAGS pointer
b start_kernel
ENDPROC(__mmap_switched)Linux kernel initialization phase
The initialization begins at start_kernel, the universal entry point for all Linux platforms. It performs hardware‑specific setup, then proceeds with a series of subsystem initializations before finally launching the first user‑space process init.
Key responsibilities of start_kernel
Architecture‑specific and generic configuration initialization.
Memory management subsystem setup.
Process management structures initialization.
Scheduler initialization.
Network subsystem preparation.
Virtual file system (VFS) initialization.
File‑system specific initialization.
Important functions called from start_kernel
setup_arch(&command_line) : Initializes architecture‑specific parameters, parses early boot tags, and performs early memory subsystem setup. command_line contains the kernel command line passed from U‑Boot (or defaults from .config).
setup_command_line , parse_early_param , parse_args : Parse and store command‑line arguments such as console=ttySAC2,115200, root=/dev/mmcblk0p2 rw, init=/linuxrc, and rootfstype=ext3.
sched_init : Initializes the scheduler, creates the run‑queue, and sets up the idle task.
rest_init : Starts two kernel threads ( kernel_init and kthreadd), mounts the root file system, and executes the first user process init. If the init path from the command line is invalid, it falls back to /sbin/init, /etc/init, /bin/init, or /bin/sh. It then enables the scheduler and launches the idle process.
Illustrative diagrams
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
