Detect installed memory

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.
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

Detect installed memory

Post by muisei »

How can I get the amount of installed RAM?
"All parts should go together without forcing. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer." -- IBM maintenance manual, 1975
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
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.
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post 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...
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post 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...
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
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.
User avatar
inflater
Member
Member
Posts: 1309
Joined: Thu Sep 28, 2006 10:32 am
Location: Slovakia
Contact:

Post 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
Last edited by inflater on Sun Nov 12, 2006 2:29 am, edited 1 time in total.
My web site: http://inflater.wz.cz (Slovak)
Derrick operating system: http://derrick.xf.cz (Slovak and English :P)
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Post by earlz »

lol that works on about every emulator but it doesn't work on my real pc.. but it's from 2001 so..
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
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.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post 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?
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post 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 ?.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
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.
User avatar
smiddy
Member
Member
Posts: 127
Joined: Sun Oct 24, 2004 11:00 pm
Location: In my cube, like a good leming. ;-)

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