Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8019421
  • 博文数量: 159
  • 博客积分: 10424
  • 博客等级: 少将
  • 技术积分: 14615
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-14 12:45
个人简介

啦啦啦~~~

文章分类
文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(10)

2011年(116)

2010年(22)

分类: LINUX

2013-02-21 23:58:06

作者: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内核的两次解析


阅读(14059) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

gfree_wind2013-02-24 23:27:06

Bean_lee:这次博客升级不好,做出来的界面比较丑。兄弟可以试试Markdown。这个容易学,30分钟就能学的不错。然后博客的格式也不错。

Markdown弄完了,可以直接粘贴过来吗?

貌似评论都不会发消息通知了。

回复 | 举报

Bean_lee2013-02-24 17:57:13

这次博客升级不好,做出来的界面比较丑。兄弟可以试试Markdown。这个容易学,30分钟就能学的不错。然后博客的格式也不错。