Preserve SSE state in interrupt handler

Hello folks

Clang has support for "interrupt" attribute at x86 platform. It is one
of the features needed for os development. And it works great in the
most cases.

There is one thing that I would like to clarify. What if the interrupt
handler uses SSE instructions? It is expected that a handler preserves
exact execution state. And the CPU state includes SSE/FPU as well.

I compiled a handler with SSE using clang 4.0. I see that %xmm
registers are saved to stack one-by-one:

  movaps %xmm0, %(rsp)
  movaps %xmm1, %10(rsp)
  movaps %xmm2, %20(rsp)
.....

But this code does not preserve SSE register state like MXCSR. Without
it the handler might change the SSE state and it will affect task that
was executing during the interrupt handling.

I think the interrupt attribute at x86 should be enhanced:
* In addition to SSE data register, SSE state registers should be
preserved as well
* The same rule applied to FPU state if handler uses FPU
* Instead of iterating all registers would it make sense to use more
convenient FXSAVE instruction ("preserve FPU/SSE state")?

What do you think?

I don’t believe the interrupt attribute is intended for this purpose: certainly no mainstream operating systems use it. It is intended for people writing embedded systems that use x86, not for x86 systems using full-blown operating systems, which typically need much more control over how context switches happen as a result of interrupts.

David

Hi

I don’t believe the interrupt attribute is intended for this purpose: certainly no mainstream operating systems use it. It is intended for people writing embedded systems that use x86, not for x86 systems using full-blown operating systems, which typically need much more control over how context switches happen as a result of interrupts.

w.r.t. interrupt handling requirements there is not much difference
between embedded systems and full-blown OS. Both types of project
require correct, fast and easy-to-use interruption handling.

certainly no mainstream operating systems use it

The reason for this attribute low adoption is that the attribute is
not fully functional on both major compilers yet. Actually GCC added
this attribute to x86 only few months back (the fact Clang had this
feature implemented was one of the reasons why GCC finally added it).
We will have higher adoption of this feature once we have attribute
that handles full CPU state preservation correctly (i.e. saves SSE/FPU
state as well) with all major compilers.

Not really. In an embedded system, interrupt handlers are usually in a way similar to signals in userspace code. In an operating system, interrupts are a lot more complex and have deep interactions with the scheduler and the driver model. The exception frame must have a fixed layout that is specified by the kernel in question so that it can be context switched and the scheduler can handle preemption in the presence of lower-priority interrupts already on the stack.

This attribute was added at the request of Intel for their (now mostly cancelled) IoT parts, to make it easier to port code from microcontrollers. It was never intended for operating system use. Even on platforms where the interrupt attribute has been supported for 20 years, it is almost never used by complex operating systems for precisely the reasons outlined above.

David