Because in the next level is quite limited, the improper place, also hoped that everybody criticizes the correction ^_^
Based on Linux 2.6.32
The include\asm-generic\vmlinux.lds.h is defined as follows:
This is tantamount to arranging a series of section sequences of names such as. Initcall<level>[s].init, the smaller the level, the more forward.
The symbol __initcall_start records the beginning of the area, and the __initcall_end symbol records the end of the area.
__early_initcall_end the area further into two paragraphs.
#define Initcalls \
* (. initcallearly.init) \
Vmlinux_symbol (__early_initcall_end) =.; \
* (. initcall0.init) \ * (
. initcall0s.init) \
* (. initcall1.init) \
* (. Initcall1s.init) \
* (. initcall2.init) \
* (. initcall2s.init) \
* (. initcall3.init) \
* (. initcall3s.init) \ * (
. initcall4.init) \
* (. initcall4s.init) \
* ( . initcall5.init) \
* (. initcall5s.init) \
* (. initcallrootfs.init) \
* (. Initcall6.init) \
* (. initcall6s.init) \
* (. initcall7.init) \
* (. initcall7s.init)
#define Init_calls \
vmlinux_symbol (__initcall_start) =.; \
initcalls \
vmlinux_symbol (__initcall_end) =.;
Let's see what's Inside these sections.
Take the Pci_driver_init function as an example (located in Drivers/pci/pci-driver.c).
static int __init pci_driver_init (void)
{return
bus_register (&pci_bus_type);
}
Postcore_initcall (Pci_driver_init);
The visible function follows a call to a Postcore_initcall macro that tracks the definition of the macro and gets the following code.
#define Postcore_initcall (FN) __define_initcall ("2", fn,2)
#define __define_initcall (level,fn,id) \
Static initcall_t __initcall_# #fn # #id __used \
__attribute__ (__section__ (". Initcall" level. Init ")) = fn
In this case, Postcore_initcall (Pci_driver_init), after the expansion, is actually the following content
Static initcall_t __initcall_pci_driver_init2 __attribute__ ((__used__)) \
__attribute__ (__section__ (". Initcall2.init ")) = Pci_driver_init;
And initcall_t is actually a function pointer type, defined as
typedef int (*initcall_t) (void);
In this case, the above macro expands, is actually defined a variable __initcall_pci_driver_init2, he is a function pointer, pointing to Pci_driver_init. This variable is linked to the section named. Initcall2.init.
Of course, in the kernel, there are some of the following macros similar to Postcore_initcall (located in include/linux/init.h).
Calling these macros results in the corresponding function pointer variables that are linked to the corresponding name, such as. Initcall<level>[s].init's section.
#define Pure_initcall (FN) __define_initcall ("0", fn,0)
#define Core_initcall (FN) __define_initcall ("1 ", fn,1)
#define CORE_INITCALL_SYNC (FN) __define_initcall (" 1s ", fn,1s)
#define Postcore_initcall (FN) __define_initcall ("2", fn,2)
#define POSTCORE_INITCALL_SYNC (FN) __define_initcall ("2s", fn,2s)
# Define Arch_initcall (FN) __define_initcall ("3", fn,3)
#define ARCH_INITCALL_SYNC (FN) __define_ Initcall ("3s", fn,3s)
#define Subsys_initcall (FN) __define_initcall ("4", fn,4)
#define Subsys_ Initcall_sync (FN) __define_initcall ("4s", fn,4s)
#define Fs_initcall (FN) __define_initcall ("5", fn , 5)
#define FS_INITCALL_SYNC (FN) __define_initcall ("5s", fn,5s)
#define Rootfs_initcall (FN) _ _define_initcall ("Rootfs", Fn,rootfs)
#define Device_initcall (FN) __define_initcall ("6", fn,6)
# Define Device_initcall_sync (FN) __define_initcall ("6s", fn,6s)
#define Late_initcall (FN) __define_ Initcall ("7", fn,7)
#define LATE_INITCALL_SYNC (FN) __define_initcall ("7s", fn,7s)
Here's the question, how are these function pointers invoked? Then look down.
When the kernel initializes, the Kernel_init function (located in INIT/MAIN.C) executes the following functions sequentially, completing the invocation of these initialization functions.
Visible, the invocation order is that the function pointers in the section with the smaller level are called first.
static void __init do_pre_smp_initcalls (void)
{
initcall_t *call;
for (call = __initcall_start, call < __early_initcall_end call++)
Do_one_initcall (*call);
}
static void __init do_initcalls (void)
{
initcall_t *call;
for (call = __early_initcall_end, call < __initcall_end call++)
Do_one_initcall (*call);
/* Make sure there are no pending stuff from Initcall sequence
/flush_scheduled_work ();
}