my machine RAM is full of dirty bits after power on

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.
Post Reply
miaowei
Member
Member
Posts: 84
Joined: Wed Dec 18, 2013 9:10 am

my machine RAM is full of dirty bits after power on

Post 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.
madanra
Member
Member
Posts: 149
Joined: Mon Sep 07, 2009 12:01 pm

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

Post 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.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

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

Post 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).
miaowei
Member
Member
Posts: 84
Joined: Wed Dec 18, 2013 9:10 am

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

Post 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 ...
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

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

Post 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.
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

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

Post 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.
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

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

Post 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).
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

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

Post by Boris »

Preserve those dirty bytes and use them as a funny hardware random numbers generator.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post by Combuster »

Boris wrote:Preserve those dirty bytes and use them as a funny hardware random numbers generator.
Please don't. Just, don't.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
halofreak1990
Member
Member
Posts: 41
Joined: Thu Aug 09, 2012 5:10 am

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

Post 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.
<PixelToast> but i cant mouse

Porting is good if you want to port, not if you want maximum quality. -- sortie
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

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

Post 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
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Post Reply