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.
'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.
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.
There are two ways to enter kernel mode:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With