Open In App

User mode and Kernel mode Switching

Improve
Improve
Like Article
Like
Save
Share
Report

In it’s life span a process executes in user mode and kernel mode. The User mode is normal mode where the process has limited access. While the Kernel mode is the privileged mode where the process has unrestricted access to system resources like hardware, memory, etc. A process can access I/O Hardware registers to program it, can execute OS kernel code and access kernel data in Kernel mode. Anything related to Process management, IO hardware management, and Memory management requires process to execute in Kernel mode. This is important to know that a process in Kernel mode get power to access any device and memory, and same time any crash in kernel mode brings down the whole system. But any crash in user mode brings down the faulty process only. The kernel provides System Call Interface (SCI), which are the entry points for kernel. System Calls are the only way through which a process can go into kernel mode from user mode. Below diagram explains user mode to kernel mode transition in detail.

In modern operating systems, software runs in two distinct modes: user mode and kernel mode. User mode is a restricted mode that limits the software’s access to system resources, while kernel mode is a privileged mode that allows software to access system resources and perform privileged operations.

When a user-level application needs to perform an operation that requires kernel mode access, such as accessing hardware devices or modifying system settings, it must make a system call to the operating system kernel. The operating system switches the processor from user mode to kernel mode to execute the system call, and then switches back to user mode once the operation is complete.

This switching between user mode and kernel mode is known as mode switching or context switching. Mode switching involves saving the current context of the processor in memory, switching to the new mode, and loading the new context into the processor. This process can be time-consuming and resource-intensive, as it involves switching between different privilege levels and saving and restoring processor state.

Here are some key points about user mode and kernel mode switching:

  1. User mode is a restricted mode that limits access to system resources, while kernel mode is a privileged mode that allows access to system resources.
  2. User mode applications must make system calls to access kernel mode resources or perform privileged operations.
  3. Mode switching involves saving the current context of the processor, switching to the new mode, and loading the new context into the processor.
  4. Mode switching can be time-consuming and resource-intensive, and can impact system performance.
  5. Modern operating systems use various techniques to minimize mode switching, such as caching kernel mode data in user mode, and using hardware support for virtualization and context switching.

User mode to Kernel Mode switching

To go into Kernel mode, an application process.

  • Calls the Glibc library function.
  • Glibc library knows the proper way of calling System Call for different architectures. It setup passing arguments as per architecture’s Application Binary Interface (ABI) to prepare for System Call entry.
  • Now Glibc calls SWI instruction (Software Interrupt instruction for ARM), which puts processor into Supervisor mode by updating Mode bits of CPSR register and jumps to vector address 0x08.
  • Till now process execution was in User mode. After SWI instruction execution, the process is allowed to execute kernel code. Memory Management Unit (MMU) will now allow kernel Virtual memory access and execution, for this process.
  • From Vector address 0x08, process execution loads and jumps to SW Interrupt handler routine, which is vector_swi() for ARM.
  • In vector_swi(), System Call Number (SCNO) is extracted from SWI instruction and execution jumps to system call function using SCNO as index in system call table sys_call_table.
  • After System Call execution, in return path, user space registers are restored before starting execution in User Mode.

To support kernel mode and user mode, processor must have hardware support for different privilege modes. For example ARM processor supports seven different modes.

Processor Mode CPSR Mode bits Remark
User   10000 No privilege or user mode
FIQ   10001 Fast Interrupt mode
IRQ   10010 Interrupt mode
Supervisor   10011 Kernel mode
Abort   10111 Mode for memory violation handling
Undefined   11011 Undefined instruction handling mode
System   11111 Same as Supervisor mode but with re-entrancy

Conclusion : For any system, privilege mode and non-privilege mode is important for access protection. The processor must have hardware support for user/kernel mode. System Call Interfaces (SCI) are the only way to transit from User space to kernel space. Kernel space switching is achieved by Software Interrupt, which changes the processor mode and jump the CPU execution into interrupt handler, which executes corresponding System Call routine.


Last Updated : 14 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads