Introduction
The Part 1/2 of theoretical series discussed the first two components and steps for building an Embedded Linux System that are Cross Compiler Toolchain and the Bootloader.
The Part 2 will discuss Linux Kernel and FileSystem as third and fourth components of the OS.
Linux Kernel
Definition
Linux Kernel is a program and a component of Linux System that links the hardware to the system processes ensuring user applications functioning. So it provides memory management, processes management, peripheral drivers and system calls management.
User Space and Kernel Space
When executing a user application the CPU executes instructions in two possible modes the kernel mode or user mode. The kernel mode is higher privileged than the user mode since it can directly interact with the hardware while the user mode can access hardware only through System Calls. Those elements can be represented in the schematic bellow:

System calls
Linux kernel has a number of system calls that significantly varies depending on the CPU’s architecture. For example arm64 has about 440 Syscalls and are referenced each by a number (NR) and a symbolic name such as restart_syscall with the NR 0x0.
To perform a system call, the syscall function NR and parameters are put in dedicated registers. After that the CPU transits to Kernel mode by executing a specific instruction. Once in Kernel mode the Kernel determines the system call to be executed by referring to the register containing the NR. The CPU then transits back to user mode after the system call function is completed to continue user code execution thanks to return address stored.
The process performed in system calls to handle involved registers and instructions for kernel mode transition is different depending on the CPU architecture. The convention defining a processes is called ABI (Application Binary Interface).
Some ABI examples:
- ARM (32-bit/EABI)
In case of ARM32-bit/EABI CPU, the syscall function NR is stored in r7 register. The arg0-to-arg5 parameters are stored in r0-to-r5 registers. The instruction to transit to Kernel mode is a jump to system call handler address by “swi 0x0” assembly instruction. - Intel x86
In case of intel 32-bit CPU, the syscall function is stored in eax register. The arguments arg0-to-arg5 are stored in ebx, ecx, edx, esi, edi and ebp registers. The transition to kernel mode is done by kernel interruption using the instruction “int $0x80” .
More details about ABIs can be found in syscalls.
For embedded Linux in architectures such as ARM the kernel by itself is not able to interact with hardware components. This is because it does not contain any description about the board in which it’s going to run. Here is the need for a DTB (Device Tree Blob) as a complementary binary to the kernel.
Device Tree Blob (DTB)
DTB is a binary that contains description about supported hardware components in a system. It allows embedded operating systems to be adaptable to different devices.
DTB (.dtb) is the resulted binary of the .dts (Device Tree Source) compilation. DTS files are written in Device Tree syntax and they include .dtsi (Device Tree Source Include) files that represent common hardware to variety of platforms. The source files are compiled using DTC (Device Tree Compiler). More information about DTB can be found in device-tree.
To be complete an operating system needs a file system to manage data present in its storage devices.
Note: One may ask if the DTB is needed for the kernel to be able to control hardware components, what about the bootloader ? it also interacts with the hardware so doesn’t it need a DTB ?
The answer is yes the bootloader uses the same DTB as the kernel. The DTB implementation and how it differs from the kernel and boot will be discussed in the practical series.
Root File System
A File System is a form in which the data and files are organized on a storage device. It allows the OS to classify and manage the content of a USB flash or an SSD drive. A file system can be accessed as a directory after it’s mounted on a storage disk.
In Linux systems directories and files are organized as defined in the FSH standard (File System Hierarchy) that has the root (/) on top of it. The Root File System is then the structure in which the directories and files are presented in the “/” directory.
It contains:
- Some directories such as /bin that has user commands (ls, tar, wget…)
- Other file systems such as /usr and /var file system that stores data files used by applications running on the system
In the boot process, once loaded and started by bootloader the kernel will then mount the root filesystem and launch the /sbin/init program that initializes user space utilities and services.
More information about filesystem can be found in FileSystem-Structure.
Equipping the system with basic commands can be done manually by compiling some projects such as coreutils for manipulating text, files and shell and modutils for kernel modules management. A second possibility is by configuring and compiling all needed tools in one project such as busybox that provides a set of utilities.
Leave a Reply