嵌入式Linux2.6內核啟動流程蒼松書屋_第1頁
嵌入式Linux2.6內核啟動流程蒼松書屋_第2頁
嵌入式Linux2.6內核啟動流程蒼松書屋_第3頁
嵌入式Linux2.6內核啟動流程蒼松書屋_第4頁
嵌入式Linux2.6內核啟動流程蒼松書屋_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、linux內核構成(國嵌)linux/arch/arm/boot/compressed/head.s1解壓縮2初始化3啟動應用程序1 arch/arm/boot/compressed/makefile arch/arm/boot/compressed/vmlinux.lds2. arch/arm/kernel/vmlinux.ldslinux內核啟動流程(國嵌)arch/arm/boot/compressed/start.s(head.s負責解壓縮)start: .type start,#function .rept 8 mov r0, r0 .endr b 1f .word 0x016f28

2、18 magic numbers to help the loader .word start absolute load/run zimage address .word _edata zimage end address1: mov r7, r1 save architecture id mov r8, r2 save atags pointer這也標志著u-boot將系統(tǒng)完全的交給了os,bootloader生命終止。之后代碼在133行會讀取cpsr并判斷是否處理器處于supervisor模式從u-boot進入kernel,系統(tǒng)已經處于svc32模式;而利用angel進入則處于user模

3、式,還需要額外兩條指令。之后是再次確認中斷關閉,并完成cpsr寫入 mrs r2, cpsr get current mode tst r2, #3 not user? bne not_angel mov r0, #0x17 angel_swireason_entersvc swi 0x123456 angel_swi_armnot_angel: mrs r2, cpsr turn off interrupts to orr r2, r2, #0xc0 prevent angel from running msr cpsr_c, r2 然后在lc0地址處將分段信息導入r0-r6、ip、sp等寄

4、存器,并檢查代碼是否運行在與鏈接時相同的目標地址,以決定是否進行處理。由于現(xiàn)在很少有人不使用loader和tags,將zimage燒寫到rom直接從0x0位置執(zhí)行,所以這個處理是必須的(但是zimage的頭現(xiàn)在也保留了不用loader也可啟動的能力)。arm架構下自解壓頭一般是鏈接在0x0地址而被加載到0x30008000運行,所以要修正這個變化。涉及到r5寄存器存放的zimage基地址 r6和r12(即ip寄存器)存放的got(global offset table) r2和r3存放的bss段起止地址 sp棧指針地址 很簡單,這些寄存器統(tǒng)統(tǒng)被加上一個你也能猜到的偏移地址 0x30008000

5、。該地址是s3c2410相關的,其他的arm處理器可以參考下表pxa2xx是0xa0008000 ixp2x00和ixp4xx是0x00008000 freescale i.mx31/37是0x80008000 ti davinci dm64xx是0x80008000 ti omap系列是0x80008000 at91rm/sam92xx系列是0x20008000 cirrus ep93xx是0x00008000 這些操作發(fā)生在代碼172行開始的地方,下面只粘貼一部分 add r5, r5, r0 add r6, r6, r0 add ip, ip, r0后面在211行進行bss段的清零工作n

6、ot_relocated: mov r0, #01: str r0, r2, #4 clear bss str r0, r2, #4 str r0, r2, #4 str r0, r2, #4 cmp r2, r3 blo 1b 然后224行,打開cache,并為后面解壓縮設置64kb的臨時malloc空間 bl cache_on mov r1, sp malloc space above stack add r2, sp, #0x10000 64k max 接下來238行進行檢查,確定內核解壓縮后的image目標地址是否會覆蓋到zimage頭,如果是則準備將zimage頭轉移到解壓出來的內核

7、后面 cmp r4, r2 bhs wont_overwrite sub r3, sp, r5 > compressed kernel size add r0, r4, r3, lsl #2 allow for 4x expansion cmp r0, r5 bls wont_overwrite mov r5, r2 decompress after malloc space mov r0, r5 mov r3, r7 bl decompress_kernel真實情況在大多數(shù)的應用中,內核編譯都會把壓縮的zimage和非壓縮的image鏈接到同樣的地址,s3c2410平臺下即是0x300

8、08000。這樣做的好處是,人們不用關心內核是image還是zimage,放到這個位置執(zhí)行就ok,所以在解壓縮后zimage頭必須為真正的內核讓路。在250行解壓完畢,內核長度返回值存放在r0寄存器里。在內核末尾空出128字節(jié)的??臻g用,并且使其長度128字節(jié)對齊。 add r0, r0, #127 + 128 alignment + stack bic r0, r0, #127 align the kernel length算出搬移代碼的參數(shù):計算內核末尾地址并存放于r1寄存器,需要搬移代碼原來地址放在r2,需要搬移的長度放在r3。然后執(zhí)行搬移,并設置好sp指針指向新的棧(原來的棧也會被內核

