Re: [PATCH] x86 interrupt attribute patch [2/2]

2016-06-02 Thread Sandra Loosemore

On 06/01/2016 08:19 AM, Koval, Julia wrote:


diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 2d4f028..3e6a796 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5266,6 +5266,79 @@ On x86-32 targets, the @code{stdcall} attribute causes 
the compiler to
 assume that the called function pops off the stack space used to
 pass arguments, unless it takes a variable number of arguments.

+@item no_caller_saved_registers
+@cindex @code{no_caller_saved_registers} function attribute, x86
+Use this attribute to indicate that the specified function has no
+caller-saved registers. That is, all registers are callee-saved. For
+example, this attribute can be used for a function, called from an


s/function,/function/

The documentation parts are OK with that change.

-Sandra



RE: [PATCH] x86 interrupt attribute patch [2/2]

2016-06-01 Thread Koval, Julia
> I'm also confused; I can see how an interrupt handler might have different 
> register usage conventions, but do regular functions called from inside an 
> interrupt handler function really use a non-standard call/return sequence?
Usually, they don't. But because of this, the interrupt handler should save all 
the context, because any register can be used in the called function and we 
have no information about it. This is bad for performance. That's why 
no_caller_saved_registers attribute was proposed - the function with this 
attribute saves/restores all modified registers itself, and an interrupt 
function can call it without saving full context. Here is the discussion: 
https://gcc.gnu.org/ml/gcc-patches/2015-09/msg02277.html

Here is the patch with the fixes. Ok for trunk?

Thanks,
Julia

-Original Message-
From: Sandra Loosemore [mailto:san...@codesourcery.com] 
Sent: Monday, May 30, 2016 10:37 PM
To: Koval, Julia <julia.ko...@intel.com>; gcc-patches@gcc.gnu.org
Cc: Lu, Hongjiu <hongjiu...@intel.com>; vaalfr...@gmail.com; ubiz...@gmail.com; 
l...@redhat.com; Zamyatin, Igor <igor.zamya...@intel.com>
Subject: Re: [PATCH] x86 interrupt attribute patch [2/2]

On 05/30/2016 07:31 AM, Koval, Julia wrote:
> Hi,
> Here is the fixed version of the patch. Ok for trunk?
>
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 
> 2d4f028..f4bd7dd 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -5266,6 +5266,96 @@ On x86-32 targets, the @code{stdcall} attribute 
> causes th e compiler to  assume that the called function pops off the 
> stack space used to  pass arguments, unless it takes a variable number 
> of arguments.
>
> +@item no_caller_saved_registers
> +@cindex @code{no_caller_saved_registers} function attribute, x86 Use 
> +this attribute to indicate that the specified function has no 
> +caller-saved registers, for example for a function, called from an 
> +interrupt handler.  That is, all registers are callee-saved.

I think the "for example" information should come after the second sentence to 
improve the flow here.  I'm also confused; I can see how an interrupt handler 
might have different register usage conventions, but do regular functions 
called from inside an interrupt handler function really use a non-standard 
call/return sequence?

> +The compiler generates proper function entry and exit sequences to 
> +save and restore any modified registers, except for the EFLAGS 
> +register.  Since GCC doesn't preserve MPX, SSE, MMX nor x87 states, 
> +the GCC option, @option{-mgeneral-regs-only}, should be used to

Please delete both commas in the line above.

> +compile functions with @code{no_caller_saved_registers} attribute.
> +
> +@emph{Note for compiler implementers:} If the compiler generates MPX, 
> +SSE, MMX or x87 instructions in a function with 
> +@code{no_caller_saved_registers} attribute or functions called from a 
> +function with @code{no_caller_saved_registers} attribute may contain 
> +MPX, SSE, MMX or x87 instructions, the compiler must save and restore 
> +the corresponding state.

A "Note for compiler implementers" has no place in user documentation. 
You should just document what GCC does, if it is relevant to how a user would 
use this feature.

It also seems like the admonition in this note that the compiler must 
save/restore the state contradicts the previous paragraph, where you say GCC 
doesn't preserve the state.

