What is the difference between Trap and Interrupt?

X86Operating SystemKernelInterruptCpu Architecture

X86 Problem Overview


What is the difference between Trap and Interrupt?

If the terminology is different for different systems, then what do they mean on x86?

X86 Solutions


Solution 1 - X86

A trap is an exception in a user process. It's caused by division by zero or invalid memory access. It's also the usual way to invoke a kernel routine (a system call) because those run with a higher priority than user code. Handling is synchronous (so the user code is suspended and continues afterwards). In a sense they are "active" - most of the time, the code expects the trap to happen and relies on this fact.

An interrupt is something generated by the hardware (devices like the hard disk, graphics card, I/O ports, etc). These are asynchronous (i.e. they don't happen at predictable places in the user code) or "passive" since the interrupt handler has to wait for them to happen eventually.

You can also see a trap as a kind of CPU-internal interrupt since the handler for trap handler looks like an interrupt handler (registers and stack pointers are saved, there is a context switch, execution can resume in some cases where it left off).

Solution 2 - X86

Traps and interrupts are closely related. Traps are a type of exception, and exceptions are similar to interrupts.

Intel x86 defines two overlapping categories, vectored events (interrupts vs exceptions), and exception classes (faults vs traps vs aborts).

All of the quotes in this post are from the April 2016 version of the Intel Software Developer Manual. For the (definitive and complex) x86 perspective, I recommend reading the SDM's chapter on Interrupt and Exception handling.

Vectored Events

Vectored Events (interrupts and exceptions) cause the processor to jump into an interrupt handler after saving much of the processor's state (enough such that execution can continue from that point later).

Exceptions and interrupts have an ID, called a vector, that determines which interrupt handler the processor jumps to. Interrupt handlers are described within the Interrupt Descriptor Table.

Interrupts

> Interrupts occur at random times during the execution of a program, in response to signals from hardware. System hardware uses > interrupts to handle events external to the processor, such as > requests to service peripheral devices. Software can also generate > interrupts by executing the INT n instruction.

Exceptions

> Exceptions occur when the processor detects an error condition while > executing an instruction, such as division by zero. The processor > detects a variety of error conditions including protection violations, > page faults, and internal machine faults.

Exception Classifications

> Exceptions are classified as faults, traps, or aborts depending on the way they are reported and whether the instruction > that caused the exception can be restarted without loss of program or > task continuity.

Summary: traps increment the instruction pointer, faults do not, and aborts 'explode'.

Trap

> A trap is an exception that is reported immediately following the > execution of the trapping instruction. Traps allow execution of a > program or task to be continued without loss of program continuity. > The return address for the trap handler points to the instruction to > be executed after the trapping instruction.

Fault

> A fault is an exception that can generally be corrected and that, > once corrected, allows the program to be restarted with no loss of > continuity. When a fault is reported, the processor restores the > machine state to the state prior to the beginning of execution of the > faulting instruction. The return address (saved contents of the CS and > EIP registers) for the fault handler points to the faulting > instruction, rather than to the instruction following the faulting > instruction.

Example: A page fault is often recoverable. A piece of an application's address space may have been swapped out to disk from ram. The application will trigger a page fault when it tries to access memory that was swapped out. The kernel can pull that memory from disk to ram, and hand control back to the application. The application will continue where it left off (at the faulting instruction that was accessing swapped out memory), but this time the memory access should succeed without faulting.

An illegal-instruction fault handler that emulates floating-point or other missing instructions would have to manually increment the return address to get the trap-like behaviour it needs, after seeing if the faulting instruction was one it could handle. x86 #UD is a "fault", not a "trap". (The handler would need a pointer to the faulting instruction to figure out which instruction it was.)

Abort

> An abort is an exception that does not always report the precise > location of the instruction causing the exception and does not allow a > restart of the program or task that caused the exception. Aborts are > used to report severe errors, such as hardware errors and inconsistent > or illegal values in system tables.

Edge Cases

Software invoked interrupts (triggered by the INT instruction) behave in a trap-like manner. The instruction completes before the processor saves its state and jumps to the interrupt handler.

Solution 3 - X86

Generally speaking, terms like exceptions, faults, aborts, Traps, and Interrupts all mean the same thing and are called "Interrupts".

Coming to the difference between Trap and Interrupt:

> Trap: Is a programmer initiated and expected transfer of control to a special handler routine. (For ex: 80x86 INT instruction is a good example)

Where as

> Interrupt(Hardware): Is a program control interruption based on an external hardware event external to the CPU (For ex: Pressing a key on the keyboard or a time out on a timer chip)

Solution 4 - X86

A trap is a special kind of interrupt which is commonly referred to as a software interrupt. An interrupt is a more general term which covers both hardware interrupts (interrupts from hardware devices) and software interrupts (interrupts from software, such as traps).

Solution 5 - X86

A trap is called by code like programs and used e. g. to call OS routines (i. e. normally synchronous). An interrupt is called by events (many times hardware, like the network card having received data, or the CPU timer), and - as the name suggests - interrupts the normal control flow, as the CPU has to switch to the driver routine to handle the event.

Solution 6 - X86

I think Traps are caused by the execution of current instruction and thus they are called as synchronous events. where as interrupts are caused by an independent instruction that is running in the processor which are related to external events and thus are known as asynchronous ones.

Solution 7 - X86

Interrupts are hardware interrupts, while traps are software-invoked interrupts. Occurrences of hardware interrupts usually disable other hardware interrupts, but this is not true for traps. If you need to disallow hardware interrupts until a trap is served, you need to explicitly clear the interrupt flag. And usually the interrupt flag on the computer affects (hardware) interrupts as opposed to traps. This means that clearing this flag will not prevent traps. Unlike traps, interrupts should preserve the previous state of the CPU.

Solution 8 - X86

An interrupt is a hardware-generated change-of-flow within the system. An interrupt handler is summoned to deal with the cause of the interrupt; control is then returned to the interrupted context and instruction. A trap is a software-generated interrupt. An interrupt can be used to signal the completion of an I/O to obviate the need for device polling. A trap can be used to call operating system routines or to catch arithmetic errors.

Solution 9 - X86

A trap is a software interrupt.If you write a program in which you declare a variable having divide by zero value then it is treated as a trap.Whenever you run this program it will throw same error at the same time.System call is a special version of trap in which a program asks os for its required service. In case of interrupt(a general word for hardware interrupts)like an i/o error,the cpu is interrupted at random time and off course it is not the fault of our programmers.It is the hardware that brings them up.

Solution 10 - X86

A Trap can be identified as a transfer of control, which is initiated by the programmer. The term Trap is used interchangeably with the term Exception (which is an automatically occurring software interrupt). But some may argue that a trap is simply a special subroutine call. So they fall in to the category of software-invoked interrupts. For example, in 80×86 machines, a programmer can use the int instruction to initiate a trap. Because a trap is always unconditional the control will always be transferred to the subroutine associated with the trap. The exact instruction, which invokes the routine for handling the trap is easily identified because an explicit instruction is used to specify a trap. Trap Vs Interrupt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - X86Aaron DigullaView Answer on Stackoverflow
Solution 2 - X86ruthafjordView Answer on Stackoverflow
Solution 3 - X86ChesharView Answer on Stackoverflow
Solution 4 - X86Paul RView Answer on Stackoverflow
Solution 5 - X86FrankView Answer on Stackoverflow
Solution 6 - X86chetan pawarView Answer on Stackoverflow
Solution 7 - X86Anamik SarvaiyaView Answer on Stackoverflow
Solution 8 - X86Dinesh ReddyView Answer on Stackoverflow
Solution 9 - X86Sanketssj5View Answer on Stackoverflow
Solution 10 - X86Exploring BitsView Answer on Stackoverflow