本课时主要介绍检测内存容量的方法,以及如何将这些信息保存起来,以供后续操作系统使用。

内存检测方法: INT 0x15, EAX = 0xE820

我没有找到专门介绍内存检测方法的官方文档,只在osdev网站上(见文末参考资料)中找到相关的检测方法。具体来看,其中有简单也有复杂的方法,有的只在某些机器上可用。推荐的一种方法如下(摘自osdev):
第一次调用时,ES: DI存储保存读取的信息的存储位置
● 清除EBX,设置为0
● EDX需要设置成:0x534D4150
● EAX设置成:0xE820
● ECX设置成:24
● 执行INT 0x15
● 返回结果:EAX = 0x534D4150,CF标志清0,EBX被设置成某个数值用于下次调用,CL=实际读取的字节数
后续调用:
● EDX需要设置成:0x534D4150
● EAX重设为0xE820
● ECX重设为24
● 执行INT 0x15
● 返回结果:EAX = 0x534D4150,CF标志清0。如果EBX=0,则表明读取完毕,否则当前条目有效。

具体实现算法如下(摘自osdev)。本课时中采用了这种方法,代码上略做了修改。其中内联汇编的部分是直接复制的下面的代码,没有必要深究。毕竟我们的主要目的是实现操作系统,而不是掌握内联汇编的细节性写法。

__asm__(".code16gcc\n");
 
// SMAP entry structure
#include <stdint.h>
typedef struct SMAP_entry {
 
    uint32_t BaseL; // base address uint64_t
    uint32_t BaseH;
    uint32_t LengthL; // length uint64_t
    uint32_t LengthH;
    uint32_t Type; // entry Type,值为1时表明为我们可用的RAM空间
    uint32_t ACPI; // extended, bit0=0时表明此条目应当被忽略
 
}__attribute__((packed)) SMAP_entry_t;
 
int detectMemory(SMAP_entry_t* buffer, int maxentries)
{
    uint32_t contID = 0;
    int entries = 0, signature, bytes;
    do 
    {
        __asm__ __volatile__ ("int  $0x15" 
                : "=a"(signature), "=c"(bytes), "=b"(contID)
                : "a"(0xE820), "b"(contID), "c"(24), "d"(0x534D4150), "D"(buffer));
        if (signature != 0x534D4150) 
            return -1; // error
        if (bytes > 20 && (buffer->ACPI & 0x0001) == 0) {
            // ignore this entry
        }
        else {
            buffer++;
            entries++;
        }
    } 
    while (contID != 0 && entries < maxentries);
    return entries;
}
 
// 具体使用
    SMAP_entry_t* smap = (SMAP_entry_t*) 0x1000;
    const int smap_size = 0x2000;
 
    int entry_count = detectMemory(smap, smap_size / sizeof(SMAP_entry_t));
    if (entry_count == -1) {
        // error - halt system and/or show error message
        [...]
    } else {
        // process memory map
        [...]
    }
}

参考资料

● 内存检测方法:https://wiki.osdev.org/Detecting_Memory_(x86)



登陆发表评论