Linux啟動引數及實現 __setup與early_param(講的不錯,轉載)
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);
1,所有的系統啟動引數都是由形如 static int __init foo(char *str);的函式來支援的
注:#define __init __attribute__ ((__section__ (".init.text")))
申明所有的啟動引數支援函式都放入.init.text段
2.1,用__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陣列
2.2用early_param巨集來申明需要'早期'處理的啟動引數,例如在
arch/i386/kernel/setup.c就有如下的申明:
early_param("mem", parse_mem);
展開後和__setup是一樣的只是early引數不一樣,因此會在do_early_param
中被處理
3,核心對啟動引數的解析:下面函式歷遍obs_kernel_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);