A brief discussion on the Linux kernel's support for floating-point operations

A brief discussion on the Linux kernel's support for floating-point operations

Currently, most CPUs support floating-point units (FPUs). FPUs are placed outside the processor core as a separate coprocessor. However, for embedded processors, floating-point operations are rarely used, so some embedded processors remove the floating-point coprocessor.

X86 processors generally have FPU. However, the ARM PPC MIPS processor does not have an FPU.

How the Linux kernel handles floating-point operations can be discussed in terms of processors with and without FPU.

(The following is a summary of my personal knowledge. I haven’t done much research. I hope you can point out any mistakes and learn together.)

1. For processors with FPU

1 For the Linux kernel, the kernel itself is compiled by default with the -msoft-float option, and is compiled as a soft floating-point program by default. Soft floating-point means that the gcc compiler simulates floating-point operations (provided by the glibc library) and replaces floating-point operation codes with fixed-point operations.

For processors with FPU, we can remove the compilation option -msoft-float, usually in arch/xxx/Makefile. Compile the kernel as hard floating point, that is, let the processor's floating point instructions calculate floating point,

Hard floating-point operations are definitely more efficient than simulated fixed-point operations. (There are generally no floating-point operations in kernel code, so the efficiency is not greatly affected)

2 For apps running on the kernel, especially for graphics programs such as QT, which have a lot of floating-point operations, we can compile them directly because the processor supports floating-point operations and floating-point operation instructions.

2. For processors without FPU

1 For the Linux kernel, the -msoft-float option is used by default when compiling, and the program is compiled as a soft floating-point program by default. The Linux kernel compilation does not rely on linking any library, and the kernel implements the corresponding simulated floating-point ABI.

2 For apps running on the kernel, there are two ways to handle floating-point operations:

(1) The kernel simulates soft floating point.

The application is compiled directly using hard floating point (the compiler compiles into hard floating point program by default).

As for the kernel, the PPC MIPS processors I know of all have special floating-point operation exception handling. When the program encounters a floating-point instruction and cannot run the floating-point instruction, the hardware will generate a corresponding interrupt exception. The kernel floating-point exception handler performs soft floating-point simulation operations based on the instruction content, returns the operation result, and then restores execution to user space.

For ARM, I did not find any exception entry for floating-point calculations in its exception introduction, but the kernel also has support for its soft floating point.

When configuring the ARM Linux kernel, you should see the following configuration:

 menu "Floating point emulation"
 comment "At least one emulation must be selected"
 config FPE_NWFPE
  ...

This is used to configure the simulated floating point processor in the kernel.

How ARM implements support for exception simulation soft floating point, the specific implementation needs to be carefully read in the code when there is time, in arch/arm/nwfpe.

The advantage of this method is that the application does not need to be recompiled, and the floating-point simulation only needs to be turned on in the kernel, which is very convenient to use.

However, the disadvantages are also obvious. Each floating-point operation triggers an interrupt exception, switching between user space and kernel space, and the execution efficiency is too low.

(2) Recompile the app using soft floating point

This can avoid the above problems. When compiling the app, you need to connect to the glibc library. Use --msoft-float to use glibc's simulated floating point instead of fixed-point operations. The advantage of this is that the running performance will be better.

But the disadvantage is that the ABI used may change due to the use of different compilation options. If a library or application does not use the same compilation options (different ABI),

Unexpected situations may occur when the system is running, and it may even crash.

According to the recent debugging records of a PPC processor, the kernel started normally and entered the console but died at a certain address. There were many floating-point operations in the user space. After querying the IC, it was found that the FPU was removed and the processor floating-point exception was not enabled.

In this way, when encountering a floating-point instruction, the processor will not trigger an exception and will not know how to run the instruction.

Therefore, when porting the kernel, you must also understand whether the processor has an FPU. If the processor removes the FPU and the core does not perform corresponding processing (enabling floating-point exceptions), the results of the APP's floating-point instructions are unpredictable. In this case, you can use a soft floating-point tool chain to compile the APP.

Here’s a little thought:

For a processor, if there is a floating-point exception in the processor design (MIPS PPC both have it), it can also be connected to an FPU.

After connecting to the FPU, floating-point exceptions must be shielded in the processor core, otherwise floating-point operations will still produce floating-point exceptions, and the FPU will have no practical significance.

If there is no FPU, floating-point exceptions must be enabled in the processor core. Otherwise, it will be the same problem as the one I encountered above. The processor does not know how to run the floating-point instruction and the result is unpredictable.

The above brief discussion on Linux kernel's support for floating-point operations is all I have to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to install gcc and kernel-devel in Linux system
  • Use the interface provided by the kernel to print the process number (pid)
  • A brief discussion on how to print the function call stack in the Linux kernel
  • How to print the function name corresponding to the function pointer in linux kernel
  • Learn how to use NEON to accelerate algorithms in kernel state
  • Analysis of the problem of "Couldn't find hvm kernel for Ubuntu tree." when installing 64-bit Ubuntu under kvm command line in Ubuntu
  • CentOS7 upgrade kernel kernel5.0 version
  • Solution to the conflict between Linux kernel and SVN versions

<<:  WeChat applet + ECharts to achieve dynamic refresh process record

>>:  Tutorial on installing and changing the root password of MySQL 5.7.20 decompressed version

Recommend

Talk about the understanding of CSS attribute margin

1.What is margin? Margin is used to control the sp...

How to use & and nohup in the background of Linux

When we work in a terminal or console, we may not...

Advanced and summary of commonly used sql statements in MySQL database

This article uses examples to describe the common...

Discussion on the Issues of Image Button Submission and Form Repeated Submission

In many cases, in order to beautify the form, the ...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...

Function overloading in TypeScript

Table of contents 1. Function signature 2. Functi...

Configure Java development environment in Ubuntu 20.04 LTS

Download the Java Development Kit jdk The downloa...

Use html-webpack-plugin' to generate HTML page plugin in memory

When we package the webpackjs file, we introduce ...

Nine advanced methods for deduplicating JS arrays (proven and effective)

Preface The general methods are not listed here, ...

Detailed explanation of Docker container network port configuration process

Exposing network ports In fact, there are two par...

A detailed introduction to deploying RabbitMQ environment with docker

Prerequisites: Docker is already installed 1. Fin...

How to choose the right index in MySQL

Let’s take a look at a chestnut first EXPLAIN sel...

Detailed installation and configuration tutorial of MySQL 5.7 under Win10

1. Unzip MySQL 5.7 2. Create a new configuration ...

What are the drawbacks of deploying the database in a Docker container?

Preface Docker has been very popular in the past ...

Interpreting MySQL client and server protocols

Table of contents MySQL Client/Server Protocol If...