What exactly is a trap?

49 Views Asked by At

From what I understand, there are software interrupts and hardware interrupts. Software interrupts include exception and trap, where a trap is called explicitly (such as in the case of systemcalls) and exceptions are called implicitly (such as in the case of division by 0, page fault, etc)

Am I right? Are traps only used for systemcalls?

All the explanations I've read seem to contradict each other.

1

There are 1 best solutions below

0
On

There is no consistent terminology. Different architectures/vendors use those words to mean different things.

For example, on Motorola 68000 traps are exceptions called by trap or trapv instructions. They are called explicitly, like you said, however they are still considered exceptions - because Motorola calls all interrupts "exceptions".

On x86, exceptions are the first 32 reserved interrupts - they can be implicitly called by the CPU. They are further divided into traps, faults and aborts:

  • Trap always happen at the end of instruction, next instruction address is saved. Returning from interrupt goes to the next instruction.
  • Fault happens at the beginning of the instruction, current instruction address is saved. Returning from interrupt tries to run the instruction again.
  • Abort is non-recoverable, you should not return from that interrupt.

On x86, any interrupt (including hardware and reserved ones) can be explicitly called in software. The syscall instruction is not considered an interrupt at all.