Linux内核的early_param原理追踪

来源:互联网 发布:电脑工作备忘录软件 编辑:程序博客网 时间:2024/05/19 04:52

作者:gfree.wind@gmail.com

博客:blog.focus-linux.net linuxfocus.blog.chinaunix.net 
微博:weibo.com/glinuxer
QQ技术群:4367710
 
本文的copyleft归gfree.wind@gmail.com所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。

=========================================================================================================================================================

early_param用于注册内核选项解析的处理函数。与之类似的,__setup也是用于这个目的。在后文会慢慢看出这两者的区别。


先看它们的定义:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define __setup_param(str, unique_id, fn,early)           \
    staticconst char __setup_str_##unique_id[] __initconst \
        __aligned(1)= str; \
    staticstruct obs_kernel_param __setup_##unique_id \
        __used__section(.init.setup)          \
        __attribute__((aligned((sizeof(long)))))   \
        ={ __setup_str_##unique_id, fn, early }
 
#define __setup(str,fn)                   \
    __setup_param(str,fn, fn, 0)
 
#define early_param(str,fn)                   \
    __setup_param(str,fn, fn, 1)


看这个宏定义,很容易让人眼花缭乱,不如以一个实例来说明一下:

如early_param("debug",nf_debug_setup);

将其展开则为

 

1
2
3
static const char __setup_str_nf_debug_setup[] __initconst __aligned(1) ="debug";
static struct obs_kernel_param __setup_nf_debug_setup __used__section(.init.setup) __attribute__((aligned((sizeof(long)))))
    ={ __setup_str_nf_debug_setup, nf_debug_setup, 1 };


这里关键的变量就是__setup_nf_debug_setup,而关键中的关键是__section(.init.setup)。通过__section宏,编译器会将__setup_nf_debug_setup放置在.init.setupsection中。然后我们查看arch/x86/kernel/vmlinux.lds内核链接器的脚本文件。可以找到下面这几行语句 __setup_start= .; *(.init.setup) __setup_end =.;

对于内核连接脚本文件,我们不需要十分明白,也可以看出,这里__setup_start指向了.init.setup开头的地址,而__setup_end指向了.init.setup的结束地址。——有兴趣的同学,可以自行查找vmlinux.lds的资料。我对其也是一知半解呵。


搜索__setup_start,可以发现有两个函数do_early_param和obsolete_checksetup会引用它。

其调用流程如下start_kernel->parse_early_param->parse_early_options->parse_args->do_early_param

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static int __init do_early_param(char*param, char *val, const char *unused)
{
    conststruct obs_kernel_param *p;
 
    for(p = __setup_start; p < __setup_end; p++) {
        if((p->early && parameq(param, p->str)) ||
            (strcmp(param,"console")== 0 &&
             strcmp(p->str,"earlycon")== 0)
        ){
            if(p->setup_func(val) != 0)
                printk(KERN_WARNING
                       "Malformedearly option '%s'\n",param);
        }
    }
    
    return0;
}


在do_early_param中,通过__setup_start和__setup_end,它遍历了.init.setup段中的structobs_kernel_param变量。如果p->early为真且为对应的选项字符串,则调用注册的处理函数p->setup_func。


到这里,我们已经明白了early_param的作用机制。

那么利用__setup注册的处理函数,何时会被调用呢?这里简单说明一下:

在start_kernel调用完parse_early_param,其会再次调用parse_args->unknown_bootoption->obsolete_checksetup,这里会调用__setup注册的处理函数。

这也就是Linux内核的两次解析


0 0
原创粉丝点击