9、覆蓋掉) add r1, r5, r0 end of decompressed kernel adr r2, reloc_start ldr r3, lc1 add r3, r2, r31: ldmia r2!, r9 - r14 copy relocation code stmia r1!, r9 - r14 ldmia r2!, r9 - r14 stmia r1!, r9 - r14 cmp r2, r3 blo 1b add sp, r1, #128 relocate the stack搬移完成后刷新cache,因為代碼地址變化了不能讓cache再命中被內核覆蓋的老地址。然后跳轉到新的

10、地址繼續(xù)執(zhí)行 bl cache_clean_flush add pc, r5, r0 call relocation code注意zimage在解壓后的搬移和跳轉會給gdb調試內核帶來麻煩。因為用來調試的符號表是在編譯是生成的,并不知道以后會被搬移到何處去,只有在內核解壓縮完成之后,根據(jù)計算出來的參數(shù)“告訴”調試器這個變化。以撰寫本文時使用的zimage為例,內核自解壓頭重定向后,reloc_start地址由0x30008360變?yōu)?x30533e60。故我們要把vmlinux的符號表也相應的從0x30008000后移到0x30533b00開始,這樣gdb就可以正確的對應源代碼和機器指令。隨著

11、頭部代碼移動到新的位置,不會再和內核的目標地址沖突,可以開始內核自身的搬移了。此時r0寄存器存放的是內核長度(嚴格的說是長度外加128byte的棧),r4存放的是內核的目的地址0x30008000,r5是目前內核存放地址,r6是cpu id,r7是machine id,r8是atags地址。代碼從501行開始reloc_start: add r9, r5, r0 sub r9, r9, #128 do not copy the stack debug_reloc_start mov r1, r41: .rept 4 ldmia r5!, r0, r2, r3, r10 - r14 reloca

12、te kernel stmia r1!, r0, r2, r3, r10 - r14 .endr cmp r5, r9 blo 1b add sp, r1, #128 relocate the stack接下來在516行清除并關閉cache,清零r0,將machine id存入r1,atags指針存入r2,再跳入0x30008000執(zhí)行真正的內核imagecall_kernel: bl cache_clean_flush bl cache_off mov r0, #0 must be zero mov r1, r7 restore architecture number mov r2, r8

13、restore atags pointer mov pc, r4 call kernel內核代碼入口在arch/arm/kernel/head.s文件的83行。首先進入svc32模式,并查詢cpu id,檢查合法性 msr cpsr_c, #psr_f_bit | psr_i_bit | svc_mode ensure svc mode and irqs disabled mrc p15, 0, r9, c0, c0 get processor id bl _lookup_processor_type r5=procinfo r9=cpuid movs r10, r5 invalid proc

14、essor (r5=0)? beq _error_p yes, error 'p'接著在87行進一步查詢machine id并檢查合法性 bl _lookup_machine_type r5=machinfo movs r8, r5 invalid machine (r5=0)? beq _error_a yes, error 'a'其中_lookup_processor_type在linux-2.6.24-moko-linuxbj/arch/arm/kernel/head-common.s文件的149行,該函數(shù)首將標號3的實際地址加載到r3,然后將編譯時生成的

15、_proc_info_begin虛擬地址載入到r5,_proc_info_end虛擬地址載入到r6,標號3的虛擬地址載入到r7。由于adr偽指令和標號3的使用,以及_proc_info_begin等符號在linux-2.6.24-moko-linuxbj/arch/arm/kernel/vmlinux.lds而不是代碼中被定義,此處代碼不是非常直觀,想弄清楚代碼緣由的讀者請耐心閱讀這兩個文件和adr偽指令的說明。r3和r7分別存儲的是同一位置標號3的物理地址(由于沒有啟用mmu,所以當前肯定是物理地址)和虛擬地址,所以兒者相減即得到虛擬地址和物理地址之間的offset。利用此offset,將r

