Linux内核中浮点数的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13886338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:03:19  来源:igfitidea点击:

Use of floating point in the Linux kernel

linuxfloating-pointlinux-kernel

提问by NPE

I am reading Robert Love's "Linux Kernel Development", and I came across the following passage:

我正在阅读 Robert Love 的“Linux 内核开发”,我看到了以下段落:

No (Easy) Use of Floating Point

When a user-space process uses floating-point instructions, the kernel manages the transition from integer to floating point mode. What the kernel has to do when using floating-point instructions varies by architecture, but the kernel normally catches a trap and then initiates the transition from integer to floating point mode.

Unlike user-space, the kernel does not have the luxury of seamless support for floating point because it cannot easily trap itself. Using a floating point inside the kernel requires manually saving and restoring the floating point registers, among other possible chores. The short answer is: Don't do it!Except in the rare cases, no floating-point operations are in the kernel.

没有(容易)使用浮点数

当用户空间进程使用浮点指令时,内核管理从整数到浮点模式的转换。内核在使用浮点指令时必须做的事情因架构而异,但内核通常会捕获陷阱,然后启动从整数模式到浮点模式的转换。

与用户空间不同,内核没有对浮点的无缝支持,因为它不能轻易地自陷。在内核中使用浮点需要手动保存和恢复浮点寄存器,以及其他可能的杂务。简短的回答是:不要这样做!除了极少数情况外,内核中没有浮点运算。

I've never heard of these "integer" and "floating-point" modes. What exactly are they, and why are they needed? Does this distinction exist on mainstream hardware architectures (such as x86), or is it specific to some more exotic environments? What exactly does a transition from integer to floating point mode entail, both from the point of view of the process and the kernel?

我从未听说过这些“整数”和“浮点”模式。它们究竟是什么,为什么需要它们?这种区别是否存在于主流硬件架构(例如 x86)上,还是特定于一些更奇特的环境?从进程和内核的角度来看,从整数模式到浮点模式的转换到底意味着什么?

采纳答案by DigitalRoss

Because...

因为...

  • many programs don't use floating point or don't use it on any given time slice; and
  • saving the FPU registers and other FPU state takes time; therefore
  • 许多程序不使用浮点数或不在任何给定的时间片上使用它;
  • 保存 FPU 寄存器和其他 FPU 状态需要时间;所以

...an OS kernel may simply turn the FPU off.Presto, no state to save and restore, and therefore faster context-switching. (This is what modemeant, it just meant that the FPU was enabled.)

...操作系统内核可以简单地关闭 FPU。Presto,无需保存和恢复状态,因此上下文切换速度更快。(这就是模式的意思,它只是意味着启用了 FPU。)

If a program attempts an FPU op, the program will trap into the kernel, the kernel will turn the FPU on, restore any saved state that may already exist, and then return to re-execute the FPU op.

如果程序尝试执行 FPU op,程序将陷入内核,内核将打开 FPU,恢复可能已经存在的任何保存状态,然后返回以重新执行 FPU op。

At context switch time, it knows to actually go through the state save logic. (And then it may turn the FPU off again.)

在上下文切换时,它知道实际执行状态保存逻辑。(然后它可能会再次关闭 FPU。)

By the way, I believe the book's explanation for the reason kernels (and not just Linux) avoid FPU ops is ... not perfectly accurate.1

顺便说一句,我相信这本书对内核(而不仅仅是 Linux)避免 FPU 操作的原因的解释......并不完全准确。1

The kernel cantrap into itself and does so for many things. (Timers, page faults, device interrupts, others.) The real reason is that the kernel doesn't particularly needFPU ops and also needs to run on architectures without an FPU at all. Therefore, it simply avoids the complexity and runtime required to manage its own FPU context by not doing ops for which there are always other software solutions.

内核可以陷入自己的陷阱,并且在很多事情上都会这样做。(计时器、页面错误、设备中断等。)真正的原因是内核并不特别需要FPU 操作,而且还需要在根本没有 FPU 的架构上运行。因此,它通过不执行总是有其他软件解决方案的操作,简单地避免了管理自己的 FPU 上下文所需的复杂性和运行时间。

It's interesting to note how often the FPU state would have to be saved if the kernel wanted to use FP . . .every system call, every interrupt, every switch between kernel threads. Even if there was a need for occasional kernel FP,2it would probably be faster to do it in software.

有趣的是,如果内核想要使用 FP ,则必须多长时间保存一次 FPU 状态。. . 每个系统调用、每个中断、内核线程之间的每次切换。即使偶尔需要内核 FP,2在软件中完成它可能会更快。



1. That is, dead wrong.
2. There are a few cases I know about where kernel software contains a floating point arithmetic implementation.Some architectures implement traditional FPU ops in hardware but leave some complex IEEE FP operations to software. (Think: denormal arithmetic.)When some odd IEEE corner case happens they trap to software which contains a pedantically correct emulation of the ops that can trap.
1.也就是说,大错特错。
2. 关于内核软件在何处包含浮点算术实现,我知道有几个案例一些架构在硬件中实现传统的 FPU 操作,但将一些复杂的 IEEE FP 操作留给软件。(想想:非正规算术。)当一些奇怪的 IEEE 极端情况发生时,它们会陷入软件,其中包含可以陷阱的操作的迂腐正确的仿真。

回答by Hot Licks

With some kernel designs the floating-point registers are not saved when a "kernel" or "system" task is task-switched out. (This is because the FP registers are large and take both time and space to save.) So if you attempt to use FP the values will go "poof" randomly.

对于某些内核设计,当“内核”或“系统”任务被任务切换时,浮点寄存器不会被保存。(这是因为 FP 寄存器很大并且需要时间和空间来保存。)因此,如果您尝试使用 FP,这些值将随机“poof”。

In addition, some hardware floating-point schemes rely on the kernel to handle "oddball" situations (eg, zero division) via a trap, and the required trap mechanism may be at a higher "level" than the kernel task is currently running.

此外,一些硬件浮点方案依赖于内核通过陷阱处理“奇数”情况(例如,零除法),并且所需的陷阱机制可能处于比当前正在运行的内核任务更高的“级别”。

For these reasons (and a couple more) some hardware FP schemes will trap when you use an FP instruction for the first time in a task. If you're permitted to use FP then a floating-point flag is turned on in the task, if not, you're shot by the firing squad.

由于这些原因(以及更多原因),当您在任务中第一次使用 FP 指令时,一些硬件 FP 方案会陷入困境。如果您被允许使用 FP,则任务中会打开一个浮点标志,否则,您将被行刑队开枪射击。