Potential GCC bug?

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.
darkstart
Posts: 8
Joined: Fri Jul 18, 2008 8:50 am

Potential GCC bug?

Post 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.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Potential GCC bug?

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Potential GCC bug?

Post 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!!
darkstart
Posts: 8
Joined: Fri Jul 18, 2008 8:50 am

Re: Potential GCC bug?

Post 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.
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: Potential GCC bug?

Post 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?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Potential GCC bug?

Post 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?
darkstart
Posts: 8
Joined: Fri Jul 18, 2008 8:50 am

Re: Potential GCC bug?

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Potential GCC bug?

Post 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.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Potential GCC bug?

Post 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
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Potential GCC bug?

Post 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...
darkstart
Posts: 8
Joined: Fri Jul 18, 2008 8:50 am

Re: Potential GCC bug?

Post 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.
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: Potential GCC bug?

Post by Combuster »

If you noticed, they are identical except for the location into the file where it put the result...
"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 ]
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Potential GCC bug?

Post 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.
darkstart
Posts: 8
Joined: Fri Jul 18, 2008 8:50 am

Re: Potential GCC bug?

Post 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.
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: Potential GCC bug?

Post 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?
"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 ]
Post Reply