Linux内核启动过程
Linux Version: 4.9
ARM32
1. /arch/arm/boot/uncompressed/head.S - with no compressed image, skip this step
2. /arch/arm/kernel/head.S
3. /Jump to start_kernel() - /init/main.c
##Note: something can be omitted
asmlinkage __visible void __init start_kernel(void)
{
char *command_line;
char *after_dashes;
smp_setup_processor_id(); ====> (/arch/arm/kernel/setup.c) pr_info("Booting Linux on physical CPU 0x%x\n", mpidr);
pr_notice("%s", linux_banner); ====> print the linux_banner
setup_arch(&command_line); ====> parse the command_line and the machine type ==> goto #1A
setup_command_line(command_line); ====> init the command_line
pr_notice("Kernel command line: %s\n", boot_command_line);
setup_log_buf(0);
mm_init(); ====> memory init
/* Do the rest non-__init'ed, we're now alive */
rest_init(); =====> go to #2A
}
#####
#1A
#########
/arch/arm/kernel/setup.c
void __init setup_arch(char **cmdline_p)
{
const struct machine_desc *mdesc;
setup_processor(); ===> pr_info("CPU: %s [%08x] revision %d (ARMv%s), cr=%08lx\n"
mdesc = setup_machine_fdt(__atags_pointer); ====> find the machine type by fdt(device tree) ==> go to #1A1
if (!mdesc)
mdesc = setup_machine_tags(__atags_pointer, __machine_arch_type);====> find the machine type by tags
machine_desc = mdesc;
machine_name = mdesc->name;
dump_stack_set_arch_desc("%s", mdesc->name);
parse_early_param(); ====> parse_early_options(tmp_cmdline);====>goto #1A2
#ifdef CONFIG_MMU
early_paging_init(mdesc);====> MMU init
#endif
unflatten_device_tree(); ====> unflatten device tree to global parameter [struct device_node *of_root]
}
#####
#1A1
#########
/arch/arm/kernel/devicetree.c
const struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
{
if (!dt_phys || !early_init_dt_verify(phys_to_virt(dt_phys))) ====> Check the invalid of the physical address from u-boot
return NULL;
mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach); ====> find the match machine type
if (!mdesc) {
early_print("\nError: unrecognized/unsupported "
"device tree compatible list:\n[ ");
dump_machine_table(); /* does not return */ ====> dump the valid machine type, !!!!!if this occur, not return and hang on
}
early_init_dt_scan_nodes(); ==> parse the boot command_line [bootargs=ttyS0,115200 earlyprintk]
==> parse Initialize {size,address}-cells info
==> parse /* Setup memory, calling early_init_dt_add_memory_arch */
}
#####
#1A2
#########
##After register the earlyprintk, then early_printk() and printk would be valid,
##It can output the log to the console directly, not store the buffer and output later.
void __init parse_early_options(char *cmdline)
{
parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
do_early_param);
}
static int __init do_early_param(char *param, char *val,
const char *unused, void *arg)
{
const struct obs_kernel_param *p;
for (p = __setup_start; p < __setup_end; p++) {
if ((p->early && parameq(param, p->str)) ||
(strcmp(param, "console") == 0 &&
strcmp(p->str, "earlycon") == 0)
) {
if (p->setup_func(val) != 0)
pr_warn("Malformed early option '%s'\n", param);
}====> here would register the earlycon ==> go to #1A2.a
}
/* We accept everything at this stage. */
return 0;
}
#####
#1A2.a
#########
Log:
/arch/arm/kernel/early_printk.c
static void early_console_write(struct console *con, const char *s, unsigned n)
{
early_write(s, n);
}
static struct console early_console_dev = {
.name = "earlycon",
.write = early_console_write,
.flags = CON_PRINTBUFFER | CON_BOOT,
.index = -1,
};
static int __init setup_early_printk(char *buf)
{
early_console = &early_console_dev;
register_console(&early_console_dev); ====> /kernel/printk.c
return 0;
}
early_param("earlyprintk", setup_early_printk);
#####
#2A
#########
static noinline void __ref rest_init(void)
{
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
kernel_thread(kernel_init, NULL, CLONE_FS); ====> init the fock thread with kernel_init ==>go to #2A1
}
#####
#2A1
#####
static int __ref kernel_init(void *unused)
{
kernel_init_freeable(); ====> go to #2A1.1
/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* trying to recover a really broken machine.
*/
if (execute_command) {
ret = run_init_process(execute_command);
if (!ret)
return 0;
panic("Requested init %s failed (error %d).",
execute_command, ret);
}
if (!try_to_run_init_process("/sbin/init") ||
!try_to_run_init_process("/etc/init") ||
!try_to_run_init_process("/bin/init") ||
!try_to_run_init_process("/bin/sh")) ====> run system mount, initrd? initramfs?
return 0;
}
#####
#2A1.1
#########
static noinline void __init kernel_init_freeable(void)
{
do_basic_setup(); --> driver_init(); =====
}
/**
* driver_init - initialize driver model.
*
* Call the driver model init functions to initialize their
* subsystems. Called early from init/main.c.
*/
void __init driver_init(void)
{
/* These are the core pieces */
devtmpfs_init();
devices_init();
buses_init();
classes_init();
firmware_init();
hypervisor_init();
/* These are also core pieces, but must come after the
* core core pieces.
*/
platform_bus_init(); ====> do platform init, after this, the module and driver would be loaded
cpu_dev_init();
memory_dev_init();
container_dev_init();
of_core_init();
}
Linux Version: 4.9
ARM32
1. /arch/arm/boot/uncompressed/head.S - with no compressed image, skip this step
2. /arch/arm/kernel/head.S
3. /Jump to start_kernel() - /init/main.c
##Note: something can be omitted
asmlinkage __visible void __init start_kernel(void)
{
char *command_line;
char *after_dashes;
smp_setup_processor_id(); ====> (/arch/arm/kernel/setup.c) pr_info("Booting Linux on physical CPU 0x%x\n", mpidr);
pr_notice("%s", linux_banner); ====> print the linux_banner
setup_arch(&command_line); ====> parse the command_line and the machine type ==> goto #1A
setup_command_line(command_line); ====> init the command_line
pr_notice("Kernel command line: %s\n", boot_command_line);
setup_log_buf(0);
mm_init(); ====> memory init
/* Do the rest non-__init'ed, we're now alive */
rest_init(); =====> go to #2A
}
#####
#1A
#########
/arch/arm/kernel/setup.c
void __init setup_arch(char **cmdline_p)
{
const struct machine_desc *mdesc;
setup_processor(); ===> pr_info("CPU: %s [%08x] revision %d (ARMv%s), cr=%08lx\n"
mdesc = setup_machine_fdt(__atags_pointer); ====> find the machine type by fdt(device tree) ==> go to #1A1
if (!mdesc)
mdesc = setup_machine_tags(__atags_pointer, __machine_arch_type);====> find the machine type by tags
machine_desc = mdesc;
machine_name = mdesc->name;
dump_stack_set_arch_desc("%s", mdesc->name);
parse_early_param(); ====> parse_early_options(tmp_cmdline);====>goto #1A2
#ifdef CONFIG_MMU
early_paging_init(mdesc);====> MMU init
#endif
unflatten_device_tree(); ====> unflatten device tree to global parameter [struct device_node *of_root]
}
#####
#1A1
#########
/arch/arm/kernel/devicetree.c
const struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
{
if (!dt_phys || !early_init_dt_verify(phys_to_virt(dt_phys))) ====> Check the invalid of the physical address from u-boot
return NULL;
mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach); ====> find the match machine type
if (!mdesc) {
early_print("\nError: unrecognized/unsupported "
"device tree compatible list:\n[ ");
dump_machine_table(); /* does not return */ ====> dump the valid machine type, !!!!!if this occur, not return and hang on
}
early_init_dt_scan_nodes(); ==> parse the boot command_line [bootargs=ttyS0,115200 earlyprintk]
==> parse Initialize {size,address}-cells info
==> parse /* Setup memory, calling early_init_dt_add_memory_arch */
}
#####
#1A2
#########
##After register the earlyprintk, then early_printk() and printk would be valid,
##It can output the log to the console directly, not store the buffer and output later.
void __init parse_early_options(char *cmdline)
{
parse_args("early options", cmdline, NULL, 0, 0, 0, NULL,
do_early_param);
}
static int __init do_early_param(char *param, char *val,
const char *unused, void *arg)
{
const struct obs_kernel_param *p;
for (p = __setup_start; p < __setup_end; p++) {
if ((p->early && parameq(param, p->str)) ||
(strcmp(param, "console") == 0 &&
strcmp(p->str, "earlycon") == 0)
) {
if (p->setup_func(val) != 0)
pr_warn("Malformed early option '%s'\n", param);
}====> here would register the earlycon ==> go to #1A2.a
}
/* We accept everything at this stage. */
return 0;
}
#####
#1A2.a
#########
Log:
/arch/arm/kernel/early_printk.c
static void early_console_write(struct console *con, const char *s, unsigned n)
{
early_write(s, n);
}
static struct console early_console_dev = {
.name = "earlycon",
.write = early_console_write,
.flags = CON_PRINTBUFFER | CON_BOOT,
.index = -1,
};
static int __init setup_early_printk(char *buf)
{
early_console = &early_console_dev;
register_console(&early_console_dev); ====> /kernel/printk.c
return 0;
}
early_param("earlyprintk", setup_early_printk);
#####
#2A
#########
static noinline void __ref rest_init(void)
{
/*
* We need to spawn init first so that it obtains pid 1, however
* the init task will end up wanting to create kthreads, which, if
* we schedule it before we create kthreadd, will OOPS.
*/
kernel_thread(kernel_init, NULL, CLONE_FS); ====> init the fock thread with kernel_init ==>go to #2A1
}
#####
#2A1
#####
static int __ref kernel_init(void *unused)
{
kernel_init_freeable(); ====> go to #2A1.1
/*
* We try each of these until one succeeds.
*
* The Bourne shell can be used instead of init if we are
* trying to recover a really broken machine.
*/
if (execute_command) {
ret = run_init_process(execute_command);
if (!ret)
return 0;
panic("Requested init %s failed (error %d).",
execute_command, ret);
}
if (!try_to_run_init_process("/sbin/init") ||
!try_to_run_init_process("/etc/init") ||
!try_to_run_init_process("/bin/init") ||
!try_to_run_init_process("/bin/sh")) ====> run system mount, initrd? initramfs?
return 0;
}
#####
#2A1.1
#########
static noinline void __init kernel_init_freeable(void)
{
do_basic_setup(); --> driver_init(); =====
}
/**
* driver_init - initialize driver model.
*
* Call the driver model init functions to initialize their
* subsystems. Called early from init/main.c.
*/
void __init driver_init(void)
{
/* These are the core pieces */
devtmpfs_init();
devices_init();
buses_init();
classes_init();
firmware_init();
hypervisor_init();
/* These are also core pieces, but must come after the
* core core pieces.
*/
platform_bus_init(); ====> do platform init, after this, the module and driver would be loaded
cpu_dev_init();
memory_dev_init();
container_dev_init();
of_core_init();
}