5
votes

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?
1
1. it switched to a code segment selector with user privilege level using whatever means 2. is the reverse, but the specialized instructions are syscall and sysenter.Jester
There are multiple ways of doing this. Which one is used depends on the operating system.Cody Gray♦
in general and not specific to an architecture, you would typically have an instruction that is a soft interrupt if you will, by simply executing that instruction the processor would switch modes to a more protected mode based on the design of the processor, and execute code from a known place based on the design of the procesor, and that code is now priveledged, not complicated. I could call mine syc, have the implementation switch to protected mode and by design start execution from the physical address 0x1000. Not complicated.old_timer
then what the operating system does with that if it uses it all all is up to the operating system. often they have a fair amount of freedom. Windows syscalls dont have to match LInux syscalls for example, but that is architecture dependent, it could be designed into the instruction set that operands cause specific things.old_timer
Unfortunately, the x86 architecture you tagged is too involved to give you a short answer. It boils down to what Jester said but I believe that wouldn't satisfy you as an answer. If you feel like it, read the Intel SDM 3 and feel free to come back answering your own question. The protection is described in the first handful of chapters.Margaret Bloom

1 Answers

2
votes

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.