Page 1 of 2

Potential GCC bug?

Posted: Sat Sep 20, 2008 4:06 pm
by darkstart
Hello,
I'm trying to enable paging from JamesM's tutorial and I keep getting a weird GCC bug. My source to enable the paging bit of CR0 is

Code: Select all

	uint32 cr0;
	__asm__ volatile ("mov %%cr0, %0" : "=r"(cr0));
	cr0 |= 0x80000000;
	__asm__ volatile ("mov %0, %%cr0" :: "r"(cr0));
When it's run through GCC to Gas assembly, that bit looks like:

Code: Select all

        mov     DWORD PTR [%esp+12], %eax
        or      DWORD PTR [%esp+12], -2147483648
        mov     %eax, DWORD PTR [%esp+12]
The part that's interesting to me is that it's OR'ing a negative number, when I never fed it one and it should know that it can't do that. The code above results in a triple-fault when run. If I remove the negative sign in front of the number, it runs fine and successfully enables paging. Is this a GCC bug, or is it normal? BTW, my GCC version is 4.1.2, targeting i586-elf.

Re: Potential GCC bug?

Posted: Sun Sep 21, 2008 12:22 am
by thepowersgang
That negative number is actually 0x80000000. GCC is passing gas a negative number because that is what the top bit would mean. When this code is run it should work without a hitch and when disassembled should show the hex representation.

Re: Potential GCC bug?

Posted: Sun Sep 21, 2008 4:23 am
by JamesM
As thepowersgang said, that negative number is actually 0x80000000+something in signed decimal representation.