> +
> +@item interrupt
> +@cindex @code{interrupt} function attribute, x86 Use this attribute 
> +to indicate that the specified function is an interrupt handler or an 
> +exception handler (depending on parameters, passed

Delete the comma.

> +to the function, explained further).  The compiler generates function 
> +entry and exit sequences suitable for use in an interrupt handler 
> +when this attribute is present.  The @code{IRET} instruction, instead 
> +of the @code{RET} instruction, is used to return from interrupt 
> +handlers.  All registers, except for the EFLAGS register which is 
> +restored by the @code{IRET} instruction, are preserved by the 
> +compiler.  Since GCC doesn't preserve MPX, SSE, MMX nor x87 states, 
> +the GCC option, @option{-mgeneral-regs-only}, should be used to 
> +compile interrupt and

Delete the two previous commas.

> +exception handlers.
> +
> +@emph{Note for compiler implementers:} If the compiler generates MPX, 
> +SSE, MMX or x87 instructions in an interrupt or exception handler, or 
> +functions called from an interrupt or exception handler may contain 
> +MPX, SSE, MMX or x87 instructions, the compiler must save and restore 
> +the corresponding state.

Again, this is user documentation.  Just explain what GCC does if it is 
relevant to how a user would use the feature you are docume

Re: [PATCH] x86 interrupt attribute patch [2/2]

2016-05-30 Thread Sandra Loosemore

On 05/30/2016 07:31 AM, Koval, Julia wrote:

Hi,
Here is the fixed version of the patch. Ok for trunk?

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 2d4f028..f4bd7dd 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5266,6 +5266,96 @@ On x86-32 targets, the @code{stdcall} attribute causes th
e compiler to
 assume that the called function pops off the stack space used to
 pass arguments, unless it takes a variable number of arguments.

+@item no_caller_saved_registers
+@cindex @code{no_caller_saved_registers} function attribute, x86
+Use this attribute to indicate that the specified function has no
+caller-saved registers, for example for a function, called from an
+interrupt handler.  That is, all registers are callee-saved.


I think the "for example" information should come after the second 
sentence to improve the flow here.  I'm also confused; I can see how an 
interrupt handler might have different register usage conventions, but 
do regular functions called from inside an interrupt handler function 
really use a non-standard call/return sequence?



+The compiler generates proper function entry and exit sequences to
+save and restore any modified registers, except for the EFLAGS
+register.  Since GCC doesn't preserve MPX, SSE, MMX nor x87 states,
+the GCC option, @option{-mgeneral-regs-only}, should be used to


Please delete both commas in the line above.


+compile functions with @code{no_caller_saved_registers} attribute.
+
+@emph{Note for compiler implementers:} If the compiler generates MPX,
+SSE, MMX or x87 instructions in a function with
+@code{no_caller_saved_registers} attribute or functions called from
+a function with @code{no_caller_saved_registers} attribute may contain
+MPX, SSE, MMX or x87 instructions, the compiler must save and restore
+the corresponding state.


A "Note for compiler implementers" has no place in user documentation. 
You should just document what GCC does, if it is relevant to how a user 
would use this feature.


It also seems like the admonition in this note that the compiler must 
save/restore the state contradicts the previous paragraph, where you say 
GCC doesn't preserve the state.



+
+@item interrupt
+@cindex @code{interrupt} function attribute, x86
+Use this attribute to indicate that the specified function is an
+interrupt handler or an exception handler (depending on parameters, passed


Delete the comma.


+to the function, explained further).  The compiler generates function
+entry and exit sequences suitable for use in an interrupt handler when
+this attribute is present.  The @code{IRET} instruction, instead of the
+@code{RET} instruction, is used to return from interrupt handlers.  All
+registers, except for the EFLAGS register which is restored by the
+@code{IRET} instruction, are preserved by the compiler.  Since GCC
+doesn't preserve MPX, SSE, MMX nor x87 states, the GCC option,
+@option{-mgeneral-regs-only}, should be used to compile interrupt and


