|
以阅读源代码的方式研究 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
CONFIG_PCMCIA_NINJA_SCSI=m
CONFIG_PCMCIA_QLOGIC=m
CONFIG_PCMCIA_HERMES=m
CONFIG_PCMCIA_3C589=m
CONFIG_PCMCIA_3C574=m
CONFIG_PCMCIA_FMVJ18X=m
CONFIG_PCMCIA_PCNET=m
CONFIG_PCMCIA_AXNET=m
CONFIG_PCMCIA_NMCLAN=m
CONFIG_PCMCIA_SMC91C92=m
CONFIG_PCMCIA_XIRC2PS=m
CONFIG_PCMCIA_IBMTR=m
CONFIG_PCMCIA_XIRCOM=m
CONFIG_PCMCIA_XIRTULIP=m
CONFIG_PCMCIA_RAYCS=m
CONFIG_PCMCIA_NETWAVE=m
CONFIG_PCMCIA_WAVELAN=m
CONFIG_PCMCIA_WVLAN=m
CONFIG_PCMCIA_SERIAL_CS=m
呵呵,这样一看,mandrake缺省配置的东西真不少,就连我根本用不上的IRDA和PCMCIA都编译成模块了。有了这些代码后,在开机时,就会看到这些启动信息:
[root@c4 linux-2.4.19-9mdk]#dmesg
......
mtrr: v1.40 (20010327) Richard Gooch (rgooch@atnf.csiro.au)
mtrr: detected mtrr type: Intel
PCI: PCI BIOS revision 2.10 entry at 0xfdb81, last bus=3
PCI: Using configuration type 1
PCI: Probing PCI hardware
Unknown bridge resource 0: assuming transparent
PCI: Using IRQ router PIIX [8086/2440] at 00:1f.0
isapnp: Scanning for PnP cards...
isapnp: No Plug & Play device found
Linux NET4.0 for Linux 2.4
......
二、prepare_namespace()函数
接下来要执行的是prepare_namespace()函数。这个函数在/usr/src/linux-2.4.19-9mdk/init/do_mounts.c文件中。内容如下:
- /*
- * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
- */
- void prepare_namespace(void)
- {
- int is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
- #ifdef CONFIG_ALL_PPC
- extern void arch_discover_root(void);
- arch_discover_root();
- #endif /* CONFIG_ALL_PPC */
- #ifdef CONFIG_BLK_DEV_INITRD
- if (!initrd_start)
- mount_initrd = 0;
- real_root_dev = ROOT_DEV;
- #endif
- sys_mkdir("/dev", 0700);
- sys_mkdir("/root", 0700);
- sys_mknod("/dev/console", S_IFCHR|0600, MKDEV(TTYAUX_MAJOR, 1));
- #ifdef CONFIG_DEVFS_FS
- sys_mount("devfs", "/dev", "devfs", 0, NULL);
- do_devfs = 1;
- #endif
- create_dev("/dev/root", ROOT_DEV, NULL);
- if (mount_initrd) {
- if (initrd_load() && ROOT_DEV != MKDEV(RAMDISK_MAJOR, 0)) {
- handle_initrd();
- goto out;
- }
- } else if (is_floppy && rd_doload && rd_load_disk(0))
- ROOT_DEV = MKDEV(RAMDISK_MAJOR, 0);
- mount_root();
- out:
- sys_umount("/dev", 0);
- sys_mount(".", "/", NULL, MS_MOVE, NULL);
- sys_chroot(".");
- mount_devfs_fs ();
- }
复制代码
这段代码主要是决定根设备安装在那儿,在中间要处理一下RAM disk并判断是不是软盘启动的。
。RAM主要用来在核心安装根文件系统之前,预先装入一些模块。如果在lilo中指定了一个initrd.img映像文件,则内核在安装根设备之前,把它装上,否则正常安装根设备。
三、转入用户态运行
在完成初始化后,系统接着执行free_initmem(),将初始化过程中使用的内在释放。然后执行unlock_kernel(),这个函数想必就是前面lock_kernel()的逆操作了。然后以可读可写方式打开一个控制台设备。并复制两个文件描述符。
最后,init检查是否有给定的指今,如果没有,则按顺序检查是否存在/sbin/init、/etc/init、/bin/init和/bin/sh等文件,如果存在,则跳转执行相应和程序。一般情况下,系统都会启动/sbin/init程序,从此以后创建的进程都会在用户态运行。下一步的系统启动过程,也由/sbin/init来接着完成。 |
|