Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly instructions to switch the CPU to user mode and to kernel mode?

Based on my understanding, when an interrupt is fired, the CPU will switch to kernel mode, and when the interrupt is handled, the operating system will switch the CPU back to user mode.

Now my questions are:

  1. How did the operating system switch the CPU to user mode (what is the assembly instruction used?).
  2. Is there an assembly instruction that switches the CPU to kernel mode, or does the switch to kernel mode only happens when an interrupt is fired?
like image 304
rony_t Avatar asked Oct 29 '22 06:10

rony_t


1 Answers

How did the operating system switch the CPU to user mode (what is the assembly instruction used?).

Processors have special return from interrupt instructions. The name of the instruction varies among processors but they all do roughly the same thing. REI, IRET are examples.

Is there an assembly instruction that switches the CPU to kernel mode, or does the switch to kernel mode only happens when an interrupt is fired?

There are two ways for a process to get into kernel mode: (1) trigger an exception or (2) execute a special instruction. E.g.,

    DIVL2 #0, R0

Will get you into kernel mode as will

 int a = b / 0 ;

The instruction for getting into kernel mode varies by processors but examples include CHMK, INT. The i86 family has multiple ways of doing this.

like image 193
user3344003 Avatar answered Nov 11 '22 15:11

user3344003