Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a trap instruction do in an operating system?

While studying operating systems concepts, I saw the term trap instruction. "A TRAP instruction is performed to switch from user mode to kernel mode." I failed to understand what a trap instruction does.

like image 596
Borhan Rabbani Avatar asked Mar 10 '18 03:03

Borhan Rabbani


People also ask

Why is it called a trap instruction?

'Trap' because it intercepts an exception. When the CPU issues an "undefined operation" exception, it switches into supervisor mode and checks the low-order part of the instruction for an index into a jump table of routines which implement the desired behavior, for example, software floating point routines.

Does trap instruction execute in user mode?

The user-mode program places values in registers, or creates a stack frame with arguments, to indicate what specific service it requires from the operating system. The user-mode program then performs the trap instruction.


1 Answers

There are two ways to enter kernel mode:

  1. Interrupt
  2. Exception

When either occurs, the processor dispatches to the appropriate handler in its interrupt dispatch table (or similar mechanism). This table is defined by the operating system.

A trap is an exception where the instruction cannot be restarted. In contrast, a fault is an exception where the instruction can be restarted.

Every processor I am aware of has some instruction that explicitly causes an exception in order to get into kernel mode. Such instructions are used to implement operating system services.

like image 178
user3344003 Avatar answered Dec 18 '22 16:12

user3344003