您的位置:首页 > 运维架构 > Linux

linux 2.6源代码情景分析笔记之内存3

2009-12-23 03:31 573 查看
关于e820

e820是BIOS的(int 0x15)中断关联的。在使用此中断时,ax中必须是e820(ireg.ax=0xe820;)。通过此中断可以得到内存的系统布局。通过do循环,每次得到一段。

/linux32/arch/x86/include/asm/e820.h

宏E820_MAP是struct e820entry数据结构的指针,存放在参数块中位移为0x2d0的地方。

#define E820MAP 0x2d0 /* our map */

#define E820MAX 128 /* number of entries in E820MAP */

#define E820NR 0x1e8 /* # entries in E820MAP */

#define E820_RAM 1

#define E820_RESERVED 2

#define E820_ACPI 3 /* usable as RAM once ACPI tables have been read */

#define E820_NVS 4//此项表明rom eprom flash存储器。

#define E820_UNUSABLE 5

#include <linux/types.h>

struct e820entry {

__u64 addr; /* start of memory segment */

__u64 size; /* size of memory segment */

__u32 type; /* type of memory segment */

} __attribute__((packed));

#define E820_X_MAX (E820MAX + 3 * MAX_NUMNODES)

struct e820map {

__u32 nr_map;

struct e820entry map[E820_X_MAX];

};

每个结构都是对一个物理区间的描述,并且一个物理区间必须是同一类型。如果有一片地址连续的物理内存空间,其中一部分是ram另一部分是rom,就要分成两个区间。即使同属ram,如果其中一部分要保留用于特殊目的,那也属于不同的一个分区。

从0xf0000-0xfffff的最高4k,就是bios使用的,只要有bios存在,就需要两个区间。那么nr_map就不能小于2.现在1mb的边界已经打破,将1mb以上的空间叫HIGH_MEMORY。为了兼容,就要保留最初的1mb.

#define HIGH_MEMORY (1024*1024)

从e820中得到的是系统内存布局图。

从下面对e820的使用上就可以看出,其的位图地址和类型之间的关系。开始到结尾:e820.map[i].addr--->(e820.map[i].addr + e820.map[i].size).

然后是按照几个类型来打印,宏在上面已经列出。

/linux32/arch/x86/kernel

void __init e820_print_map(char *who)

{

int i;

for (i = 0; i < e820.nr_map; i++) {

printk(KERN_INFO " %s: %016Lx - %016Lx ", who,(unsigned long long) e820.map[i].addr,(unsigned long long)(e820.map[i].addr + e820.map[i].size));

e820_print_type(e820.map[i].type);

printk(KERN_CONT "/n");

}

}

static void __init e820_print_type(u32 type)

{

switch (type) {

case E820_RAM:

case E820_RESERVED_KERN:

printk(KERN_CONT "(usable)");

break;

case E820_RESERVED:

printk(KERN_CONT "(reserved)");

break;

case E820_ACPI:

printk(KERN_CONT "(ACPI data)");

break;

case E820_NVS:

printk(KERN_CONT "(ACPI NVS)");

break;

case E820_UNUSABLE:

printk(KERN_CONT "(unusable)");

break;

default:

printk(KERN_CONT "type %u", type);

break;

}

}

得到他的是这个函数:

#include "boot.h"

#define SMAP 0x534d4150 /* ASCII "SMAP" */

static int detect_memory_e820(void)

{

int count = 0;

struct biosregs ireg, oreg;

struct e820entry *desc = boot_params.e820_map;

static struct e820entry buf; /* static so it is zeroed */

initregs(&ireg);

ireg.ax = 0xe820;

ireg.cx = sizeof buf;

ireg.edx = SMAP;

ireg.di = (size_t)&buf;

do {

intcall(0x15, &ireg, &oreg);

ireg.ebx = oreg.ebx; /* for next iteration... */

if (oreg.eflags & X86_EFLAGS_CF)

break;

if (oreg.eax != SMAP) {

count = 0;

break;

*desc++ = buf;

count++;

} while (ireg.ebx && count < ARRAY_SIZE(boot_params.e820_map));

return boot_params.e820_entries = count;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: