内核窥秘之一:start_kernel()运行过程记录 http://blog.csdn.net/yyplc/article/details/7030983

本文系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函数一样。
主要流程有:
[cpp]  view plain copy
  1. {  
  2. …  
  3. printk(KERN_NOTICE"%s", linux_banner);  //输出linux版本信息  
  4. setup_arch(&command_line);   //设置与初始化硬件体系相关的环境并调用  
  5.                            //pageint_inig() ->devicemaps_init(mdesc);初始化异常向量表,初始化zone memory maps,初始化结构体:MACHINE_START等  
  6. sched_init()                 //初始化调度器…先于中断开始前  
  7. printk(boot_command_line);   //提取分析核心启动参数过程(从bootloader 中传递)  
  8. parse_early_param();  
  9. parse_args  
  10. trap_init();                  //自陷入口函数初始化,针对此版本arm中直接return  
  11. early_irq_init();              //中断初始化过程….  
  12. init_IRQ();            
  13. init_timers();                //初始化定时器,开启定时器软中断服务以及注册服务程序以及初始化各CPU中的tev_base等init_timers()->run_timer_softirq()->__run_timers()..   
  14. timekeeping_init();   
  15. time_init(); //设置定时器及返回当前时间  
  16. console_init() //初步的初始化控制台,此控制台只能打印出一些简单//的启动信息…  
  17. vmalloc_init(); //  
  18. vfs_caches_init_early(); //  
  19. mem_init(); //初始化内存并计算可用内存大小  
  20. kmem_cache_init(); // 初始化SLAB缓存分配器  
  21. calibrate_delay(); //延迟校准,jiffy,记录系统的定时器的节拍数,每变化一次代表了系统定时器2个连续节拍时间的间隔。  
  22. fork_init(num_physpages); //初始化max_threads,init_task参数为fork()提供参考  
  23. buffer_init(); //初始化块设备读写缓冲区  
  24. vfs_caches_init(num_physpages);   //初始化虚拟文件系统 inode_init() ->files_init() ->mnt_init()...  
  25. signals_init(); //初始化内核信号队列….  
  26. rest_init(); //最后实际进入reset_init()函数,包括所有剩下的硬件//驱动,线程初始化等过程…这也最终完成//start_kernel//的启动过程。  
  27. }  

在进入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()过程:
[cpp]  view plain copy
  1. run_init_process("/sbin/init");  
  2. run_init_process("/etc/init");  
  3. run_init_process("/bin/init");  
  4. run_init_process("/bin/sh");  
