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

发布于 2024-09-04 12:16:30 字数 116 浏览 11 评论 0原文

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

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-09-11 12:16:30

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

看一下 _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

引用:

您可以使用 x86“安全”地完成此操作

kernel_fpu_begin(); ...
kernel_fpu_end();

并确保所有 FP 内容
介于这两件事之间,并且
你没有做任何事情
可能会出现故障或睡眠。

kernel_fpu_xxx() 宏确保
抢占已关闭等,所以
上述内容应该始终是安全的。

即使如此,当然,在
内核假设你实际上
当然,拥有 FPU。内核中的 FP 仿真包是
应该与内核 FP 指令一起使用。

哦,因为内核没有链接
有了 libc,你就不能使用任何东西
甚至有点幻想。这一切都必须是
gcc 可以在线执行的操作,
没有任何函数调用。

换句话说:规则是你
真的不应该在中使用 FP
核心。有一些方法可以做到这一点,但是
他们往往是为了某些真实
特殊情况,特别是做
MMX/XMM 工作。即唯一“合适的”FPU
用户实际上是RAID校验和
MMX 的东西。

莱纳斯

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

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 :-)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文