Arm Community
Arm Community
  • Site
  • User
  • Site
  • Search
  • User
Arm Community blogs
Arm Community blogs
Embedded and Microcontrollers blog How to extract exception information form an exception stack frame
  • Blogs
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
More blogs in Arm Community blogs
  • AI blog

  • Announcements

  • Architectures and Processors blog

  • Automotive blog

  • Embedded and Microcontrollers blog

  • Internet of Things (IoT) blog

  • Laptops and Desktops blog

  • Mobile, Graphics, and Gaming blog

  • Operating Systems blog

  • Servers and Cloud Computing blog

  • SoC Design and Simulation blog

  • Tools, Software and IDEs blog

Tags
  • svc_call
  • exception_handling
  • Cortex-M
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

How to extract exception information form an exception stack frame

Yasuhiko Koumoto
Yasuhiko Koumoto
February 7, 2015

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.

Anonymous
Parents
  • Liviu Ionescu
    Liviu Ionescu over 10 years ago

    I recommend you get your information from the documentation, and not from various rumours.

    I just tried setting the CONTROL register to 2, and everything was fine, the value remained persistent and the next call used the PSP, not MSP.

    The test was performed on a FRDM KL25 board.

    Liviu

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Comment
  • Liviu Ionescu
    Liviu Ionescu over 10 years ago

    I recommend you get your information from the documentation, and not from various rumours.

    I just tried setting the CONTROL register to 2, and everything was fine, the value remained persistent and the next call used the PSP, not MSP.

    The test was performed on a FRDM KL25 board.

    Liviu

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
Children
No Data
Embedded and Microcontrollers blog
  • Formally verifying a floating-point division routine with Gappa – part 2

    Simon Tatham
    Simon Tatham
    A method of testing whether a numerical error analysis using Gappa really matches the code it is intended to describe.
    • September 4, 2025
  • Formally verifying a floating-point division routine with Gappa – part 1

    Simon Tatham
    Simon Tatham
    Learn the basics of using Gappa for numerical error analysis, using floating-point division in Arm machine code as a case study.
    • September 4, 2025
  • Adapting Kubernetes for high-performance IoT Edge deployments

    Alexandre Peixoto Ferreira
    Alexandre Peixoto Ferreira
    In this blog post, we address heterogeneity in IoT edge deployments using Kubernetes.
    • August 21, 2024