Page 1 of 1

Memory mapping

Posted: Sat Oct 09, 2004 11:08 pm
by Crazed123
Hiya, I've gotten a kernel to boot and get the Multiboot info struct, but how do I use it for memory mapping for an allocator? And why are there multiple memory structs pointed to by the Multiboot struct?

Re:Memory mapping

Posted: Sun Oct 10, 2004 1:08 pm
by Dreamsmith
Crazed123 wrote: Hiya, I've gotten a kernel to boot and get the Multiboot info struct, but how do I use it for memory mapping for an allocator? And why are there multiple memory structs pointed to by the Multiboot struct?
There are multiple memory structs because you need a struct for each region of memory, and unless your computer either has zero reserved memory or zero free memory, there would necessarily have to be at least two. In practice, there's usually a lot more (9 different regions in the memory map on my laptop, for example). You need to look at the type field of each struct to determine whether that region is usable or not. Type 1 means available, type 2 means reserved, other types should be treated as reserved as well unless you're sure you know what you're doing.

Re:Memory mapping

Posted: Sun Oct 10, 2004 2:39 pm
by Crazed123
So how would I turn that into something an allocator can use without already having dynamic allocation implemented?

Re:Memory mapping

Posted: Mon Oct 11, 2004 2:05 am
by Brendan
Hi,
Crazed123 wrote: So how would I turn that into something an allocator can use without already having dynamic allocation implemented?
This depends on how your OS will manage physical memory. If your OS manages 4 Kb pages only (and doesn't use physical addresses that are more than 32 bit) then perhaps something like:

Code: Select all

for(area = 0; area++; area < totalAreas) {
   if(memoryStruct.type == 1) {
      startAddress = memoryStruct.startAddress;
      endAddress = memoryStruct.endAddress;
      if((startAddress & 0xFFF) != 0) {
         startAddress = (startAddress & 0xFFFFF000) + 0x1000;
      }
      if(endAddress >= 4 Gb) endAddress = 4 Gb;
      if(startAddress >= 4 Gb) startAddress = 4 Gb;
      endAddress = endAddress & 0xFFFFF000;

      while(startAddress < endAddress) {
         if(startAddress isn't used by something that's de-allocated later) {
            freePhysicalPage(startAddress);
         }
         startAddress += 0x1000;
      }
   }
   memoryStruct = memoryStruct + memoryStruct.size;
}

Cheers,

Brendan

Re:Memory mapping

Posted: Mon Oct 11, 2004 10:32 am
by Crazed123
Yeah, my OS will be managing 4kb pages with bitmaps and super-bitmap. Almost got the bitmaps part down pat. Thanks!