Fault on reboot (solved)

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
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Fault on reboot (solved)

Post by salil_bhagurkar »

Hi... This is a basic problem. But I can't seem to find the solution for it. When using QEMU, my first boot is successful, but when i reboot without restarting QEMU (using outb(0xfe, 0x64)), i get a fault. This is probably due to the stale data in the RAM which creates undesired conditions. But I can't really find out what I need to do. Is there any section clearing(bss) I need to do on boot?
Last edited by salil_bhagurkar on Sun Apr 12, 2009 9:47 am, edited 1 time in total.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Fault on reboot

Post by Dex »

Maybe you could try the new method or place a delay
static inline void mach_reboot(void)
{
int i;
- for (i = 0; i < 100; i++) {
- kb_wait();
- udelay(50);
- outb(0x60, 0x64); /* write Controller Command Byte */
- udelay(50);
- kb_wait();
- udelay(50);
- outb(0x14, 0x60); /* set "System flag" */
- udelay(50);
- kb_wait();
- udelay(50);
- outb(0xfe, 0x64); /* pulse reset low */
- udelay(50);
- }
+ u8 cmd ;
+
+
+ /* old method, works on most machines */
+ for (i = 0; i < 100; i++) {
+ kb_wait();
+ udelay(50);
+ outb(0xfe, 0x64); /* pulse reset low */
+ udelay(50);
+ }
+
+ /* new method, sets the "System flag" which when set,
+ indicates successful completion of the keyboard controller
+ self-test (Basic Assurance Test, BAT). This is needed
+ for some machines with no keyboard plugged in.
+ This read-modify-write sequence sets only the system flag. */
+ for (i = 0; i < 100; i++) {
+ outb(0x20, 0x64); /* read Controller Command Byte */
+ udelay(50);
+ kb_wait();
+ udelay(50);
+ cmd = inb(0x60);
+ udelay(50);
+ kb_wait();
+ udelay(50);
+ outb(0x60, 0x64); /* write Controller Command Byte */
+ udelay(50);
+ kb_wait();
+ udelay(50);
+ outb(cmd | 0x04, 0x60); /* set "System flag" */
+ udelay(50);
+ kb_wait();
+ udelay(50);
+ outb(0xfe, 0x64); /* pulse reset low */
+ udelay(50);
+ }
}
I go to realmode and use this method without any problems in emulators

Code: Select all


     DB 0EAh					      ; Machine language to jump to
						      ;    address FFFF:0000 (reboot)
       DW 0000h
       DW 0FFFFh				      ; No return required
						      ;    we're rebooting!
User avatar
Stevo14
Member
Member
Posts: 179
Joined: Fri Mar 07, 2008 3:40 am
Location: Arad, Romania

Re: Fault on reboot

Post by Stevo14 »

A quick way to test if it is indeed a problem with uninitialized memory would be to write a function that actually zeros all of the memory from the end of your kernel to the end of memory. Run the function at the start of the kernel. If the problem disappears, then it should suffice to zero all dynamic memory allocations (eg. kmalloc();).

Obviously, remove the function after testing and only test with small amounts of memory (unless you are willing to wait :) ).
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fault on reboot

Post by salil_bhagurkar »

Stevo14 wrote:A quick way to test if it is indeed a problem with uninitialized memory would be to write a function that actually zeros all of the memory from the end of your kernel to the end of memory. Run the function at the start of the kernel. If the problem disappears, then it should suffice to zero all dynamic memory allocations (eg. kmalloc();).

Obviously, remove the function after testing and only test with small amounts of memory (unless you are willing to wait :) ).
Oh! Why didn't I think of that?

@Dex: Reboot works fine. The fault comes on next boot...
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fault on reboot (solved)

Post by salil_bhagurkar »

@Stevo14: I did that, and it worked. It was due to uninitialized data. Now i just need to scan for which structure or buffer has got uninitialized data... Thanks!
User avatar
Stevo14
Member
Member
Posts: 179
Joined: Fri Mar 07, 2008 3:40 am
Location: Arad, Romania

Re: Fault on reboot (solved)

Post by Stevo14 »

No problem. I'm glad it helped! :)

It find it easiest to automatically zero all kmalloc()'d areas inside the kmalloc() function. You may also want to write a wrapper function with a different name that does the zeroing (somewhat like malloc() and calloc() ).
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Fault on reboot (solved)

Post by salil_bhagurkar »

Zeroing allocations is not required mostly. Structures are mostly specifically initialized (not necessarily zero). Strings have a null terminating character, which i try to maintain instead of zeroing the string completely. This results in overall performance boost. Especially when you might have applications like editors which might allocate thousands of line buffers and load them with data which naturally doesn't require zeroing...
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Fault on reboot (solved)