Delete the two previous commas.


+exception handlers.
+
+@emph{Note for compiler implementers:} If the compiler generates MPX,
+SSE, MMX or x87 instructions in an interrupt or exception handler, or
+functions called from an interrupt or exception handler may contain
+MPX, SSE, MMX or x87 instructions, the compiler must save and restore
+the corresponding state.


Again, this is user documentation.  Just explain what GCC does if it is 
relevant to how a user would use the feature you are documenting.



+
+Since the direction flag in the FLAGS register in interrupt handlers
+is undetermined, cld instruction must be emitted in function prologue
+if rep string instructions are used in interrupt handler or interrupt
+handler isn't a leaf function.


This paragraph seems totally implementor-speaky and irrelevant to how a 
user would use the feature.



+
+Any interruptible-without-stack-switch code must be compiled with
+@option{-mno-red-zone} since interrupt handlers can and will, because
+of the hardware design, touch the red zone.
+
+An interrupt handler must be declared with a mandatory pointer
+argument:
+
+@smallexample
+struct interrupt_frame;
+
+__attribute__ ((interrupt))
+void
+f (struct interrupt_frame *frame)
+@{
+@}
+@end smallexample
+
+and you must define the structure the pointer pointing to, depending
+on the proper x86 interrupt frame, described in the processor's manual.


How about

@noindent
and you must define @code{struct interrupt_frame} as described in the 
processor's manual.




+
+The exception handler is very similar to the interrupt handler with
+a different mandatory function signature:
+
+@smallexample
+#ifdef __x86_64__
+typedef unsigned long long int uword_t;
+#else
+typedef unsigned int uword_t;
+#endif
+
+struct interrupt_frame;
+
+__attribute__ ((interrupt))
+void
+f (struct interrupt_frame *frame, uword_t error_code)
+@{
+  ...
+@}
+@end smallexample
+
+and compiler pops the error code off the stack before the @code{IRET}
+instruction.
+

RE: [PATCH] x86 interrupt attribute patch [2/2]

2016-05-30 Thread Koval, Julia
Hi,
Here is the fixed version of the patch. Ok for trunk?


-Original Message-
From: Sandra Loosemore [mailto:san...@codesourcery.com] 
Sent: Tuesday, May 10, 2016 11:02 PM
To: Koval, Julia <julia.ko...@intel.com>; gcc-patches@gcc.gnu.org
Cc: Lu, Hongjiu <hongjiu...@intel.com>; vaalfr...@gmail.com; ubiz...@gmail.com; 
l...@redhat.com; Zamyatin, Igor <igor.zamya...@intel.com>
Subject: Re: [PATCH] x86 interrupt attribute patch [2/2]

On 04/20/2016 07:42 AM, Koval, Julia wrote:
> diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 
> a5a8b23..82de5bf 100644
> --- a/gcc/doc/extend.texi
> +++ b/gcc/doc/extend.texi
> @@ -5263,6 +5263,83 @@ On x86-32 targets, the @code{stdcall} attribute 
> causes the compiler to  assume that the called function pops off the 
> stack space used to  pass arguments, unless it takes a variable number of 
> arguments.
>
> +@item no_caller_saved_registers
> +@cindex @code{no_caller_saved_registers} function attribute, x86 Use 
> +this attribute to indicate that the specified function has no 
> +caller-saved registers.  That is, all registers are callee-saved.
> +The compiler generates proper function entry and exit sequences to 
> +save and restore any modified registers, except for the EFLAGS 
> +register.  If the compiler generates MPX, SSE, MMX or x87 
> +instructions in a function with @code{no_caller_saved_registers} 
> +attribute or functions called from a function with 
> +@code{no_caller_saved_registers} attribute may contain MPX, SSE, MMX 
> +or x87 instructions, the compiler must save and restore the corresponding 
> state.

I cannot parse the last sentence in this paragraph.  How can the compiler know 
whether called functions may contain those instructions? 
Plus, talking about what the compiler must do seems too implementor-speaky for 
user documentation.  Maybe you mean something like "The compiler also saves and 
restores state associated with MPX, SSE, MMX, and x87 instructions." ?

