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
  • Yasuhiko Koumoto
    Yasuhiko Koumoto over 10 years ago

    Hello Liviu,

    thank you, you are right. My presentation is valid only when CONTROL.SPSEL=0, isn't it?

    In Cortex-M0 case, CONTROL.SPSEL is fixed to 0. In Cortex-M0+ case, modification capability is a option but some implementation (such as Kinetis L series) fix it to 0.

    Therefore I think my presentation would not be so wrong.

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

    If you configure a separate stack (PSP) for thread mode, when the exception occurs in thread mode, the core will save a4-a11 on PSP, then enter the handler, and switch to MSP. As any C function, your function will access a4 - a11 using SP, which in handler mode is MSP, while in fact the values were stored on PSP.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
  • Yasuhiko Koumoto
    Yasuhiko Koumoto over 10 years ago

    Hello Livui.

    can you show the case which can not be functional? I would like to know it and correct my presentation.

    Best regards,

    Yasuhiko Koumoto.

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

    Although your solution may be functional for certain cases, I doubt that without the assembly logic that you already described in the other post, you can correctly identify the exception.

    The recommended implementation (please see Joseph Yiu's book The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors) is to use an assembly function to determine if you are running on MSP or PSP and then call a C function, passing a pointer to a frame and several other arguments. You should also consider portability issues.

    An example of a production code using this implementation is available, for example, in GNU ARM Eclipse Plug-ins / Second Edition /  [06ce04]  /ilg.gnuarmeclipse.templates.core/templates/common/system/src/cor…

    This code is used in the projects generated by the templates included in GNU ARM Eclipse plug-ins.

    • Cancel
    • Up 0 Down
    • Reply
    • More
    • Cancel
<
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