如果最终调用失败,系统将会告诉我们:
panic("Noinit found.  Try passing init= option tokernel.");
OK,熟悉 linux内核start_kernel()的启动流程,编译一个内核和制作文件系统,下载到硬件中:
[cpp]  view plain copy
  1. Start Linux ...  
  2. Copy linux kernel from 0x00200000 to 0x30008000, size = 0x00200000 ... Copy Kernel to SDRAM done,NOW, Booting Linux......  
  3. Uncompressing Linux.............................................................................................................. done, booting the kernel.  
  4. 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  
  5. CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177  
  6. CPU: VIVT data cache, VIVT instruction cache  
  7. Machine: s3c2440  
  8. ATAG_INITRD is deprecated; please update your bootloader.  
  9. Memory policy: ECC disabled, Data cache writeback  
  10. CPU S3C2440A (id 0x32440001)  
  11. S3C24XX Clocks, (c) 2004 Simtec Electronics  
  12. S3C244X: core 400.000 MHz, memory 100.000 MHz, peripheral 50.000 MHz  
  13. CLOCK: Slow mode (1.500 MHz), fast, MPLL on, UPLL on  
  14. Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 16256  
  15. Kernel command line: noinitrd root=/dev/mtdblock2 init=/linuxrc console=ttySAC0  
  16. NR_IRQS:85  
  17. irq: clearing pending ext status 00080000  
  18. irq: clearing subpending status 00000003  
  19. irq: clearing subpending status 00000002  
  20. PID hash table entries: 256 (order: 8, 1024 bytes)  
  21. Console: colour dummy device 80x30  
  22. console [ttySAC0] enabled  
  23. Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)  
  24. Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)  
  25. Memory: 64MB = 64MB total  
  26. Memory: 61268KB available (3152K code, 349K data, 96K init, 0K highmem)  
  27. SLUB: Genslabs=11, HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1  
  28. Calibrating delay loop... 199.47 BogoMIPS (lpj=498688)  
  29. Mount-cache hash table entries: 512  
  30. CPU: Testing write buffer coherency: ok  
  31. net_namespace: 296 bytes  
  32. NET: Registered protocol family 16  
  33. S3C2440: Initialising architecture  
  34. S3C2440: IRQ Support  
  35. S3C24XX DMA Driver, (c) 2003-2004,2006 Simtec Electronics  
  36. DMA channel 0 at c4808000, irq 33  
  37. DMA channel 1 at c4808040, irq 34  
  38. DMA channel 2 at c4808080, irq 35  
  39. DMA channel 3 at c48080c0, irq 36  
  40. S3C244X: Clock Support, DVS off  
  41. bio: create slab <bio-0> at 0  
  42. SCSI subsystem initialized  
  43. usbcore: registered new interface driver usbfs  
  44. usbcore: registered new interface driver hub  
  45. usbcore: registered new device driver usb  
  46. s3c2440-i2c s3c2440-i2c: slave address 0x10  
  47. s3c2440-i2c s3c2440-i2c: bus frequency set to 97 KHz  
  48. s3c2440-i2c s3c2440-i2c: i2c-0: S3C I2C adapter  
  49. cfg80211: Calling CRDA to update world regulatory domain  
  50. NET: Registered protocol family 2  
  51. IP route cache hash table entries: 1024 (order: 0, 4096 bytes)  
  52. TCP established hash table entries: 2048 (order: 2, 16384 bytes)  
  53. TCP bind hash table entries: 2048 (order: 1, 8192 bytes)  
  54. TCP: Hash tables configured (established 2048 bind 2048)  
  55. TCP reno registered  
  56. NET: Registered protocol family 1  
  57. yaffs Nov 30 2011 09:27:01 Installing.   
  58. msgmni has been set to 119  
  59. alg: No test for stdrng (krng)  
  60. io scheduler noop registered (default)  
  61. Console: switching to colour frame buffer device 60x17  
  62. fb0: s3c2410fb frame buffer device  
  63. backlight initialized  
  64. GPIO-Control initialized  
  65. PWM-Test initialized  
  66. adc initialized  
  67. s3c2440-uart.0: tq2440_serial0 at MMIO 0x50000000 (irq = 70) is a S3C2440  
  68. s3c2440-uart.1: tq2440_serial1 at MMIO 0x50004000 (irq = 73) is a S3C2440  
  69. s3c2440-uart.2: tq2440_serial2 at MMIO 0x50008000 (irq = 76) is a S3C2440  
  70. loop: module loaded  
  71. dm9000 Ethernet Driver, V1.31  
  72. Now use the default MAC address: 10:23:45:67:89:ab  
  73. eth0 (dm9000): not using net_device_ops yet  
  74. eth0: dm9000e at c4814000,c4818004 IRQ 51 MAC: 10:23:45:67:89:ab (chip)  
  75. S3C24XX NAND Driver, (c) 2004 Simtec Electronics  
  76. s3c2440-nand s3c2440-nand: Tacls=3, 30ns Twrph0=7 70ns, Twrph1=3 30ns  
  77. NAND device: Manufacturer ID: 0xec, Chip ID: 0xda (Samsung NAND 256MiB 3,3V 8-bit)  
  78. Scanning device for bad blocks  
  79. Creating 3 MTD partitions on "NAND 256MiB 3,3V 8-bit":  
  80. 0x000000000000-0x000000040000 : "uboot1.1.6"  
  81. 0x000000200000-0x000000400000 : "linux-kernel"  
  82. 0x000000400000-0x00000ff80000 : "yaffs2"  
  83. ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver  
  84. s3c2410-ohci s3c2410-ohci: S3C24XX OHCI  
  85. s3c2410-ohci s3c2410-ohci: new USB bus registered, assigned bus number 1  
  86. s3c2410-ohci s3c2410-ohci: irq 42, io mem 0x49000000  
  87. usb usb1: New USB device found, idVendor=1d6b, idProduct=0001  
  88. usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1  
  89. usb usb1: Product: S3C24XX OHCI  
  90. usb usb1: Manufacturer: Linux 2.6.30.4-itspy ohci_hcd  
  91. usb usb1: SerialNumber: s3c24xx  
  92. usb usb1: configuration #1 chosen from 1 choice  
  93. hub 1-0:1.0: USB hub found  
  94. hub 1-0:1.0: 2 ports detected  
  95. Initializing USB Mass Storage driver...  
  96. usbcore: registered new interface driver usb-storage  
  97. USB Mass Storage support registered.  
  98. usbcore: registered new interface driver usbserial  
  99. usbserial: USB Serial Driver core  
  100. USB Serial support registered for pl2303  
  101. usbcore: registered new interface driver pl2303  
  102. pl2303: Prolific PL2303 USB to serial adaptor driver  
  103. s3c2410_udc: debugfs dir creation failed -19  
  104. s3c2440-usbgadget s3c2440-usbgadget: S3C2440: increasing FIFO to 128 bytes  
  105. mice: PS/2 mouse device common for all mice  
  106. S3C24XX RTC, (c) 2004,2006 Simtec Electronics  
  107. s3c2410-rtc s3c2410-rtc: rtc disabled, re-enabling  
  108. s3c2410-rtc s3c2410-rtc: rtc core: registered s3c as rtc0  
  109. usbcore: registered new interface driver zc0301  
  110. gspca: main v2.5.0 registered  
  111. usbcore: registered new interface driver zc3xx  
  112. zc3xx: registered  
  113. usbcore: registered new interface driver uvcvideo  
  114. USB Video Class driver (v0.1.0)  
  115. S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics  
  116. s3c2410-wdt s3c2410-wdt: watchdog inactive, reset disabled, irq enabled  
  117. mapped channel 0 to 0  
  118. s3c2440-sdi s3c2440-sdi: powered down.  
  119. s3c2440-sdi s3c2440-sdi: initialisation done.  
  120. s3c2440-sdi s3c2440-sdi: powered down.  
  121. usbcore: registered new interface driver usbhid  
  122. usbhid: v2.6:USB HID core driver  
  123. Advanced Linux Sound Architecture Driver Version 1.0.18a.  
  124. No device for DAI UDA134X  
  125. No device for DAI s3c24xx-i2s  
  126. S3C24XX_UDA134X SoC Audio driver  
  127. UDA134X SoC Audio Codec  
  128. asoc: UDA134X <-> s3c24xx-i2s mapping ok  
  129. ALSA device list:  
  130.   #0: S3C24XX_UDA134X (UDA134X)  
  131. TCP cubic registered  
  132. lib80211: common routines for IEEE802.11 drivers  
  133. s3c2410-rtc s3c2410-rtc: hctosys: invalid date/time  
  134. yaffs: dev is 32505858 name is "mtdblock2"  
  135. yaffs: passed flags ""  
  136. yaffs: Attempting MTD mount on 31.2, "mtdblock2"  
  137. yaffs: auto selecting yaffs2  
  138. yaffs: restored from checkpoint  
  139. yaffs_read_super: isCheckpointed 1  
  140. VFS: Mounted root (yaffs filesystem) on device 31:2.  
  141. Freeing init memory: 96K  
  142. Try to bring eth0 interface up ...  
  143. ifconfig eth0 hw ether 00:11:22:33:44:55  
  144.   
  145.   
  146. Please press Enter to activate this console. eth0: link up, 100Mbps, full-duplex, lpa 0x45E1  
  147. ifconfig eth0 192.168.2.209 netmask 255.255.254.0 up  
  148. add default gw 192.168.2.254  
  149. Done  
  150.   
  151.   
  152. [root@WSN /]#   

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值