1
votes

I studied that The instruction to switch to kernel mode is an example of a privileged instruction. The hardware allows privileged instructions to be executed only in kernel mode.

But if the switch to kernel mode can be only executed in Kernel Mode (?), if the system in user mode, how can it switch to kernel mode ?

3
User process doesn't make it directly. Instead user process makes syscall, and syscall handler switches to kernel mode and executes code in kernel mode.user28434'mstep
exact duplicate of linkmightyWOZ

3 Answers

2
votes

Lets see it step by step:

  1. A process is running.
  2. An exception comes while the process is running.
  3. The process is interrupted to call the exception or interrupt handler.
  4. The instructions, known as the trap or system call handler, read the details of the requested service + arguments, and then perform this request in kernel mode.
  5. Now we are back in user mode at the position where the interrupt was called.
1
votes

There are two ways to get into kernel mode: exception or an interrupt. In most processors, exceptions and interrupt have assigned numbers. The operating system must define a table that contains an array of pointers to handlers for each exception and interrupt.

If you divide by zero in your code, you are going to get into kernel mode as an exception. Then the processor jumps to the handler for divide by zero.

If you call a system service (on most systems) you execute an instruction that causes an exception to get you into kernel mode. The processor invokes the handler for the system service.

This is the general way things work. The Intel chips have bizarre ways to get into kernel mode.

0
votes

I had answered a question similar to this sometime back, you can check it here: How to switch from user mode to kernel mode?

In short, in order for a user space program to enter into kernel mode, it needs to make explicit system calls such as open, read, write etc. that results in software interrupt(SWI) causing the system to switch to kernel mode. You can find more detailed answered in the given link, hope it helps.