Linux内核的early_param原理追踪

作者: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
/*
  * 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);

将其展开则为

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.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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 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内核的两次解析

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值