在Linux内核中使用浮点 [英] Use of floating point in the Linux kernel

查看:223
本文介绍了在Linux内核中使用浮点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读罗伯特·洛夫(Robert Love)的Linux内核开发,我碰到以下的一段话:
$ b


)使用浮点

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

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


我从来没有听说过这些整数和浮点模式。他们究竟是什么,为什么他们需要?主流硬件体系结构(如x86)上是否存在这种差别,还是特定于某些更具异国情调的环境?从进程和内核的角度来看,从整数到浮点模式的转换究竟是什么?

解决方案

因为...




  • 许多程序不使用浮点数或不使用它给定时间片;保存FPU寄存器和其他FPU状态需要花费时间; 因此



... OS内核可以简单地关闭FPU。 Presto,没有状态保存和恢复,因此更快的上下文切换。 (这就是 mode 的意思,它只是意味着FPU已经启用了。)

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



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

顺便说一下,我相信这本书解释内核(而不仅仅是Linux)的原因是为了避免FPU操作内核可以陷入自身,并且对许多事情都这样做。 (定时器,页面错误,设备中断,其他)。真正的原因是内核不需要FPU操作,也需要在没有FPU的体系结构上运行。因此,它只是避免了复杂性和运行时间管理自己的FPU上下文,因为没有经常使用其他软件解决方案的操作。

有趣的是,如果内核想使用FP ,FPU状态将不得不被保存。 。每个系统调用,每个中断,每个内核线程之间的切换。即使偶尔需要内核FP,在软件中执行它也可能会更快。




1.也就是说,错误。

2.我知道一些情况内核软件包含浮点运算实现。一些体系结构在硬件上实现传统的FPU操作,但将一些复杂的IEEE FP操作留给软件。 (想一想:非正常运算)当一些奇怪的IEEE角落案例发生时,他们会陷入软件,其中包含一个可以捕获的操作的正常模拟。


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

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?

解决方案

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

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

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.

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

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

The kernel can trap into itself and does so for many things. (Timers, page faults, device interrupts, others.) The real reason is that the kernel doesn't particularly need FPU 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.

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,2 it would probably be faster to do it in software.


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.

这篇关于在Linux内核中使用浮点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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