内核窥秘之一:start_kernel()运行过程记录

本文系itspy原创,复制/转载请尽量标明原出处http://blog.csdn.net/yyplc/article/details/7030983,谢谢!
解开linux内核的神秘面纱,它是怎么运行起来的呢?它是怎么做到的?做了什么,怎么做,流程如何等等,均需要一个清晰的过程(因为要给我们一个说法~!),如何更好的亲密、深入的了解,接触它,找到一个很好的突破点是很关键的---那就是‘从头开始’(从linux的头开始)。本文将结合源码浅析并硬件平台上运行实践验证。
内核版本: linux-2.6.30.4
硬件CPU: s3c2440(arm920vt)
所有的程序员都知道,无论怎样,所有的入口点都是从‘main’函数开始的(start_kernel)。 废话~~
Linux的‘头’叫做一个start_kernel的函数,就是我们通常所说的main函数一样。
主要流程有:
{
…
printk(KERN_NOTICE"%s", linux_banner);  //输出linux版本信息
setup_arch(&command_line);   //设置与初始化硬件体系相关的环境并调用
                           //pageint_inig() ->devicemaps_init(mdesc);初始化异常向量表,初始化zone memory maps,初始化结构体:MACHINE_START等
sched_init()                 //初始化调度器…先于中断开始前
printk(boot_command_line);   //提取分析核心启动参数过程(从bootloader 中传递)
parse_early_param();
parse_args
trap_init();                  //自陷入口函数初始化,针对此版本arm中直接return
early_irq_init();              //中断初始化过程….
init_IRQ();          
init_timers();                //初始化定时器,开启定时器软中断服务以及注册服务程序以及初始化各CPU中的tev_base等init_timers()->run_timer_softirq()->__run_timers().. 
timekeeping_init(); 
time_init(); //设置定时器及返回当前时间
console_init() //初步的初始化控制台,此控制台只能打印出一些简单//的启动信息…
vmalloc_init(); //
vfs_caches_init_early(); //
mem_init(); //初始化内存并计算可用内存大小
kmem_cache_init(); // 初始化SLAB缓存分配器
calibrate_delay(); //延迟校准,jiffy,记录系统的定时器的节拍数,每变化一次代表了系统定时器2个连续节拍时间的间隔。
fork_init(num_physpages); //初始化max_threads,init_task参数为fork()提供参考
buffer_init(); //初始化块设备读写缓冲区
vfs_caches_init(num_physpages);   //初始化虚拟文件系统 inode_init() ->files_init() ->mnt_init()...
signals_init(); //初始化内核信号队列….
rest_init(); //最后实际进入reset_init()函数,包括所有剩下的硬件//驱动,线程初始化等过程…这也最终完成//start_kernel//的启动过程。
}

在进入reset_init(),首先创建系统的第一个线程kernel_init(),线程pid=1,接着创建