I also think the documentation needs to give some hint about why a user would 
want to put this attribute on a function.

> +
> +@item interrupt
> +@cindex @code{interrupt} function attribute, x86 Use this attribute 
> +to indicate that the specified function is an interrupt handler.  The 
> +compiler generates function entry and exit sequences suitable for use 
> +in an interrupt handler when this attribute is present.  The 
> +@code{IRET} instruction, instead of the @code{RET} instruction, is 
> +used to return from interrupt handlers.  All registers, except for 
> +the EFLAGS register which is restored by the @code{IRET} instruction, 
> +are preserved by the compiler.  If the compiler generates MPX, SSE, 
> +MMX or x87 instructions in an interrupt handler, or functions called 
> +from an interrupt handler may contain MPX, SSE, MMX or x87 
> +instructions, the compiler must save and restore the corresponding 
> +state.

Similar problems here.

 From the further discussion that follows, it appears that you can use the 
"interrupt" attribute on exception handlers as well, but the paragraph above 
only mentions interrupt handlers.

> +
> +Since the direction flag in the FLAGS register in interrupt handlers 
> +is undetermined, cld instruction must be emitted in function prologue 
> +if rep string instructions are used in interrupt handler or interrupt 
> +handler isn't a leaf function.

Again, this sounds like implementor-speak, and there are grammatical errors 
(noun/verb disagreement, missing articles).  Do users of this attribute need to 
know what instructions the compiler is emitting?  We already say above that it 
causes GCC to generate suitable entry and exit sequences.

> +
> +Any interruptible-without-stack-switch code must be compiled with
> +@option{-mno-red-zone} since interrupt handlers can and will, because
> +of the hardware design, touch the red zone.
> +
> +An interrupt handler must be declared with a mandatory pointer
> +argument:
> +
> +@smallexample
> +struct interrupt_frame;
> +
> +__attribute__ ((interrupt))
> +void
> +f (struct interrupt_frame *frame)
> +@{
> +@}
> +@end smallexample
> +
> +and user must properly define the structure the pointer pointing to.

"user" == "you" in the GCC user manual.

How do you "properly define" this structure, or is that a stupid 
question?  (I know very little about x86, maybe this is obvious to experts.)

> +
> +The exception handler is very similar to the interrupt handler with
> +a different mandatory function signature:
> +
> +@smallexample
> +#ifdef __x86_64__
> +typedef unsigned long long int uword_t;
> +#else
> +typedef unsigned int uword_t;
> +#endif
> +
> +struct interrupt_frame;
> +
> +__attribute__ ((interrupt))
> +void
&

Re: [PATCH] x86 interrupt attribute patch [2/2]

2016-05-10 Thread Sandra Loosemore

On 04/20/2016 07:42 AM, Koval, Julia wrote:

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index a5a8b23..82de5bf 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -5263,6 +5263,83 @@ On x86-32 targets, the @code{stdcall} attribute causes 
the compiler to
 assume that the called function pops off the stack space used to
 pass arguments, unless it takes a variable number of arguments.

+@item no_caller_saved_registers
+@cindex @code{no_caller_saved_registers} function attribute, x86
+Use this attribute to indicate that the specified function has no
+caller-saved registers.  That is, all registers are callee-saved.
+The compiler generates proper function entry and exit sequences to
+save and restore any modified registers, except for the EFLAGS
+register.  If the compiler generates MPX, SSE, MMX or x87 instructions
+in a function with @code{no_caller_saved_registers} attribute or
+functions called from a function with @code{no_caller_saved_registers}
+attribute may contain MPX, SSE, MMX or x87 instructions, the compiler
+must save and restore the corresponding state.


I cannot parse the last sentence in this paragraph.  How can the 
compiler know whether called functions may contain those instructions? 
Plus, talking about what the compiler must do seems too 
implementor-speaky for user documentation.  Maybe you mean something 
like "The compiler also saves and restores state associated with MPX, 
SSE, MMX, and x87 instructions." ?


