http://blog.chinaunix.net/uid-26859697-id-4782816.html

虽说前文分析内存管理框架构建的实现,提到了<span style="-ms-word-wrap: break-word;">find_zone_movable_pfns_for_nodes()</span>,但这里不准备复述什么,仅针对<span style="-ms-word-wrap: break-word;">required_movablecore</span>和<span style="-ms-word-wrap: break-word;">required_kernelcore</span>做一个补充。

以<span style="-ms-word-wrap: break-word;">required_movablecore</span>为例,代码中没有很清晰地表明该值从何而来,仅有一处<span style="-ms-word-wrap: break-word;">cmdline_parse_movablecore()</span>疑似赋值的实现:


1. 【file:/mm/page_alloc.c】
2. /
3.   movablecore=size sets the amount of memory for use for allocations that
4.   can be reclaimed or migrated.
5.  /
6. static int __init cmdline_parse_movablecore(char *p)
7. {
8.     return cmdline_parse_core(p, &required_movablecore);
9. }
而其中<span style="-ms-word-wrap: break-word;">cmdline_parse_core()</span>实现:


1. 【file:/mm/page_alloc.c】
2. static int __init cmdline_parse_core(char p, unsigned long core)
3. {
4.     unsigned long long coremem;
5.     if (!p)
6.         return -EINVAL;
7.  
8.     coremem = memparse(p, &p);
9.     core = coremem >> PAGE_SHIFT;
10.  
11.     / Paranoid check that UL is enough for the coremem value */
12.     WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
13.  
14.     return 0;
15. }
可以推测其值是由此而来,继而可以找到:


1. 【file:/mm/page_alloc.c】
2. early_param("movablecore", cmdline_parse_movablecore);
&nbsp;

这是一个函数注册宏,该宏展开后:

static const char __setup_str_cmdline_parse_movablecore[] __attribute__ ((__section__(&quot;.init.rodata&quot;))) __attribute__((aligned(1))) = &quot;movablecore&quot;; static struct obs_kernel_param __setup_cmdline_parse_movablecore __attribute__((__used__)) __attribute__ ((__section__(&quot;.init.setup&quot;))) __attribute__((aligned((sizeof(long))))) = { __setup_str_cmdline_parse_movablecore, cmdline_parse_movablecore, 1 }

由此借用于<span style="-ms-word-wrap: break-word;">__section__</span>属性定义,编译器通过<span style="-ms-word-wrap: break-word;">arch/x86/kernel/vmlinux.lds</span>链接脚本把<span style="-ms-word-wrap: break-word;">__setup_cmdline_parse_movablecore</span>放置在<span style="-ms-word-wrap: break-word;">.init.setup</span>段中。

注册的函数将会在<span style="-ms-word-wrap: break-word;">do_early_param()</span>中调用,相关代码:


1. 【file:/init/main.c】
2. / Check for early params. /
3. static int init do_early_param(char param, char val, const char unused)
4. {
5.     const struct obs_kernel_param p;
6.  
7.     for (p =
setup_start; p < __setup_end; p++) {
8.         if ((p->early && parameq(param, p->str)) ||
9.             (strcmp(param, "console") == 0 &&
10.              strcmp(p->str, "earlycon") == 0)
11.         ) {
12.             if (p->setup_func(val) != 0)
13.                 pr_warn("Malformed early option '%s'\n", param);
14.         }
15.     }
16.     / We accept everything at this stage. /
17.     return 0;
18. }
&nbsp;

可以看到借助于<span style="-ms-word-wrap: break-word;">__setup_start</span>和<span style="-ms-word-wrap: break-word;">__setup_end</span>的范围标记,将遍历<span style="-ms-word-wrap: break-word;">.init.setup</span>段中的排布的<span style="-ms-word-wrap: break-word;">obs_kernel_param</span>结构体,找到匹配的字符串及<span style="-ms-word-wrap: break-word;">early</span>成员为<span style="-ms-word-wrap: break-word;">true</span>的情况下,将会调用其中的<span style="-ms-word-wrap: break-word;">setup_func</span>钩子函数,由此将对应数据初始化。

do_early_param()函数在初始化时的调用关系:

start_kernel()

-&gt;setup_arch()


-&gt;parse_early_param()


-&gt;parse_early_options()


-&gt;parse_args()


-&gt;parse_one()


-&gt;do_early_param()
这里只能说明<span style="-ms-word-wrap: break-word;">required_movablecore</span>和<span style="-ms-word-wrap: break-word;">required_kernelcore</span>在何处初始化的,而其值的由来呢?其值通过入参传递,在<span style="-ms-word-wrap: break-word;">parse_early_param()</span>函数中通过<span style="-ms-word-wrap: break-word;">strlcpy()</span>来自于<span style="-ms-word-wrap: break-word;">boot_command_line</span>全局变量。而<span style="-ms-word-wrap: break-word;">boot_command_line</span>早期则是在<span style="-ms-word-wrap: break-word;">/arch/x86/kernel/head_32.s</span>的初始化中来自于<span style="-ms-word-wrap: break-word;">boot_params</span>。具体 <span style="-ms-word-wrap: break-word;">do_early_param()</span>处理的数据来自于<span style="-ms-word-wrap: break-word;">grub.cfg</span>,也就是说<span style="-ms-word-wrap: break-word;">required_movablecore</span>和<span style="-ms-word-wrap: break-word;">required_kernelcore</span>也是来自于此。

好了,暂且分析至此,避免走偏了,该文也是一时好奇钻研了一下,记之以作备忘。

&nbsp;