OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Passing a stack frame by value
PostPosted: Fri Sep 20, 2019 1:57 am 
Offline

Joined: Mon Sep 16, 2019 12:57 am
Posts: 5
I spent about three hours yesterday debugging an issue triggered by a gcc optimisation. I'm posting it in the hope that it may save someone some time. Basically, my ISR look like this:
Code:
...
pusha
call handler
popa
...
iret

where handler is the following C function:
Code:
void handler(isr_stack_frame_t stack)
{
   /* possibly do something to the stack */
}

Obviously (in retrospect), this is wrong, because the compiler is free to elide any modifications to stack that the function makes (and indeed it did so when compiling with -O2).

I wonder if there is a way to beat the compiler into submission by explaining that the stack is going to be valid after the call. For example, adding a call to an empty function (defined in another compilation unit!) that takes a pointer to the stack as argument does the trick, but I don't think it's guaranteed to. I guess the calling convention makes no guarantees about the validity of the arguments on the stack after the call, so I should probably give up and pass the stack frame by reference, shouldn't I?


Top
 Profile  
 
 Post subject: Re: Passing a stack frame by value
PostPosted: Fri Sep 20, 2019 2:08 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
You should.

Were you perhaps following a tutorial known to have this bug?


Top
 Profile  
 
 Post subject: Re: Passing a stack frame by value
PostPosted: Fri Sep 20, 2019 2:11 am 
Offline

Joined: Mon Sep 16, 2019 12:57 am
Posts: 5
Quote:
Were you perhaps following a tutorial known to have this bug?


Ahah, no, I take all the blame. :)


Top
 Profile  
 
 Post subject: Re: Passing a stack frame by value
PostPosted: Fri Sep 20, 2019 2:44 am 
Offline

Joined: Mon Sep 16, 2019 12:57 am
Posts: 5
Note that the Little Book about OS Development makes the same mistake. Here's the github issue about it.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 37 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