Page 1 of 1

my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 1:41 am
by miaowei
hello, everyone.
i find a strange thing, i write a function in my kernel to scan RAM from 32M ~ 896M, four bytes one step. i want see how many dirty machine words( not zero) exist after power on.

Code: Select all

int scan_dirty_machine_words(unsigned start, unsigned end){
	int count = 0;
	for(int i = start; i < end; i += 4){
		unsigned x  = *(unsigned *)(i+PAGE_OFFSET);
		if( x ){
			oprintf("%u ", x);
			count++;
		}
	}
	return count;
}
oprintf("dirty words :%x\n", scan_dirty_machine_words(0x100000 * 32, 0x100000 * 896))
the result is 0xd7ad3e9 !!
approximately 800M's dirty area.
i used to think the RAM war cleared by CPU before BIOS.

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 1:44 am
by madanra
No, RAM is not cleaned on boot. There are no assumptions you can make about the contents of RAM when the computer is first turned on.

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 2:09 am
by alexfru
A more interesting experiment would be to run an image recognizer on that memory! All kinds of recognizers, actually. People have been finding Christ in bread toasts and tree stumps. Who knows what you may find in there! Perhaps, you may find a proof of an important mathematical conjecture or a beautiful painting or a touching symphony!

Seriously, why would you care? Depending on how long the machine was off and on what the BIOS did to the memory, you may find in there data from a previously run OS. Or you may probably use the content to initialize your random generator (provided, there's enough entropy, I guess). Other than that, the data is probably as good as junk (I'm not sure if it can reveal anything useful about its own organization and operation).

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 2:54 am
by miaowei
alexfru wrote:Seriously, why would you care?
In the early period of my wring the kernel, i assume the machine ram is clean beyond 1M. For a long time, my kernel runs will on virtual machine, but cracked when i test it on my home PC these days.
I found out the reason, so ...

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 3:07 am
by alexfru
I've heard something about memory scrubbing by the BIOS/chipset on some machines, but that's not all machines and I'm not sure there's any guarantee as to the memory state (scrubbing with random numbers or with zeroes or with some pattern) when the BIOS hands off control to the OS. In any case, you shouldn't expect anything. Unless, of course, you're in full control of the hardware.

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 10:32 am
by Boris
Make no assumptions except what is specified.
By instance , if your kernel is ELF, the memory where your .bss segment is, is cleared.
Always assume the worst, like the multiboot header being right after your kernel in memory.

You should clear whatever you take from your physical allocator for security AND safety.

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 11:40 am
by onlyonemac
alexfru wrote:A more interesting experiment would be to run an image recognizer on that memory! All kinds of recognizers, actually. People have been finding Christ in bread toasts and tree stumps. Who knows what you may find in there! Perhaps, you may find a proof of an important mathematical conjecture or a beautiful painting or a touching symphony!
Or more likely, fragments of partially-intact data from previous use. Whether or not you find a beautiful painting in there or not depends on whether or not the previous user of the system looked at any beautiful paintings (and likewise with touching symphonies).

In reality, as long as the charge in the DRAM chips has dissipated (which it almost certainly has), you'll probably find just white noise. Statistically, the distribution of bits in memory that has been "initialised" by electromagnetic noise while the system is turned off is even, resulting in white noise (or whatever the graphical equivalent of that is, depending on how you interpret the data).

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 12:16 pm
by Boris
Preserve those dirty bytes and use them as a funny hardware random numbers generator.

Re: my machine RAM is full of dirty bits after power on

Posted: Fri Jun 17, 2016 3:41 pm
by Combuster
Boris wrote:Preserve those dirty bytes and use them as a funny hardware random numbers generator.
Please don't. Just, don't.

Re: my machine RAM is full of dirty bits after power on

Posted: Wed Jun 29, 2016 4:19 am
by halofreak1990
miaowei wrote:
alexfru wrote:Seriously, why would you care?
In the early period of my wring the kernel, i assume the machine ram is clean beyond 1M. For a long time, my kernel runs will on virtual machine, but cracked when i test it on my home PC these days.
I found out the reason, so ...
This is why you should always initialize your variables and clear any buffers you allocate before use.

Re: my machine RAM is full of dirty bits after power on

Posted: Wed Jun 29, 2016 8:47 am
by SpyderTL
Also, if you are ever planning on dynamically allocating memory (malloc()), or if you are ever planning on enabling hardware paging, you are going to have to deal with "dirty" memory, one way or another.

Either the OS should clear any memory that it allocates, or all of your applications should expect any (non-code) memory to be filled with garbage.

Speaking of garbage, I'm not sure if I mentioned it on here before, but while I was testing out my memory stream reader, dumping the individual bytes to the screen, I found some text in the BIOS area of my physical laptop that said something along the lines of "This code is for non-production use only."

Hehe. Whoops. :P