This article was reproduced from: HTTPS://GITHUB.COM/TONGXINV/ONEBOOK/ISSUES/9
Linux kernel segment attribute mechanism
Taking Subsys_initcall and Module_init as an example
Subsys_initcall is a macro that is defined in linux/init.h
. After the macro is expanded, it is discovered that the function of the macro is to put its declared function into a specific segment:.initcall4.init
subsys_initcall __define_initcall("4",fn,4)
The following files are in/include/linux/init.h:
Analyzing the Module_init macro, you can see that it puts the function into the .initcall6.init
segment
module_init __initcall device_initcall __define_initcall("6",fn,6)
Open the/arch/arm/kernel/vmlinux.lds file in the compiled kernel source tree (not compiled without this file):
sections{. = 0xC0000000 + 0x00008000;. Init: { /* Init Code and data*/_stext =.; _sinittext =* (. head.text) * (. init.text) * (. Cpuinit.text) * (. Meminit.text) ... = ALIGN (+) .* (. Init.setup) __setup_end = .; __initcall_start = * (. initcallearly.init) __early_initcall_end = . * (. initcall0.init) * (. initcall0s.init) * (. initcall1.init) Span class= "Pl-k" >* (. initcall1s.init) * (. Initcall2.init) ... __initcall_end =
The kernel needs to do a lot of things in sequence during the boot process, how the kernel implements a number of initialization operations in order of precedence. The kernel solution is to classify all functions to be called when the kernel is started, execute the kernel function and each class will be invoked in a certain order. These classification names are called. Initcallx.init. The value of x is from 1 to 8. Kernel developers when writing kernel code, as long as the function set the appropriate level, these functions will be linked to the time of the specific segment, the kernel starts with a sequence of steps to execute the individual segments (through a function, the link script only specifies the location of a program segment in memory).
Kernel Source code:
The following files are in/INIT/MAIN.C
initcall_t __initcall_start[], __initcall_end[], __early_initcall_end[]; do_initcalls (void) {initcall_t *fn; for (fn = __early_initcall_end; fn < __initcall_end; fn++)Do_one_initcall (*FN); */Flush_scheduled_work ();}
Execution Do_initcalls will be executed in a set order, through the contents of the function can be guessed that the principle is the link script set up the order, and then do_initcalls execution will be in accordance with the link script set in the order of the traversal.
After analysis, it can be seen that the role of Subsys_initcall and Module_init is the same, but the former function is declared earlier than the latter in the kernel when the execution sequence of the boot
In addition: Do_initcalls How to be called, simply see the process of lowering
Start_kernel (), Rest_init ()->kernel_init () Do_basic_setup ()->do_initcalls ()
Linux kernel segment attribute mechanism "Go"