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

来源:互联网 发布:ps cs6色环插件 mac 编辑:程序博客网 时间:2024/06/06 20:23
本文系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_argstrap_init();                  //自陷入口函数初始化,针对此版本arm中直接returnearly_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 2011CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177CPU: VIVT data cache, VIVT instruction cacheMachine: s3c2440ATAG_INITRD is deprecated; please update your bootloader.Memory policy: ECC disabled, Data cache writebackCPU S3C2440A (id 0x32440001)S3C24XX Clocks, (c) 2004 Simtec ElectronicsS3C244X: core 400.000 MHz, memory 100.000 MHz, peripheral 50.000 MHzCLOCK: Slow mode (1.500 MHz), fast, MPLL on, UPLL onBuilt 1 zonelists in Zone order, mobility grouping on.  Total pages: 16256Kernel command line: noinitrd root=/dev/mtdblock2 init=/linuxrc console=ttySAC0NR_IRQS:85irq: clearing pending ext status 00080000irq: clearing subpending status 00000003irq: clearing subpending status 00000002PID hash table entries: 256 (order: 8, 1024 bytes)Console: colour dummy device 80x30console [ttySAC0] enabledDentry cache hash table entries: 8192 (order: 3, 32768 bytes)Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)Memory: 64MB = 64MB totalMemory: 61268KB available (3152K code, 349K data, 96K init, 0K highmem)SLUB: Genslabs=11, HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1Calibrating delay loop... 199.47 BogoMIPS (lpj=498688)Mount-cache hash table entries: 512CPU: Testing write buffer coherency: oknet_namespace: 296 bytesNET: Registered protocol family 16S3C2440: Initialising architectureS3C2440: IRQ SupportS3C24XX DMA Driver, (c) 2003-2004,2006 Simtec ElectronicsDMA channel 0 at c4808000, irq 33DMA channel 1 at c4808040, irq 34DMA channel 2 at c4808080, irq 35DMA channel 3 at c48080c0, irq 36S3C244X: Clock Support, DVS offbio: create slab <bio-0> at 0SCSI subsystem initializedusbcore: registered new interface driver usbfsusbcore: registered new interface driver hubusbcore: registered new device driver usbs3c2440-i2c s3c2440-i2c: slave address 0x10s3c2440-i2c s3c2440-i2c: bus frequency set to 97 KHzs3c2440-i2c s3c2440-i2c: i2c-0: S3C I2C adaptercfg80211: Calling CRDA to update world regulatory domainNET: Registered protocol family 2IP 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 registeredNET: Registered protocol family 1yaffs Nov 30 2011 09:27:01 Installing. msgmni has been set to 119alg: No test for stdrng (krng)io scheduler noop registered (default)Console: switching to colour frame buffer device 60x17fb0: s3c2410fb frame buffer devicebacklight initializedGPIO-Control initializedPWM-Test initializedadc initializeds3c2440-uart.0: tq2440_serial0 at MMIO 0x50000000 (irq = 70) is a S3C2440s3c2440-uart.1: tq2440_serial1 at MMIO 0x50004000 (irq = 73) is a S3C2440s3c2440-uart.2: tq2440_serial2 at MMIO 0x50008000 (irq = 76) is a S3C2440loop: module loadeddm9000 Ethernet Driver, V1.31Now use the default MAC address: 10:23:45:67:89:abeth0 (dm9000): not using net_device_ops yeteth0: dm9000e at c4814000,c4818004 IRQ 51 MAC: 10:23:45:67:89:ab (chip)S3C24XX NAND Driver, (c) 2004 Simtec Electronicss3c2440-nand s3c2440-nand: Tacls=3, 30ns Twrph0=7 70ns, Twrph1=3 30nsNAND device: Manufacturer ID: 0xec, Chip ID: 0xda (Samsung NAND 256MiB 3,3V 8-bit)Scanning device for bad blocksCreating 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) Drivers3c2410-ohci s3c2410-ohci: S3C24XX OHCIs3c2410-ohci s3c2410-ohci: new USB bus registered, assigned bus number 1s3c2410-ohci s3c2410-ohci: irq 42, io mem 0x49000000usb usb1: New USB device found, idVendor=1d6b, idProduct=0001usb usb1: New USB device strings: Mfr=3, Product=2, SerialNumber=1usb usb1: Product: S3C24XX OHCIusb usb1: Manufacturer: Linux 2.6.30.4-itspy ohci_hcdusb usb1: SerialNumber: s3c24xxusb usb1: configuration #1 chosen from 1 choicehub 1-0:1.0: USB hub foundhub 1-0:1.0: 2 ports detectedInitializing USB Mass Storage driver...usbcore: registered new interface driver usb-storageUSB Mass Storage support registered.usbcore: registered new interface driver usbserialusbserial: USB Serial Driver coreUSB Serial support registered for pl2303usbcore: registered new interface driver pl2303pl2303: Prolific PL2303 USB to serial adaptor drivers3c2410_udc: debugfs dir creation failed -19s3c2440-usbgadget s3c2440-usbgadget: S3C2440: increasing FIFO to 128 bytesmice: PS/2 mouse device common for all miceS3C24XX RTC, (c) 2004,2006 Simtec Electronicss3c2410-rtc s3c2410-rtc: rtc disabled, re-enablings3c2410-rtc s3c2410-rtc: rtc core: registered s3c as rtc0usbcore: registered new interface driver zc0301gspca: main v2.5.0 registeredusbcore: registered new interface driver zc3xxzc3xx: registeredusbcore: registered new interface driver uvcvideoUSB Video Class driver (v0.1.0)S3C2410 Watchdog Timer, (c) 2004 Simtec Electronicss3c2410-wdt s3c2410-wdt: watchdog inactive, reset disabled, irq enabledmapped channel 0 to 0s3c2440-sdi s3c2440-sdi: powered down.s3c2440-sdi s3c2440-sdi: initialisation done.s3c2440-sdi s3c2440-sdi: powered down.usbcore: registered new interface driver usbhidusbhid: v2.6:USB HID core driverAdvanced Linux Sound Architecture Driver Version 1.0.18a.No device for DAI UDA134XNo device for DAI s3c24xx-i2sS3C24XX_UDA134X SoC Audio driverUDA134X SoC Audio Codecasoc: UDA134X <-> s3c24xx-i2s mapping okALSA device list:  #0: S3C24XX_UDA134X (UDA134X)TCP cubic registeredlib80211: common routines for IEEE802.11 driverss3c2410-rtc s3c2410-rtc: hctosys: invalid date/timeyaffs: dev is 32505858 name is "mtdblock2"yaffs: passed flags ""yaffs: Attempting MTD mount on 31.2, "mtdblock2"yaffs: auto selecting yaffs2yaffs: restored from checkpointyaffs_read_super: isCheckpointed 1VFS: Mounted root (yaffs filesystem) on device 31:2.Freeing init memory: 96KTry to bring eth0 interface up ...ifconfig eth0 hw ether 00:11:22:33:44:55Please press Enter to activate this console. eth0: link up, 100Mbps, full-duplex, lpa 0x45E1ifconfig eth0 192.168.2.209 netmask 255.255.254.0 upadd default gw 192.168.2.254Done[root@WSN /]# 
原创粉丝点击