Lately, I've been working on my "bootstrap kernel", whose goal is to turn longmode on and make the real kernel's life easier. One of the thing I want it to do is create a nice memory map which, contrary to the one from the BIOS that I get through GRUB, is complete, meaning that it includes memory zones occupied by the kernel and its modules.
However, this map uses strings, and its size can't be guessed in advance. So I should use dynamic memory allocation. But to do so, I need a map of the memory...
And BAM, chicken and egg paradox !
I see two ways of solving this problem :
-Static memory allocation. Let's claim we'll need a maximum of x kB of memory for that map of memory, and reserve a buffer of that size in the kernel. It's ugly, and not very robust since it relies on an assumption, but it's simple and it works.
-Another way would be to use the following algorithm :
- Parse the memory once, deduce the size required by the memory map.
- Parse memory another time, find a hole in memory to hold the memory map using first fit or best fit.
- Finally, parse memory for the third time, this time actually writing the memory map