How the processor handles the case of division by zero

It is curious what the processor / processor does in general, or, say, on the Intel and Linux processor, when it divides by null instruction. Also, how is the error relayed in the application so that it can register the error or notify the developer?

Thank!

+7
interrupt signals cpu operating-system interrupt-handling
May 26 '14 at 22:20
source share
2 answers

To answer in general terms, instead of going into gory details for Linux on x86_64 that probably obscure the concepts.

Processors typically throw an exception interrupt, such as dividing by zero or dereferencing a NULL pointer. These interrupts are captured, for example, when hardware interrupts interrupt the execution of the current program and return OS control, which then processes the event. Despite the fact that the actions are very dependent on the environment, usually the program can be terminated, all resources are freed (memory, open files) and optional core dumps / stacks created for debugging purposes in the developer's system.

The runtime can be configured so that an exception handler is called, perhaps the scripting language wants to catch an integer division by 0 or an integer overflow, and then throw a programming language exception or create diagnostics to help the programmer understand where and where why this happened. Another traditional opportunity is to receive a signal that can be captured by the application and processed, or lead to termination.

On some RISC processors, software traps in the OS will be launched to eliminate inconsistent data accesses, so reading memory will work, but with limited performance. In the past, traps were sometimes used to emulate specific instructions, but which were not implemented in hardware by a specific CPU model. I also saw hardware memory errors logged as the OS initiates the ECC memory recovery operation, although it is handled differently on x86.

System calls actually use the same mechanism to transition from a user space application to the OS kernel, which then processes the event, hence the generic term trap .

+10
May 26 '14 at 23:04
source share

Let me try to answer this a little differently. Each handler I worked with defines the structure of the interrupt vector. On Intel chips, this structure is called the interrupt dispatch table (IDT). The interrupt vector is an array of function pointers. Each entry in the array corresponds to a specific event (interruption or exception (error or trap)).

The operating system configures the functions (interrupt handler, exception handler) for each event. When division by zero occurs, it throws an exception. The CPU responds by calling the exception handler in the interrupt vector corresponding to division by zero. On Pentium, this is the first entry in the table.

+3
May 27 '14 at
source share



All Articles