Linux Kernel Space

In Linux, we have two spaces where applications generally run, the kernel system space and the user space. Generally, with default kernel configuration, the user space takes the 0–3GB space whilst the kernel space takes the 3–4GB space, more in-depth details here.

The kernel space is where we have system memory for low level applications on the kernel running. The user space is the environment where our user processes function and execute.

How User Space Interacts With Kernel Space Through System Calls

The two memory spaces are separated by a finely tuned permission layer called Rings. These Rings define how privileged or unprivileged the requirements of an application need to be before certain actions can be granted.

Ring Layers for X86 Systems

These rings are not peculiar to Linux but are a well defined layout in operating systems , although the functional area of each level is assigned based on the CPU architecture that the OS is running on. To switch between the user and kernel space, we apply an operation through a System Call, simply called a syscall.

This uses defined kernel functions accessible from user space applications to request access to kernel level functionality. The diagram below perfectly explains how this order is defined.

A hierarchical overview of the operating system layers

Whenever an applications makes a request to a kernel level function, an interrupt is sent which tells the processor to stop whatever it is doing and attend to that particular request, you can think about it like context switching if it makes it easier to understand. Provided the user space application has relevant permission, there’s a context switch to the kernel space, the user space application awaits a response back after the context switch has started and the required program/functionality in the kernel space is executed through the aid of the appropriate interrupt handler.

tmp_buf = mmap(file, len); # mmap here is from a C library # This is called a memory map and it's a C function

# It allocates a certain amount of memory for a task, file etc.

# Since memory is a kernel space resource, a syscall is made to the mmap syscall in the linux kernel to make this request possible