I also think the documentation needs to give some hint about why a user 
would want to put this attribute on a function.



+
+@item interrupt
+@cindex @code{interrupt} function attribute, x86
+Use this attribute to indicate that the specified function is an
+interrupt handler.  The compiler generates function
+entry and exit sequences suitable for use in an interrupt handler when
+this attribute is present.  The @code{IRET} instruction, instead of the
+@code{RET} instruction, is used to return from interrupt handlers.  All
+registers, except for the EFLAGS register which is restored by the
+@code{IRET} instruction, are preserved by the compiler.  If the
+compiler generates MPX, SSE, MMX or x87 instructions in an interrupt
+handler, or functions called from an interrupt handler may contain MPX,
+SSE, MMX or x87 instructions, the compiler must save and restore the
+corresponding state.


Similar problems here.

From the further discussion that follows, it appears that you can use 
the "interrupt" attribute on exception handlers as well, but the 
paragraph above only mentions interrupt handlers.



+
+Since the direction flag in the FLAGS register in interrupt handlers
+is undetermined, cld instruction must be emitted in function prologue
+if rep string instructions are used in interrupt handler or interrupt
+handler isn't a leaf function.


Again, this sounds like implementor-speak, and there are grammatical 
errors (noun/verb disagreement, missing articles).  Do users of this 
attribute need to know what instructions the compiler is emitting?  We 
already say above that it causes GCC to generate suitable entry and exit 
sequences.



+
+Any interruptible-without-stack-switch code must be compiled with
+@option{-mno-red-zone} since interrupt handlers can and will, because
+of the hardware design, touch the red zone.
+
+An interrupt handler must be declared with a mandatory pointer
+argument:
+
+@smallexample
+struct interrupt_frame;
+
+__attribute__ ((interrupt))
+void
+f (struct interrupt_frame *frame)
+@{
+@}
+@end smallexample
+
+and user must properly define the structure the pointer pointing to.


"user" == "you" in the GCC user manual.

How do you "properly define" this structure, or is that a stupid 
question?  (I know very little about x86, maybe this is obvious to experts.)



