Page 1 of 2
Real PC crash [solved]
Posted: Sun Aug 23, 2009 11:41 am
by Srowen
I have tried different times my os in both Virtual Box and Qemu. But now, if i try to boot it to my computer (a pentium 4 with 512mb of ram) it crasches. It loads grub, i choose to boot my os and then the pc restart. Any suggest??
Re: Real PC crash
Posted: Sun Aug 23, 2009 11:47 am
by neon
Just have your system display information. It will give you some idea of how far it gets before it crashes, and try to narrow it down until you know where it fails at. Its what I do in these situations.
Re: Real PC crash
Posted: Sun Aug 23, 2009 1:56 pm
by Troy Martin
Something in your PC that's different than an emulated box isn't happy with your code or vice versa, creating a triple fault. To quote Brian Kernighan:
[quote="K, from "Unix for Beginners""]The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.[/quote]
Re: Real PC crash
Posted: Sun Aug 23, 2009 7:22 pm
by 01000101
Troy Martin wrote:Something in your PC that's different than an emulated box isn't happy with your code or vice versa, creating a triple fault. To quote Brian Kernighan:
[quote="K, from "Unix for Beginners""]The most effective debugging tool is still careful thought, coupled with judiciously placed print statements.
[/quote]
QFT! Excellent quote.
OP: one of the top reasons that an OS works great in an emulator then fails on real hardware is due to initialized memory (zero'd memory). When some of your code expects an array to be zero'd or something like that, bad things happen.
But like TroyMartin said, lots of printf() statements and/or some keyboard-enabled stepping would help.
Re: Real PC crash
Posted: Sun Aug 23, 2009 8:06 pm
by gravaera
As an added bit to the debugging methodology tip: I sometimes also find infinite loops to be helpful.
e.g:
I need to know exactly which point in a function an undefined/unexpected behaviour occurs, etc. I placean infinite loop in a narrow area where I KNOW the error is occurring, and keep recompiling and relocating my infinite loop until I get the exact point of deviation.
Works great when the error you're trying to track is causing a triple fault, and you can't quite manage to read the printed bits before you get a reset.
Re: Real PC crash
Posted: Sun Aug 23, 2009 10:53 pm
by Troy Martin
Another good idea: make some basic exception handlers for at least exception 0, 6, and 13. 14 as well if you have paging.
Re: Real PC crash
Posted: Mon Aug 24, 2009 4:30 am
by Srowen
ok i found where my pc reset.. it has some problem when i try to enable paging.. maybe there something wrong here:
Code: Select all
asm volatile("mov %0, %%eax":: "r"(address));
asm volatile("mov %eax, %cr3");
unit_32b cr0;
asm volatile("mov %cr0, %eax");
asm volatile("mov %%eax, %0": "=r"(cr0));
cr0 |= 0x80000000; // Enable paging!
asm volatile("mov %0, %%eax":: "r"(cr0));
asm volatile("mov %eax, %cr0");
any suggestions?
(I've checked the mapping of the kernel and it sould be ok..it crash while i try to enable paging only)
Re: Real PC crash
Posted: Mon Aug 24, 2009 7:09 am
by NickJohnson
The biggest difference (from a non-device standpoint) between a real machine and an emulator is that an emulator zeroes out all memory before running. If the area of memory you are using for paging structures is not part of .bss (which would be cleared by the bootloader, usually), you have to clear it first.
Re: Real PC crash
Posted: Mon Aug 24, 2009 11:07 am
by Srowen
NickJohnson wrote:The biggest difference (from a non-device standpoint) between a real machine and an emulator is that an emulator zeroes out all memory before running. If the area of memory you are using for paging structures is not part of .bss (which would be cleared by the bootloader, usually), you have to clear it first.
but the problem is that i've done it.. i'll check this another time..maybe i haven't see something..
Re: Real PC crash
Posted: Mon Aug 24, 2009 12:54 pm
by quok
Srowen wrote:ok i found where my pc reset.. it has some problem when i try to enable paging.. maybe there something wrong here:
Code: Select all
asm volatile("mov %0, %%eax":: "r"(address));
asm volatile("mov %eax, %cr3");
unit_32b cr0;
asm volatile("mov %cr0, %eax");
asm volatile("mov %%eax, %0": "=r"(cr0));
cr0 |= 0x80000000; // Enable paging!
asm volatile("mov %0, %%eax":: "r"(cr0));
asm volatile("mov %eax, %cr0");
any suggestions?
(I've checked the mapping of the kernel and it sould be ok..it crash while i try to enable paging only)
Erm, you really don't see the problem there? There's some big ones, so let's tear this apart.
Code: Select all
asm volatile("mov %0, %%eax":: "r"(address));
asm volatile("mov %eax, %cr3");
So first thing you're doing is taking whatever register address is in, and putting it in eax. Then you're taking eax and putting it in cr3, except your inline asm is incorrect. You need to double the % signs when using a register name, and you aren't. You also aren't specifying eax as a clobber, so GCC may end up putting something in eax between your two asm statements. This operation can really be done with a single inline asm call, like so:
Code: Select all
asm volatile ( "mov %0, %%cr3" :: "r"(address) : "cr3" );
Now your second problem:
Code: Select all
unit_32b cr0;
asm volatile("mov %cr0, %eax");
asm volatile("mov %%eax, %0": "=r"(cr0));
cr0 |= 0x80000000; // Enable paging!
asm volatile("mov %0, %%eax":: "r"(cr0));
asm volatile("mov %eax, %cr0");
Again this can be shortened up quite a bit, and again you aren't specifying registers, clobbers, or outputs properly. About the only thing you do get right is specifying the inputs. Let's try this:
Code: Select all
uint_32b cr0;
asm volatile ( "mov %%cr0, %0" : "=r"(cr0) );
cr0 |= 0x80000000;
asm volatile ( "mov %0, %%cr0" : /* no outputs */ : "r"(cr0) );
Now that can be shortened up even more, since you
can have multiple asm statements in a single asm() call. You just have to be
very careful about properly specifying clobbers, inputs, and outputs:
Code: Select all
uint_32b cr0;
asm volatile (
"movl %%cr0, %%eax\n\t"
"orl %%eax, $0x80000000\n\t"
"movl %%eax, %%cr0"
: /* no outputs */ : "a"(cr0) : "memory" );
A couple of things here. First, \n\t is used to make the output asm code nicely formatted. You could have it all on one line separated by semi-colons instead as well. Notice there's no outputs as you never come back to C code. I specify eax as the input register, and so GCC will ensure that eax contains the value of cr0 before the asm code is ever called. Since we have eax listed as an input, we don't list it as a clobber. However I do specify the "memory" clobber (and intentionally left it out of the above examples although it could have been used there) to let GCC know that the value of any of the other registers (including EFLAGS and the CR registers which GCC wouldn't normally touch) have changed.
One caveat to the above code: cr0 isn't listed as an output in any way shape or form, so don't expect to be able to update it and have it contain what you think is the correct value.
Re: Real PC crash
Posted: Mon Aug 24, 2009 3:38 pm
by Srowen
i've tried your code but the gcc give me some error in the ASM line... i correct then and i have this code:
Code: Select all
asm volatile( "mov %0, %%cr3" :: "r"(adress));
asm volatile("movl %cr0, %eax\n\t orl $0x80000000, %eax\n\t movl %eax, %cr0");
but i crash as before and the virtual box is still running..
Re: Real PC crash
Posted: Mon Aug 24, 2009 3:43 pm
by pcmattman
but i crash as before and the virtual box is still running..
Are you sure the source of the crash is here, exactly? Is it somewhere after enabling paging (say, printing a string to the screen) rather than this function exactly?
Are you depending on uninitialised memory being zero anywhere? Have you mapped the text framebuffer (0xB8000) in your paging code? Have you mapped the stack in your paging code?
Re: Real PC crash
Posted: Mon Aug 24, 2009 3:54 pm
by Srowen
last test... i've upload the img file of my os and also the binary so if someone wants to try on his pc...
the img file:
http://www.easy-share.com/1907476159/immagine.img
the bin file:
http://www.easy-share.com/1907476161/oxygen.bin
(if you try the img file, you have to choose Oxygen in the grub menu..)
thanks to everyone that help me..
Re: Real PC crash
Posted: Mon Aug 24, 2009 4:26 pm
by Srowen
pcmattman wrote:but i crash as before and the virtual box is still running..
Are you sure the source of the crash is here, exactly? Is it somewhere after enabling paging (say, printing a string to the screen) rather than this function exactly?
Are you depending on uninitialised memory being zero anywhere? Have you mapped the text framebuffer (0xB8000) in your paging code? Have you mapped the stack in your paging code?
yes i'm sure.. i've mapped all the memory for the os.. and if i comment only this function the os run, without paging, but run.. and i also think that if i have a problem in mapping some parts of memory i shoud notice it also in the virtual machine..
and it isn't a problem in the interrupt because i tried to disable them and it crashes the same..
i'm desperate..
Re: Real PC crash
Posted: Mon Aug 24, 2009 4:33 pm
by neon
Hm, the image file runs fine for me in bochs and VPC...