#define __setup_param(str, unique_id, fn, early) /
static char __setup_str_##unique_id[] __initdata = str; /
static struct obs_kernel_param __setup_##unique_id /
__attribute_used__ /
__attribute__((__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)
__setup与early_param不同的是,early_param宏注册的内核选项必须要在其他内核选项之前被处理。
在函数start_kernel中,parse_early_param处理early_param定义的参数,parse_args处理__setup定义的参数。
parse_early_param();
parse_args("Booting kernel", static_command_line, __start___param,
__stop___param - __start___param,
&unknown_bootoption);
- 所有的系统启动参数都是由形如static int __init foo(char* str);了的函数来支持的。
#define __init __attribute__ ((__section__ (".init.text")))
由上可知,所有的启动参数支持函数都放入.init.text段。
- 用__setup宏来导出参数的支持函数
__setup("foo=" , foo);
展开后就是如下的形式
static char __setup_str_foo[] __initdata = "foo=";
static struct obs_kernel_param __setup_foo
__attribute_used__
__attribute__((__section__(".init.setup")))
__attribute__((aligned((sizeof(long)))))
= { __setup_str_foo, foo, 0 };//"foo=",foo,0
也就是说,启动参数被封装到obs_kernel_param结构中,所有的内核启动参数青菜成内核映像.init.setup段中的一个obs_kernel_param数组。
- 用early_param宏来申明需要早期处理的启动参数。展开后和__setup是一样的只是early参数不一样,因此会在do_early_param中处理。
- 内核对启动参数的解析。
static int __init do_early_param(char *param, char *val)
{
struct obs_kernel_param *p;
for (p = __setup_start; p < __setup_end; p++) {
if (p->early && strcmp(param, p->str) == 0) {
if (p->setup_func(val) != 0)
printk(KERN_WARNING
"Malformed early option '%s'/n", param);
}
}
/* We accept everything at this stage. */
return 0;
}
函数在parse_early_param中被调用,而parse_early_param在start_kernel中被调用,parse_early_param之后的parse_args会调用下面函数
static int __init obsolete_checksetup(char *line)
{
struct obs_kernel_param *p;
int had_early_param = 0;
p = __setup_start;
do {
int n = strlen(p->str);
if (!strncmp(line, p->str, n)) {
if (p->early) {
/* Already done in parse_early_param?
* (Needs exact match on param part).
* Keep iterating, as we can have early
* params and __setups of same names 8( */
if (line[n] == '/0' || line[n] == '=')
had_early_param = 1;
} else if (!p->setup_func) {
printk(KERN_WARNING "Parameter %s is obsolete,"
" ignored/n", p->str);
return 1;
} else if (p->setup_func(line + n))//调用支持函数
return 1;
}
p++;
} while (p < __setup_end);
return had_early_param;
}
init/main.c中启动参数申明列表
__setup("nosmp", nosmp);
__setup("maxcpus=", maxcpus);
__setup("reset_devices", set_reset_devices);
__setup("debug", debug_kernel);
__setup("quiet", quiet_kernel);
__setup("loglevel=", loglevel);
__setup("init=", init_setup);
__setup("rdinit=", rdinit_setup);
__setup("initcall_debug", initcall_debug_setup);
最后欢迎大家访问我的个人网站:1024s