Complete Linux Kernel Analysis --- math_emulate.c Program

Source: Internet
Author: User

All functions in the math_emulate.c program can be divided into three parts: the first class is the interface function math_emulate () of the exception handler that does not exist on the device, only this function; the second type is the main function do_emu () for floating point instruction simulation and processing, and there is only one function. In addition, all the functions are simulation-assisted functions, including the functions in other C language programs.
In a PC that does not contain a 80387 coprocessor chip, if the simulation mark EM = 1 is set in CR0 during kernel initialization, when the CPU encounters a floating point instruction, it will cause the CPU to interrupt int 7 abnormally and call the math_emulate (long ___ false) function at line 1 of the program during the interrupt processing.

In the math_emulate () function, if it is determined that the current process has not used the simulated co-processing operation, the simulation 80387 control Word, state Word and Tag Word will be used) initialize, set all 6 types of coprocessor exception shielding bits in the control word, and reset the status word and Feature Word. Then the main function do_emu () is called for simulation processing (). The parameter used is the return address pointer that calls the math_emulate () function during the Interrupt Processing of the info structure below. The info structure is actually a structure consisting of some data that is gradually imported into the stack after the int7 interrupt is generated from the CPU. Therefore, it is basically the same as the distribution of data in the kernel stack when the system calls it. For more information, see the "include/linux/math_emu.h" and "kernel/sys_call.s" sections.


The do_emu () function (line 1) first checks whether a simulated internal coprocessor exception has occurred Based on the status word. If yes, set the busy bit B (bit 15) of the status word; otherwise, reset the busy bit B. Then obtain the two-byte floating point instruction code that generates a coprocessor exception from the EIP field in the info structure above, and shield the same ESC code (Binary 11011) in each floating point instruction code) after the bit, perform software simulation on the specific floating point instruction based on the code value. To facilitate processing, the function uses five switch statements for processing based on five types of floating point instruction codes. For example, the first switch statement (line 1) is used to process floating point commands that do not involve addressing memory operands. The last two switch statements (419th and 432 rows) are used to process the memory-related commands of the operands. For the next type of instruction, the basic process of its processing is to first obtain the valid address of the memory operand according to the addressing mode byte in the instruction code, then read the corresponding data (integer, real number, or BCD code value) from the valid address ). Then, convert the read value to the temporary real number format used for internal processing of 80387. After calculation, convert the value in the temporary real number format to the original data type and save it to the user data zone.

In addition, when simulating a floating point command, if it finds that the floating point command is invalid, the program immediately calls the discard execution function _ math_abort (). This function sends a specified signal to the current execution process, and modifies the returned address (___ math_ret) of the call math_emulate () function during the suspension of the stack pointer esp ), and immediately return to the interrupted processing process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.