HomeCommunityEmbedded and Microcontrollers blog
February 7, 2015

How to extract exception information form an exception stack frame

Hi all,my presentation is related to Cortex-M exception handling. I have found the method to extract exception information form an exception stack frame.  Usually it can be done by getting the stack top by using the inline assembler as described...

By Yasuhiko Koumoto

Share
Reading time 1 minute

Hi all,

my presentation is related to Cortex-M exception handling. I have found the method to extract exception information form an exception stack frame.  Usually it can be done by getting the stack top by using the inline assembler as described in the post error: Hard Fault Handler. My presentation does not use the inline assembler. It will use the calling and receiving conventions of a function.

This procedure will be only valid under assumption the stack pointer is not switched. It means SPSEL bit of CONTROL register is "0".

As you know, arguments for a function will be passed via registers and the stack. In almost all case, the first 4 arguments will be passed via registers (i.e. r0, r1, r2 and r3) and as for after the 5th arguments will be passed via the stack. Therefore, for an exception handler declared as

void exception_handler(int a0, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, int a10, int a11)
{
    /* the body of the handler */
}
 

the stack contents just before calling the exception handler would be set from a4 to a11. That is,

   a0 ... register r0 value

   a1 ... register r1 value

   a2 ... register r2 value

   a3 ... register r3 value

   a4 ... stacked r0 (i.e. the same as a0)

   a5 ... stacked r1 (i.e. the same as a1)

   a6 ... stacked r2 (i.e. the same as a2)

   a7 ... stacked r3 (i.e. the same as a3)

   a8 ... stacked r12

   a9 ... stacked lr

   a10 .. stacked pc

   a11 .. stacked psr

If you want to get the SVCall parameter,

      scv_param = *(char *)(a10 - 2);

would be able to get it in the exception handler.

If you want to change the return address,

    *(short *)(&a10) = NEW_RETURN_ADDRESS;

would be realized it.

Although I have not seen it, for the compiler which had not adopted the register argument passing, it would be the a0 would be the stacked r0, the a1 would be the stacked r1, and so on.

How about my idea. Or someone had already mentioned it.

Best regards,

Yasuhiko Koumoto.


1Log in to like this post
Share

Article text

Re-use is only permitted for informational and non-commercial or personal use only.

placeholder