% The Linux Kernel [back](index.md) TODO: A word about how the kernel is in charge of enforcing security TODO: Talk about `dmesg` Role ---- A kernel is a piece of software that is loaded at boot time. It is a component that is responsible for "orchestrating" the OS in different manners. There are different types of kernel in this world, and they may have different responsibilities. We will be focusing solely on the role of the Linux kernel here. ### Hardware control and abstraction One goal for a kernel is to be the "glue" between the hardware and other software. For example, let's suppose two pieces of software want to store data in a hard drive. Without hardware control, if these two softwares were to run concurrently, they both would try to control the spinning hard drive, potentially trying to make it go in different directions. Without hardware control, each software would have to consciously and constantly collaborate with each other to avoid conflict when accessing each piece of hardware. With the Linux kernel accessing the hardware is gated by an API: each software that wants to access the hard drive has to go use system calls (e.g.: `read(2)` / `write(2)`), and the kernel will then schedule these tasks sequentially. Another important point, is that without the Linux kernel, each piece of software would have to care if the hard drive is a spinning disk, an SSD, or a USB stick, or if is connected using a SATA cable, a USB cable, etc. The Linux kernel allows us to not care about this, and provides us with the filesystem abstraction, and mount points. To the software developer doing a `read` on a file doesn't change if the file is stored on an SSD or even on a network filesystem, because the kernel will check that for us, and apply the appropriate logic. ### Multitasking Another big role of the Linux kernel is to allow multiple programs to run concurrently: the CPU doesn't have knowledge of processes or threads, it just runs binary code. Let's say we have a CPU with 4 cores. That means that the CPU can truly run only 4 tasks in parallel. As a user with this CPU, this doesn't impact me that I'm running a graphical interface, a web browser, a mail client, a terminal window, and a video player. Each of these 5 applications feels responsive even if the CPU can only run 4 of them at the time. This is because the kernel is responsible for switching the tasks that the CPU runs (a.k.a. scheduling). Theses tasks / applications are switched quickly and in such a way that they feel responsive to the user. The Linux kernel also provides developers with another abstraction to execute tasks concurrently: threads. Threads are akin to having multiple processes, except that when scheduled, they keep the same memory region for their heap. Without the kernel, the software developer would have to care about the CPU architecture, how much cores it has, and would have to find the right assembly instructions for the given CPU to run code in parallel, and if they want to run more tasks concurrently than the amount of available CPU cores, they would have to implement their own task scheduler. Thankfully we do not have to care about that, because the Linux kernel cares about it for us instead. Sources ------- The official sources of the Linux kernel can be found in [kernel.org](https://www.kernel.org)[^distrib-patches]. [^distrib-patches]: Note that these are the *original* sources. Lots of GNU/Linux distributions incorporate additional patches to the source of their Linux kernel. This might be done for various reasons, like for security, performance, or additional experimental features (e.g. [Debian](https://sources.debian.org/patches/linux/5.7.6-1/), [Gentoo](https://gitweb.gentoo.org/proj/linux-patches.git/tree/?h=5.7)) The official programming language of the Linux kernel is C. It is heavily documented in the `Documentation` subdirectory, using the sphinx documentation system. Versioning ---------- TODO: this section Dependencies ------------ To compile a Linux kernel whose version is greater than 5.2, one needs the following dependencies: TODO: Also refer to their Debian, Ubuntu, etc. packages. Configuring the kernel ---------------------- ```sh make nconfig ``` Building the kernel ------------------- ```sh make -j8 ``` Running the kernel ------------------ TODO: link the QEMU article