OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 4:42 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Physical memory management?
PostPosted: Thu Mar 05, 2015 4:57 pm 
Offline
Member
Member

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 587
Hello.

My OS uses a very simple physical memory allocator: basically, it has a "placement frame", which is the lowest unsued frame, and is initialized at the 2MB mark. When a frame is freed, it pushes it onto a stack; and frames from the stacked are all popped on requests before the next placement frame is returned.

My problem is this: how do I know I don't accidentally allocate a frame which belongs to some memory-mapped device? Obviously I can't rely on drivers to tell me that, because to load them, I need to allocate memory in the first place.

I've heard that GRUB (or other multiboot-compliant bootloaders) can give me a 'memory map'. Is this what gives me all the necessary information to ensure I don't allocate a frame that belongs to a device? If not, how does your OS do it?

Thank you for help.


Top
 Profile  
 
 Post subject: Re: Physical memory management?
PostPosted: Thu Mar 05, 2015 7:08 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2004 11:00 pm
Posts: 874
Location: WA
mariuszp wrote:
Hello.

My OS uses a very simple physical memory allocator: basically, it has a "placement frame", which is the lowest unsued frame, and is initialized at the 2MB mark. When a frame is freed, it pushes it onto a stack; and frames from the stacked are all popped on requests before the next placement frame is returned.

My problem is this: how do I know I don't accidentally allocate a frame which belongs to some memory-mapped device?

because a memory mapped device doesn't respond to memory addresses belonging to "free ram"

Quote:
I've heard that GRUB (or other multiboot-compliant bootloaders) can give me a 'memory map'. Is this what gives me all the necessary information to ensure I don't allocate a frame that belongs to a device? If not, how does your OS do it?

yes, basically

note that there is no reason to assume that your memory is in order in a single place, for instance, you might have memory from 0-2MB then nothing from 2MB-6MB then more memory from 6MB-7MB then nothing from 7MB-26MB then more memory from 26MB-3GB then nothing form 3GB-7GB then more memory from 7GB-18GB then nothing from 18GB-129GB then more memory... etc. so its not just "devices" you need to worry about, you need to be sure there is actually memory (and not nothing) at that address

whatever bootloader you are using (whether it is GRUB, or a proper one you make yourself) will get memory information from the firmware (the method will change for different firmware, but your bootloader will know how to get it for whatever system you booted from) and turn that information into some kind of table or "map" of memory, which describes all memory, and pass that to your physical memory manager (note that the "memory map" GRUB gives you is simply copied from the firmware, and includes duplicate spaces (memory mapped multiple times), fragments (single large section of available memory might be listed as several smaller segments) and is not sorted in any kind of order... none of these things should be in the memory map your bootloader gives to the physical memory manager initialization

from this map, you need to create a list of available memory frames -- the simplest implementation on x86 you simply make a list of 4k-aligned blocks of 4k memory (discard anything not 4k aligned or smaller than 4k, and break larger blocks into 4k blocks) -- now you have a list of physical pages, if you did this correctly, all these blocks will be valid memory, none point to void, none point to non-memory devices, none point to memory that is used by the firmware, etc. -- this is your list of valid frames of physical memory, and can be allocated at any time here (note this is the simplest, not the best, implementation)

_________________
## ---- ----- ------ Intel Manuals
OSdev wiki


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 95 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group