Bug 67552 - [meta-bug] x86 interrupt attribute
Summary: [meta-bug] x86 interrupt attribute
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: other (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: 7.0
Assignee: Not yet assigned to anyone
URL:
Keywords: meta-bug
Depends on: 69749 66960 67630 67634 67648 67698 67841 67850 67855 68037 69575 69596 69734
Blocks:
  Show dependency treegraph
 
Reported: 2015-09-11 15:39 UTC by H.J. Lu
Modified: 2017-07-18 15:13 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-09-14 00:00:00


Attachments
A patch to remove railing whitespaces in interrupt-switch-abi.c (418 bytes, patch)
2015-09-11 15:41 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2015-09-11 15:39:58 UTC
This meta bug covers all interrupt attribute issues on jkoval/interrupt/master
branch in GCC git repo.
Comment 1 H.J. Lu 2015-09-11 15:41:24 UTC
Created attachment 36324 [details]
A patch to remove railing whitespaces in interrupt-switch-abi.c
Comment 2 H.J. Lu 2015-09-11 15:45:58 UTC
Red zone isn't supported in interrupt handler:

'interrupt'
     Use this attribute to indicate that the specified void function
     without arguments 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 'IRET' instruction,
     instead of the 'RET' instruction, is used to return from interrupt
     handlers.  All registers, except for the EFLAGS register which is
     restored by the 'IRET' instruction, are preserved by the compiler.
     The red zone isn't supported in an interrupt handler; that is an
     interrupt handler can't access stack beyond the current stack
     pointer.

It is wrong to do

-  if (crtl->args.pops_args && crtl->args.size)
+  if (ix86_is_interrupt_p ())
+    {
+      if (ix86_using_red_zone ())
+          emit_insn (gen_adddi3 (
+                   gen_rtx_REG (DImode, SP_REG),
+                   gen_rtx_REG (DImode, SP_REG),
+                   GEN_INT (128)));

GCC should assume that red zone isn't used in interrupt handler.
Comment 3 H.J. Lu 2015-09-11 17:43:25 UTC
X87 instructions should be disallowed in interrupt handler:

[hjl@gnu-6 interrupt-1]$ cat f.i
extern long double y, x;

void
__attribute__((interrupt))
fn1 (void)
{
  x += y;
}
[hjl@gnu-6 interrupt-1]$ make f.s
/export/build/gnu/gcc-5/build-x86_64-linux/gcc/xgcc -B/export/build/gnu/gcc-5/build-x86_64-linux/gcc/ -O2 -S -o f.s f.i
[hjl@gnu-6 interrupt-1]$ cat f.s
	.file	"f.i"
	.section	.text.unlikely,"ax",@progbits
.LCOLDB1:
	.text
.LHOTB1:
	.p2align 4,,15
	.globl	fn1
	.type	fn1, @function
fn1:
.LFB0:
	.cfi_startproc
	fldt	y(%rip)
	addq	$-128, %rsp
	fldt	x(%rip)
	faddp	%st, %st(1)
	fstpt	x(%rip)
	ret
	.cfi_endproc
.LFE0:
	.size	fn1, .-fn1
	.section	.text.unlikely
.LCOLDE1:
	.text
.LHOTE1:
	.ident	"GCC: (GNU) 5.2.1 20150911"
	.section	.note.GNU-stack,"",@progbits
[hjl@gnu-6 interrupt-1]$
Comment 4 H.J. Lu 2015-09-11 17:44:11 UTC
MMX instructions should be disallowed in interrupt handler:

[hjl@gnu-6 interrupt-1]$ cat m.i 
typedef short __v4hi __attribute__ ((__vector_size__ (8)));
typedef int __m64 __attribute__ ((__vector_size__ (8), __may_alias__));

extern __m64 y, x;

void
__attribute__((interrupt))
fn1 (void)
{
  x = (__m64) __builtin_ia32_packsswb ((__v4hi) x, (__v4hi) y);
}
[hjl@gnu-6 interrupt-1]$ make m.s
/export/build/gnu/gcc-5/build-x86_64-linux/gcc/xgcc -B/export/build/gnu/gcc-5/build-x86_64-linux/gcc/ -O2 -S -o m.s m.i
[hjl@gnu-6 interrupt-1]$ cat m.s
	.file	"m.i"
	.section	.text.unlikely,"ax",@progbits
.LCOLDB0:
	.text
.LHOTB0:
	.p2align 4,,15
	.globl	fn1
	.type	fn1, @function
fn1:
.LFB0:
	.cfi_startproc
	movq	x(%rip), %mm0
	addq	$-128, %rsp
	packsswb	y(%rip), %mm0
	movq	%mm0, x(%rip)
	ret
	.cfi_endproc
.LFE0:
	.size	fn1, .-fn1
	.section	.text.unlikely
.LCOLDE0:
	.text
.LHOTE0:
	.ident	"GCC: (GNU) 5.2.1 20150911"
	.section	.note.GNU-stack,"",@progbits
[hjl@gnu-6 interrupt-1]$
Comment 5 Julia Koval 2015-09-14 09:26:33 UTC
Sorry, I don't understand why we shouldn't preserve the red zone. The function "foo", executing before the interrupt was called, used its red zone. If the interrupt does not adjust the stack pointer, who control that "foo" function's red zone would not be overwritten?
Comment 6 H.J. Lu 2015-09-14 11:25:26 UTC
(In reply to Yulia Koval from comment #5)
> Sorry, I don't understand why we shouldn't preserve the red zone. The
> function "foo", executing before the interrupt was called, used its red
> zone. If the interrupt does not adjust the stack pointer, who control that
> "foo" function's red zone would not be overwritten?

Please take a look at Exception- or Interrupt-Handler Procedures in
Intel64 and IA-32 Architectures Software Developer’s Manual vol 3.
There is no red zone on stack of interrupt handler, which is controlled
by processor, independent of any calling conventions.
Comment 7 H.J. Lu 2015-09-18 12:48:41 UTC
I think we should place

 if (current_function_decl && ix86_is_interrupt_p ())
    {
      for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
        {
          if (!STACK_REGNO_P (i) && !MMX_REGNO_P (i))
            {
              if (i == BP_REG || i == SP_REG)
                continue;
              if (i >= ARGP_REG && i <= FRAME_REG)
                continue;
              call_used_regs[i] = 0;
              fixed_regs[i] = 0;
            }
        }
    }

at the beginning of ix86_conditional_register_usage.
Comment 8 H.J. Lu 2017-07-18 15:13:26 UTC
Implemented for GCC 7.