Real PC crash [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.
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Real PC crash [solved]

Post 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??
Last edited by Srowen on Wed Aug 26, 2009 1:21 am, edited 1 time in total.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Real PC crash

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Real PC crash

Post 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]
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Real PC crash

Post 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.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Real PC crash

Post 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. :wink:
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Real PC crash

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Re: Real PC crash

Post 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)
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Real PC crash

Post 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.
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Re: Real PC crash

Post 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..
quok
Member
Member
Posts: 490
Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA

Re: Real PC crash

Post 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.
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Re: Real PC crash

Post 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..
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Real PC crash

Post 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?
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Re: Real PC crash

Post 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..
Srowen
Member
Member
Posts: 60
Joined: Thu Feb 26, 2009 2:31 pm
Location: Genova, ITALY

Re: Real PC crash

Post 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.. :cry: :cry: :cry: i'm desperate..
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Real PC crash

Post by neon »

Hm, the image file runs fine for me in bochs and VPC...
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply