Search code examples
memorybiosmemory-managementacpix86

Using BIOS int 0x15/E820 to view the memory map


I am doing some baremetal programming on x86. I am in 16 bit mode and I emulate using qemu... I use int 0x15/ax = 0xE820 to view the memory map... Using the qemu monitor (to examine the region where I wrote the map) I get the following information about the memorymap:

Start=0 Length= 0x9fc00 (type 1)
Start=0x9fc00 Length= 0x400 (type 2) 
Start=0xf0000 Length= 0x10000 (type 2)
Start=0x100000 Length= 0x7ee0000 (type 1)
Start=0x7fe0000 Length= 0x10000 (type 2)
Start=0xfffc0000 Length = 0x40000 (Type 2)

So, are the segments that are not in this map (Eg. 0xA0000) belong to something like MMIO? I know that type 2 memory regions are used by BIOS, etc. ( Start=0x9fc00 Length= 0x400 (type 2) is EDBA) What happens when I write to these regions?

Note: I don't have type 3(ACPI) probably because I'm using qemu. In real hardware I most probably should get type 3 regions too. Is that RAM also?


Solution

  • This BIOS call is filly described in INT 15h, AX=E820h - Query System Address Map, where it is noted in the section "Assumptions and Limitations":

    1. The BIOS will return address ranges describing base board memory and ISA or PCI memory that is contiguous with that baseboard memory.
    2. The BIOS WILL NOT return a range description for the memory mapping of PCI devices, ISA Option ROM's, and ISA plug & play cards. This is because the OS has mechanisms available to detect them.
    3. The BIOS will return chipset defined address holes that are not being used by devices as reserved.
    4. Address ranges defined for base board memory mapped I/O devices (for example APICs) will be returned as reserved.
    5. All occurrences of the system BIOS will be mapped as reserved. This includes the area below 1 MB, at 16 MB (if present) and at end of the address space (4 gig).
    6. Standard PC address ranges will not be reported. Example video memory at A0000 to BFFFF physical will not be described by this function. The range from E0000 to EFFFF is base board specific and will be reported as suits the bas board.
    7. All of lower memory is reported as normal memory. It is OS's responsibility to handle standard RAM locations reserved for specific uses, for example: the interrupt vector table(0:0) and the BIOS data area(40:0).

    The Wikipedia article Detecting Memory (x86) add in its section "BIOS Function: INT 0x15, EAX = 0xE820" adds the following rules:

    • After getting the list, it may be desirable to: sort the list, combine adjacent ranges of the same type, change any overlapping areas to the most restrictive type, and change any unrecognised "type" values to type 2.
    • Type 3 "ACPI reclaimable" memory regions may be used like (and combined with) normal "available RAM" areas as long as you're finished using the ACPI tables that are stored there (i.e. it can be "reclaimed").
    • Types 2, 4, 5 (reserved, ACPI non-volatile, bad) mark areas that should be avoided when you are allocating physical memory.
    • Treat unlisted regions as Type 2 -- reserved.
    • Your code must be able to handle areas that don't start or end on any sort of "page boundary".

    Conclusion: The ranges that are not found in the map are functional, perhaps occupied by device memory such as video.