Post by frank »

I would zero the memory just in case, that way everything knows that it has a nice clean slab of memory to work with.

As for memory given to user processes, its a security issue if you don't zero the memory before giving it to the process. What if super secret password program was running and freed a page of super secret memory expecting the kernel to zero it and then the memory was given to another process, that would be a major security violation.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Fault on reboot (solved)

Post by Firestryke31 »

An application expecting memory to be zeroed is just asking for bugs. If super-secret password program wanted to be assured that super-secret page won't be read after being freed, it should zero it out itself. Any other application will be full of bugs the moment you even try to use it on another OS.

If an application expects a certain location in memory to be zeroed before use, it should do it itself to keep the applications that don't need it from suffering the speed penalty of zeroing every memory allocation before use, physical, virtual, or whatever.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Fault on reboot (solved)

Post by Candy »

Firestryke31 wrote:An application expecting memory to be zeroed is just asking for bugs. If super-secret password program wanted to be assured that super-secret page won't be read after being freed, it should zero it out itself. Any other application will be full of bugs the moment you even try to use it on another OS.

If an application expects a certain location in memory to be zeroed before use, it should do it itself to keep the applications that don't need it from suffering the speed penalty of zeroing every memory allocation before use, physical, virtual, or whatever.
Your OS can confiscate memory for other reasons - swapping, alignment, memory failures, DMA buffers, hibernation, hot swap of memory banks, stuff like that. Your application therefore does not have the guarantee that what is memory now will be the same later on, so the OS will need to guarantee that pages allocated to the app with any data of the app on it (even one bit) gets wiped completely clean before any other app can use it. In other words, the OS must guarantee that data from app X does not leak from that app if X is supposed to be secure at any point. The Orange Book had this as a guideline for C2 IIRC.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Fault on reboot (solved)

Post by frank »

Firestryke31 wrote:If an application expects a certain location in memory to be zeroed before use, it should do it itself to keep the applications that don't need it from suffering the speed penalty of zeroing every memory allocation before use, physical, virtual, or whatever.
If done correctly then there should be no speed penalty at all to insure that memory is zeroed before use. When an application frees a page place it on a dirty pages list. Then in idle time you can clear the pages and place them on a zeroed list. Of course you need a way to force a page to be cleaned if you run out of pages on the zeroed list.
Firestryke31 wrote:An application expecting memory to be zeroed is just asking for bugs. If super-secret password program wanted to be assured that super-secret page won't be read after being freed, it should zero it out itself. Any other application will be full of bugs the moment you even try to use it on another OS.
Can you tell me one OS that doesn't zero memory before it is handed out? AFAIK all the major ones do it. And as Candy said the application can't always be sure that it has complete control over all pages allocated to it. The OS can pretty much do anything it wants to the pages as long as it maintains the illusion of complete control to the program.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Fault on reboot (solved)

Post by Firestryke31 »

I'll admit I completely forgot about an OS forcibly using a page for something else, which an application would have no control over or knowledge of. However, I still think it's a bad idea to assume an OS will clear memory for you. Just as it may be a bad idea to assume it won't. Simply put, don't assume anything. Just my personal view, as I've been burned by that several times.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Fault on reboot (solved)

Post by Candy »

Firestryke31 wrote:I'll admit I completely forgot about an OS forcibly using a page for something else, which an application would have no control over or knowledge of. However, I still think it's a bad idea to assume an OS will clear memory for you. Just as it may be a bad idea to assume it won't. Simply put, don't assume anything. Just my personal view, as I've been burned by that several times.
You can't even assume it'll clear memory for you. You *will* be wrong, as 90% of allocations aren't handled by your OS. Most are stack pointer movements, which is implicitly not cleared by C and C++ (and assembly - but then you consciously move the stack pointer so it's more obvious) as it's quicker. Even alloca() doesn't clear your stack, and that's not always called even in Windows.

Not to mention that the malloc/free or new/delete contracts also do not clear memory so nobody will expect you to.

If you really want cleared memory, adjust the compiler to do it. Saves piles of work and works reliably. Also, you won't be using C or C++ anymore.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Re: Fault on reboot (solved)

Post by frank »

I was never actually talking about malloc and its cousins but more about the direct page level allocations that malloc uses to build the heap. Am I incorrect in thinking that those pages are cleared by the OS before handling them to malloc. If you get into a discussion about malloc then its the C Library and not the OS itself and since that memory gets recycled over and over again then you have to make sure to clear it before using it.
Post Reply