0x80000000 in decimal is 2147483648, the protected mode bit (bit 0) must have made it negative. (Look up 2's complement arithmetic for an explanation).

There's no bug there, the triple fault will be because your paging code is faulty!!

Re: Potential GCC bug?

Posted: Sun Sep 21, 2008 11:44 am
by darkstart
I ran the same source through my distro's(openSuSE 10.3) generic compiler, it gave the same negative number and it worked. When using my cross-compiler, it aborts with the triple-fault.

Re: Potential GCC bug?

Posted: Mon Sep 22, 2008 10:34 pm
by pcmattman
If I remove the negative sign in front of the number, it runs fine and successfully enables paging.
That is a bit unusual - it *might* be taking 0x80000000 as an integer, so perhaps casting to a uint32 could fix it?

Re: Potential GCC bug?

Posted: Tue Sep 23, 2008 4:11 am
by JamesM
darkstart wrote:I ran the same source through my distro's(openSuSE 10.3) generic compiler, it gave the same negative number and it worked. When using my cross-compiler, it aborts with the triple-fault.
Define "worked". How did you test it?

Re: Potential GCC bug?

Posted: Tue Sep 23, 2008 4:05 pm
by darkstart
pcmattman wrote:
If I remove the negative sign in front of the number, it runs fine and successfully enables paging.
That is a bit unusual - it *might* be taking 0x80000000 as an integer, so perhaps casting to a uint32 could fix it?
I tried and it still faulted. I also tried feeding it the positive base-10 and the negative base-10 number in the C code; they faulted with the cross-compiler.
JamesM wrote:
darkstart wrote:I ran the same source through my distro's(openSuSE 10.3) generic compiler, it gave the same negative number and it worked. When using my cross-compiler, it aborts with the triple-fault.
Define "worked". How did you test it?

When I say worked, I mean that it returned successfully from my init_paging() function, returned to main(), and sat in an idle loop. I added your test code to the end of main(), trying to read an invalid location, and it faulted correctly, with interrupt 14(0xE) called. I use qemu to test my kernel, but I can try bochs as well.

Re: Potential GCC bug?

Posted: Wed Sep 24, 2008 6:56 am
by JamesM
darkstart wrote:
pcmattman wrote:
If I remove the negative sign in front of the number, it runs fine and successfully enables paging.
That is a bit unusual - it *might* be taking 0x80000000 as an integer, so perhaps casting to a uint32 could fix it?
I tried and it still faulted. I also tried feeding it the positive base-10 and the negative base-10 number in the C code; they faulted with the cross-compiler.
JamesM wrote:
darkstart wrote:I ran the same source through my distro's(openSuSE 10.3) generic compiler, it gave the same negative number and it worked. When using my cross-compiler, it aborts with the triple-fault.
Define "worked". How did you test it?

When I say worked, I mean that it returned successfully from my init_paging() function, returned to main(), and sat in an idle loop. I added your test code to the end of main(), trying to read an invalid location, and it faulted correctly, with interrupt 14(0xE) called. I use qemu to test my kernel, but I can try bochs as well.
Interesting. What's the target of your cross compiler? Can you please compile the kernel with both suse builtin GCC and cross compiled GCC and paste the relevant parts of the objdump of each? Make sure you use the same objdump utility for both.

Cheers.

Re: Potential GCC bug?

Posted: Wed Sep 24, 2008 7:29 am
by jal
darkstart wrote:

Code: Select all

        mov     DWORD PTR [%esp+12], %eax
        or      DWORD PTR [%esp+12], -2147483648
        mov     %eax, DWORD PTR [%esp+12]
What I find rather weird, other than you seemingly not having heard of two's complement, is that the disassembled code shows no sign of cr0 (using eax instead), where your inline assembly code implies there should be. I'm not to sure about inline assembly AT&T-style, but surely something goes wrong there.


JAL

Re: Potential GCC bug?

Posted: Wed Sep 24, 2008 12:06 pm
by JamesM
jal wrote:
darkstart wrote:

Code: Select all

        mov     DWORD PTR [%esp+12], %eax
        or      DWORD PTR [%esp+12], -2147483648
        mov     %eax, DWORD PTR [%esp+12]
What I find rather weird, other than you seemingly not having heard of two's complement, is that the disassembled code shows no sign of cr0 (using eax instead), where your inline assembly code implies there should be. I'm not to sure about inline assembly AT&T-style, but surely something goes wrong there.


JAL
That'll teach me for not actually sanity checking the code I comment about...

Re: Potential GCC bug?

Posted: Wed Sep 24, 2008 2:14 pm
by darkstart
jal wrote:
darkstart wrote:

Code: Select all

        mov     DWORD PTR [%esp+12], %eax
        or      DWORD PTR [%esp+12], -2147483648
        mov     %eax, DWORD PTR [%esp+12]
What I find rather weird, other than you seemingly not having heard of two's complement, is that the disassembled code shows no sign of cr0 (using eax instead), where your inline assembly code implies there should be. I'm not to sure about inline assembly AT&T-style, but surely something goes wrong there.


JAL
I've heard of it, just never actually used it before. I think the DWORD PTR [%esp+12] is a pointer to the cr0 register.

As for the object dump, here's the cross-compiler's code:

Code: Select all

0000036e <switch_page_dir>:
 36e:   83 ec 10                sub    $0x10,%esp
 371:   8b 44 24 14             mov    0x14(%esp),%eax
 375:   a3 00 00 00 00          mov    %eax,0x0
 37a:   8b 44 24 14             mov    0x14(%esp),%eax
 37e:   05 00 10 00 00          add    $0x1000,%eax
 383:   0f 22 d8                mov    %eax,%cr3
 386:   0f 20 c0                mov    %cr0,%eax
 389:   89 44 24 0c             mov    %eax,0xc(%esp)
 38d:   81 4c 24 0c 00 00 00    orl    $0x80000000,0xc(%esp)
 394:   80 
 395:   8b 44 24 0c             mov    0xc(%esp),%eax
 399:   0f 22 c0                mov    %eax,%cr0
 39c:   83 c4 10                add    $0x10,%esp
 39f:   c3                      ret    
And here is the generic compiler's:

Code: Select all

00000383 <switch_page_dir>:
 383:   83 ec 10                sub    $0x10,%esp
 386:   8b 44 24 14             mov    0x14(%esp),%eax
 38a:   a3 00 00 00 00          mov    %eax,0x0
 38f:   8b 44 24 14             mov    0x14(%esp),%eax
 393:   05 00 10 00 00          add    $0x1000,%eax
 398:   0f 22 d8                mov    %eax,%cr3
 39b:   0f 20 c0                mov    %cr0,%eax
 39e:   89 44 24 0c             mov    %eax,0xc(%esp)
 3a2:   81 4c 24 0c 00 00 00    orl    $0x80000000,0xc(%esp)
 3a9:   80 
 3aa:   8b 44 24 0c             mov    0xc(%esp),%eax
 3ae:   0f 22 c0                mov    %eax,%cr0
 3b1:   83 c4 10                add    $0x10,%esp
 3b4:   c3                      ret    
I used the generic objdump on both. If I used the wrong flag on objdump(-S), tell me and I apologize in advance. I haven't used it but a few times before.

Re: Potential GCC bug?

Posted: Wed Sep 24, 2008 4:00 pm
by Combuster
If you noticed, they are identical except for the location into the file where it put the result...

Re: Potential GCC bug?

Posted: Fri Sep 26, 2008 6:38 am
by JamesM
They're identical, as combuster said. And now I understand your earlier snippet - you pasted this part:
39e: 89 44 24 0c mov %eax,0xc(%esp)
3a2: 81 4c 24 0c 00 00 00 orl $0x80000000,0xc(%esp)
3a9: 80
3aa: 8b 44 24 0c mov 0xc(%esp),%eax
But not the rest.

Re: Potential GCC bug?

Posted: Mon Oct 27, 2008 5:19 pm
by darkstart
How much more do you want? I can post the entire code if need be. BTW, I did some more debugging and Qemu throws three debug interrupts and a non-maskable interrupt.

Re: Potential GCC bug?

Posted: Tue Oct 28, 2008 4:01 am
by Combuster
Could it be that you have a race condition somewhere else?
I mean if you tested one of the versions just once, you might've gotten lucky and *not* get a crash for that one round. It's just completely strange that the signed version crashes while the unsigned version does not, and the resulting binary is identical.

Have you tried running the original code in bochs? Does it still crash there, and if yes, where?