I decided to see what regions of memory I can use for free physical pages, and I know that a memory map can give me this info.
From what I know, GRUB provides a multiboot info structure, and it contains a data about the memory map: `mmap_addr' and `mmap_length' fields. In my case, the first one is addressing to 0x100A8 every time I boot the OS. And the second one is 0x90 (also every time). Also, the 6th bit in `flags' is set, this means that the memory map is correct. According to the multiboot specification, the structure of an entry of the memory map looks like this:
Code: Select all
u32 -4 size
u64 0 base_addr
u64 8 length
u32 16 type
OK, I'm using offset 0 for `size' instead of `-4'. The result looks pretty, but incorrect: see the attachement.
Here is the code:
Code: Select all
int mmap_addr = mb_info->mmap_addr;
int mmap_length = mb_info->mmap_length;
char str[32];
puts ("mmap_addr = 0x");
puts (itoa (mmap_addr, str, 16));
puts (", mmap_length = 0x");
puts (itoa (mmap_length, str, 16));
puts ("\n");
int i = mmap_addr;
while (i < (mmap_addr + mmap_length))
{
int *size = (int *) i;
int *base_addr_low = (int *) i + 4;
int *base_addr_high = (int *) i + 8;
int *length_low = (int *) i + 12;
int *length_high = (int *) i + 16;
int *type = (int *) i + 20;
puts (" size = 0x");
puts (itoa (*size, str, 16));
puts (", start = 0x");
puts (itoa (*base_addr_low, str, 16));
puts (", end = 0x");
puts (itoa (*base_addr_low + *length_low, str, 16));
puts (", type = 0x");
puts (itoa (*type, str, 16));
puts ("\n");
i += *size + 4; // Add 4 since the size is at offset 0, not -4.
}