[cfe-dev] RFC: Support x86 interrupt and exception handlers

H.J. Lu via cfe-dev cfe-dev at lists.llvm.org
Wed Sep 23 15:35:33 PDT 2015

On Tue, Sep 22, 2015 at 11:13 AM, Richard Henderson <rth at redhat.com> wrote:
> HJ, I think Hal is right.  Providing the data via arguments is vastly superior
> to providing it via builtins.  I had actually been thinking the same thing myself.
> It should be easy to check that the function has the correct signature in the
> hook adding the attribute.  It should also be easy to check for the attribute
> at the beginning of ix86_function_arg et al, in order to handle these special
> cases.

Thanks for all feedbacks.  Here is the updated spec.

The interrupt and exception handlers are called by x86 processors.  X86
hardware pushes information onto stack and calls the handler.  The
requirements are

1. Both interrupt and exception handlers must use the 'IRET' instruction,
instead of the 'RET' instruction, to return from the handlers.
2. All registers are callee-saved in interrupt and exception handlers.
3. The difference between interrupt and exception handlers is the
exception handler must pop 'ERROR_CODE' off the stack before the 'IRET'

The design goals of interrupt and exception handlers for x86 processors

1. Support both 32-bit and 64-bit modes.
2. Flexible for compilers to optimize.
3. Easy to use by programmers.

To implement interrupt and exception handlers for x86 processors, a
compiler should support:

'interrupt' attribute

Use this attribute to indicate that the specified function with
mandatory arguments is an interrupt or exception handler.  The compiler
generates function entry and exit sequences suitable for use in an
interrupt handler when this attribute is present.  The 'IRET' instruction,
instead of the 'RET' instruction, is used to return from interrupt or
exception handlers.  All registers, except for the EFLAGS register which
is restored by the 'IRET' instruction, are preserved by the compiler.

Any interruptible-without-stack-switch code must be compiled with
-mno-red-zone since interrupt handlers can and will, because of the
hardware design, touch the red zone.

1. interrupt handler must be declared with a mandatory argument:

#ifdef __x86_64__
typedef unsigned long long int uword_t;
typedef unsigned int uword_t;

struct interrupt_frame
  uword_t ip;
  uword_t cs;
  uword_t flags;
  uword_t sp;
  uword_t ss;

__attribute__ ((interrupt))
f (struct interrupt_frame *frame)

2. exception handler:

The exception handler is very similar to the interrupt handler with
a different mandatory function signature:

__attribute__ ((interrupt))
f (struct interrupt_frame *frame, uword_t error_code)

and compiler pops `ERROR_CODE' off stack before the 'IRET' instruction.

The exception handler should only be used for exceptions which push an
error code and all other exceptions must use the interrupt handler.
The system will crash if the wrong handler is used.

More information about the cfe-dev mailing list