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