Detecting RAM installed

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.
codemastersnake

Detecting RAM installed

Post by codemastersnake »

Code: Select all

 outport(0x70,0x31); 
 highmem = inportb(0x71);
 highmem =highmem << 8;
 writeln("\nHIGHMEM: ");
 writeint(highmem/1024);
 writeln(" MB");
Above is the code to detect the amount of ram installed on a m/c..... But it only detects RAM <=64MB... even if the mem is above 64 mb it shows only 64mb...

Is there any way by which I could detect memory more than 64MB.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Detecting RAM installed

Post by Solar »

Please check the FAQ before posting questions...

How do I determine the amount of RAM?
Every good solution is obvious once you've found it.
NotTheCHEAT

Re:Detecting RAM installed

Post by NotTheCHEAT »

What if you use a protected mode bootloader and it isn't GRUB? CMOS is unsafe, probing is unsafe... how are we supposed to find the memory size? How does GRUB find it?
codemastersnake

Re:Detecting RAM installed

Post by codemastersnake »

Good Question notthecheat!
CMOS is unsafe.... but if programmed carefully it's can be very helpful....

I would also like to know how does grub detects memory...

also there is not good information in the wiki....
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Detecting RAM installed

Post by bubach »

Chris Giese has some very good source on how to get the amount of RAM from various BIOS calls at his homepage: http://my.execpc.com/~geezer/osd/boot/biosmem.asm

/ Christoffer
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
NotTheCHEAT

Re:Detecting RAM installed

Post by NotTheCHEAT »

Yeah, but you can't use BIOS in protected mode... I'm using the protected mode bootloader you gave me, and pretty tight on space (I don't know if I can fit memory detection code in there without making some major modifications...

CMOS is unsafe, especially since the maximum it will return is 99 MB and most people today have more than that. And probing is unsafe because you could mess up any memory-mapped devices. Hmmm... maybe if I use PCI config space to detect which areas belong to memory-mapped devices, so I avoid probing those areas? Also, only probe above what CMOS says we have, and only if that is 99 MB.

1.Read CMOS to see how much memory it says we have.
cm = Installed Memory as returned by CMOS
2.Check PCI Config space to see what areas are for memory- mapped devices.
dt = list of Don't Touch areas
3.If cm < 99 MB, assume amount of memory installed is cm
and exit.
4.If cm = 99 MB, begin probing memory, starting at 99 MB and
avoiding areas listed in dt:
-Write 10h to current memory cell.
-Read current memory cell. If not 10h, end of memory.
-Write 00h to current memory cell.
5.When at end of memory, record location as size of memory.

Of course, it isn't fool-proof. I might add some sort of manual override, so the user can type in the true amount of memory that they have (in case the result returned by this is not the right amount).

Maybe I should just go back to using a real mode bootloader, so I can check this type of thing at startup. Or maybe I should give in to everyones saying that I should use GRUB "or else my teeth will fall out and all my relatives will die".
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Detecting RAM installed

Post by Brendan »

Hi,
NotTheCHEAT wrote:Of course, it isn't fool-proof. I might add some sort of manual override, so the user can type in the true amount of memory that they have (in case the result returned by this is not the right amount).
That isn't foolproof either. Just for fun, go into your BIOS settings a see if there's a "OS/2 Compatible Mode" (it might be called something else, and is more likely on computers from Pentium to Pentium III) that can be enabled/disabled. If enabled (IIRC), this will make a "hole" in physical memory from 15 MB to 16 MB, so a computer with 32 MB installed will have RAM up to 0x00F00000, then nothing from 0x00F00000 to 0x01000000 and more RAM from 0x01000000 to 0x02100000.

Another problem is "multi-socket" AMD computers (NUMA), where different RAM banks may be at different addresses - for e.g. 512 MB with 256 MB from 0x00000000 to 0x10000000 and another 256 MB from 0x40000000 to 0x50000000.

In either case the user would enter the correct amount of RAM but you won't know where the RAM is or isn't. Of course the user can also enter the wrong amount of RAM :).
NotTheCHEAT wrote:Maybe I should just go back to using a real mode bootloader, so I can check this type of thing at startup. Or maybe I should give in to everyones saying that I should use GRUB "or else my teeth will fall out and all my relatives will die".
I don't say "just use GRUB", and never will. My recommendation is: use GRUB or detect memory with the BIOS functions (see the link to Chris Giese's code posted earlier).

@Codemaster Snake: GRUB uses the BIOS functions to detect memory, just like Chris Giese's code (and Linux, Windows, my OS, etc).


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
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Detecting RAM installed

Post by Pype.Clicker »

and if you really want to use the specific bootloader XYZ without touching it, you can still do the following:
- write a small boot sector
- make it check how much memory the system has through BIOS calls
- write the result somewhere in memory where bootloader and BIOS won't mind (e.g. in some unused interrupt vector such as INT21h :) )
- load the bootsector for the "real loader" at 7c00
- jump to it
NotTheCHEAT

