Linux system porting: Kernel startup process

Linux system porting: Kernel startup process

To start the linux kernel code, first look at his link script vmlinux.lds file, the file is as follows:

20220321193507

Indicates a function entity entry: stext function, located in arch/arm/kernel/head.S

There is a segment comment on the function entry

20220321193847

It is stipulated that MMU, D-cache, r0=0, r1=machine nr (that is, machine ID), r2=atags or the first address of the device tree (dtb) should be closed before executing the function (r1, r2 are the parameters passed in by the function )

The function first performs some judgments and conversions to determine that the mode is in BE8 and uses Thumb instructions

20220321194431

Then use the safe_svcmode_maskall macro to shield IRQ and FIQ interrupts and switch to SVC mode; save the original CPSR value saved in r9 to SPSR

safe_svcmode_maskall r9

Then read the processor ID, the ID value is stored in the r9 register

20220321194836

Call the function __lookup_processor_type to check whether the current system supports this CPU, and if so, get the procinfo information to r5

20220321194950

The Linux kernel abstracts each processor into a proc_info_list structure, and each processor corresponds to a procinfo, so the corresponding procinfo structure is found through the processor ID

The following macro definitions perform different functions according to the definitions, skip these and do not look at them

Line 121, call the function __vet_atags to verify the validity of atags or device tree (dtb)

20220321195149

Then call __create_page_tables to create a page table and save the address of the function __mmap_switched to the r13 register

20220321203709

Finally, call the __enable_mmu function to enable the MMU, __enable_mmu turns on the MMU by calling __turn_mmu_on, and then __turn_mmu_on will execute the __mmap_switched function saved in r13 at the end, __mmap_switched finally calls start_kernel to start the Linux kernel

The start_kernel function is defined in the file init/main.c. This function completes some initialization work before Linux starts by calling many sub-functions. There are many sub-functions here, which will not be expanded in detail. These functions will set the stack size and initialize the stack. , configure some architecture-related code, complete some kernel initialization, and call the rest_init function after everything is ready

The rest_init function is defined in the file init/main.c

20220321204748

First call the function rcu_scheduler_starting to start the RCU lock scheduler

Then call the function kernel_thread to create the kernel_init process. The init process is a kernel process at first (that is, running in the kernel mode), and then the init process will look for a program named "init" in the root file system, and this "init" program is in user mode. , by running this "init" program, the init process will realize the transition from kernel mode to user mode

Then call the function kernel_thread to create the kthreadd kernel process, the PID of this kernel process is 2, and the kthreadd process is responsible for the scheduling and management of all kernel processes

Finally, the function cpu_startup_entry is called to enter the idle process. cpu_startup_entry will call cpu_idle_loop . came

Enter ps -A under Linux interrupt to see the init process and ktthreadd process

20220321205241

systemd is the init process

It is very important to use the init process. Some content is initialized in the process. According to the parameters of ramdisk_execute_command and execute_command, a runnable init program is found in the root file system. If all are empty, then execute "/sbin/init", "/ etc/init", "/bin/init" and "/bin/sh", the four backup init programs, if these four are not executed, it will be finished and the system will fail to start. In addition, the init process will also call The do_basic_setup function is used to complete the initialization of the device driver under Linux, complete the initialization of the driver model subsystem under Linux, and open the device "/dev/console" as the standard input and output of the system, realize command line interaction, and call the function prepare_namespace to Mount the root file system, so the do_basic_setup function is very important for the init process!

The above is a general process of linux kernel startup

Guess you like

Origin blog.csdn.net/qq_45396672/article/details/123646158