The kernel directory kernelmath contains the math coprocessor simulation code file, which contains 9 C language programs. See Table 11-1. This chapter is closely related to the specific hardware structure.
The kernel directory kernel/math contains the math coprocessor simulation code file, which contains 9 C language programs. See Table 11-1. The content of this chapter is closely related to the specific hardware structure. Therefore, you need to have a deep knowledge about the Intel CPU and coprocessor command code structure. Fortunately, this content is not closely related to kernel implementation, so skipping this chapter does not prevent readers from fully understanding the kernel implementation methods. However, if you can understand the content of this chapter, it will be of great help to implement system-level applications (such as compilation and disassembly programs) and compile coprocessor floating point processing programs.
11.1 Overall function description
Generally, three methods can be used to perform computation with a large amount of computing on a computer. One is to directly use common CPU commands for computing. Because CPU commands are a type of general commands, complex and large amounts of computation work requires the compilation of complex computation subprograms, generally, only professionals familiar with mathematics and computers can compile these subprograms. Another way is to configure a mathematical coprocessor chip for the CPU. The use of coprocessor chips can greatly simplify the difficulty of mathematical processing programming, and the computing speed and efficiency will also multiply, but additional hardware investment is required. Another method is to use a simulation program at the kernel level to simulate the computing functions of the coprocessor. This method may be the one with the lowest computing speed and efficiency, but it is as convenient as using a coprocessor for programmers to compile computing programs, in addition, the program can be run on a machine with a coprocessor without any changes to the program.
In the initial development of Linux 0.1x or even Linux 0.9x kernel, the mathematical coprocessor chip 80387 (or its compatible chip) is expensive and has always been a luxury in general PCs. Therefore, unless there is a large amount of scientific computing or a special need, generally 80387 chips are not installed in the PC. Although the current Intel processor has built-in mathematical coprocessor functional components, the current operating system does not need to include the coprocessor simulation program code, however, because the 80387 simulation program is fully built on the basis of simulating the processing structure of the 80387 chip and analyzing the instruction code structure, after learning this chapter, the reader can not only understand the 80387 coprocessor programming method, it is also helpful for compilation and disassembly processing.
If the 80386 PC does not contain the 80387 math coprocessor chip, when the CPU executes a coprocessor command, the "device does not exist" exception is interrupted 7. The exception handling code starts at row sys_call.s 158th. If the OS has set the EM bit of the CPU control register CR0 during initialization, then math_emulate () in math_emulate.c will be called () function to use the software to "interpret" Each coprocessor command.
Math_emulate.c, The Mathematical co-processor simulation program in Linux 0.12, fully simulates the 80387 chip to execute the co-processor command. Before processing a coprocessor command, the program will first establish a "soft" 80387 environment in the memory using data structures and other types, including imitating all 80387 internal stack-type accumulators ST [], control word register CWD, status word register SWD, and feature word TWD (TAG word) Register, then analyze the current coprocessor command operation code that causes the exception, and execute the corresponding mathematical simulation operation according to the specific operation code. Therefore, before describing the processing process of the math_emulate.c program, it is necessary to introduce the internal structure and basic working principle of 80387.
11.1.1 floating point data type
This section describes the floating point data types used by coprocessor. First, let's briefly review several representation methods of integer numbers, and then describe several standard representation methods of floating point numbers and the temporary real number representation method used in 80387 operations.
1. integer data type
For Intel 32-bit CPUs, there are three basic unsigned data types: byte, word, and double word ), there are 8, 16, and 32 bits, respectively. The expression of the unsigned number is very simple. each bit in the byte represents a binary number and has different weights according to its position. For example, an unsigned binary number 0b10001011 can be expressed:
U = 0b10001011 = 1x27 + 0x26 + 0x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20 = 139
It corresponds to the decimal number 139. One (20) with the smallest weight is generally referred to as the lowest valid Bit (LSB, Least Significant Bit), while the most valid Bit (27) is called the highest valid Bit (MSB, most Significant Bit ).