411 REFERENCE · Wikibooks
The Linux Kernel/System/Booting
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Booting and halting
Boot sequence
Power-on self-test Firmware/POST – hardware initialization, device detection
GRUB Bootloader (GRUB, systemd-boot, EFI stub admin-guide/efi-stub.html) – loads kernel image into memory
Kernel – decompresses, initializes, starts 1 init
Kernel booting
This is loaded in two stages - in the first stage the kernel (as a compressed image file) is loaded into memory and decompressed, and a few fundamental functions such as essential hardware and basic memory management (memory paging) are set up.
Control is then switched one final time to the main kernel start process calling start_kernel, which then performs the majority of system setup (interrupts, the rest of memory management, device and driver initialization, etc.) before spawning separately, the idle process and scheduler, and the init process (which is executed in user space).
Kernel loading stage
The kernel as loaded is typically an image file, compressed into either zImage or bzImage formats with zlib.
A routine at the head of it does a minimal amount of hardware setup, decompresses the image fully into high memory, and takes note of any RAM disk if configured.
It then executes kernel startup via startup_64 (for x86_64 architecture).
arch/x86/boot – boot protocol, CPU feature detection (x86 Feature Flags arch/x86/cpuinfo.html)
arch/x86/boot/compressed/vmlinux.lds.S - linker script defines entry startup_64 in
arch/x86/boot/compressed/head_64.S - assembly of extractor
extract_kernel - extractor in language C
prints
Decompressing Linux... done.
Booting the kernel.
Kernel startup stage
The startup function for the kernel (also called the swapper or process 0) establishes memory management (paging tables and memory paging), detects the type of CPU and any additional functionality such as floating point capabilities, and then switches to non-architecture specific Linux kernel functionality via a call to start_kernel.
↯ Startup call hierarchy:
arch/x86/kernel/vmlinux.lds.S – linker script
arch/x86/kernel/head_64.S – assembly of uncompressed startup code
arch/x86/kernel/head64.c – platform dependent startup:
x86_64_start_kernel
x86_64_start_reservations
init/main.c – main initialization code
init/init_task.c – initial task (PID 0, swapper), init_task
init/do_mounts.c – root device mounting (mount_root, prepare_namespace)
init/do_mounts_initrd.c – initrd (initial ramdisk) unpacking and pivot to real root
init/do_mounts_rd.c – RAM disk image mounting (legacy rd= parameter)
init/initramfs.c – built-in initramfs (cpio) unpacking (populate_rootfs)
init/calibrate.c – BogoMIPS delay loop calibration (calibrate_delay)
init/version.c – kernel version string and build timestamp (linux_banner)
init/noinitramfs.c – default root mount when no initramfs is configured
start_kernel 200 SLOC
mm_init
mem_init
vmalloc_init
sched_init
rcu_init – Read-copy-update
rest_init
kernel_init - deferred kernel thread #1
kernel_init_freeable This and following functions are defined with attribute __init
prepare_namespace
initrd_load
mount_root
run_init_process obviously runs the first process 1 init
kthreadd – deferred kernel thread #2
cpu_startup_entry
do_idle
🛠️ Utilities
8 dracut – generate initramfs images; dracut --list-modules shows available modules
1 lsinitrd – list contents of an initramfs image
start_kernel executes a wide range of initialization functions. It sets up interrupt handling (IRQs), further configures memory, starts the 1 init process (the first user-space process), and then starts the idle task via cpu_startup_entry. Notably, the kernel startup process also mounts the initial ramdisk (initrd) that was loaded previously as the temporary root file system during the boot phase. The initrd allows driver modules to be loaded directly from memory, without reliance upon other devices (e.g. a hard disk) and the drivers that are needed to access them (e.g. a SATA driver). This split of some drivers statically compiled into the kernel and other drivers loaded from initrd allows for a smaller kernel. The root file system is later switched via a call to 8 pivot_root / 2 pivot_root which unmounts the temporary root file system and replaces it with the use of the real one, once the latter is accessible. The memory used by the temporary root file system is then reclaimed.
⚙️ Internals
arch/x86/Kconfig.debug – x86 debugging Kconfig options
linux/smpboot.h
kernel/smpboot.c – common SMP CPU bringup/teardown
arch/x86/kernel/smpboot.c – x86 SMP booting
linux/kallsyms.h, kernel/kallsyms.c – the /proc/kallsyms kernel symbol table
...
📖 References
Article about booting of the kernel
Initial RAM disk admin-guide/initrd.html
initcall_debug, boot argument
Linux startup process
init
Linux (U)EFI boot process
The kernel’s command-line parameters admin-guide/kernel-parameters.html
The EFI Boot Stub admin-guide/efi-stub.html
Boot Configuration admin-guide/bootconfig.html
Boot time memory management core-api/boot-time-mm.html
Kernel booting process
Kernel initialization process
📚 Further reading
Boot-time tracing trace/boottime-trace.html
E820
💾 Historical
http://tldp.org/HOWTO/Linux-i386-Boot-Code-HOWTO/
http://www.tldp.org/LDP/lki/lki-1.html
http://www.tldp.org/HOWTO/KernelAnalysis-HOWTO-4.html
Halting or rebooting
🔧 TODO
⚲ API
linux/reboot.h
linux/stop_machine.h
print_stop_info
stop_machine
stop_core_cpuslocked
2 reboot ↪ sys_reboot
machine_restart or
machine_halt or
machine_power_off
2 kexec_load ↪ kernel/kexec.c – load a new kernel for later execution (kdump (Linux) kdump)
reboot_mode
linux/reboot-mode.h
reboot_mode_driver
devm_reboot_mode_register
⚙️ Internals
kernel/reboot.c – reboot, halt and power-off
kernel/stop_machine.c – stop all CPUs for safe code patching
cpu_stopper
cpu_stop_init
cpu_stopper_thread – "migration" tasks
arch/x86/kernel/reboot.c – x86 reboot methods
The_Linux_Kernel/Softdog Driver
Power management
Keyword: suspend, alarm, hibernation.
⚲ API
/sys/power/
/sys/kernel/debug/wakeup_sources
⌨️ hands-on:
sudo awk '{gsub("^ ","?")} NR>1 {if ($6) {print $1}}' /sys/kernel/debug/wakeup_sources
linux/pm.h
linux dev_pm_ops
linux/pm_qos.h
linux/pm_clock.h
linux/pm_domain.h
linux/pm_wakeirq.h
linux/pm_wakeup.h
wakeup_source
wakeup_source_register
linux/suspend.h
pm_suspend suspends the system
linux/freezer.h – process freezer for suspend/hibernate
Suspend and wakeup depend on
2 timer_create and 2 timerfd_create with clock ids CLOCK_REALTIME_ALARM or CLOCK_BOOTTIME_ALARM will wake the system if it is suspended.
2 epoll_ctl with flag EPOLLWAKEUP blocks suspend
See also 7 capabilities CAP_WAKE_ALARM, CAP_BLOCK_SUSPEND
⚙️ Internals
CONFIG_PM
CONFIG_SUSPEND
kernel/power – suspend, hibernate and PM core
alarm_init
kernel/time/alarmtimer.c – alarm timer for wakeup events
drivers/base/power: wakeup_sources
📖 References
PM administration admin-guide/pm
CPU and Device PM driver-api/pm
Power Management power
sysfs power testing ABI admin-guide/abi-testing.html#file-testing-sysfs-power
https://lwn.net/Kernel/Index/#Power_management
PowerTOP
cpupower
tlp – apply laptop power management settings
ACPI – Advanced Configuration and Power Interface
Runtime PM
Keywords: runtime power management, devices power management opportunistic suspend, autosuspend, autosleep.
⚲ API
/sys/devices/.../power/:
async autosuspend_delay_ms control runtime_active_kids runtime_active_time runtime_enabled runtime_status runtime_suspended_time runtime_usage
linux/pm_runtime.h
pm_runtime_mark_last_busy
devm_pm_runtime_enable
pm_runtime_enable
pm_runtime_disable
pm_runtime_get – asynchronous get
pm_runtime_get_sync
pm_runtime_resume_and_get – preferable synchronous get
pm_runtime_put
pm_runtime_put_noidle – just decrement usage counter
pm_runtime_put_sync
pm_runtime_put_autosuspend
SET_RUNTIME_PM_OPS
👁 Example: ac97_pm
⚙️ Internals
CONFIG_PM_AUTOSLEEP
...
📖 References
Runtime Power Management Framework for I/O Devices power/runtime_pm.html
CPU idle power saving methods for real-time workloads
Sysfs devices PM API
Power Management for USB
Opportunistic suspend
📚 Further reading
https://deepwiki.com/torvalds/linux/5-hardware-drivers