Memory mapping

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Crazed123

Memory mapping

Post 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?
Dreamsmith

Re:Memory mapping

Post 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.
Crazed123

Re:Memory mapping

Post by Crazed123 »

So how would I turn that into something an allocator can use without already having dynamic allocation implemented?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Memory mapping

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Crazed123

Re:Memory mapping

Post by Crazed123 »

Yeah, my OS will be managing 4kb pages with bitmaps and super-bitmap. Almost got the bitmaps part down pat. Thanks!
Post Reply