在Linux中的上下文切换器上保存了什么寄存器状态? [英] What register state is saved on a context switch in Linux?

查看:423
本文介绍了在Linux中的上下文切换器上保存了什么寄存器状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您希望在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).

推荐答案

由于似乎没有人回答这个问题,所以让我冒险.

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

看看_math_restore_cpu和__unlazy_fpu方法.

Take a look at the _math_restore_cpu and __unlazy_fpu methods.

您可以在这里找到它们:

You can find them here:

  • http://www.cs.fsu.edu/~baker/devices/lxr/http/ident?i=math_state_restore
  • http://www.cs.fsu.edu/~baker/devices/lxr/http/ident?i=__unlazy_fpu

类似x86的处理器具有用于保存(fnsave)和还原(frstor)FPU状态的单独指令,因此操作系统似乎负担了保存/还原它们的负担.

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.

我认为除非usermode进程已使用FPU单元,否则Linux上下文切换将不会为您保存它.

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

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

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.

  • http://www.cs.fsu.edu/~baker/devices/lxr/http/ident?i=kernel_fpu_begin
  • http://www.cs.fsu.edu/~baker/devices/lxr/http/ident?i=kernel_fpu_end

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

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

引用:

您可以使用xp在x86上安全"地进行操作

You can do it "safely" on x86 using

kernel_fpu_begin(); ... kernel_fpu_end();

kernel_fpu_begin(); ... kernel_fpu_end();

并确保所有 FP素材 介于这两件事之间,并且 你什么都不做 可能会出现故障或睡眠.

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.

kernel_fpu_xxx()宏确保 抢占被关闭等等,所以 以上应该永远是安全的.

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

当然,即使如此, 内核假设您实际上 当然,他们有FPU.内核内FP仿真程序包是 应该与内核FP指令一起使用.

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.

哦,由于内核没有链接 使用libc,您将无法使用任何东西 甚至遥不可及.一切都必须 gcc可以内联执行的操作 没有任何函数调用.

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.

换句话说:规则是你 真的不应该在 核心.有很多方法可以做到,但是 他们往往是一些 real 特殊情况,尤其是在做 MMX/XMM工作.即唯一的适当" FPU 用户实际上是RAID校验和 MMX内容.

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.

鼻窦

无论如何,您真的要依靠英特尔的浮点运算单元吗? http://en.wikipedia.org/wiki/Pentium_FDIV_bug (只是在开玩笑:-).

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

这篇关于在Linux中的上下文切换器上保存了什么寄存器状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