User space to kernel space transition

kernel

Both physically (hardware, rings) and logically (software), what is user space to kernel space transition? And vice versa.

Or more generally what goes on when there is a "space" (or ring) transition taking place? And what makes it costly (one of the main issue of Minix)?

This question follows for comments under this answer.

Best Answer

From the operating system's perspective, user space is the execution of code from a process, and kernel space is the execution of code from the kernel. A transition from user space to kernel space is the entry into a system call, and a transition from kernel space to user space is the return from a system call.

From the processor's perspective, user space and kernel space are privilege levels. “Ring 3” and “ring 0” are the names used on Intel x86 processors (there are rings 1 and 2, but most unices don't use them, because there isn't a lot of use for them). Other CPU types use different names, for example user mode and privileged (or system) modes on ARM.

Transition between modes is done by instructions that both change the processor mode and jump to a different address; the detail of these instructions is very much dependent on the CPU type. In addition to switching the processor mode and jumping to a different address, the mode transition instructions typically perform some other tasks such as swapping the value of a few registers (again, this is very CPU-dependent).

A transition from user mode to kernel mode has to jump to a fixed address, which has been set by the kernel. In order to maintain the security of the system, code in user mode cannot be allowed to switch the processor to kernel mode except to invoke a piece of code (the system call entry point) which has been specially crafted to work securely no matter what the user mode code has been doing. The system call entry point first saves register values into a designated memory area, and performs any other necessary bookkeeping, then it reads the system call parameters and dispatches the system call to the appropriate function. Depending on the processor type, the bookkeeping may involve setting the MMU tables appropriately. The kernel may decide at any point to pause the calling process and schedule another process.

A transition from kernel mode to user mode can happen anywhere the kernel deems fit. The kernel restores the saved registers, the MMU configuration and anything else that is necessary, and writes the system call's return value, and finally jumps back to the instruction in the process after the system call.