pid=2, kthreadadd() 线程,接着开始任务调度schedule(),进入cpu_idle()
剩下的都由任务调度器scheduler进行任务/线程调度了。
其中在kenernel_init 线程中主要完成了:do_basic_setup() 调用driver_init()、do_init_calls()(调用那些为__init属性的函数)等函数,完成相关的PCI和network和加载剩下各种的设备驱动。
接着prepare_namespace(),这里完成一个重要的工作,那就是挂载根文件系统,这里决定系统需要挂载什么,在哪里挂载。当以上一切都准备好之后,最后进入init_post()函数中:
在init_post()中调用free_initmem()释放__init_begin到_init_end之间的空间,会出现:
Freed initmemory 92K 
然后调用sys_open()为系统打开控制台,如果找不到将会出现:
Warning: unableto open an initial console.
接着也就是最后调用run_init_process(command)函数,command是这里之前bootloader传给内核的参数command_line。通常嵌入式系统这个参数主要是linuxrc文件,其实际是到busybox的一个软连接。
如果command调用失败,系统将继续以下run_init_process()过程:
run_init_process("/sbin/init");
run_init_process("/etc/init");
run_init_process("/bin/init");
run_init_process("/bin/sh");
如果最终调用失败,系统将会告诉我们:
panic("Noinit found.  Try passing init= option tokernel.");
OK,熟悉 linux内核start_kernel()的启动流程,编译一个内核和制作文件系统,下载到硬件中:
Start Linux ...
Copy linux kernel from 0x00200000 to 0x30008000, size = 0x00200000 ... Copy Kernel to SDRAM done,NOW, Booting Linux......
Uncompressing Linux.............................................................................................................. done, booting the kernel.
Linux version 2.6.30.4-itspy (root@wsn-Inspiron-1427) (gcc version 4.3.3 (Sourcery G++ Lite 2009q1-203) ) #27 Thu Dec 1 14:15:49 CST 2011
CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177
CPU: VIVT data cache, VIVT instruction cache
Machine: s3c2440
ATAG_INITRD is deprecated; please update your bootloader.
Memory policy: ECC disabled, Data cache writeback
CPU S3C2440A (id 0x32440001)
S3C24XX Clocks, (c) 2004 Simtec Electronics
S3C244X: core 400.000 MHz, memory 100.000 MHz, peripheral 50.000 MHz
CLOCK: Slow mode (1.500 MHz), fast, MPLL on, UPLL on
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 16256
Kernel command line: noinitrd root=/dev/mtdblock2 init=/linuxrc console=ttySAC0
NR_IRQS:85
irq: clearing pending ext status 00080000
irq: clearing subpending status 00000003
irq: clearing subpending status 00000002
PID hash table entries: 256 (order: 8, 1024 bytes)
Console: colour dummy device 80x30
console [ttySAC0] enabled
Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)
Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)
Memory: 64MB = 64MB total
Memory: 61268KB available (3152K code, 349K data, 96K init, 0K highmem)
SLUB: Genslabs=11, HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
Calibrating delay loop... 199.47 BogoMIPS (lpj=498688)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
net_namespace: 296 bytes
NET: Registered protocol family 16
S3C2440: Initialising architecture
S3C2440: IRQ Support
S3C24XX DMA Driver, (c) 2003-2004,2006 Simtec Electronics
DMA channel 0 at c4808000, irq 33
DMA channel 1 at c4808040, irq 34
DMA channel 2 at c4808080, irq 35
DMA channel 3 at c48080c0, irq 36
S3C244X: Clock Support, DVS off
bio: create slab <bio-0> at 0
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
s3c2440-i2c s3c2440-i2c: slave address 0x10
s3c2440-i2c s3c2440-i2c: bus frequency set to 97 KHz
s3c2440-i2c s3c2440-i2c: i2c-0: S3C I2C adapter
cfg80211: Calling CRDA to update world regulatory domain
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 2048 (order: 2, 16384 bytes)
TCP bind hash table entries: 2048 (order: 1, 8192 bytes)
TCP: Hash tables configured (established 2048 bind 2048)
TCP reno registered
NET: Registered protocol family 1
yaffs Nov 30 2011 09:27:01 Installing. 
msgmni has been set to 119
alg: No test for stdrng (krng)
io scheduler noop registered (default)
Console: switching to colour frame buffer device 60x17
fb0: s3c2410fb frame buffer device
backlight initialized
GPIO-Control initialized
PWM-Test initialized
adc initialized
s3c2440-uart.0: tq2440_serial0 at MMIO 0x50000000 (irq = 70) is a S3C2440
s3c2440-uart.1: tq2440_serial1 at MMIO 0x50004000 (irq = 73) is a S3C2440
s3c2440-uart.2: tq2440_serial2 at MMIO 0x50008000 (irq = 76) is a S3C2440
loop: module loaded
dm9000 Ethernet Driver, V1.31
Now use the default MAC address: 10:23:45:67:89:ab
eth0 (dm9000): not using net_device_ops yet
eth0: dm9000e at c4814000,c4818004 IRQ 51 MAC: 10:23:45:67:89:ab (chip)
S3C24XX NAND Driver, (c) 2004 Simtec Electronics
s3c2440-nand s3c2440-nand: Tacls=3, 30ns Twrph0=7 70ns, Twrph1=3 30ns
NAND device: Manufacturer ID: 0xec, Chip ID: 0xda (Samsung NAND 256MiB 3,3V 8-bit)
Scanning device for bad blocks
Creating 3 MTD partitions on "NAND 256MiB 3,3V 8-bit":
0x000000000000-0x000000040000 : "uboot1.1.6"
0x000000200000-0x000000400000 : "linux-kernel"
0x000000400000-0x00000ff80000 : "yaffs2"
ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
s3c2410-ohci s3c2410-ohci: S3C24XX OHCI
s3c2410-ohci s3c2410-ohci: new USB bus registered, assigned bus number 1
s3c2410-ohci s3c2410-ohci: irq 42, io mem 0x49000000
usb usb1: New USB device found, idVendor=1d6b, idProduct=0001
usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1
usb usb1: Product: S3C24XX OHCI
usb usb1: Manufacturer: Linux 2.6.30.4-itspy ohci_hcd
usb usb1: SerialNumber: s3c24xx
usb usb1: configuration #1 chosen from 1 choice
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 2 ports detected
Initializing USB Mass Storage driver...
usbcore: registered new interface driver usb-storage
USB Mass Storage support registered.
usbcore: registered new interface driver usbserial
usbserial: USB Serial Driver core
USB Serial support registered for pl2303
usbcore: registered new interface driver pl2303
pl2303: Prolific PL2303 USB to serial adaptor driver
s3c2410_udc: debugfs dir creation failed -19
s3c2440-usbgadget s3c2440-usbgadget: S3C2440: increasing FIFO to 128 bytes
mice: PS/2 mouse device common for all mice
S3C24XX RTC, (c) 2004,2006 Simtec Electronics
s3c2410-rtc s3c2410-rtc: rtc disabled, re-enabling
s3c2410-rtc s3c2410-rtc: rtc core: registered s3c as rtc0
usbcore: registered new interface driver zc0301
gspca: main v2.5.0 registered
usbcore: registered new interface driver zc3xx
zc3xx: registered
usbcore: registered new interface driver uvcvideo
USB Video Class driver (v0.1.0)
S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics
s3c2410-wdt s3c2410-wdt: watchdog inactive, reset disabled, irq enabled
mapped channel 0 to 0
s3c2440-sdi s3c2440-sdi: powered down.
s3c2440-sdi s3c2440-sdi: initialisation done.
s3c2440-sdi s3c2440-sdi: powered down.
usbcore: registered new interface driver usbhid
usbhid: v2.6:USB HID core driver
Advanced Linux Sound Architecture Driver Version 1.0.18a.
No device for DAI UDA134X
No device for DAI s3c24xx-i2s
S3C24XX_UDA134X SoC Audio driver
UDA134X SoC Audio Codec
asoc: UDA134X <-> s3c24xx-i2s mapping ok
ALSA device list:
  #0: S3C24XX_UDA134X (UDA134X)
TCP cubic registered
lib80211: common routines for IEEE802.11 drivers
s3c2410-rtc s3c2410-rtc: hctosys: invalid date/time
yaffs: dev is 32505858 name is "mtdblock2"
yaffs: passed flags ""
yaffs: Attempting MTD mount on 31.2, "mtdblock2"
yaffs: auto selecting yaffs2
yaffs: restored from checkpoint
yaffs_read_super: isCheckpointed 1
VFS: Mounted root (yaffs filesystem) on device 31:2.
Freeing init memory: 96K
Try to bring eth0 interface up ...
ifconfig eth0 hw ether 00:11:22:33:44:55


Please press Enter to activate this console. eth0: link up, 100Mbps, full-duplex, lpa 0x45E1
ifconfig eth0 192.168.2.209 netmask 255.255.254.0 up
add default gw 192.168.2.254
Done


[root@WSN /]# 

你可能感兴趣的:(c,linux,command,table,interface,linux内核)