User mode and Kernel mode Switching
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.
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|
|Abort||10111||Mode for memory violation handling|
|Undefined||11011||Undefined instruction handling mode|
|System||11111||Same as Supervisor mode but with re-entrancy|
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.