cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
tscott.phoenix
Observer
Observer
5,691 Views
Registered: ‎06-03-2011

Is it possible to update the stack pointer directly from C code?

Jump to solution

The title says it all.  Is it possible to update the Microblaze's stack pointer, R1, directly from C code?  I've got a function that *never* returns and would like to reset the stack pointer upon entering this function, but am not sure how to do this.  I'm thinking in-line assembly might work or perhaps there's a function somewhere that can achieve this. 

 

I've tried:

 

asm ("addik r1,r0,%0": : "r" (_stack));

 

but that fails during linking with a "undefined reference to `r3'" error.

 

Much thanks!

--tim

 

0 Kudos
1 Solution

Accepted Solutions
martinthompson
Explorer
Explorer
7,365 Views
Registered: ‎08-14-2007

The correct way to do this is to inform the compiler that the function doesn't return.  It will then sort it all out for you!

 

For GCC, use the noreturn attribute as documented here:

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

 

EG:

 

void non_returning_func() __attribute__ ((noreturn));


void non_returning_func(/* params */) {

  /* etc. */

}


Martin Thompson
martin.j.thompson@trw.com
http://www.conekt.co.uk/capabilities/electronic-hardware

View solution in original post

0 Kudos
5 Replies
martinthompson
Explorer
Explorer
7,366 Views
Registered: ‎08-14-2007

The correct way to do this is to inform the compiler that the function doesn't return.  It will then sort it all out for you!

 

For GCC, use the noreturn attribute as documented here:

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

 

EG:

 

void non_returning_func() __attribute__ ((noreturn));


void non_returning_func(/* params */) {

  /* etc. */

}


Martin Thompson
martin.j.thompson@trw.com
http://www.conekt.co.uk/capabilities/electronic-hardware

View solution in original post

0 Kudos
tscott.phoenix
Observer
Observer
5,673 Views
Registered: ‎06-03-2011

Awesome.  I got the solution I was looking from tech support.  The info is contained in oslib_rm.pdf.  Under the section entitled "MicroBlaze Processor Pseudo-asm Macro Summary", there is a macro mtgpr(rn,v), that allows you to modify GPRs.  Using that I was able to modify R1 (the stack pointer) directly.  Works like a charm!

 

Cheers,

--tim

 

0 Kudos
martinthompson
Explorer
Explorer
5,662 Views
Registered: ‎08-14-2007

OK, you have *a* solution.  I prefer mine as you tell the compiler what your actual problem is and it sorts out the details.


Hacking directly on the stack pointer is something I would reserve for much more extreme circumstances when there's no other way of getting the job done!  You never know when what you've done might become invalid.

Martin Thompson
martin.j.thompson@trw.com
http://www.conekt.co.uk/capabilities/electronic-hardware
0 Kudos
tscott.phoenix
Observer
Observer
5,660 Views
Registered: ‎06-03-2011

That's great, but I probably didn't fully define the context I wanted to do this in.

 

1) System Start up

2) Jump to 'main' entry point

3) Jump to my "real" main entry point - a port of our OS to the Microblaze architecture.  We never return from here.

4) Pre-fill the stack with a signature for metrics capture

5) Re-init stack with its base value.  Prior to doing this, it was consuming 64 bytes unnecessarily. 

 

Yes, 64 bytes is a pretty paltry amount, but being a picky embedded engineer, I wanted to make sure we maximized our stack space.

 

That said, I learned something new based on your post and appreciate your input.

 

--tim

 

0 Kudos
martinthompson
Explorer
Explorer
5,657 Views
Registered: ‎08-14-2007

OK, that is a bit more than "just a non-returning function", I can see your need for accessing the SP - although I'd still be tempted to use the GCC attribute for that bit - or is it a cross-project call that it doesn't work for?

 

(I'm also one of the picky embedded types, I can entirely understand your point about not wasting 64 bytes of stack :)

Thanks for the details,

Martin 

Martin Thompson
martin.j.thompson@trw.com
http://www.conekt.co.uk/capabilities/electronic-hardware
0 Kudos