手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux物理内存探测

[日期:2012-01-02] 来源:Linux社区  作者:bullbat [字体:大 中 小]
其中存放中断返回值得结构如下
  1. struct e820entry {  
  2.     __u64 addr; /* start of memory segment */  
  3.     __u64 size; /* size of memory segment */  
  4.     __u32 type; /* type of memory segment */  
  5. } __attribute__((packed));  
在内核初始化跳入start_kernel函数后执行以下初始化

start_kernel()->setup_arch()->setup_memory_map()

  1. /*调用x86_init.resources.memory_setup()实现对e820内存图的优化, 
  2. 将e820中得值保存在e820_saved中,打印内存图 
  3. */  
  4. void __init setup_memory_map(void)  
  5. {  
  6.     char *who;  
  7.     /*调用x86体系下的memory_setup函数*/  
  8.     who = x86_init.resources.memory_setup();  
  9.     /*保存到e820_saved中*/  
  10.     memcpy(&e820_saved, &e820, sizeof(struct e820map));  
  11.     printk(KERN_INFO "BIOS-provided physical RAM map:\n");  
  12.     /*打印输出*/  
  13.     e820_print_map(who);  
  14. }  

在x86_init.c中定义了x86下的memory_setup函数

  1. struct x86_init_ops x86_init __initdata = {  
  2.   
  3.     .resources = {  
  4.         ……  
  5.         .memory_setup       = default_machine_specific_memory_setup,  
  6.     },  
  7.         ……  
  8. };  
 
  1. char *__init default_machine_specific_memory_setup(void)  
  2. {  
  3.     char *who = "BIOS-e820";  
  4.     u32 new_nr;  
  5.     /* 
  6.      * Try to copy the BIOS-supplied E820-map. 
  7.      * 
  8.      * Otherwise fake a memory map; one section from 0k->640k, 
  9.      * the next section from 1mb->appropriate_mem_k 
  10.      */  
  11.     new_nr = boot_params.e820_entries;  
  12.     /*将重叠的去除*/  
  13.     sanitize_e820_map(boot_params.e820_map,  
  14.             ARRAY_SIZE(boot_params.e820_map),  
  15.             &new_nr);  
  16.     /*去掉重叠的部分后得到的内存个数*/  
  17.     boot_params.e820_entries = new_nr;  
  18.     /*将其赋值到全局变量e820中,小于0时,为出错处理*/  
  19.     if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)  
  20.       < 0) {  
  21.         ……  
  22.     }  
  23.   
  24.     /* In case someone cares... */  
  25.     return who;  
  26. }  

append_e820_map调用__append_e820_map实现

  1. static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)  
  2. {  
  3.     while (nr_map) {/*循环nr_map次调用,添加内存块到e820*/  
  4.         u64 start = biosmap->addr;  
  5.         u64 size = biosmap->size;  
  6.         u64 end = start + size;  
  7.         u32 type = biosmap->type;  
  8.         /* Overflow in 64 bits? Ignore the memory map. */  
  9.         if (start > end)  
  10.             return -1;  
  11.         /*添加函数*/  
  12.         e820_add_region(start, size, type);  
  13.         biosmap++;  
  14.         nr_map--;  
  15.     }  
  16.     return 0;  
  17. }  

 
  1. void __init e820_add_region(u64 start, u64 size, int type)  
  2. {  
  3.     __e820_add_region(&e820, start, size, type);  
  4. }  

e820为e820map结构

  1. struct e820map {  
  2.     __u32 nr_map;  
  3.     struct e820entry map[E820_X_MAX];  
  4. };  

其中E820_X_MAX大小为128.

  1. tatic void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,  
  2.                      int type)  
  3. {  
  4.     int x = e820x->nr_map;  
  5.   
  6.     if (x >= ARRAY_SIZE(e820x->map)) {  
  7.         printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");  
  8.         return;  
  9.     }  
到这里,物理内存就已经从BIOS中读出来存放到全局变量e820中,e820是linux内核中用于建立内存管理框架的基础。在后面我们会看到,建立初始化节点、管理区会用到他。见 http://www.linuxidc.com/Linux/2012-01/50757.htmlinux
相关资讯       Linux基础教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款