作者:gfree.wind@gmail.com
=========================================================================================================================================================
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内核的两次解析