Page 1 of 2
Detect installed memory
Posted: Sat Nov 11, 2006 4:35 pm
by muisei
How can I get the amount of installed RAM?
Posted: Sat Nov 11, 2006 6:44 pm
by Brynet-Inc
http://www.osdev.org/osfaq2/index.php/H ... f%20RAM%3F
It would be good search the wiki's or the forum before asking a question that's likely been asked before..
A majority of people who are writing a 32bit kernel request memory information from grub, There are other methods like directly probing the memory but it's usually not advised.
If you want to detect memory for purely cosmetic reasons it's possibly not worth the trouble.
Posted: Sat Nov 11, 2006 7:16 pm
by earlz
why do they not put how grub does it... they say memory probing is so dangerous but not all bios's support getting memory size, so I really hate that page...
Posted: Sat Nov 11, 2006 8:36 pm
by Brendan
Hi,
hckr83 wrote:why do they not put how grub does it... they say memory probing is so dangerous but not all bios's support getting memory size, so I really hate that page...
IMHO the memory detection code used by GRUB isn't very good. The methods it uses are:
- Try BIOS Int 0x15, eax = 0xE820
If that didn't work, try BIOS Int 0x15, ax = 0xE801 and BIOS Int 0x12
If that didn't work, try BIOS Int 0x15, ah = 0x88 and BIOS Int 0x12
If that didn't work, complain.
It doesn't take into account any bugs that are known to effect some BIOSs (see Ralph Brown's Interrupt List), doesn't remove null entries from the map returned by "eax = 0xE820" (or sort entries), and in general, doesn't really try too hard to make sure that correct results are returned.
For GRUB, the correct amount of memory can be specified by the user in the boot script, so getting it wrong or failing to detect memory isn't considered a huge problem. This is a common approach I've never quite liked - "If things get tricky, just give up and make the user sort it out."....
Cheers,
Brendan
Posted: Sat Nov 11, 2006 10:06 pm
by Tyler
Well theoretically... if some real OS developer didn't want to rely on the Damn BIOS to do every little detail... how does the BIOS go about finding all this information about the size and who owns what? I've been trying to code an entire device interface all day... and the BIOS has been driving me mad.. so excuse my tone toward it... but i hate it... if i had been around in the 70's things would be simple today...
Posted: Sat Nov 11, 2006 10:10 pm
by earlz
I'm assuming that bios gets it from the chipset, so if you did that in your os you'd have to support every chipset which would be impossible...
Posted: Sat Nov 11, 2006 11:06 pm
by Brendan
Hi,
hckr83 wrote:I'm assuming that bios gets it from the chipset, so if you did that in your os you'd have to support every chipset which would be impossible...
This is correct...
For most BIOS's, they can't use any RAM until they detect the type of RAM installed, then detect the size of each memory module, then configure the chipset to use the detected RAM. All of this depends on chipset specific methods, and is usually documented in the datasheets for the memory controller (northbridge).
It is entirely impossible for an OS to use this method because the OS's code is in RAM, which must be severely messed with during the detection phase. For an example, imagine what would happen if every second byte of RAM disappeared.
Cheers,
Brendan
Posted: Sun Nov 12, 2006 2:21 am
by inflater
I assume that you work in Protected Mode.
When I can't use BIOS, a last resort - I am using CMOS, as long that "OUT" and "IN" opcodes are in Protected Mode ENABLED.
I am using this code (Real Mode, you must likely convert it)
Code: Select all
mov al,31h
out 70h,al
jmp @@delay ;little delay for slower processors
@@delay:
in al,71h
mov ah,al
mov al,30h
out 70h,al
jmp @@delay2
@@delay2:
in al,71h
; --== now in AX is XMS installed above 1 MB in Kilobytes ==--
I found a little bug in this function, it returns 15256 kB XMS and i have better memory (not 16 MB); maybe you will get better luck.
inflater
Posted: Sun Nov 12, 2006 2:26 am
by earlz
lol that works on about every emulator but it doesn't work on my real pc.. but it's from 2001 so..
Posted: Sun Nov 12, 2006 2:54 am
by Brendan
Hi,
Just thought I'd post a link to some of
the best memory detection I've ever seen.
IMHO if your OS can't use BIOS functions where it must, then fixing your design flaws is probably more important than memory detection. In a perfect world the BIOS wouldn't be such a mess, but life is like that sometimes...
Cheers,
Brendan
Posted: Sun Nov 12, 2006 8:30 am
by Dex
How does this work my cousin, built the latest supper doper PC, from the best spec he could find, when he come to installing a OS, it would not install XP or linux ( some read error, about can not find this and that even though they where there).
Any way i try Dex4u and it run fine detected right amount of ram 2048 etc, run all test programs.
He try everything in the end it was the wrong ram, even though this is the type that this board needed.
But the thing is, It run fine with my OS, what it could not do, probably was run at full tilt.
What i am saying, is i could of used the ram in its basic form, this probably goes for any RAM.
Posted: Sun Nov 12, 2006 1:41 pm
by Candy
Dex wrote:How does this work my cousin, built the latest supper doper PC, from the best spec he could find, when he come to installing a OS, it would not install XP or linux ( some read error, about can not find this and that even though they where there).
Any way i try Dex4u and it run fine detected right amount of ram 2048 etc, run all test programs.
He try everything in the end it was the wrong ram, even though this is the type that this board needed.
But the thing is, It run fine with my OS, what it could not do, probably was run at full tilt.
What i am saying, is i could of used the ram in its basic form, this probably goes for any RAM.
Did you get out of the caches?
Posted: Sun Nov 12, 2006 4:23 pm
by Dex
Not 100 per cent sure, but i open lots of differant things in a text editor big txt files, also displayed bmp images etc.
But does it not load from it from main ram anyway, before caches it ?.
Posted: Sun Nov 12, 2006 7:43 pm
by Brendan
Hi,
Dex wrote:Not 100 per cent sure, but i open lots of differant things in a text editor big txt files, also displayed bmp images etc.
But does it not load from it from main ram anyway, before caches it ?.
That depends how it's loaded. If you use DMA or bus mastering then it goes direct to RAM and then gets read into CPU caches from RAM.
If you use programmed I/O it goes the other way - from the device through I/O ports to CPU registers to cache to RAM. In this case, for a "top of the line" modern CPU you could have 3 MB or 4 MB caches containing the most recently used code and data.
Cheers,
Brendan
Posted: Sun Nov 12, 2006 7:46 pm
by smiddy
Just my tar pence:
I personally use every method I have available to detect RAM. I start with CMOS (limited yes, but none the less available), I then go through BIOS, using all methods, E820h being the last BIOS call (and for a
real PC works for most newer one since 2003). I then use PnP calls to determine RAM, which is very close to the E820 with a MAP, I also use SMBIOS to determine what is says is there from a hardware perspective, and finally I do a memory probe being careful of certain areas where ROM and BIOS are located, I also use PCI and scanning to determine the ROMS.
This method isn't all encompassing, as you might think it is. I've yet to determine RAM using ACPI tables (if capable), and I haven't even considered GRUB, I've never used it...<shrug>
Oh, BTW, I compare all methods above to get a clear idea via my own algorithm to determine
actual RAM. Then I use my own mapping. You can download my OS @
Http://smiddyOS.asmhackers.net to veiw the avialble memory after it's installed and watch the details as they're produced in order to get an idea of what is happening.