fast method to detect memory capicity?

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.
Locked
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

fast method to detect memory capicity?

Post by lemonyii »

Hi!
i come across a "fast" method to detect memory today(BeansOS). But i thought it has some problem,though it runs properly on all qemu,bochs,and vmware.

Code: Select all

void ram_detect(void) {
	//write and verify from 8M ,every 4M has a read point.
	volatile unsigned char* checkpoint = (char*)CHECKMEMBASE;	//8M
	volatile unsigned char tmp;
	ram_size = 0;
	while (1) {
		//read
		tmp = *checkpoint;
		tmp = (~tmp);
		//write
		*checkpoint = tmp;
		//verify
		if (tmp != *checkpoint) { //check end.
			ram_size = (unsigned long)checkpoint;
			break;
		}
		checkpoint += CHECKMEMSTEP;	//+4M 
		if ((unsigned long)checkpoint >= MAXRAMSIZE) {
  		//in this case, 2G-4M has check passed,so 0-(2G-1byte) can be use
			ram_size = MAXRAMSIZE;
			break;
		}
	}
	max_ramaddr = ram_size-1;
	printk('s', "ram detect finish ram_size is:");
	printk('n', ram_size/1024/1024);
	printk('s', "M\n");
}
what if it writes to some memory-mapped IO, like local APIC? I think it is too dangerous, but it is so simple that I want to post here to see if some says it is all right.
And if not, is there any simple way to detect memory capicity?(it will be great if the reserved area is shown)
I've been always using int15h with ARDS.
thanks
Enjoy my life!------A fish with a tattooed retina
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: fast method to detect memory capicity?

Post by Candy »

Try the wiki for why this won't be reliable:

http://wiki.osdev.org/Detecting_Memory_ ... to_probing

and stuff that is (and is quicker):

http://wiki.osdev.org/Detecting_Memory_%28x86%29
Locked