16、5和r6中保存的虛擬地址轉變?yōu)槲锢淼刂穇lookup_processor_type: adr r3, 3f ldmda r3, r5 - r7 sub r3, r3, r7 get offset between virt&phys add r5, r5, r3 convert virt addresses to add r6, r6, r3 physical address space然后從proc_info中讀出內核編譯時寫入的processor id和之前從cpsr中讀到的processor id對比,查看代碼和cpu硬件是否匹配(想在arm920t上運行為cortex-a8編譯的

17、內核?不讓?。?。如果編譯了多種處理器支持,如versatile板,則會循環(huán)每種type依次檢驗,如果硬件讀出的id在內核中找不到匹配,則r5置0返回1:ldmiar5, r3, r4 value, maskandr4, r4, r9 mask wanted bitsteqr3, r4beq2faddr5, r5, #proc_info_sz sizeof(proc_info_list)cmpr5, r6blo1bmovr5, #0 unknown processor2:movpc, lr _lookup_machine_type在linux-2.6.24-moko-linuxbj/arch/a

18、rm/kernel/head-common.s文件的197行,編碼方法與檢查processor id完全一樣,請參考前段_lookup_machine_type:adrr3, 3bldmiar3, r4, r5, r6subr3, r3, r4 get offset between virt&physaddr5, r5, r3 convert virt addresses toaddr6, r6, r3 physical address space1:ldrr3, r5, #machinfo_type get machine typeteqr3, r1 matches loader n

19、umber?beq2f foundaddr5, r5, #sizeof_machine_desc next machine_desccmpr5, r6blo1bmovr5, #0 unknown machine2:movpc, lr代碼回到head.s第92行,檢查atags合法性,然后創(chuàng)建初始頁表bl_vet_atagsbl_create_page_tables 創(chuàng)建頁表的代碼在218行,首先將內核起始地址-0x4000到內核起始地址之間的16k存儲器清0_create_page_tables:pgtblr4 page table address/* * clear the 16k leve

20、l 1 swapper page table */movr0, r4movr3, #0addr6, r0, #0x40001:strr3, r0, #4strr3, r0, #4strr3, r0, #4strr3, r0, #4teqr0, r6bne1b 然后在234行將proc_info中的mmu_flags加載到r7ldrr7, r10, #procinfo_mm_mmuflags mm_mmuflags在242行將pc指針右移20位,得到內核第一個1mb空間的段地址存入r6,在s3c2410平臺該值是0x300。接著根據(jù)此值存入映射標識movr6, pc, lsr #20 start

21、 of kernel sectionorrr3, r7, r6, lsl #20 flags + kernel basestrr3, r4, r6, lsl #2 identity mapping完成頁表設置后回到102行,為打開虛擬地址映射作準備。設置sp指針,函數(shù)返回地址lr指向_enable_mmu,并跳轉到linux-2.6.24-moko-linuxbj/arch/arm/mm/proc-arm920.s的386行,清除i-cache、d-cache、write buffer和tlb_arm920_setup:movr0, #0mcrp15, 0, r0, c7, c7 invali

22、date i,d caches on v4mcrp15, 0, r0, c7, c10, 4 drain write buffer on v4#ifdef config_mmumcrp15, 0, r0, c8, c7 invalidate i,d tlbs on v4#endif然后返回head.s的158行,加載domain和頁表,跳轉到_turn_mmu_on_enable_mmu:#ifdef config_alignment_traporrr0, r0, #cr_a#elsebicr0, r0, #cr_a#endif#ifdef config_cpu_dcache_disableb

23、icr0, r0, #cr_c#endif#ifdef config_cpu_bpredict_disablebicr0, r0, #cr_z#endif#ifdef config_cpu_icache_disablebicr0, r0, #cr_i#endifmovr5, #(domain_val(domain_user, domain_manager) | domain_val(domain_kernel, domain_manager) | domain_val(domain_table, domain_manager) | domain_val(domain_io, domain_cl

24、ient)mcrp15, 0, r5, c3, c0, 0 load domain access registermcrp15, 0, r4, c2, c0, 0 load page table pointerb_turn_mmu_on在194行把mmu使能位寫入mmu,激活虛擬地址。然后將原來保存在sp中的地址載入pc,跳轉到head-common.s的_mmap_switched,至此代碼進入虛擬地址的世界movr0, r0mcrp15, 0, r0, c1, c0, 0 write control regmrcp15, 0, r3, c0, c0, 0 read id regmovr3,

25、 r3movr3, r3movpc, r13在head-common.s的37行開始清除內核bss段,processor id保存在r9,machine id報存在r1,atags地址保存在r2,并將控制寄存器保存到r7定義的內存地址。接下來跳入linux-2.6.24-moko-linuxbj/init/main.c的507行,start_kernel函數(shù)。這里只粘貼部分代碼(第一個c語言函數(shù),作一系列的初始化)_mmap_switched:adrr3, _switch_data + 4ldmiar3!, r4, r5, r6, r7cmpr4, r5 copy data segment i

