OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:31 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Interesting GCC bug
PostPosted: Sun Jun 20, 2021 2:27 pm 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 182
Hi everyone, today i wanna share with you an interesting GCC bug i found.
I was working on FPU initialization in my kernel, and finally got to the point where i needed to use FXSAVE in order to retrieve the FPU MXCSR mask.
Since it was a throw away FPU state i decided to put it on the stack like this:
Code:
alignas(16) u8 fxsave_region[512] {};
asm volatile ("fxsave %0" : "=m"(fxsave_region));


However, that code would generate GPF at all times, even though the fxsave_region address i logged to the console seemed to be aligned to 32 bytes (0xC0888DA0).
After trying different things (using attribute aligned, moving the variable around, adding * before fxsave_region in the asm statement) i ended up looking at the dissasembly,
which was relatively simple:
Code:
fxsave [ebp-0x218]


Since that code would GPF i had the exact EBP value at the time of the GPF, which was 0xC0888FC4.
As you can see 0xC0888FC4 - 0x218 = 0xC0888DAC, which is 0xC bytes past the address that fxsave_region is actually located at.

For some reason GCC uses a wrong offset to load my stack array.
Not sure what this bug is about, but the workaround i found was to use alignas(32) instead of alignas(16).
I must note that i use GCC 10.1.0.
Since i don't want this to break later on, I'm just going to use kmalloc instead.

If you know anything about this bug please let me know.


Top
 Profile  
 
 Post subject: Re: Interesting GCC bug
PostPosted: Sun Jun 20, 2021 2:56 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
It's not a GCC bug. Your stack is not aligned correctly. The i386 psABI (section 2.2.2) requires the stack to be 16-byte aligned.


Top
 Profile  
 
 Post subject: Re: Interesting GCC bug
PostPosted: Sun Jun 20, 2021 3:02 pm 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 182
Octocontrabass wrote:
It's not a GCC bug. Your stack is not aligned correctly. The i386 psABI (section 2.2.2) requires the stack to be 16-byte aligned.


I verified that my stack begin and end pointers are both page aligned.


Top
 Profile  
 
 Post subject: Re: Interesting GCC bug
PostPosted: Sun Jun 20, 2021 3:19 pm 
Offline
Member
Member

Joined: Sun Apr 05, 2020 1:01 pm
Posts: 182
Octocontrabass wrote:
It's not a GCC bug. Your stack is not aligned correctly. The i386 psABI (section 2.2.2) requires the stack to be 16-byte aligned.


Never mind, you're right!

Quote:
value (%esp+ 4) is always a multiple of 16 (32 or 64) when control is transferred
to the function entry point.


This is what i was missing in my pre-kernel. Thanks a lot. Crazy how it didn't reveal itself earlier!


Top
 Profile  
 
 Post subject: Re: Interesting GCC bug
PostPosted: Sun Jun 20, 2021 10:33 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
8infy wrote:
This is what i was missing in my pre-kernel. Thanks a lot. Crazy how it didn't reveal itself earlier!
In x86, there is very little that depends hard on alignment, and even less that depends on stack alignment. In this case you were using fxsave on a stack variable, which is a bit weird. Normally, the fxsave area is allocated in kernel heap (along with the rest of the task descriptor), and then the alignment is for the heap allocator do determine.

_________________
Carpe diem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], SemrushBot [Bot] and 75 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group