Page 1 of 1
Determining maximum memory address
Posted: Mon Sep 07, 2009 10:44 am
by zman97211
I've been reading up on paging and memory management, and decided to implement a paging scheme using a bitmap to keep tabs on what pages are currently allocated/free. I've created some simple functions to implement the bitset, but I would like to keep only enough bits to manage all available physical memory.
Here's a screenshot of my kernel running in bochs (configured for 32 MB of RAM), showing the reported values passed from Grub:
What is the best way to determine the "maximum address" in physical memory? Or perhaps the maximum page number, defining page 0 as bytes 0-4095, page 1 as 4096-8191, etc.
My first thought is that some older computers don't seem to support the method Grub uses to build it's memory map (I have a 486 on which Grub doesn't pass one to my kernel). So, in this case, can I take the "mmap_upper", add 1024 to it, multiply by 1024 to get the highest address? When I do this, I get a value that is 64kb short (it stops where the ACPI reclaimable memory is at).
If the memory map is present, I can then simply look for the highest block of memory referenced in it and use it's base+length to get the highest amount of RAM.
My concern is calculating a bit high and attempting to use memory that isn't there, or calculating low and letting a bit go to waste.
Since this is part of the initialization of my physical memory manager, I'm willing to let a section of RAM go to waste that is smaller than a page, although I'm making the assumption that RAM ends on a page boundary.
Re: Determining maximum memory address
Posted: Mon Sep 07, 2009 10:50 am
by tantrikwizard
let me search the wiki for you.
http://wiki.osdev.org/Detecting_Memory_(x86) wow, that took under 15 seconds....
Re: Determining maximum memory address
Posted: Mon Sep 07, 2009 10:59 am
by zman97211
I've read that page. I just re-read it.
It doesn't answer one of my questions from above...
Me wrote:My first thought is that some older computers don't seem to support the method Grub uses to build it's memory map (I have a 486 on which Grub doesn't pass one to my kernel). So, in this case, can I take the "mmap_upper", add 1024 to it, multiply by 1024 to get the highest address? When I do this, I get a value that is 64kb short (it stops where the ACPI reclaimable memory is at).
Can I safely add 64k to the value from the mmap_lower/mmap_upper value returned by Grub? If not, will I be wasting 64k of memory (8 pages)? Just because it's like that in bochs, are these results typical?
You missed a paren in your link btw...
Re: Determining maximum memory address
Posted: Mon Sep 07, 2009 11:31 am
by gravaera
Nice witty comeback, but tantrikwizard was generally spot on.
The advisable thing to do is to convert the mem map into 4kb units, and build a bitmap in two passes:
Pass one should be used to tell how many bits are quired for the bitmap. Therefore how long the array should be.
Pass two should be used to now, run through the Mbdata again, and map all the reserved areas as used (and 'pinned' or your own implementation defined method of telling your Mem Mgr that this mem should not be given to apps at any time), and all the reclaimable areas as used, but more in the sense of able to be restored to the bitmap for use by applications.
By doing the 2 pass method, you, in your first pass can store the number of pages in memory in a global variable (or class private variable in an OO case), and that would be your 'guard' against going too high in the bitmap, or trying to allocate one bit that isn't there, or anything like that.
By mapping in all pages that are reserved, you avoid your allocator being able to give away reserved pages to applications.
What I do is start off by initializing the memory manager in low mem. I just start taking out pages as necessary (after of course mapping the first page as used, and the Vidmem buffer), and I place the relevant strucutres (I use C++, so for me that's objects) there. Using the mem I have taken for granted in lower mem, I establish the Dynamic Mem Allocator for the kernel and userspace. So I dynamically allocate stuff in low mem until I have a real Dynamic allocator, all the while keeping track of which pages I use up while setting up Dynamic Mem Allocation.
To generate the bitmap for High mem (As you can actually just start off at boot with a bitmap of exactly 1MB worth of pages for Low mem confidently), I pass through, as indicated above, and generate the bitmap array using the now established Dynamic Mem Allocator. This way I waste nothing except exactly 8 pages in low mem while setting up Dynamic Mem Allocation.
Re: Determining maximum memory address
Posted: Mon Sep 07, 2009 7:46 pm
by Cognition
I believe
this will help you.
Edit: If you mean specifically through GRUB well the wiki breaks that down pretty neatly for you as well. You should be able to get the highest possible address from just adding 0x100000 to mem_upper*1024. As it notes BIOS calls can be quirky and it's generally poor practice to hard code a value simply because it makes more sense on one system.
Re: Determining maximum memory address
Posted: Mon Sep 07, 2009 8:16 pm
by Brendan
Hi,
zman97211 wrote:My first thought is that some older computers don't seem to support the method Grub uses to build it's memory map (I have a 486 on which Grub doesn't pass one to my kernel). So, in this case, can I take the "mmap_upper", add 1024 to it, multiply by 1024 to get the highest address? When I do this, I get a value that is 64kb short (it stops where the ACPI reclaimable memory is at).
The "int 0x15, eax=0xE820" BIOS function (that GRUB uses to get the memory map) is part of the ACPI specification. If GRUB can't tell you a memory map, then the computer is too old to support this BIOS function and too old to support ACPI, and there won't be any ACPI reclaimable area. In this case, adding anything to "mmap_upper" would be a mistake.
Also note that (for older computers that don't support "int 0x15, eax=0xE820") the "mmap_upper" may be incorrect and/or inadequate. Incorrect because GRUB isn't very good and doesn't take into account various BIOS bugs. There's several reasons why it could be inadequate.
To begin with, RAM above 0x00100000 may not be contiguous (e.g. there may be a hole for ISA devices from 0x00F00000 to 0x00FFFFFF), and in this case the BIOS (and GRUB) will only return the RAM that starts at 0x00100000 (e.g. it might say that there's 14 MiB of RAM at 0x00100000 but can't tell you that there's another 16 MiB of RAM at 0x01000000). Also some of the BIOS functions GRUB relies on are limited. For example, "int 0x15, ah=0x88" returns the number of contiguous 1 KiB blocks of RAM starting at 0x00100000 as a 16-bit integer, which is limited to 65536 KiB of RAM starting at 0x00100000. If there's 32 MiB of RAM (or 31 MiB starting at 0x00100000) some BIOSs will only tell you about 65536 KiB (the maximum it can tell you about) and some BIOSs will only tell you about 64512 KiB.
If GRUB's dodgy memory detection creates dodgy results then the user can override it with the "uppermem" command. This is also unable to handle systems with more than one area of RAM above 0x00100000 (and there's no way for the user to supply a correct/complete memory map). It also means that when GRUB's memory detection does return correct results, the end user can stuff things up by overriding it with their own wrong results...
For a hobby OS, GRUB is probably "good enough". If you care about reliability and want to make sure the OS works correctly on the largest number of computers possible, then my advice is to do it yourself. For example, after GRUB has booted your code you can switch back to real mode, detect the memory properly, then switch back to protected mode.
Finally, if you're building a bitmap that has to cover all possible pages of RAM, and if you want to support "hot add" memory some time in the future, then you need to look for and parse the "SRAT" ACPI table before deciding how large your bitmap needs to be.
Cheers,
Brendan
Re: Determining maximum memory address
Posted: Tue Sep 08, 2009 1:23 pm
by zman97211
Thanks for all the constructive replies, I appreciate it.
A little off topic - but I notice this forum occasionally has replies like I had above, and then others post obviously lost on what they're doing, and get post after post of help...

Re: Determining maximum memory address
Posted: Tue Sep 08, 2009 8:49 pm
by tantrikwizard
zman97211 wrote:A little off topic -...
Right, you have a mixed crowd here. There is the mix of seasoned and professional engineers and then there is the group of illiterate teens who ask dumb questions like yours and waste a lot of people's time in the process. There's nothing new in any of the responses on this thread, just the same old regurgitated stuff that is printed all over the web that you would have found if you had just done some research.
Re: Determining maximum memory address
Posted: Tue Sep 08, 2009 9:37 pm
by Brendan
Hi,
zman97211 wrote:A little off topic - but I notice this forum occasionally has replies like I had above, and then others post obviously lost on what they're doing, and get post after post of help...

There's a mixed crowd here. There's seasoned and professional engineers, and then there's some illiterate teens who accuse others of not searching for answers before they've even bothered to understand the questions being asked. For an example, for your "
mmap_upper doesn't return all RAM" question nothing in the OSdev wiki clearly explains why GRUB or the BIOS functions don't include RAM used for the ACPI reclaimable area, and unless you're familiar with the history of 80x86 BIOS functions and have some experience with these functions you could spend a month trying to figure it out and still end up without an adequate answer.
Most of the more experienced people here know that the quickest way to handle beginner questions is to provide useful answers. This can stop a topic dead in it's tracks - "beginner's question, good answer, done". Unfortunately some people have nothing better to do than trolling and provoking others, and waste far more time not providing useful answers than they would if they actually tried to help people.
Cheers,
Brendan
Re: Determining maximum memory address
Posted: Tue Sep 08, 2009 11:28 pm
by kmcguire
I completely agree with Brendan, that the quickest way to handle beginner question is to provide useful answers.
Not to mention the sub-title to the forums:
"The place to Start for Operating System Developers"
Re: Determining maximum memory address
Posted: Wed Sep 09, 2009 1:22 am
by kop99
If you already enable a20 line, then you can implement your own memory test.
step 1: You can write a simple magic byte (ex:0xAA55) to 1M's memory address(0x100000).
step 2: Then read it back, and compare it to magic byte. If result is same, that memory
address is efficient.
step 3: Add 0x100000 to original memory address, and repeat step 2, until result isn't same.
step 4: Then that address is your memory's final address.
here is simple code.
Code: Select all
get_memory_size:
push ebp
mov ebp, esp
push ecx
push ebx
mov ecx, 0x0
mov ebx, dword [ss:ebp + 8] ; start address from parameter
and ebx, 0xFFF00000
continue:
add ebx, 0x100000 ; add 1M
inc ecx ; count capacity in MB
mov byte[ds:ebx], 0xAA ; write magic number
mov al, byte[ds:ebx] ; read it
cmp al, 0xAA ; compare it
je continue ; if equal, repeat
mov eax, ecx ; return capacity
pop ebx
pop ecx
pop ebp
retn
Re: Determining maximum memory address
Posted: Wed Sep 09, 2009 2:27 am
by Brendan
Hi,
kop99 wrote:If you already enable a20 line, then you can implement your own memory test.
step 1: You can write a simple magic byte (ex:0xAA55) to 1M's memory address(0x100000).
step 2: Then read it back, and compare it to magic byte. If result is same, that memory
address is efficient.
step 3: Add 0x100000 to original memory address, and repeat step 2, until result isn't same.
step 4: Then that address is your memory's final address.
here is simple code.
I seriously hope you're not using this code in anything that needs to work on more than just your own computer. See
here for plenty of good reasons why it's broken.
Cheers,
Brendan
Re: Determining maximum memory address
Posted: Wed Sep 09, 2009 6:29 am
by zman97211
tantrikwizard wrote:There is the mix of seasoned and professional engineers and then there is the group of illiterate teens who ask dumb questions like yours and waste a lot of people's time in the process.
Right, and there is a mixed crowd here. Those of professional computer science engineers and professional electrical engineers who, while having a sense of professionalism and expertise at other slightly-related things, might not have specific knowledge or experience when it comes to OS-specific topics. If you feel it's a waste of your time to answer a question, click on the next thread.
However, I would hope that we all are able to act professionally.