views:

144

answers:

1

Where in Linux would you look to find out what registers are saved on a context switch? I'm wondering, for example, if it is safe to use FP or vector registers in kernel-mode driver code (mostly interested in x86-64 and ARM, but I'm hoping for an architecture-independent answer).

+3  A: 

Since no one seems to have answered this, let me venture.

Take a look at the _math_restore_cpu and __unlazy_fpu methods.

You can find them here:

The x86 like processors have separate instructions for saving (fnsave) and restore (frstor) FPU state and so it looks like the OS is burdened with saving/restoring them.

I presume unless the FPU unit has been used by the usermode process, linux context switch will not save it for you.

So you need to do it yourself (in your driver) to be sure. You can use kernel_fpu_begin/end to do it in your driver, but is generally not a good idea.

Why it is not a good idea? From Linus himself: http://lkml.indiana.edu/hypermail/linux/kernel/0405.3/1620.html

Quoted:

You can do it "safely" on x86 using

kernel_fpu_begin(); ... kernel_fpu_end();

and make sure that all the FP stuff is in between those two things, and that you don't do anything that might fault or sleep.

The kernel_fpu_xxx() macros make sure that preemption is turned off etc, so the above should always be safe.

Even then, of course, using FP in the kernel assumes that you actually have an FPU, of course. The in-kernel FP emulation package is not supposed to work with kernel FP instructions.

Oh, and since the kernel doesn't link with libc, you can't use anything even remotely fancy. It all has to be stuff that gcc can do in-line, without any function calls.

In other words: the rule is that you really shouldn't use FP in the kernel. There are ways to do it, but they tend to be for some real special cases, notably for doing MMX/XMM work. Ie the only "proper" FPU user is actually the RAID checksumming MMX stuff.

Linus

In any case, do you really want to rely on Intel's floating point unit? http://en.wikipedia.org/wiki/Pentium_FDIV_bug (just kidding :-)).

Moron