Re:Detecting RAM installed

Post by NotTheCHEAT »

Yeah, I'm thinking of using a second stage bootloader, unless I can somehow hack memory detection via BIOS into "bootloader XYZ", which is already very close to being too large. The alternative is to use a normal, real-mode bootloader, and detect memory in the kernel (or in the bootloader, since I'll have a lot of extra space.) I'm hoping to be able to fit a memory size check into the bootloader I'm using, but I'd have to remove some other stuff.

According to the FAQ, there are several different calls to check how much memory there is. If I only have space for one, which one should that be? Which is the most reliable, and most common on most systems?
codemastersnake

Re:Detecting RAM installed

Post by codemastersnake »

I have written a chunk of code for RM users:

Code: Select all

unsigned int ext_MEM(void)
{
 unsigned char lowmem, highmem;
 unsigned int total;

 outportb(0x70, 0x30);
 lowmem = inportb(0x71);
 outportb(0x70, 0x31);
 highmem = inportb(0x71);

 asm mov ah, highmem;
 asm mov al, lowmem;
 asm mov total, ax;

 return total + 1024;
}

unsigned int base_MEM(void)
{
 unsigned char lowmem, highmem;
 unsigned int total;

 outportb(0x70, 0x15);
 lowmem = inportb(0x71);
 outportb(0x70, 0x16);
 highmem = inportb(0x71);

 asm mov ah, highmem;
 asm mov al, lowmem;
 asm mov total, ax;

 return total;
}
can be helpful....

And I think that CMOS can return more than 99MB if one use 32bit registers.... plz Correct me if i am wrong...
DennisCGc

Re:Detecting RAM installed

Post by DennisCGc »

Codemaster Snake wrote:
And I think that CMOS can return more than 99MB if one use 32bit registers.... plz Correct me if i am wrong...
No, you're still limited to the CMOS' limits, which is 2 bytes for the total amount of memory.

To detect the amount of memory you should use the BIOS instead in realmode. (ax=e820h, ah=88h, etc.)

DennisCGc.
codemastersnake

Re:Detecting RAM installed

Post by codemastersnake »

thanks for suggestions...
But if we are limited to CMOS limitations then how does one detect physical ram in PM

Reason I am not using BIOS is because I don't like to...... Rather I like to use direct approach with ports and pointers..
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Detecting RAM installed

Post by Brendan »

Hi,
Codemaster Snake wrote:But if we are limited to CMOS limitations then how does one detect physical ram in PM
The CMOS value itself is only a legacy thing, and IMHO modern OSs shouldn't rely on it - there's no "CMOS location standard" that specifies exactly what each location in the CMOS must contain, if you see what I mean. The only thing that makes it work most of the time is that BIOS manufacturers like to maintain backwards compatibility where possible.

For working out how much memory is installed (and where) without using the BIOS, read through this:

http://www.x86.org/articles/memsize/memsizing.htm

Take special notice of the last paragraph - you could try to write different code for each motherboard/chipset and you still won't get it to work unless your code is on a ROM chip.
Codemaster Snake wrote:Reason I am not using BIOS is because I don't like to...... Rather I like to use direct approach with ports and pointers..
Are you sure? It seems possible that you may not want to use the BIOS because you didn't do enough research when you wrote your bootloader, and now it's too hard to add real mode memory detection code to the boot sequence (it won't fit in 512 bytes perhaps). I can't think of any other reason why someone might try so hard to avoid the only sane/reliable method of detecting memory....


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.
smiddy

Re:Detecting RAM installed

Post by smiddy »

You can probe memory from protected mode. However, most people would recommend against it. You can also use PnP BIOS in protected mode to get the device information which has the amount of memory for each device as well as on board RAM. However, using PnP BIOS in protected mode or real mode is very tricky and will require you to do a lot of research on how to implement it. It took me about three months of research and application at a part time pace.

I suspect there is a couple of other ways to, SMBIOS in protected mode and perhaps ACPI. Both of which would also require some research before implentation.
NotTheCHEAT

Re:Detecting RAM installed

Post by NotTheCHEAT »

The CMOS value itself is only a legacy thing, and IMHO modern OSs shouldn't rely on it - there's no "CMOS location standard" that specifies exactly what each location in the CMOS must contain, if you see what I mean.[/quote

That's wrong, the CMOS contains the system time. How else are we supposed to know what time it is if we don't use the CMOS?

But I do agree. CMOS is legacy, and cannot be relied upon for many things.

I added code to my bootloader that uses INT 15h AX=E801h to detect memory size. This may not work in older computers, unfortunately, but space was limited and I wanted to be able to get the memory size. I will add code to my kernel so that if it didn't work, the kernel will probe past the value stored in CMOS. This may not be ideal, but it's what works for me, and for most modern computers it will be able to use the BIOS.

@smiddy: Can you give some code for using the PnP BIOS (to do anything)? It would be helpful... or at least if you could give some links for more information.
Post Reply