linux - 在 Linux 中的上下文切换中保存什么寄存器状态?

标签 linux linux-device-driver

在 Linux 中,您会在哪里查找上下文切换时保存的寄存器?例如,我想知道在内核模式驱动程序代码中使用 FP 或向量寄存器是否安全(主要对 x86-64 和 ARM 感兴趣,但我希望得到一个独立于体系结构的答案)。

最佳答案

由于似乎没有人回答过这个问题,所以让我冒昧一下。

看看 _math_restore_cpu 和 __unlazy_fpu 方法。

您可以在这里找到它们:

类似 x86 的处理器有单独的指令来保存 (fnsave) 和恢复 (frstor) FPU 状态,因此看起来操作系统负担着保存/恢复它们的负担。

我假设除非 FPU 单元已被用户模式进程使用,否则 linux 上下文切换不会为您保存它。

所以你需要自己做(在你的驱动程序中)才能确定。您可以在驱动程序中使用 kernel_fpu_begin/end 来执行此操作,但这通常不是一个好主意。

为什么这不是一个好主意?来自 Linus 本人:http://lkml.indiana.edu/hypermail/linux/kernel/0405.3/1620.html

引用:

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

无论如何,你真的要依赖英特尔的浮点单元吗? http://en.wikipedia.org/wiki/Pentium_FDIV_bug (开玩笑 :-))。

关于linux - 在 Linux 中的上下文切换中保存什么寄存器状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3006697/

相关文章:

c - 使用 C 创建子进程和父进程

c - 前向声明 C

linux-kernel - 什么是设备树中的 reg 属性?

linux - request_irq() 和 setup_irq() 的区别

Linux下获取相机照片的C程序

c - 如何在内核模块代码中包含 C 回溯?

Linux 设备驱动程序注册错误

ruby - 在 Ruby 中创建临时文件?

linux - 如何创建自定义 Oracle Linux 7u2 iso 镜像

linux - crontab 不执行复杂的 bash 脚本