Page 1 of 1

Detecting Memory and Memory Bitmap

Posted: Thu Jun 21, 2012 5:25 am
by mark3094
Hi,

In my bootloader, I run INT15, 0x820 to get a map of memory, and INT15, 0x801 to get the total amount of memory.

For 0x801, DX returns 751, meaning that there are 751 64KB blocks of memory above the 16MB mark.
Some quick math shows that there is 62.9375MB of memory in the system.

I am using VMWare Workstation with 64MB assigned to the VM. I did read somewhere that virtual machines do not actually allocate the exact amount of memory that you configure, which would explain the discrepancy that I am seeing.

When it comes to my physical memory manager (just a bitmap at this stage), is it safe to assume that the system has 62.9375MB of memory, or is there some reason that I'm missing the rest? I know it's not a lot of memory in the real world, but I don't want to make some fundamental mistake that leads to faults in future.


Thank you for your help

Re: Detecting Memory and Memory Bitmap

Posted: Thu Jun 21, 2012 5:30 am
by gerryg400
Depending on the algorithm you use to find the bit that represents each page, you may want to have a bitmap that runs from the lowest available address to the highest address. If there are any holes in the memory then set the bits that represent the holes permanently to 1.

So the important thing isn't how much memory there is but which addresses have memory.

Re: Detecting Memory and Memory Bitmap

Posted: Thu Jun 21, 2012 5:36 am
by mark3094
Won't I need to know the size of memory so I know how big the bitmap is?
I'll need to know the size of the bitmap so I know when to stop searching it (ie for a free block of memory)

EDIT: I just re-read your post, and I think I understand. I don't need the size, because I can just stop searching when I reach the highest entry...
As I'm only working with physical memory for now, how will I know at what point to stop allocating memory? The highest address in the memory map is up at around 4GB, but I only have 64MB of physical memory

Re: Detecting Memory and Memory Bitmap

Posted: Thu Jun 21, 2012 5:45 am
by gerryg400
mark3094 wrote:Won't I need to know the size of memory so I know how big the bitmap is?
I'll need to know the size of the bitmap so I know when to stop searching it (ie for a free block of memory)

EDIT: I just re-read your post, and I think I understand. I don't need the size, because I can just stop searching when I reach the highest entry...
As I'm only working with physical memory for now, how will I know at what point to stop allocating memory? The highest address in the memory map is up at around 4GB, but I only have 64MB of physical memory
You need to have a formula/function/macro that converts between 'phys address' and 'bitmap position'. Think about how these functions will work. This addr=>bit function needs to know the highest and lowest address so that it can check values passed to it. The bit=>addr function needs to know the beginning and end of the bitmap for the same reason.

Re: Detecting Memory and Memory Bitmap

Posted: Thu Jun 21, 2012 5:50 am
by mark3094
OK, I've got it now. Thanks.

I'm going to start by initialising everything up to 4GB as used (only a 32-bit system) and only mark certain regions as available based on the memory map.


Thanks again for your help,

Re: Detecting Memory and Memory Bitmap

Posted: Thu Jun 21, 2012 6:03 am
by gerryg400
Are you sure there is memory up near the 4GB mark ? Maybe APICs, frame buffer etc. ?

If there is memory you have a serious hole in your memory map. In this case consider have a more complex pair of conversion functions that don't require such a big bitmap.

Re: Detecting Memory and Memory Bitmap

Posted: Sat Jun 23, 2012 1:34 am
by mark3094
I have attached the output of the memory map. Does it look OK?

Re: Detecting Memory and Memory Bitmap

Posted: Sun Jun 24, 2012 1:42 am
by gerryg400
Yep, it's probably okay. Notice that the memory type of the memory near 4GB is 2 ? That's reserved and cannot be used. The highest usable (type 1) memory is around the 64M mark.

Re: Detecting Memory and Memory Bitmap

Posted: Sun Jun 24, 2012 7:24 pm
by mark3094
Thank you for all your help.

I now have a bitmap setup to cover up to 4GB of space. I have the info from the memory map and the bitmap itself allocated in the bitmap.

The next thing I'm thinking of is how to allocate the memory area for the Kernel.
The start is easy, as it's currently loaded to 0x100000.
The end is the tricky part. What's the best way to get the length of the Kernel? Count the bytes in the boot loader as it's loading?

Also (I hope this isn't a stupid question), what's the best way to determine how much memory the kernel is using for variables? I presume I have to protect against variables being overwritten. What's the best way to do that?

I will keep researching for now

Thanks again

Re: Detecting Memory and Memory Bitmap

Posted: Sun Jun 24, 2012 8:14 pm
by gerryg400
mark3094 wrote:I now have a bitmap setup to cover up to 4GB of space.
But you only have 64MB of RAM. Did you read my last post ?

Re: Detecting Memory and Memory Bitmap

Posted: Sun Jun 24, 2012 8:27 pm
by mark3094
gerryg400 wrote:But you only have 64MB of RAM. Did you read my last post ?
Yeah, I did. Just for simplicity while I'm learning how this works, I have created a bitmap that can store a map of up to 4GB. Anything over 64MB is marked as 'allocated' so it can't be used. When I have my basic memory manager working correctly, I will fine tune it further.

Re: Detecting Memory and Memory Bitmap

Posted: Sun Jun 24, 2012 8:31 pm
by gerryg400
Cool, that's no problem. I was just worried I'd confused the issue for you.

Re: Detecting Memory and Memory Bitmap

Posted: Sun Jun 24, 2012 8:38 pm
by mark3094
No Worries. You've actually helped me understand it far more than I did before.

I've been reading about this, and it looks like I need to allocate space in the bitmap for the Kernel code, plus the BSS, heap and stack?
Does that sound about right?