26、f needed1:cmpner5, r6ldrnefp, r4, #4strnefp, r5, #4bne1basmlinkage void _init start_kernel(void)char * command_line;extern struct kernel_param _start_param, _stop_param;smp_setup_processor_id();/* * need to run as early as possible, to initialize the * lockdep hash: */lockdep_init();debug_objects_ea

27、rly_init();cgroup_init_early();local_irq_disable();early_boot_irqs_off();early_init_irq_lock_class();/* * interrupts are still disabled. do necessary setups, then * enable them */lock_kernel();tick_init();boot_cpu_init();page_address_init();printk(kern_notice);printk(linux_banner);setup_arch(&co

28、mmand_line);mm_init_owner(&init_mm, &init_task);setup_command_line(command_line);setup_per_cpu_areas();setup_nr_cpu_ids();smp_prepare_boot_cpu();/* arch-specific boot-cpu hooks */* * set up the scheduler prior starting any interrupts (such as the * timer interrupt). full topology setup happe

29、ns at smp_init() * time - but meanwhile we still have a functioning scheduler. */sched_init();/* * disable preemption - early bootup scheduling is extremely * fragile until we cpu_idle() for the first time. */preempt_disable();build_all_zonelists();page_alloc_init();printk(kern_notice "kernel c

30、ommand line: %sn", boot_command_line);parse_early_param();parse_args("booting kernel", static_command_line, _start_param, _stop_param - _start_param, &unknown_bootoption);if (!irqs_disabled() printk(kern_warning "start_kernel(): bug: interrupts were ""enabled *very*

31、 early, fixing itn");local_irq_disable();sort_main_extable();trap_init();rcu_init();/* init some links before init_isa_irqs() */early_irq_init();init_irq();pidhash_init();init_timers();hrtimers_init();softirq_init();timekeeping_init();time_init();sched_clock_init();profile_init();if (!irqs_disa

32、bled()printk(kern_crit "start_kernel(): bug: interrupts were " "enabled earlyn");early_boot_irqs_on();local_irq_enable();/* * hack alert! this is early. we're enabling the console before * we've done pci setups etc, and console_init() must be aware of * this. but we do wa

33、nt output early, in case something goes wrong. */console_init();if (panic_later)panic(panic_later, panic_param);lockdep_info();/* * need to run this when irqs are enabled, because it wants * to self-test hard/soft-irqs on/off lock inversion bugs * too: */locking_selftest();#ifdef config_blk_dev_init

34、rdif (initrd_start && !initrd_below_start_ok && page_to_pfn(virt_to_page(void *)initrd_start) < min_low_pfn) printk(kern_crit "initrd overwritten (0x%08lx < 0x%08lx) - " "disabling it.n", page_to_pfn(virt_to_page(void *)initrd_start), min_low_pfn);initrd_sta

35、rt = 0;#endifvmalloc_init();vfs_caches_init_early();cpuset_init_early();page_cgroup_init();mem_init();enable_debug_pagealloc();cpu_hotplug_init();kmem_cache_init();debug_objects_mem_init();idr_init_cache();setup_per_cpu_pageset();numa_policy_init();if (late_time_init)late_time_init();calibrate_delay

36、();pidmap_init();pgtable_cache_init();prio_tree_init();anon_vma_init();#ifdef config_x86if (efi_enabled)efi_enter_virtual_mode();#endifthread_info_cache_init();cred_init();fork_init(num_physpages);proc_caches_init();buffer_init();key_init();security_init();vfs_caches_init(num_physpages);radix_tree_i

37、nit();signals_init();/* rootfs populating might need page-writeback */page_writeback_init();#ifdef config_proc_fsproc_root_init();#endifcgroup_init();cpuset_init();taskstats_init_early();delayacct_init();check_bugs();acpi_early_init(); /* before lapic and smp init */ftrace_init();/* do the rest non-

38、_init'ed, we're now alive */rest_init();tatic noinline void _init_refok rest_init(void)_releases(kernel_lock)int pid;kernel_thread(kernel_init, null, clone_fs | clone_sighand);numa_default_policy();pid = kernel_thread(kthreadd, null, clone_fs | clone_files);kthreadd_task = find_task_by_pid_ns(pid, &init_pid_ns);unlock_kernel();/* * the boot idle thread must execute schedule() * at least once to get things moving: */init_idle_bootup_task(current);rcu_scheduler_starting()

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論