Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPU Switches from User mode to Kernel Mode : What exactly does it do? How does it makes this transition?

CPU Switches from User mode to Kernel Mode : What exactly does it do? How does it makes this transition?

EDIT:

Even if it is architecture dependent please provide me with an answer. The architecture is up to you. Tell me for the architecture you know about.

I want to get an idea about what all things will be involved in it.

like image 567
claws Avatar asked Mar 19 '10 16:03

claws


People also ask

How does the system switch from user mode to kernel mode?

The system is in user mode when the operating system is running a user application such as handling a text editor. The transition from user mode to kernel mode occurs when the application requests the help of operating system or an interrupt or a system call occurs.

How can processor communicate between the system mode and the user mode?

A processor in a computer running Windows has two different modes: user mode and kernel mode. The processor switches between the two modes depending on what type of code is running on the processor. Applications run in user mode, and core operating system components run in kernel mode.

How does the CPU get into kernel mode?

When an interrupt occurs, CPU hardware switches to the kernel mode. Switching to user mode (from kernel mode) done by setting CPU mode bit (by an instruction). Privileged instructions can be executed only in kernel mode.

How does CPU switch from process to process?

A context switching is a process that involves switching of the CPU from one process or task to another. In this phenomenon, the execution of the process that is present in the running state is suspended by the kernel and another process that is present in the ready state is executed by the CPU.


1 Answers

Note: this is mostly relevant to x86 architecture. Here's a somewhat simplified explanation.

The transition is usually caused by one of the following:

  • Fault (e.g. a page fault or some other exception caused by executing an instruction)
  • Interrupt (e.g. a keyboard interrupt or I/O finishing)
  • Trap (e.g. a system call)

What normally happens is that system checks the Interrupt Descriptor Table (IDT). Each exception (interrupt, fault, etc.) has a number associated with it which is used to index into this table.

From this table the CPU can determine the interrupt handler to run.

As part of the transition the following changes (generally) take effect:

  • Switch to Kernel stack
  • EFLAGS are saved
  • Code segment selector and EIP are saved.
  • stack segment selector and stack pointer are saved
  • Start executing the interrupt handler
  • The general purpose registers are saved (handler's job)
  • Segment selectors are changed to Kernel ones (handler's job)

You're now in kernel mode.

Hope that helps :)

like image 109
Omar Alrubaiyan Avatar answered Sep 18 '22 20:09

Omar Alrubaiyan