- struct e820entry {
- __u64 addr; /* start of memory segment */
- __u64 size; /* size of memory segment */
- __u32 type; /* type of memory segment */
- } __attribute__((packed));
start_kernel()->setup_arch()->setup_memory_map()
- /*调用x86_init.resources.memory_setup()实现对e820内存图的优化,
- 将e820中得值保存在e820_saved中,打印内存图
- */
- void __init setup_memory_map(void)
- {
- char *who;
- /*调用x86体系下的memory_setup函数*/
- who = x86_init.resources.memory_setup();
- /*保存到e820_saved中*/
- memcpy(&e820_saved, &e820, sizeof(struct e820map));
- printk(KERN_INFO "BIOS-provided physical RAM map:\n");
- /*打印输出*/
- e820_print_map(who);
- }
在x86_init.c中定义了x86下的memory_setup函数
- struct x86_init_ops x86_init __initdata = {
- .resources = {
- ……
- .memory_setup = default_machine_specific_memory_setup,
- },
- ……
- };
- char *__init default_machine_specific_memory_setup(void)
- {
- char *who = "BIOS-e820";
- u32 new_nr;
- /*
- * Try to copy the BIOS-supplied E820-map.
- *
- * Otherwise fake a memory map; one section from 0k->640k,
- * the next section from 1mb->appropriate_mem_k
- */
- new_nr = boot_params.e820_entries;
- /*将重叠的去除*/
- sanitize_e820_map(boot_params.e820_map,
- ARRAY_SIZE(boot_params.e820_map),
- &new_nr);
- /*去掉重叠的部分后得到的内存个数*/
- boot_params.e820_entries = new_nr;
- /*将其赋值到全局变量e820中,小于0时,为出错处理*/
- if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
- < 0) {
- ……
- }
- /* In case someone cares... */
- return who;
- }
append_e820_map调用__append_e820_map实现
- static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
- {
- while (nr_map) {/*循环nr_map次调用,添加内存块到e820*/
- u64 start = biosmap->addr;
- u64 size = biosmap->size;
- u64 end = start + size;
- u32 type = biosmap->type;
- /* Overflow in 64 bits? Ignore the memory map. */
- if (start > end)
- return -1;
- /*添加函数*/
- e820_add_region(start, size, type);
- biosmap++;
- nr_map--;
- }
- return 0;
- }
- void __init e820_add_region(u64 start, u64 size, int type)
- {
- __e820_add_region(&e820, start, size, type);
- }
e820为e820map结构
- struct e820map {
- __u32 nr_map;
- struct e820entry map[E820_X_MAX];
- };
其中E820_X_MAX大小为128.
- tatic void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
- int type)
- {
- int x = e820x->nr_map;
- if (x >= ARRAY_SIZE(e820x->map)) {
- printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
- return;
- }
