Page 1 of 1

fast method to detect memory capicity?

Posted: Tue Aug 24, 2010 6:33 am
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

Re: fast method to detect memory capicity?

Posted: Tue Aug 24, 2010 7:04 am
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