zoukankan      html  css  js  c++  java
  • linux子系统的初始化_subsys_initcall()

    copy from :https://my.oschina.net/u/572632/blog/305492

    概述

           内核选项的解析完成之后,各个子系统的初始化即进入第二部分—入口函数的调用。通常USB、PCI这样的子系统都会有一个名为subsys_initcall的入口,如果你选择它们作为研究内核的切入点,那么就请首先找到它。

    section的声明

        C 语言中attribute属性的section是在目标文件链接时可以用于主动定制代码的位置,具体可以WIKI,下面看linux kernel中是如何定义的。 

          以下代码来自 linux内核源码中 include/linux/init.h 文件。下面使用相同语法规则的变量名存放了各个初始化函数的地址。

            更重要的是其section属性也是按照一定规则构成的。

            关于section见 http://lihuize123123.blog.163.com/blog/static/878290522010420111428109/

    /* initcalls are now grouped by functionality into separate 
     * subsections. Ordering inside the subsections is determined
     * by link order. 
     * For backwards compatibility, initcall() puts the call in 
     * the device init subsection.
     *
     * The `id' arg to __define_initcall() is needed so that multiple initcalls
     * can point at the same handler without causing duplicate-symbol build errors.
     */
    
    #define __define_initcall(level,fn,id) 
    	static initcall_t __initcall_##fn##id __used 
    	__attribute__((__section__(".initcall" level ".init"))) = fn
    
    /*
     * Early initcalls run before initializing SMP.
     *
     * Only for built-in code, not modules.
     */
    #define early_initcall(fn)		__define_initcall("early",fn,early)
    
    /*
     * A "pure" initcall has no dependencies on anything else, and purely
     * initializes variables that couldn'be statically initialized.
     *
     * This only exists for built-in code, not for modules.
     */
    #define pure_initcall(fn)		__define_initcall("0",fn,0)
    
    #define core_initcall(fn)		__define_initcall("1",fn,1)
    #define core_initcall_sync(fn)		__define_initcall("1s",fn,1s)
    #define postcore_initcall(fn)		__define_initcall("2",fn,2)
    #define postcore_initcall_sync(fn)	__define_initcall("2s",fn,2s)
    #define arch_initcall(fn)		__define_initcall("3",fn,3)
    #define arch_initcall_sync(fn)		__define_initcall("3s",fn,3s)
    #define subsys_initcall(fn)		__define_initcall("4",fn,4)
    #define subsys_initcall_sync(fn)	__define_initcall("4s",fn,4s)
    #define fs_initcall(fn)			__define_initcall("5",fn,5)
    #define fs_initcall_sync(fn)		__define_initcall("5s",fn,5s)
    #define rootfs_initcall(fn)		__define_initcall("rootfs",fn,rootfs)
    #define device_initcall(fn)		__define_initcall("6",fn,6)
    #define device_initcall_sync(fn)	__define_initcall("6s",fn,6s)
    #define late_initcall(fn)		__define_initcall("7",fn,7)
    #define late_initcall_sync(fn)		__define_initcall("7s",fn,7s)
    
    #define __initcall(fn) device_initcall(fn)
    
    #define __exitcall(fn) 
    	static exitcall_t __exitcall_##fn __exit_call = fn
    
    #define console_initcall(fn) 
    	static initcall_t __initcall_##fn 
    	__used __section(.con_initcall.init) = fn
    
    #define security_initcall(fn) 
    	static initcall_t __initcall_##fn 
    	__used __section(.security_initcall.init) = fn

    注册

            这些入口有个共同的特征,它们都是使用__define_initcall宏定义的。它们的调用也不是随便的,而是按照一定顺序的,这个顺序就取决于__define_initcall宏。__define_initcall宏用来将指定的函数指针放到.initcall.init节里。

    .initcall.init节

            内核可执行文件由许多链接在一起的对象文件组成。对象文件有许多节,如文本、数据、init数据、bass等等。这些对象文件都是由一个称为链接器脚本的文件链接并装入的。这个链接器脚本的功能是将输入对象文件的各节映射到输出文件中;换句话说,它将所有输入对象文件都链接到单一的可执行文件中,将该可执行文件的各节装入到指定地址处。 vmlinux.lds是存在于arch/<target>/目录中的内核链接器脚本,它负责链接内核的各个节并将它们装入内存中特定偏移量处。在vmlinux.lds文件里查找initcall.init就可以看到下面的内容

    #define INITCALLS							
    	*(.initcallearly.init)						
    	VMLINUX_SYMBOL(__early_initcall_end) = .;			
      	*(.initcall0.init)						
      	*(.initcall0s.init)						
      	*(.initcall1.init)						
      	*(.initcall1s.init)						
      	*(.initcall2.init)						
      	*(.initcall2s.init)						
      	*(.initcall3.init)						
      	*(.initcall3s.init)						
      	*(.initcall4.init)						
      	*(.initcall4s.init)						
      	*(.initcall5.init)						
      	*(.initcall5s.init)						
    	*(.initcallrootfs.init)						
      	*(.initcall6.init)						
      	*(.initcall6s.init)						
      	*(.initcall7.init)						
      	*(.initcall7s.init)

    这就告诉我们.initcall.init节又分成了7个子节,而xxx_initcall入口函数指针具体放在哪一个子节里边儿是由xxx_initcall的定义中,__define_initcall宏的参数决定的,比如core_initcall将函数指针放在.initcall1.init子节,device_initcall将函数指针放在了.initcall6.init子节等等。各个子节的顺序是确定的,即先调用.initcall1.init中的函数指针再调用.initcall2.init中的函数指针,等等。不同的入口函数被放在不同的子节中,因此也就决定了它们的调用顺序。

    注意:设备驱动程序中常见的module_init(x)函数,查看init.h文件发现

    /**
     * module_init() - driver initialization entry point
     * @x: function to be run at kernel boot time or module insertion
     * 
     * module_init() will either be called during do_initcalls() (if
     * builtin) or at module insertion time (if a module).  There can only
     * be one per module.
     */
    #define module_init(x)	__initcall(x);
    
    #define __initcall(fn) device_initcall(fn)
    
    /* Don'use these in modules, but some people do... */
    #define early_initcall(fn)		module_init(fn)
    #define core_initcall(fn)		module_init(fn)
    #define postcore_initcall(fn)		module_init(fn)
    #define arch_initcall(fn)		module_init(fn)
    #define subsys_initcall(fn)		module_init(fn)
    #define fs_initcall(fn)			module_init(fn)
    #define device_initcall(fn)		module_init(fn)
    #define late_initcall(fn)		module_init(fn)
    #define __define_initcall(level,fn) 
    	static initcall_t __initcall_##fn __used 
    	__attribute__((__section__(".initcall" level ".init"))) = fn
    
    /* Userspace initcalls shouldn't depend on anything in the kernel, so we'll
     * make them run first.
     */
    #define __initcall(fn) __define_initcall("1", fn)
    
    #define __exitcall(fn) static exitcall_t __exitcall_##fn __exit_call = fn
    
    #define __init_call	__used __section(.initcall.init)

    这样推断 module_init 调用优先级为6低于subsys_initcall调用优先级4

    调用

    static void __init do_initcalls(void)
    {
    	initcall_t *fn;
    
    	for (fn = __early_initcall_end; fn < __initcall_end; fn++)
    		do_one_initcall(*fn);
    
    	/* Make sure there is no pending stuff from the initcall sequence */
    	flush_scheduled_work();
    }
    
    int __init_or_module do_one_initcall(initcall_t fn)
    {
    	int count = preempt_count();
    	int ret;
    
    	if (initcall_debug)
    		ret = do_one_initcall_debug(fn);
    	else
    		ret = fn();
    
    	msgbuf[0] = 0;
    
    	if (ret && ret != -ENODEV && initcall_debug)
    		sprintf(msgbuf, "error code %d ", ret);
    
    	if (preempt_count() != count) {
    		strlcat(msgbuf, "preemption imbalance ", sizeof(msgbuf));
    		preempt_count() = count;
    	}
    	if (irqs_disabled()) {
    		strlcat(msgbuf, "disabled interrupts ", sizeof(msgbuf));
    		local_irq_enable();
    	}
    	if (msgbuf[0]) {
    		printk("initcall %pF returned with %s
    ", fn, msgbuf);
    	}
    
    	return ret;
    }
    Always Believe Something Beauitful Will Be Happen
  • 相关阅读:
    webstorm配置github 以及本地代码上传github。
    日期控件------moment.js
    图片查看器
    日期
    配置一个node服务器
    git代码管理
    vue
    javascript (字符串, 数组, 对象 , 日期 和 操作元素节点 动画 定时器)
    html css
    JS常用方法
  • 原文地址:https://www.cnblogs.com/Oude/p/12221453.html
Copyright © 2011-2022 走看看