#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); |
分析:
early_param宏可以展开为:_setup_param("debug",nf_debug_setup,debug_setup,1);
继而可以展开为:
static const char _setup_debug_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}
struct obs_kernel_param { const char *str; int (*setup_func)(char *); int early; };
通过_section宏,编译器会将_setup_nf_debug_setup放置在.init.setup中。
arch/x86/kernel/vmlinux.lds中,__setup_start指向了.init.setup开头的地址,而__setup_end指向了.init.setup的结束地址。
start_kernel->parse_early_param->parse_early_options
|
void __init parse_early_param(void) { static __initdata int done = 0; static __initdata char tmp_cmdline[COMMAND_LINE_SIZE]; if (done) return; /* All fall through to do_early_param. */ strlcpy(tmp_cmdline, boot_command_line, COMMAND_LINE_SIZE);//复制启动命令行数据 parse_early_options(tmp_cmdline); done = 1; } |
void __init parse_early_options(char *cmdline) { parse_args("early options", cmdline, NULL, 0, 0, 0, do_early_param); } |
/* Args looks like "foo=bar,bar2 baz=fuz wiz". */ int parse_args(const char *doing,//“early options” char *args,//命令行参数 const struct kernel_param *params,//NULL unsigned num,//0 s16 min_level,//0 s16 max_level,//0 int (*unknown)(char *param, char *val, const char *doing)//do_early_param) { char *param, *val; /* Chew leading spaces */ args = skip_spaces(args); if (*args) pr_debug("doing %s, parsing ARGS: '%s'\n", doing, args); while (*args) { int ret; int irq_was_disabled; args = next_arg(args, ¶m, &val); irq_was_disabled = irqs_disabled(); ret = parse_one(param, val, doing, params, num, min_level, max_level, unknown); if (irq_was_disabled && !irqs_disabled()) pr_warn("%s: option '%s' enabled irq's!\n", doing, param); switch (ret) { case -ENOENT: pr_err("%s: Unknown parameter `%s'\n", doing, param); return ret; case -ENOSPC: pr_err("%s: `%s' too large for parameter `%s'\n", doing, val ?: "", param); return ret; case 0: break; default: pr_err("%s: `%s' invalid for parameter `%s'\n", doing, val ?: "", param); return ret; } } /* All parsed OK. */ return 0; } |
//命令行参数的解析parse_one static int parse_one(char *param,char *val,const struct kernel_param *params,unsigned num_params,int (*handle_unknown)(char *param, char *val)) { unsigned int i; int err; /* Find parameter */ for (i = 0; i < num_params; i++) { //num_params=0 if (parameq(param, params[i].name)) { if (!val && params[i].ops->set != param_set_bool) return -EINVAL; DEBUGP("They are equal! Calling %p\n",params[i].ops->set); mutex_lock(¶m_lock); err = params[i].ops->set(val, ¶ms[i]); mutex_unlock(¶m_lock); return err; } } if (handle_unknown) { //若handle_unknown函数存在 DEBUGP("Unknown argument: calling %p\n", handle_unknown); return handle_unknown(param, val); //则调用handle_unknown函数,参数为param,val } DEBUGP("Unknown argument `%s'\n", param); return -ENOENT; }
回溯回去handle_unknow函数就是do_early_param
static int __init do_early_param(char *param, char *val) { const struct obs_kernel_param *p; for (p = __setup_start; p < __setup_end; p++) { if ((p->early && strcmp(param, p->str) == 0) || (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段,
判断参数,进入if函数体里面
if (p->setup_func(val) != 0)这句调用了对应setup_func或early_param成员的函数,并将val作为其参数,val其实便是__setup(str, fn)或__early_param中的str
其实就是调用了fn(str)
这里的第一条if会刷选掉__setup定义的情况(除了console和earlycon参数的),因为__setup定义的obs_kernel_param结构体p->early=0
__setup定义的fn会在start_kernel->parse_args("Booting kernel", static_command_line, __start___param,__stop___param - __start___param,&unknown_bootoption);
unknown_bootoption->obsolete_checksetup函数给调用
看start_kernel中调用顺序
parse_early_param(); parse_args("Booting kernel", static_command_line, __start___param, __stop___param - __start___param,&unknown_bootoption);
可见先调用__early_param定义的解析参数函数及__setup定义的(console及earlycon)的参数解析函数
接着再调用__setup定义的其他解析参数函数.