14

Does the Linux kernel have its own SSE/AVX context?

I mean, from the point of view of a kernel module, can I use SSE/AVX instructions without worrying about user-space applications which may use it too? Or do I need to use some locks or do some context saving manually?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Ilya Matveychikov
  • 3,936
  • 2
  • 27
  • 42
  • 1
    Why the close vote I wonder ? This seems like a valid and useful question. – Paul R Sep 03 '13 at 12:30
  • Possible duplicate: http://stackoverflow.com/q/6227603/1401351. Especially see the resource linked in an answer to that question: Section 6.3 of http://www.agner.org/optimize/calling_conventions.pdf – Peter Sep 03 '13 at 13:27
  • Guys, I need to know is it possible to use AVX instructions from the LKM. No one of that links tells me that. And I don't looking for the calling conventions too. – Ilya Matveychikov Sep 03 '13 at 14:24
  • 1
    You didn't read the document? It says: "A device driver that needs to use vector registers must first save these registers by calling the function `kernel_fpu_begin()` and restore the registers by calling `kernel_fpu_end()` before returning or sleeping." There's more, like the fact that you can't use them at all in interrupt context. Short answer: Yes, you can. Long answer: But there are restrictions. Read the document! – Peter Sep 03 '13 at 14:51
  • @Peter That would be a perfectly fine answer. – CL. Sep 03 '13 at 14:58
  • @CL Thanks, I just expanded the comment slightly into an answer. – Peter Sep 03 '13 at 15:07

1 Answers1

15

The Linux kernel does not save FPU or vector registers by default to improve the speed of context switches. However, you can make use of them under certain circumstances.

Section 6.3 of http://agner.org/optimize/calling_conventions.pdf describes very well the use of vector registers in kernel mode, both in Windows and in Linux. Here's one important quote:

A device driver that needs to use vector registers must first save these registers by calling the function kernel_fpu_begin() and restore the registers by calling kernel_fpu_end() before returning or sleeping.

There's more, like the fact that you can't use them at all in interrupt context, so I suggest reading the entire section.

Peter
  • 14,559
  • 35
  • 55