Mandrake9.0的啟動過程(從init開始)(一)
2024-07-21 02:35:18
供稿:網友
以閱讀源代碼的方式研究 linux的啟動過程,是我早已有之的心愿。今天總算是開工了。由于理解系統初始化過程要有匯編的基礎,所以我只好先從init開始。
init的源代碼在/usr/src/linux-2.4.19-9mdk/init目錄下,在這個目錄下共有三個文件do_mounts.c、main.c和version.c。其中main.c就是init進程的源代碼。這段代碼并不長,只有640行。
首先用ctags -x main.c 生成一個tags文件,用vi 打開后,可以看到各個函數的索引:
LPS_PREC macro 183 main.c #define LPS_PREC 8
MAX_INIT_ARGS macro 125 main.c #define MAX_INIT_ARGS 8
MAX_INIT_ENVS macro 126 main.c #define MAX_INIT_ENVS 8
__KERNEL_SYSCALLS__ macro 12 main.c #define __KERNEL_SYSCALLS__
argv_init variable 135 main.c static char *
argv_init[MAX_INIT_ARGS+2] = { "init", NULL, };
calibrate_delay function 185 main.c void __init
calibrate_delay(void)
checksetup function 160 main.c static int __init
checksetup(char *line)
child_reaper variable 498 main.c strUCt task_struct
*child_reaper = &init_task;
cols variable 131 main.c int rows, cols;
debug_kernel function 226 main.c static int __init
debug_kernel(char *str)
do_basic_setup function 521 main.c static void __init
do_basic_setup(void)
do_initcalls function 500 main.c static void __init
do_initcalls(void)
envp_init variable 136 main.c char *
envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, };
execute_command variable 133 main.c char *execute_command;
gr_setup function 148 main.c static int __init
gr_setup(char *str)
init function 603 main.c static int init(void
*unused)
loops_per_jiffy variable 178 main.c unsigned long
loops_per_jiffy = (1<<12);
parse_options function 254 main.c static void __init
parse_options(char *line)
profile_setup function 138 main.c static int __init
profile_setup(char *str)
quiet_kernel function 234 main.c static int __init
quiet_kernel(char *str)
rest_init function 389 main.c static void rest_init(void)
rows variable 131 main.c int rows, cols;
smp_init function 349 main.c static void __init
smp_init(void)
smp_init function 361 main.c static void __init
smp_init(void)
smp_init macro 354 main.c #define smp_init() do { }
while (0)
start_kernel function 401 main.c asmlinkage void __init
start_kernel(void)
wait_init_idle variable 344 main.c unsigned long
wait_init_idle;
有了這個索引后,查找函數就方便了。再用vi 打開main.c,找到init函數,如下:
源碼:-------------------------------------------------------
static int init(void * unused)
{
lock_kernel();
do_basic_setup();
prepare_namespace();
#ifdef CONFIG_GRKERNSEC
grsecurity_init();
#endif
/*
* Ok, we have completed the initial bootup, and
* we're essentially up and running. Get rid of the
* initmem segments and start the user-mode stuff..
*/
free_initmem();
unlock_kernel();
if (open("/dev/console", O_RDWR, 0) < 0)
printk("Warning: unable to open an initial console./n");
(void) dup(0);
(void) dup(0);
/*
* 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)
execve(execute_command,argv_init,envp_init);
execve("/sbin/init",argv_init,envp_init);
execve("/etc/init",argv_init,envp_init);
execve("/bin/init",argv_init,envp_init);
execve("/bin/sh",argv_init,envp_init);
panic("No init found. Try passing init= option to kernel.");
-----------------------------------------------------------
在源代碼中,可以看到很多如同#ifdef
CONFIG_GRKERNSEC的宏定義,這些宏定義可以在/usr/src/linux-2.4.19-9mdk/目錄下的.config文件中找到。
用vi
查看.config文件中的宏定義,發現"# CONFIG_GRKERNSEC is not
set",也就是沒有定義,因此,這個宏定義可以不管它。先來看執行流程。
一、do_basic_setup()函數
init進程第一個執行的函數是lock_kernel(),這個函數在很多內核的源代碼中都有,但我沒有找到它的函數定義,只好放棄。
第二個執行的函數就是do_basic_setup(),這個函數的內容如下:
源碼:-------------------------------------------------------
/*
* Ok, the machine is now initialized. None of the devices
* have been touched yet, but the CPU subsystem is up and
* running, and memory and process management works.
*
* Now we can finally start doing some real work..
*/
static void __init do_basic_setup(void)
{
/*
* Tell the world that we're going to be the grim
* reaper of innocent orphaned children.
*
* We don't want people to have to make incorrect
* assumptions about where in the task array this
* can be found.
*/
child_reaper = current;
#if defined(CONFIG_MTRR) /* Do this after SMP initialization */
/*
* We should probably create some architecture-dependent "fixup after
* everything is up" style function where this would belong better
* than in init/main.c..
*/
mtrr_init();
#endif /*mtrr(Memory Type Range Register)是Inter P6系列處理器用來控制處理器讀寫內存范圍的。*/
#ifdef CONFIG_SYSCTL
sysctl_init();
#endif /* 對/proc文件系統和sysctl()系統調用相關部分進行初始化*/
/*
* Ok, at this point all CPU's should be initialized, so
* we can start looking into devices..
*/
#if defined(CONFIG_ARCH_S390)
s390_init_machine_check();
#endif
#ifdef CONFIG_PCI
pci_init();
#endif /* 初始化PCI總線 */
#ifdef CONFIG_SBUS
sbus_init();
#endif
#if defined(CONFIG_PPC)
ppc_init();
#endif
#ifdef CONFIG_MCA
mca_init();
#endif
#ifdef CONFIG_ARCH_ACORN
ecard_init();
#endif
#ifdef CONFIG_ZORRO
zorro_init();
#endif
#ifdef CONFIG_DIO
dio_init();
#endif
#ifdef CONFIG_NUBUS
nubus_init();
#endif
#ifdef CONFIG_ISAPNP
isapnp_init();
#endif /* 對ISA總線即插即用初始化 */
#ifdef CONFIG_TC
tc_init();
#endif
/* Networking initialization needs a process context */
sock_init(); /* 初始化網絡協議棧 */
start_context_thread();
do_initcalls();
#ifdef CONFIG_IRDA
irda_proto_init();
irda_device_init(); /* Must be done after protocol initialization */
#endif
#ifdef CONFIG_PCMCIA
init_pcmcia_ds(); /* Do this last */
#endif
}
------------------------------------------------------------
很明顯,這段代碼是用來進行對系統初始化的。開頭的一段注釋告訴我們,系統硬件此時只有cpu子系統在運轉,內存治理和進程治理也開始工作了。接下來,就是對硬件的初始化。
這一部分與硬件密切相關,在編譯核心時,將根據配置文件.config來編譯相應的部分。用vi查看.config文件,發現定義的項目如下:
CONFIG_MTRR=y
CONFIG_SYSCTL=y
CONFIG_PCI=y
# CONFIG_PCI_GOBIOS is not set
# CONFIG_PCI_GODIRECT is not set
CONFIG_PCI_GOANY=y
CONFIG_PCI_BIOS=y
CONFIG_PCI_DIRECT=y
CONFIG_PCI_NAMES=y
CONFIG_PCI_HERMES=m
# CONFIG_SBUS is not set
# CONFIG_MCA is not set
CONFIG_ISAPNP=y
CONFIG_TCIC=y
CONFIG_TC35815=m
CONFIG_IRDA=m
CONFIG_IRDA_ULTRA=y
CONFIG_IRDA_CACHE_LAST_LSAP=y
CONFIG_IRDA_FAST_RR=y
# CONFIG_IRDA_DEBUG is not set
CONFIG_PCMCIA=m
CONFIG_PCMCIA_AHA152X=m
CONFIG_PCMCIA_FDOMAIN=m
C right">(出處:清風軟件下載學院)