kernel init

Download Kernel init

Post on 18-Dec-2014

1.460 views

Category:

Technology

5 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

  • 1. Kernelinitialization Kernel initialization MichaelOpdenacker FreeElectronsCopyright20072010,FreeElectrons.CreativeCommonsBYSA3.0licenseLatestupdate:Dec20,2010,Documentsources,updatesandtranslations:http://freeelectrons.com/docs/kernelinitCorrections,suggestions,contributionsandtranslationsarewelcome! 1FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 2. Frombootloadertouserspace Bootloader Lowlevelhardware initialization Kernel initprocess Systeminitialization Fetchandcopy Linuxkernel initialization fromuserspace toRAM 2FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 3. Kernelbootstrap(1) Howthekernelbootstrapsitselfappearsinkernelbuilding. ExampleonARM(pxacpu)inLinux2.6.36: ... LDvmlinux SYSMAPSystem.map SYSMAP.tmp_System.map OBJCOPYarch/arm/boot/Image Kernel:arch/arm/boot/Imageisready ASarch/arm/boot/compressed/head.o GZIParch/arm/boot/compressed/piggy.gzip ASarch/arm/boot/compressed/piggy.gzip.o CCarch/arm/boot/compressed/misc.o CCarch/arm/boot/compressed/decompress.o ASarch/arm/boot/compressed/headxscale.o SHIPPEDarch/arm/boot/compressed/lib1funcs.S ASarch/arm/boot/compressed/lib1funcs.o LDarch/arm/boot/compressed/vmlinux OBJCOPYarch/arm/boot/zImage Kernel:arch/arm/boot/zImageisready ... 3FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 4. Kernelbootstrap(2) objcopy gzip as ld objcopy piggy.gzip.S piggy.gzip.o Image vmlinux piggy.gz zImage vmlinux head.o misc.o Stripped Compressed headcpu.oKernelproper: kernel kernel binary decompress.o Rawkernel binary executable (binary lib1funcs.o (inarch/ Kernelimage object) boot/compressed) forbootloader (ELFobject) Composite (binaryobject) asmwrapper Detailsfoundbycompiling kernelimage around withmakeV=1 (ELFobject) piggy.gzip.gz +bootstrap code 4FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 5. Bootstrapcode head.o: Architecturespecificinitializationcode. Thisiswhatisexecutedbythebootloader headcpu.o(hereheadxscale.o): CPUspecificinitializationcode decompress.o,misc.o: Decompressioncode lib1funcs.o: OptimizedARMdivisionroutines(ARMonly) 5FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 6. Bootstrapcodetasks Mainworkdonebyhead.o: Checkthearchitecture,processorandmachinetype. ConfiguretheMMU,createpagetableentries andenablevirtualmemory. Callsthestart_kernelfunctionininit/main.c. Samecodeforallarchitectures. Anybodyinterestinginkernelstartupshouldstudythisfile! 6FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 7. start_kernelmainactions Callssetup_arch(&command_line) (functiondefinedinarch//kernel/setup.c),copying thecommandlinefromwherethebootloaderleftit. Onarm,thisfunctioncallssetup_processor (inwhichCPUinformationisdisplayed)andsetup_machine (locatingthemachineinthelistofsupportedmachines). Initializestheconsoleasearlyaspossible (togeterrormessages) Initializesmanysubsystems(seethecode) Eventuallycallsrest_init. 7FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 8. rest_init:startingtheinitprocess Startinganewkernelthreadwhichwilllaterbecometheinitprocess staticnoinlinevoid__init_refokrest_init(void) __releases(kernel_lock) { intpid; rcu_scheduler_starting(); /* *Weneedtospawninitfirstsothatitobtainspid1,however *theinittaskwillendupwantingtocreatekthreads,which,if *wescheduleitbeforewecreatekthreadd,willOOPS. */ kernel_thread(kernel_init,NULL,CLONE_FS|CLONE_SIGHAND); numa_default_policy(); pid=kernel_thread(kthreadd,NULL,CLONE_FS|CLONE_FILES); rcu_read_lock(); kthreadd_task=find_task_by_pid_ns(pid,&init_pid_ns); rcu_read_unlock(); complete(&kthreadd_done); /* *Thebootidlethreadmustexecuteschedule() *atleastoncetogetthingsmoving: */ init_idle_bootup_task(current); preempt_enable_no_resched(); schedule(); preempt_disable(); /*Callintocpu_idlewithpreemptdisabled*/ cpu_idle(); } Source:Linux2.6.36 8FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 9. kernel_init kernel_initdoestwomainthings: Calldo_basic_setup Nowthatkernelservicesareready,startdeviceinitialization: (Linux2.6.36codeexcerpt): staticvoid__initdo_basic_setup(void) { cpuset_init_smp(); usermodehelper_init(); init_tmpfs(); driver_init(); init_irq_proc(); do_ctors(); do_initcalls(); } Callinit_post 9FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
  • 10. do_initcalls Callspluggablehooksregisteredwiththemacrosbelow. Advantage:thegenericcodedoesnthavetoknowaboutthem. /* *A"pure"initcallhasnodependenciesonanythingelse,andpurely *initializesvariablesthatcouldntbestaticallyinitialized. * *Thisonlyexistsforbuiltincode,notformodules. */ #definepure_initcall(fn)__define_initcall("0",fn,1) #definecore_initcall(fn)__define_initcall("1",fn,1) #definecore_initcall_sync(fn)__define_initcall("1s",fn,1s) #definepostcore_initcall(fn)__define_initcall("2",fn,2) #definepostcore_initcall_sync(fn)__define_initcall("2s",fn