Linux内核的early_param原理追踪

14190阅读 2评论2013-02-21 gfree_wind
分类:LINUX

作者: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也是用于这个目的。在后文会慢慢看出这两者的区别。


先看它们的定义:

/*
 * Only for really core code.  See moduleparam.h for the normal way.
 *
 * Force the alignment so the compiler doesn't space elements of the
 * obs_kernel_param "array" too far apart in .init.setup.
 */
#define __setup_param(str, unique_id, fn, early)            \
    static const char __setup_str_##unique_id[] __initconst \
        __aligned(1) = str; \
    static struct 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)

/* NOTE: fn is as per module_param, not __setup!  Emits warning if fn
 * returns non-zero. */
#define early_param(str, fn)                    \
    __setup_param(str, fn, fn, 1)


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

如early_param("debug", nf_debug_setup);

将其展开则为

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.setup section中。然后我们查看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

/* Check for early params. */
static int __init do_early_param(char *param, char *val, const char *unused)
{
    const struct 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
                       "Malformed early option '%s'\n", param);
        }
    }
    /* We accept everything at this stage. */
    return 0;
}


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


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

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

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

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


上一篇:TCP/IP源码(59)——TCP中的三个接收队列
下一篇:编写安全代码:不要在头文件中定义变量

文章评论