Xijklm
Xijklm

Reputation: 13

Is TRAP instruction triggered only by a syscall?

Starting to learn OS, we have been asked what is TRAP operation and when it is being called.

Trying to base my answer by reading "Modern Operating Systems" \Tanenbaum (ch.1) I've noticed 2 concepts, and that is what I understood:

a) "TRAP" - the operation, which invokes after a user program make a syscall:

syscall ->

compiler push parameters to stack, and save syscall key in register ->

compiler call a library function ->

library function call TRAP ->

TRAP switch from user mode to kernel mode ->

OS call the relevant syscall handler.

b) "trap" - any situation of switching mode from user to kernel. Not necessarily by TRAP (but by exceptions like divide by 0, operating on floats.. and ... more... that I don't know.. )

so my Qs:

  1. TRAP and trap are 2 different (though close) concepts?

  2. can an OS switch from user to kernel mode (trap) in other ways but TRAP?

  3. (continuance to (2)) What triggers TRAP call beside to a syscall?

Upvotes: 1

Views: 825

Answers (1)

user3344003
user3344003

Reputation: 21647

In general, an instruction can cause two types of EXCEPTIONS: FAULTS and TRAPS. Exceptions and interrupts are the two ways to go into kernel mode on most processors.

If you divide by zero, try to execute a privileged instruction in user mode, or access unmapped memory, you get an exception, that the processor will classify as either a fault or a trap.

The difference is that an instruction that causes a fault can be restarted. An instruction that causes a trap cannot be.

When an process causes an exception, the hardware typically pushes register values on the stack, switches to kernel mode, then uses the dispatch table to invoke the handling routine for the exception.

Most processors have instructions for explicitly causing exceptions.

Upvotes: 1

Related Questions