User mode and Kernel mode Switching
Last Updated :
14 Apr, 2023
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:
- 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.
- User mode applications must make system calls to access kernel mode resources or perform privileged operations.
- Mode switching involves saving the current context of the processor, switching to the new mode, and loading the new context into the processor.
- Mode switching can be time-consuming and resource-intensive, and can impact system performance.
- 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.
Similar Reads
Difference Between User Mode and Kernel Mode User mode and kernel mode are two working states inside a laptop's working system that determine the level of access and control, a technique can have over machine resources. Understanding the differences among these modes is critical to knowing how modern working systems manage safety and resource
5 min read
Kernel in Operating System A kernel is the core part of an operating system. It acts as a bridge between software applications and the hardware of a computer. The kernel manages system resources, such as the CPU, memory, and devices, ensuring everything works together smoothly and efficiently. It handles tasks like running pr
10 min read
Kernel Level Threads in Operating System Pre-requisites: Thread in Operating System An operating system is a program loaded into a system or computer. and manage all the other program which is running on that OS Program, it manages the all other application programs. or in other words, we can say that the OS is an interface between the use
4 min read
Kernel I/O Subsystem in Operating System The kernel provides many services related to I/O. Several services such as scheduling, caching, spooling, device reservation, and error handling - are provided by the kernel's I/O subsystem built on the hardware and device-driver infrastructure. The I/O subsystem is also responsible for protecting i
7 min read
Microkernel in Operating Systems A MicroKernel is an approach to designing an Operating System (OS). The microkernel provides very fundamental services required to run the OS like basic memory management, task scheduling, etc. Microkernels use Inter-Process Communication (IPC) for communication. In this article, we will discuss Ker
8 min read