+
+The exception handler is very similar to the interrupt handler with
+a different mandatory function signature:
+
+@smallexample
+#ifdef __x86_64__
+typedef unsigned long long int uword_t;
+#else
+typedef unsigned int uword_t;
+#endif
+
+struct interrupt_frame;
+
+__attribute__ ((interrupt))
+void
+f (struct interrupt_frame *frame, uword_t error_code)
+@{
+  ...
+@}
+@end smallexample
+
+and compiler pops the error code off the stack before the @code{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.


I think you need to move this information above the examples, and 
explain from the start that there are two flavors of handlers that have 
different information pushed on the stack by the hardware.



+
 @item target (@var{options})
 @cindex @code{target} function attribute
 As discussed in @ref{Common Function Attributes}, this attribute


-Sandra



Re: [PATCH] x86 interrupt attribute patch [2/2]

2016-05-10 Thread Uros Bizjak
On Tue, May 10, 2016 at 4:54 PM, Koval, Julia <julia.ko...@intel.com> wrote:
> Gentle ping.
>
> -Original Message-
> From: Koval, Julia
> Sent: Wednesday, April 20, 2016 4:42 PM
> To: gcc-patches@gcc.gnu.org
> Cc: Lu, Hongjiu <hongjiu...@intel.com>; vaalfr...@gmail.com; 
> ubiz...@gmail.com; l...@redhat.com; Zamyatin, Igor <igor.zamya...@intel.com>
> Subject: [PATCH] x86 interrupt attribute patch [2/2]
>
> Hi,
> Here is the new version of interrupt attribute patch. Bootstraped/regtested 
> for Linux/x86_64. Ok for trunk?
>
> 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'
> instruction.
>
> The design goals of interrupt and exception handlers for x86 processors
> are:
>
> 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.
> If the compiler generates MPX, SSE, MMX or x87 instructions in an interrupt 
> or exception handler, or functions called from an interrupt or exception 
> handler may contain MPX, SSE, MMX or x87 instructions, the compiler must save 
> and restore the corresponding state.
>
> Since the direction flag in the FLAGS register in interrupt (exception) 
> handlers is undetermined, cld instruction must be emitted in function 
> prologue if rep string instructions are used in interrupt (exception) handler 
> or interrupt (exception) handler isn't a leaf function.
>
> 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 pointer argument:
>
> struct interrupt_frame;
>
> __attribute__ ((interrupt))
> void
> f (struct interrupt_frame *frame)
> {
> ...
> }
>
> and user must properly define the structure the pointer pointing to.
>
> 2. exception handler:
>
> The exception handler is very similar to the interrupt handler with a 
> different mandatory function signature:
>
> typedef unsigned int uword_t __attribute__ ((mode (__word__)));
>
> struct interrupt_frame;
>
> __attribute__ ((interrupt))
> void
> f (struct interrupt_frame *frame, uword_t error_code) { ...
> }
>
> and compiler pops the 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.
>
> 'no_caller_saved_registers' attribute
>
> Use this attribute to indicate that the specified function has no 
> caller-saved registers.  That is, all registers are callee-saved.
> The compiler generates proper function entry and exit sequences to save and 
> restore any modified registers, except for the EFLAGS register.
> If the compiler generates MPX, SSE, MMX or x87 instructions in a function 
> with 'no_caller_saved_registers' attribute or functions called from a 
> function with 'no_caller_saved_registers' attribute may contain MPX, SSE, MMX 
> or x87 instructions, the compiler must save and restore the corresponding 
> state.
>
> The user can call functions specified with 'no_caller_saved_registers'
> attribute from an interrupt handler without saving and restoring all call 
> clobbered registers.
>
> On x86, interrupt handlers are only called by processors which push interrupt 
> data onto stack at the address where the normal return address is.  Interrupt 
> handlers must access interrupt data via pointers so that they can update 
> interrupt data.
> gcc/
>
&

RE: [PATCH] x86 interrupt attribute patch [2/2]

2016-05-10 Thread Koval, Julia
Gentle ping.

-Original Message-
From: Koval, Julia 
Sent: Wednesday, April 20, 2016 4:42 PM
To: gcc-patches@gcc.gnu.org
Cc: Lu, Hongjiu <hongjiu...@intel.com>; vaalfr...@gmail.com; ubiz...@gmail.com; 
l...@redhat.com; Zamyatin, Igor <igor.zamya...@intel.com>
Subject: [PATCH] x86 interrupt attribute patch [2/2]

Hi,
Here is the new version of interrupt attribute patch. Bootstraped/regtested for 
Linux/x86_64. Ok for trunk?

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'
instruction.

The design goals of interrupt and exception handlers for x86 processors
are:

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.
If the compiler generates MPX, SSE, MMX or x87 instructions in an interrupt or 
exception handler, or functions called from an interrupt or exception handler 
may contain MPX, SSE, MMX or x87 instructions, the compiler must save and 
restore the corresponding state.

Since the direction flag in the FLAGS register in interrupt (exception) 
handlers is undetermined, cld instruction must be emitted in function prologue 
if rep string instructions are used in interrupt (exception) handler or 
interrupt (exception) handler isn't a leaf function.

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 pointer argument:

struct interrupt_frame;

__attribute__ ((interrupt))
void
f (struct interrupt_frame *frame)
{
...
}

and user must properly define the structure the pointer pointing to.

2. exception handler:

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

typedef unsigned int uword_t __attribute__ ((mode (__word__)));

struct interrupt_frame;

__attribute__ ((interrupt))
void
f (struct interrupt_frame *frame, uword_t error_code) { ...
}

and compiler pops the 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.

'no_caller_saved_registers' attribute

Use this attribute to indicate that the specified function has no caller-saved 
registers.  That is, all registers are callee-saved.
The compiler generates proper function entry and exit sequences to save and 
restore any modified registers, except for the EFLAGS register.
If the compiler generates MPX, SSE, MMX or x87 instructions in a function with 
'no_caller_saved_registers' attribute or functions called from a function with 
'no_caller_saved_registers' attribute may contain MPX, SSE, MMX or x87 
instructions, the compiler must save and restore the corresponding state.

The user can call functions specified with 'no_caller_saved_registers'
attribute from an interrupt handler without saving and restoring all call 
clobbered registers.

On x86, interrupt handlers are only called by processors which push interrupt 
data onto stack at the address where the normal return address is.  Interrupt 
handlers must access interrupt data via pointers so that they can update 
interrupt data.
gcc/

PR target/66960
PR target/67630
PR target/67634
PR target/67841
PR target/68037
PR target/68618
PR target/68661
PR target/69575
PR target/69596
PR target/69734
* config/i386/i386-protos.h (ix86_epilogue_uses): New prototype.
* config/i386/i386.c (ix86_conditional_register_usage): Preserve
all registers, except for function return registers if there are
no caller-saved registers.
(ix86_set_func_type): New function.
(ix86_set_current_function): Call ix86_set_func

[PATCH] x86 interrupt attribute patch [2/2]

2016-04-20 Thread Koval, Julia
Hi,
Here is the new version of interrupt attribute patch. Bootstraped/regtested for 
Linux/x86_64. Ok for trunk?

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'
instruction.

The design goals of interrupt and exception handlers for x86 processors
are:

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.
If the compiler generates MPX, SSE, MMX or x87 instructions in an interrupt
or exception handler, or functions called from an interrupt or exception
handler may contain MPX, SSE, MMX or x87 instructions, the compiler must
save and restore the corresponding state.

Since the direction flag in the FLAGS register in interrupt (exception)
handlers is undetermined, cld instruction must be emitted in function
prologue if rep string instructions are used in interrupt (exception)
handler or interrupt (exception) handler isn't a leaf function.

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 pointer argument:

struct interrupt_frame;

__attribute__ ((interrupt))
void
f (struct interrupt_frame *frame)
{
...
}

and user must properly define the structure the pointer pointing to.

2. exception handler:

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

typedef unsigned int uword_t __attribute__ ((mode (__word__)));

struct interrupt_frame;

__attribute__ ((interrupt))
void
f (struct interrupt_frame *frame, uword_t error_code)
{
...
}

and compiler pops the 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.

'no_caller_saved_registers' attribute

Use this attribute to indicate that the specified function has no
caller-saved registers.  That is, all registers are callee-saved.
The compiler generates proper function entry and exit sequences to
save and restore any modified registers, except for the EFLAGS register.
If the compiler generates MPX, SSE, MMX or x87 instructions in a function
with 'no_caller_saved_registers' attribute or functions called from a
function with 'no_caller_saved_registers' attribute may contain MPX,
SSE, MMX or x87 instructions, the compiler must save and restore the
corresponding state.

The user can call functions specified with 'no_caller_saved_registers'
attribute from an interrupt handler without saving and restoring all
call clobbered registers.

On x86, interrupt handlers are only called by processors which push
interrupt data onto stack at the address where the normal return address
is.  Interrupt handlers must access interrupt data via pointers so that
they can update interrupt data.
gcc/

PR target/66960
PR target/67630
PR target/67634
PR target/67841
PR target/68037
PR target/68618
PR target/68661
PR target/69575
PR target/69596
PR target/69734
* config/i386/i386-protos.h (ix86_epilogue_uses): New prototype.
* config/i386/i386.c (ix86_conditional_register_usage): Preserve
all registers, except for function return registers if there are
no caller-saved registers.
(ix86_set_func_type): New function.
(ix86_set_current_function): Call ix86_set_func_type to set
no_caller_saved_registers and func_type.  Call reinit_regs if
caller-saved registers are changed.  Don't allow MPX, SSE, MMX
nor x87 instructions in interrupt handler nor function with
no_caller_saved_registers attribute.
(ix86_function_ok_for_sibcall): Return false if there are no
caller-saved registers.