summaryrefslogtreecommitdiffstats
path: root/kernel.md
diff options
context:
space:
mode:
Diffstat (limited to 'kernel.md')
-rw-r--r--kernel.md158
1 files changed, 158 insertions, 0 deletions
diff --git a/kernel.md b/kernel.md
new file mode 100644
index 0000000..db1680a
--- /dev/null
+++ b/kernel.md
@@ -0,0 +1,158 @@
1% The Linux Kernel
2
3[back](index.md)
4
5TODO: A word about how the kernel is in charge of enforcing security
6
7TODO: Talk about `dmesg`
8
9Role
10----
11
12A kernel is a piece of software that is loaded at boot time. It is a
13component that is responsible for "orchestrating" the OS in different
14manners. There are different types of kernel in this world, and they may
15have different responsibilities. We will be focusing solely on the role
16of the Linux kernel here.
17
18### Hardware control and abstraction
19
20One goal for a kernel is to be the "glue" between the hardware and
21other software.
22
23For example, let's suppose two pieces of software want to store data in
24a hard drive. Without hardware control, if these two softwares were to
25run concurrently, they both would try to control the spinning hard
26drive, potentially trying to make it go in different directions.
27
28Without hardware control, each software would have to consciously and
29constantly collaborate with each other to avoid conflict when accessing
30each piece of hardware.
31
32With the Linux kernel accessing the hardware is gated by an API: each
33software that wants to access the hard drive has to go use system calls
34(e.g.: `read(2)` / `write(2)`), and the kernel will then schedule
35these tasks sequentially.
36
37Another important point, is that without the Linux kernel, each piece of
38software would have to care if the hard drive is a spinning disk, an
39SSD, or a USB stick, or if is connected using a SATA cable, a USB cable,
40etc.
41
42The Linux kernel allows us to not care about this, and provides us with
43the filesystem abstraction, and mount points. To the software developer
44doing a `read` on a file doesn't change if the file is stored on an
45SSD or even on a network filesystem, because the kernel will check that
46for us, and apply the appropriate logic.
47
48### Multitasking
49
50Another big role of the Linux kernel is to allow multiple programs to
51run concurrently: the CPU doesn't have knowledge of processes or
52threads, it just runs binary code.
53
54Let's say we have a CPU with 4 cores. That means that the CPU can truly run
55only 4 tasks in parallel.
56
57As a user with this CPU, this doesn't impact me that I'm running a graphical
58interface, a web browser, a mail client, a terminal window, and a video player.
59Each of these 5 applications feels responsive even if the CPU can only run 4 of
60them at the time.
61
62This is because the kernel is responsible for switching the tasks that the CPU
63runs (a.k.a. scheduling). Theses tasks / applications are switched quickly and
64in such a way that they feel responsive to the user.
65
66The Linux kernel also provides developers with another abstraction to
67execute tasks concurrently: threads. Threads are akin to having multiple
68processes, except that when scheduled, they keep the same memory region
69for their heap.
70
71Without the kernel, the software developer would have to care about the
72CPU architecture, how much cores it has, and would have to find the
73right assembly instructions for the given CPU to run code in parallel,
74and if they want to run more tasks concurrently than the amount of
75available CPU cores, they would have to implement their own task
76scheduler.
77
78Thankfully we do not have to care about that, because the Linux kernel
79cares about it for us instead.
80
81Sources
82-------
83
84The official sources of the Linux kernel can be found in
85[kernel.org](https://www.kernel.org)[^distrib-patches].
86
87[^distrib-patches]:
88 Note that these are the *original* sources. Most GNU/Linux
89 distributions incorporate additional patches to their releases.
90
91The official programming language of the Linux kernel is C. It is
92heavily documented in the `Documentation` subdirectory, using the
93sphinx documentation system.
94
95
96Versioning
97----------
98
99TODO: this section
100
101Dependencies
102------------
103
104To compile a Linux kernel whose version is greater than 5.2, one needs
105the following dependencies:
106
107<!--
108A C compiler:: GCC, or Clang Perl 5:: An old scripting language bc:: A
109command line calculator net-tools:: Some networking-related command-line
110tools (contains for example the `ifconfig` command) openssl:: A well
111known cryptography library rsync:: A command line utility to copy /
112synchronize files and directories gmp:: A library for doing arithmetic
113with arbitrary precision mpc:: A library for doing complex arithmetic
114with arbitrary precision mpfr:: A library for doing floating-point
115arithmetic with arbitrary precision libelf:: A library for reading ELF
116file. ELF is the standard executable format used for Linux. utillinux::
117A set of system utilities for Linux (contains for example the `dmesg`
118command) bison:: A parser generator flex:: A lexical analyser generator
119cpio:: A command-line utility for CPIO compressed archives
120-->
121
122TODO: Also refer to their Debian, Ubuntu, etc. packages.
123
124Configuring the kernel
125----------------------
126
127```sh
128make nconfig
129```
130
131Building the kernel
132-------------------
133
134```sh
135make -j8
136```
137
138Running the kernel
139------------------
140
141TODO: link the QEMU article
142
143
144```c
145#include <stdio.h>
146
147/**
148 * \brief The entry point of the program.
149 *
150 * \param argc the number of command line arguments
151 * \param argv the command line argument, as an array of NULL terminated strings
152 */
153int main(int argc, char** argv) {
154 for(int i = 0; i < argc; ++i) {
155 printf("Argument %i: %s\n", i, argv[i]);
156 }
157}
158```