diff --git a/4.15.2/LKMPG-4.15.2.html b/4.15.2/LKMPG-4.15.2.html index 6a0e4e2..39e3e96 100644 --- a/4.15.2/LKMPG-4.15.2.html +++ b/4.15.2/LKMPG-4.15.2.html @@ -3,33 +3,26 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- - -The Linux Kernel Module Programming Guide is a free book; you may reproduce and/or modify it under the terms of the Open Software License, version 3.0.
@@ -401,18 +329,18 @@ If you publish or distribute this book commercially, donations, royalties, and/oThe Linux Kernel Module Programming Guide was originally written for the 2.2 kernels by Ori Pomerantz. Eventually, Ori no longer had time to maintain the document. After all, the Linux kernel is a fast moving target. Peter Jay Salzman took over maintenance and updated it for the 2.4 kernels. Eventually, Peter no longer had time to follow developments with the 2.6 kernel, so Michael Burian became a co-maintainer to update the document for the 2.6 kernels. Bob Mottram updated the examples for 3.8 and later kernels, added the sysfs chapter and modified or updated other chapters.
The Linux kernel is a moving target. There has always been a question whether the LKMPG should remove deprecated information or keep it around for historical sake. Michael Burian and I decided to create a new branch of the LKMPG for each new stable kernel version. So version LKMPG 4.12.x will address Linux kernel 4.12.x and LKMPG 2.6.x will address Linux kernel 2.6. No attempt will be made to archive historical information; a person wishing this information should read the appropriately versioned LKMPG.
@@ -423,18 +351,18 @@ The source code and discussions should apply to most architectures, but I can'tThe following people have contributed corrections or good suggestions: Ignacio Martin, David Porter, Daniele Paolo Scarpazza, Dimo Velev, Francois Audeon, Horst Schirmeier, Bob Mottram and Roman Lakeev.
So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your file system and a core dump means a reboot.
@@ -445,9 +373,9 @@ What exactly is a kernel module? Modules are pieces of code that can be loaded aLinux distros provide the commands modprobe, insmod and depmod within a package.
@@ -457,6 +385,7 @@ On Debian:sudo apt-get install build-essential kmod
sudo pacman -S gcc kmod
To discover what modules are already loaded within your current kernel use the command lsmod.
sudo lsmod
sudo cat /proc/modules
sudo lsmod | grep fat
For the purposes of following this guide you don't necessarily need to do that. However, it would be wise to run the examples within a test distro running on a virtual machine in order to avoid any possibility of messing up your system.
Before we delve into code, there are a few issues we need to cover. Everyone's system is different and everyone has their own groove. Getting your first "hello world" program to compile and load correctly can sometimes be a trick. Rest assured, after you get over the initial hurdle of doing it for the first time, it will be smooth sailing thereafter.
A module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel. We won't go into module versioning until later in this guide. Until we cover modversions, the examples in the guide may not work if you're running a kernel with modversioning turned on. However, most stock Linux distro kernels come with it turned on. If you're having trouble loading the modules because of versioning errors, compile a kernel with modversioning turned off.
It is highly recommended that you type in, compile and load all the examples this guide discusses. It's also highly recommended you do this from a console. You should not be working on this stuff in X.
@@ -539,19 +469,19 @@ It is highly recommended that you type in, compile and load all the examples thi Modules can't print to the screen like printf() can, but they can log information and warnings, which ends up being printed on your screen, but only on a console. If you insmod a module from an xterm, the information and warnings will be logged, but only to your systemd journal. You won't see it unless you look through your journalctl. To have immediate access to this information, do all your work from the console.Before you can build anything you'll need to install the header files for your kernel. On Parabola GNU/Linux:
sudo pacman -S linux-libre-headers
sudo apt-get update
apt-cache search linux-headers-$(uname -r)
@@ -571,19 +502,21 @@ This will tell you what kernel header files are available. Then for example:
sudo apt-get install kmod linux-headers-4.15.2-1-amd64
All the examples from this document are available within the examples subdirectory. To test that they compile:
cd examples
make
@@ -594,13 +527,12 @@ If there are any compile errors then you might have a more recent kernel version
Most people learning programming start out with some sort of "hello world" example. I don't know what happens to people who break with this tradition, but I think it's safer not to find out. We'll start with a series of hello world programs that demonstrate the different aspects of the basics of writing a kernel module.
@@ -614,6 +546,7 @@ Make a test directory:mkdir -p ~/develop/kernel/hello-1
cd ~/develop/kernel/hello-1
@@ -624,6 +557,7 @@ Paste this into you favourite editor and save it as hello-1.c:
/* * hello-1.c - The simplest kernel module. */ @@ -652,6 +586,7 @@ Now you'll need a Makefile. If you copy and paste this change the indentation to+-obj-m += hello-1.o all: @@ -667,6 +602,7 @@ And finally just:+@@ -676,6 +612,7 @@ If all goes smoothly you should then find that you have a compiled hello-1.komake+@@ -685,6 +622,7 @@ At this point the command:sudo modinfo hello-1.ko+@@ -694,6 +632,7 @@ should return nothing. You can try loading your shiny new module with:sudo lsmod | grep hello+@@ -703,6 +642,7 @@ The dash character will get converted to an underscore, so when you again try:sudo insmod hello-1.ko+@@ -712,6 +652,7 @@ you should now see your loaded module. It can be removed again with:sudo lsmod | grep hello+@@ -721,6 +662,7 @@ Notice that the dash was replaced by an underscore. To see what just happened insudo rmmod hello_1+@@ -743,24 +685,20 @@ Lastly, every kernel module needs to include linux/module.h. We needed to includjournalctl --since "1 hour ago" | grep kernel
Another thing which may not be immediately obvious to anyone getting started with kernel programming is that indentation within your code should be using tabs and not spaces. It's one of the coding conventions of the kernel. You may not like it, but you'll need to get used to it if you ever submit a patch upstream.
In the beginning there was printk, usually followed by a priority such as KERN_INFO or KERN_DEBUG. More recently this can also be expressed in abbreviated form using a set of print macros, such as pr_info and pr_debug. This just saves some mindless keyboard bashing and looks a bit neater. They can be found within linux/printk.h. Take time to read through the available priority macros.
Kernel modules need to be compiled a bit differently from regular userspace apps. Former kernel versions required us to care much about these settings, which are usually stored in Makefiles. Although hierarchically organized, many redundant settings accumulated in sublevel Makefiles and made them large and rather difficult to maintain. Fortunately, there is a new way of doing these things, called kbuild, and the build process for external loadable modules is now fully integrated into the standard kernel build mechanism. To learn more on how to compile modules which are not part of the official kernel (such as all the examples you'll find in this guide), see file linux/Documentation/kbuild/modules.txt.
@@ -775,18 +713,18 @@ Here's another exercise for the reader. See that comment above the return statemIn early kernel versions you had to use the init_module and cleanup_module functions, as in the first hello world example, but these days you can name those anything you want by using the module_init and module_exit macros. These macros are defined in linux/init.h. The only requirement is that your init and cleanup functions must be defined before calling the those macros, otherwise you'll get compilation errors. Here's an example of this technique:
/* * hello-2.c - Demonstrating the module_init() and module_exit() macros. * This is preferred over using init_module() and cleanup_module(). @@ -816,6 +754,7 @@ So now we have two real kernel modules under our belt. Adding another module is+obj-m += hello-1.o obj-m += hello-2.o @@ -833,9 +772,9 @@ Now have a look at linux/drivers/char/Makefile for a real world example. As you
This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The __init macro causes the init function to be discarded and its memory freed once the init function finishes for built-in drivers, but not loadable modules. If you think about when the init function is invoked, this makes perfect sense.
@@ -853,6 +792,7 @@ These macros are defined in linux/init.h and serve to free up kernel memo/* * hello-3.c - Illustrating the __init, __initdata and __exit macros. */ @@ -880,14 +820,15 @@ module_exit(hello_3_exit);
Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this:
# insmod xxxxxx.o Warning: loading xxxxxx.ko will taint the kernel: no license See http://www.tux.org/lkml/#export-tainted for information about tainted modules @@ -904,6 +845,7 @@ To reference what license you're using a macro is available called MODULE_LIC+/* * hello-4.c - Demonstrates module documentation. */ @@ -934,9 +876,9 @@ module_exit(cleanup_hello_4);
Modules can take command line arguments, but not with the argc/argv you might be used to.
@@ -950,6 +892,7 @@ The module_param() macro takes 3 arguments: the name of the variable, its type aint myint = 3; module_param(myint, int, 0);@@ -960,12 +903,13 @@ Arrays are supported too, but things are a bit different now than they were in t
int myintarray[2]; module_param_array(myintarray, int, NULL, 0); /* not interested in count */ short myshortarray[4]; int count; -module_parm_array(myshortarray, short, &count, 0); /* put count into "count" variable */ +module_param_array(myshortarray, short, &count, 0); /* put count into "count" variable */
/* * hello-5.c - Demonstrates command line argument passing to a module. */ @@ -1056,6 +1001,7 @@ I would recommend playing around with this code:+# sudo insmod hello-5.ko mystring="bebop" mybyte=255 myintArray=-1 mybyte is an 8 bit integer: 255 myshort is a short integer: 1 @@ -1086,9 +1032,9 @@ hello-5.o: invalid argument syntax for mylong: 'h'
Sometimes it makes sense to divide a kernel module between several source files.
@@ -1098,6 +1044,7 @@ Here's an example of such a kernel module./* * start.c - Illustration of multi filed modules */ @@ -1118,6 +1065,7 @@ The next file:+-/* * stop.c - Illustration of multi filed modules */ @@ -1137,6 +1085,7 @@ And finally, the makefile:+obj-m += hello-1.o obj-m += hello-2.o obj-m += hello-3.o @@ -1159,9 +1108,9 @@ This is the complete makefile for all the examples we've seen so far. The first-Building modules for a precompiled kernel
-++Building modules for a precompiled kernel
+Obviously, we strongly suggest you to recompile your kernel, so that you can enable a number of useful debugging features, such as forced module unloading (MODULE_FORCE_UNLOAD): when this option is enabled, you can force the kernel to unload a module even when it believes it is unsafe, via a sudo rmmod -f module command. This option can save you a lot of time and a number of reboots during the development of a module. If you don't want to recompile your kernel then you should consider running the examples within a test distro on a virtual machine. If you mess anything up then you can easily reboot or restore the VM.
@@ -1175,6 +1124,7 @@ Now, if you just install a kernel source tree, use it to compile your kernel mod+@@ -1184,6 +1134,7 @@ Less cryptical information are logged to the systemd journal:insmod: error inserting 'poet_atkm.ko': -1 Invalid module format+Jun 4 22:07:54 localhost kernel: poet_atkm: version magic '2.6.5-1.358custom 686 REGPARM 4KSTACKS gcc-3.3' should be '2.6.5-1.358 686 REGPARM 4KSTACKS gcc-3.3'@@ -1194,6 +1145,7 @@ In other words, your kernel refuses to accept your module because version string+# sudo modinfo hello-4.ko license: GPL author: Bob Mottram <bob@freedombone.net> @@ -1216,6 +1168,7 @@ Let's focus again on the previous error message: a closer look at the version ma+-VERSION = 4 PATCHLEVEL = 7 SUBLEVEL = 4 @@ -1232,6 +1185,7 @@ Now, please run make to update configuration and version headers and objects:+# make CHK include/linux/version.h UPD include/linux/version.h @@ -1253,13 +1207,12 @@ If you do not desire to actually compile the kernel, you can interrupt the build-Preliminaries
----How modules begin and end
-++Preliminaries
+++-How modules begin and end
+A program usually begins with a main() function, executes a bunch of instructions and terminates upon completion of those instructions. Kernel modules work a bit differently. A module always begin with either the init_module or the function you specify with module_init call. This is the entry function for modules; it tells the kernel what functionality the module provides and sets up the kernel to run the module's functions when they're needed. Once it does this, entry function returns and the module does nothing until the kernel wants to do something with the code that the module provides.
@@ -1274,9 +1227,9 @@ Every module must have an entry function and an exit function. Since there's mor-Functions available to modules
-++Functions available to modules
+-Programmers use functions they don't define all the time. A prime example of this is printf(). You use these library functions which are provided by the standard C library, libc. The definitions for these functions don't actually enter your program until the linking stage, which insures that the code (for printf() for example) is available, and fixes the call instruction to point to that code.
@@ -1294,6 +1247,7 @@ Would you like to see what system calls are made by printf()? It's easy! Compile+#include <stdio.h> int main(void) @@ -1314,9 +1268,9 @@ You can even write modules to replace the kernel's system calls, which we'll do-User Space vs Kernel Space
-++-User Space vs Kernel Space
+A kernel is all about access to resources, whether the resource in question happens to be a video card, a hard drive or even memory. Programs often compete for the same resource. As I just saved this document, updatedb started updating the locate database. My vim session and updatedb are both using the hard drive concurrently. The kernel needs to keep things orderly, and not give users access to resources whenever they feel like it. To this end, a CPU can run in different modes. Each mode gives a different level of freedom to do what you want on the system. The Intel 80386 architecture had 4 of these modes, which were called rings. Unix uses only two rings; the highest ring (ring 0, also known as `supervisor mode' where everything is allowed to happen) and the lowest ring, which is called `user mode'.
@@ -1327,9 +1281,9 @@ Recall the discussion about library functions vs system calls. Typically, you us-Name Space
-++-Name Space
+When you write a small C program, you use variables which are convenient and make sense to the reader. If, on the other hand, you're writing routines which will be part of a bigger problem, any global variables you have are part of a community of other peoples' global variables; some of the variable names can clash. When a program has lots of global variables which aren't meaningful enough to be distinguished, you get namespace pollution. In large projects, effort must be made to remember reserved names, and to find ways to develop a scheme for naming unique variable names and symbols.
@@ -1344,9 +1298,9 @@ The file /proc/kallsyms holds all the symbols that the kernel knows about-Code space
-++-Code space
+Memory management is a very complicated subject and the majority of O'Reilly's "Understanding The Linux Kernel" exclusively covers memory management! We're not setting out to be experts on memory managements, but we do need to know a couple of facts to even begin worrying about writing real modules.
@@ -1360,27 +1314,26 @@ The kernel has its own space of memory as well. Since a module is code which can-By the way, I would like to point out that the above discussion is true for any operating system which uses a monolithic kernel. This isn't quite the same thing as "building all your modules into the kernel", although the idea is the same. There are things called microkernels which have modules which get their own codespace. The GNU Hurd and the Magenta kernel of Google Fuchsia are two examples of a microkernel. +By the way, I would like to point out that the above discussion is true for any operating system which uses a monolithic kernel. This isn't quite the same thing as /"building all your modules into the kernel"/, although the idea is the same. There are things called microkernels which have modules which get their own codespace. The GNU Hurd and the Magenta kernel of Google Fuchsia are two examples of a microkernel.
-Device Drivers
-++Device Drivers
+-One class of module is the device driver, which provides functionality for hardware like a serial port. On unix, each piece of hardware is represented by a file located in /dev named a device file which provides the means to communicate with the hardware. The device driver provides the communication on behalf of a user program. So the es1370.o sound card device driver might connect the /dev/sound device file to the Ensoniq IS1370 sound card. A userspace program like mp3blaster can use /dev/sound without ever knowing what kind of sound card is installed.
-
- Major and Minor Numbers
-+
- Major and Minor Numbers
Let's look at some device files. Here are device files which represent the first three partitions on the primary master IDE hard drive:
+-# ls -l /dev/hda[1-3] brw-rw---- 1 root disk 3, 1 Jul 5 2000 /dev/hda1 brw-rw---- 1 root disk 3, 2 Jul 5 2000 /dev/hda2 @@ -1401,6 +1354,7 @@ Devices are divided into two types: character devices and block devices. The dif+crw-rw---- 1 root dial 4, 64 Feb 18 23:34 /dev/ttyS0 crw-r----- 1 root dial 4, 65 Nov 17 10:26 /dev/ttyS1 crw-rw---- 1 root dial 4, 66 Jul 5 2000 /dev/ttyS2 @@ -1421,10 +1375,11 @@ I would like to make a few last points which are implicit from the above discuss-By the way, when I say "hardware", I mean something a bit more abstract than a PCI card that you can hold in your hand. Look at these two device files: +By the way, when I say /"hardware"/, I mean something a bit more abstract than a PCI card that you can hold in your hand. Look at these two device files:
+- - +% ls -l /dev/sda /dev/sdb brw-rw---- 1 root disk 8, 0 Jan 3 09:02 /dev/sda brw-rw---- 1 root disk 8, 16 Jan 3 09:02 /dev/sdb @@ -1435,18 +1390,16 @@ brw-rw---- 1 root disk 8, 16 Jan 3 09:02 /dev/sdb By now you can look at these two device files and know instantly that they are block devices and are handled by same driver (block major 8). Sometimes two device files with the same major but different minor number can actually represent the same piece of physical hardware. So just be aware that the word "hardware" in our discussion can mean something very abstract.-Character Device drivers
----The file_operations Structure
-++Character Device drivers
+++The file_operations Structure
+The file_operations structure is defined in /usr/include/linux/fs.h, and holds pointers to functions defined by the driver that perform various operations on the device. Each field of the structure corresponds to the address of some function defined by the driver to handle a requested operation.
@@ -1456,6 +1409,7 @@ For example, every character driver needs to define a function that reads from t+struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); @@ -1498,6 +1452,7 @@ There is a gcc extension that makes assigning to this structure more convenient.+-struct file_operations fops = { read: device_read, write: device_write, @@ -1512,6 +1467,7 @@ However, there's also a C99 way of assigning to elements of a structure, and thi+struct file_operations fops = { .read = device_read, .write = device_write, @@ -1531,9 +1487,9 @@ An instance of struct file_operations containing pointers to functions that are-The file structure
-++-The file structure
+Each device is represented in the kernel by a file structure, which is defined in linux/fs.h. Be aware that a file is a kernel level structure and never appears in a user space program. It's not the same thing as a FILE, which is defined by glibc and would never appear in a kernel space function. Also, its name is a bit misleading; it represents an abstract open `file', not a file on a disk, which is represented by a structure named inode.
@@ -1548,9 +1504,9 @@ Go ahead and look at the definition of file. Most of the entries you see, like s-Registering A Device
-++-Registering A Device
+As discussed earlier, char devices are accessed through device files, usually located in /dev. This is by convention. When writing a driver, it's OK to put the device file in your current directory. Just make sure you place it in /dev for a production driver. The major number tells you which driver handles which device file. The minor number is used only by the driver itself to differentiate which device it's operating on, just in case the driver handles more than one device.
@@ -1560,6 +1516,7 @@ Adding a driver to your system means registering it with the kernel. This is syn+@@ -1578,9 +1535,9 @@ If you pass a major number of 0 to register_chrdev, the return value will be theint register_chrdev(unsigned int major, const char *name, struct file_operations *fops);-Unregistering A Device
-++-Unregistering A Device
+We can't allow the kernel module to be rmmod'ed whenever root feels like it. If the device file is opened by a process and then we remove the kernel module, using the file would cause a call to the memory location where the appropriate function (read/write) used to be. If we're lucky, no other code was loaded there, and we'll get an ugly error message. If we're unlucky, another kernel module was loaded into the same location, which means a jump into the middle of another function within the kernel. The results of this would be impossible to predict, but they can't be very positive.
@@ -1590,8 +1547,10 @@ Normally, when you don't want to allow something, you return an error code (a ne-
- try_module_get(THIS_MODULE): Increment the use count.
-- module_put(THIS_MODULE): Decrement the use count.
+- try_module_get(THIS_MODULE): Increment the use count. +
+- module_put(THIS_MODULE): Decrement the use count. +
@@ -1600,14 +1559,15 @@ It's important to keep the counter accurate; if you ever do lose track of the co
-chardev.c
-++chardev.c
+-The next code sample creates a char driver named chardev. You can cat its device file.
+@@ -1617,6 +1577,7 @@ The next code sample creates a char driver named chardev. You can cat its devicecat /proc/devices+/* * chardev.c: Creates a read-only char device that says how many times * you've read from the dev file @@ -1803,9 +1764,9 @@ The next code sample creates a char driver named chardev. You can cat its device-Writing Modules for Multiple Kernel Versions
-++-Writing Modules for Multiple Kernel Versions
+The system calls, which are the major interface the kernel shows to the processes, generally stay the same across versions. A new system call may be added, but usually the old ones will behave exactly like they used to. This is necessary for backward compatibility – a new kernel version is not supposed to break regular processes. In most cases, the device files will also remain the same. On the other hand, the internal interfaces within the kernel can and do change between versions.
@@ -1829,9 +1790,9 @@ You might already have noticed that recent kernels look different. In case you h-The /proc File System
-++The /proc File System
+-In Linux, there is an additional mechanism for the kernel and kernel modules to send information to processes — the /proc file system. Originally designed to allow easy access to information about processes (hence the name), it is now used by every bit of the kernel which has something interesting to report, such as /proc/modules which provides the list of modules and /proc/meminfo which stats memory usage statistics.
@@ -1861,12 +1822,14 @@ Each time, everytime the file /proc/helloworld is read, the function p+# cat /proc/helloworld HelloWorld!+/* procfs1.c */ @@ -1921,9 +1884,9 @@ HelloWorld!-Read and Write a /proc File
-++Read and Write a /proc File
+-We have seen a very simple example for a /proc file where we only read the file /proc/helloworld. It's also possible to write in a /proc file. It works the same way as read, a function is called when the /proc file is written. But there is a little difference with read, data comes from user, so you have to import data from user space to kernel space (with copy_from_user or get_user)
@@ -1937,6 +1900,7 @@ The only memory segment accessible to a process is its own, so when writing regu+/** * procfs2.c - create a "file" in /proc * @@ -2040,9 +2004,9 @@ The only memory segment accessible to a process is its own, so when writing regu-Manage /proc file with standard filesystem
-++Manage /proc file with standard filesystem
+-We have seen how to read and write a /proc file with the /proc interface. But it's also possible to manage /proc file with inodes. The main concern is to use advanced functions, like permissions.
@@ -2068,6 +2032,7 @@ It's important to note that the standard roles of read and write are reversed in+/* procfs3.c */ @@ -2160,9 +2125,9 @@ Still hungry for procfs examples? Well, first of all keep in mind, there are rum-Manage /proc file with seq_file
-++Manage /proc file with seq_file
+-As we have seen, writing a /proc file may be quite "complex". So to help people writting /proc file, there is an API named seq_file that helps @@ -2197,6 +2162,7 @@ Seq_file provides basic functions for file_operations, as seq_read, seq_lseek, a
+/** * procfs4.c - create a "file" in /proc * This program uses the seq_file library to manage the /proc file. @@ -2339,9 +2305,11 @@ If you want more information, you can read this web page:@@ -2352,14 +2320,15 @@ You can also read the code of fs/seq_file.c in the linux kernel.-sysfs: Interacting with your module
-++sysfs: Interacting with your module
+sysfs allows you to interact with the running kernel from userspace by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for applications or scripts. You can find sysfs directories and files under the sys directory on your system.
+@@ -2369,73 +2338,8 @@ An example of a hello world module which includes the creation of a variable accls -l /sys-@@ -2444,6 +2348,7 @@ Make and install the module:/* - * hello-sysfs.c sysfs example - */ -#include <linux/module.h> -#include <linux/kobject.h> -#include <linux/sysfs.h> -#include <linux/init.h> -#include <linux/fs.h> -#include <linux/string.h> - -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("Bob Mottram"); - -static struct kobject *mymodule; - -/* the variable you want to be able to change */ -static int myvariable = 0; - -static ssize_t myvariable_show(struct kobject *kobj, - struct kobj_attribute *attr, - char *buf) -{ - return sprintf(buf, "%d\n", myvariable); -} - -static ssize_t myvariable_store(struct kobject *kobj, - struct kobj_attribute *attr, - char *buf, size_t count) -{ - sscanf(buf, "%du", &myvariable); - return count; -} - - -static struct kobj_attribute myvariable_attribute = - __ATTR(myvariable, 0660, myvariable_show, - (void*)myvariable_store); - -static int __init mymodule_init (void) -{ - int error = 0; - - pr_info("mymodule: initialised\n"); - - mymodule = - kobject_create_and_add("mymodule", kernel_kobj); - if (!mymodule) - return -ENOMEM; - - error = sysfs_create_file(mymodule, &myvariable_attribute.attr); - if (error) { - pr_info("failed to create the myvariable file " \ - "in /sys/kernel/mymodule\n"); - } - - return error; -} - -static void __exit mymodule_exit (void) -{ - pr_info("mymodule: Exit success\n"); - kobject_put(mymodule); -} - -module_init(mymodule_init); -module_exit(mymodule_exit); +"hello-sysfs.c"body+-make sudo insmod hello-sysfs.ko@@ -2454,6 +2359,7 @@ Check that it exists:+@@ -2463,6 +2369,7 @@ What is the current value of myvariable ?sudo lsmod | grep hello_sysfs+@@ -2472,6 +2379,7 @@ Set the value of myvariable and check that it changed.cat /sys/kernel/mymodule/myvariable+echo "32" > /sys/kernel/mymodule/myvariable cat /sys/kernel/mymodule/myvariable@@ -2482,15 +2390,16 @@ Finally, remove the test module:+sudo rmmod hello_sysfs-Talking To Device Files
-++Talking To Device Files
+-Device files are supposed to represent physical devices. Most physical devices are used for output as well as input, so there has to be some mechanism for device drivers in the kernel to get the output to send to the device from processes. This is done by opening the device file for output and writing to it, just like writing to a file. In the following example, this is implemented by device_write.
@@ -2516,6 +2425,7 @@ If you want to use ioctls in your own kernel modules, it is best to receive an o+/* * chardev2.c - Create an input/output character device */ @@ -2807,6 +2717,7 @@ If you want to use ioctls in your own kernel modules, it is best to receive an o+/* * chardev.h - the header file with the ioctl definitions. * @@ -2877,6 +2788,7 @@ If you want to use ioctls in your own kernel modules, it is best to receive an o+/* * ioctl.c - the process to use ioctl's to control the kernel module * @@ -2986,9 +2898,9 @@ If you want to use ioctls in your own kernel modules, it is best to receive an o-System Calls
-++System Calls
+-So far, the only thing we've done was to use well defined kernel mechanisms to register /proc files and device handlers. This is fine if you want to do something the kernel programmers thought you'd want, such as write a device driver. But what if you want to do something unusual, to change the behavior of the system in some way? Then, you're mostly on your own.
@@ -3034,6 +2946,7 @@ Note that all the related problems make syscall stealing unfeasiable for product+/* * syscall.c * @@ -3194,13 +3107,12 @@ MODULE_LICENSE("GPL");-Blocking Processes and threads
----Sleep
-++Blocking Processes and threads
+++Sleep
+-What do you do when somebody asks you for something you can't do right away? If you're a human being and you're bothered by a human being, the only thing you can say is: "Not right now, I'm busy. Go away!". But if you're a kernel module and you're bothered by a process, you have another possibility. You can put the process to sleep until you can service it. After all, processes are being put to sleep by the kernel and woken up all the time (that's the way multiple processes appear to run on the same time on a single CPU).
@@ -3210,6 +3122,7 @@ This kernel module is an example of this. The file (called /proc/sleep) c+@@ -3247,6 +3160,7 @@ There is one more point to remember. Some times processes don't want to sleep, ttail -f+hostname:~/lkmpg-examples/09-BlockingProcesses# insmod sleep.ko hostname:~/lkmpg-examples/09-BlockingProcesses# cat_noblock /proc/sleep Last input: @@ -3271,6 +3185,7 @@ hostname:~/lkmpg-examples/09-BlockingProcesses#+/* * sleep.c - create a /proc file, and if several processes try to open it at * the same time, put all but one to sleep @@ -3540,6 +3455,7 @@ DECLARE_WAIT_QUEUE_HEAD(WaitQ);+/* cat_noblock.c - open a file and display its contents, but exit rather than * wait for input */ /* Copyright (C) 1998 by Ori Pomerantz */ @@ -3610,9 +3526,9 @@ DECLARE_WAIT_QUEUE_HEAD(WaitQ);-Completions
-++Completions
+-Sometimes one thing should happen before another within a module having multiple threads. Rather than using /proc/sleep commands the kernel has another way to do this which allows timeouts or interrupts to also happen.
@@ -3622,6 +3538,7 @@ In the following example two threads are started, but one needs to start before+#include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> @@ -3717,21 +3634,22 @@ There are other variations upon the wait_for_completion function, which i-Avoiding Collisions and Deadlocks
-++Avoiding Collisions and Deadlocks
+-If processes running on different CPUs or in different threads try to access the same memory then it's possible that strange things can happen or your system can lock up. To avoid this various types of mutual exclusion kernel functions are available. These indicate if a section of code is "locked" or "unlocked" so that simultaneous attempts to run it can't happen.
-Mutex
-++-Mutex
+You can use kernel mutexes (mutual exclusions) in much the same manner that you might deploy them in userland. This may be all that's needed to avoid collisions in most cases.
+#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> @@ -3776,18 +3694,19 @@ MODULE_LICENSE("GPL");-Spinlocks
-++Spinlocks
+-As the name suggests, spinlocks lock up the CPU that the code is running on, taking 100% of its resources. Because of this you should only use the spinlock mechanism around code which is likely to take no more than a few milliseconds to run and so won't noticably slow anything down from the user's point of view.
-The example here is "irq safe" in that if interrupts happen during the lock then they won't be forgotten and will activate when the unlock happens, using the flags variable to retain their state. +The example here is /"irq safe"/ in that if interrupts happen during the lock then they won't be forgotten and will activate when the unlock happens, using the flags variable to retain their state.
+#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> @@ -3856,14 +3775,15 @@ MODULE_LICENSE("GPL");-Read and write locks
-++Read and write locks
+-Read and write locks are specialised kinds of spinlocks so that you can exclusively read from something or write to something. Like the earlier spinlocks example the one below shows an "irq safe" situation in which if other functions were triggered from irqs which might also read and write to whatever you are concerned with then they wouldn't disrupt the logic. As before it's a good idea to keep anything done within the lock as short as possible so that it doesn't hang up the system and cause users to start revolting against the tyranny of your module.
+#include <linux/kernel.h> #include <linux/module.h> #include <linux/interrupt.h> @@ -3925,14 +3845,15 @@ Of course if you know for sure that there are no functions triggered by irqs whi-Atomic operations
-++-Atomic operations
+If you're doing simple arithmetic: adding, subtracting or bitwise operations then there's another way in the multi-CPU and multi-hyperthreaded world to stop other parts of the system from messing with your mojo. By using atomic operations you can be confident that your addition, subtraction or bit flip did actually happen and wasn't overwritten by some other shenanigans. An example is shown below.
+#include <linux/kernel.h> #include <linux/module.h> #include <linux/interrupt.h> @@ -4014,13 +3935,12 @@ MODULE_LICENSE("GPL");-Replacing Print Macros
----Replacement
-++Replacing Print Macros
+++Replacement
+-In Section 1.2.1.2, I said that X and kernel module programming don't mix. That's true for developing kernel modules, but in actual use, you want to be able to send messages to whichever tty the command to load the module came from.
@@ -4034,6 +3954,7 @@ The way this is done is by using current, a pointer to the currently running tas+/* * print_string.c - Send output to the tty we're running on, regardless if it's * through X11, telnet, etc. We do this by printing the string to the tty @@ -4147,9 +4068,9 @@ module_exit(print_string_exit);-Flashing keyboard LEDs
-++Flashing keyboard LEDs
+-In certain conditions, you may desire a simpler and more direct way to communicate to the external world. Flashing keyboard LEDs can be such a solution: It is an immediate way to attract attention or to display a status condition. Keyboard LEDs are present on every hardware, they are always visible, they do not need any setup, and their use is rather simple and non-intrusive, compared to writing to a tty or a file.
@@ -4159,6 +4080,7 @@ The following source code illustrates a minimal kernel module which, when loaded+/* * kbleds.c - Blink keyboard leds until the module is unloaded. */ @@ -4266,22 +4188,23 @@ While you have seen lots of stuff that can be used to aid debugging here, there-Scheduling Tasks
-++Scheduling Tasks
+-There are two main ways of running tasks: tasklets and work queues. Tasklets are a quick and easy way of scheduling a single function to be run, for example when triggered from an interrupt, whereas work queues are more complicated but also better suited to running multiple things in a sequence.
-Tasklets
-++Tasklets
+-Here's an example tasklet module. The tasklet_fn function runs for a few seconds and in the mean time execution of the example_tasklet_init function continues to the exit point.
+#include <linux/kernel.h> #include <linux/module.h> #include <linux/delay.h> @@ -4325,6 +4248,7 @@ So with this example loaded dmesg should show:+tasklet example init Example tasklet starts Example tasklet init continues... @@ -4333,14 +4257,15 @@ Example tasklet ends-Work queues
-++Work queues
+-To add a task to the scheduler we can use a workqueue. The kernel then uses the Completely Fair Scheduler (CFS) to execute work within the queue.
+#include <linux/module.h> #include <linux/init.h> #include <linux/workqueue.h> @@ -4377,13 +4302,12 @@ MODULE_DESCRIPTION("Workqueue example");-Interrupt Handlers
----Interrupt Handlers
-++Interrupt Handlers
+++-Interrupt Handlers
+Except for the last chapter, everything we did in the kernel so far we've done as a response to a process asking for it, either by dealing with a special file, sending an ioctl(), or issuing a system call. But the job of the kernel isn't just to respond to process requests. Another job, which is every bit as important, is to speak to the hardware connected to the machine.
@@ -4414,9 +4338,9 @@ This function receives the IRQ number, the name of the function, flags, a name f-Detecting button presses
-++Detecting button presses
+-Many popular single board computers, such as Raspberry Pis or Beagleboards, have a bunch of GPIO pins. Attaching buttons to those and then having a button press do something is a classic case in which you might need to use interrupts so that instead of having the CPU waste time and battery power polling for a change in input state it's better for the input to trigger the CPU to then run a particular handling function.
@@ -4426,6 +4350,7 @@ Here's an example where buttons are connected to GPIO numbers 17 and 18 and an L+/* * intrpt.c - Handling GPIO with interrupts * @@ -4582,9 +4507,9 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts"-Bottom Half
-++-Bottom Half
+Suppose you want to do a bunch of stuff inside of an interrupt routine. A common way to do that without rendering the interrupt unavailable for a significant duration is to combine it with a tasklet. This pushes the bulk of the work off into the scheduler.
@@ -4594,6 +4519,7 @@ The example below modifies the previous example to also run an additional task w+/* * bottomhalf.c - Top and bottom half interrupt handling * @@ -4764,22 +4690,23 @@ MODULE_DESCRIPTION("Interrupt with top and bottom half"-Crypto
-++Crypto
+-At the dawn of the internet everybody trusted everybody completely…but that didn't work out so well. When this guide was originally written it was a more innocent era in which almost nobody actually gave a damn about crypto - least of all kernel developers. That's certainly no longer the case now. To handle crypto stuff the kernel has its own API enabling common methods of encryption, decryption and your favourite hash functions.
-Hash functions
-++Hash functions
+Calculating and checking the hashes of things is a common operation. Here is a demonstration of how to calculate a sha256 hash within a kernel module.
+-#include <linux/module.h> #include <crypto/internal/hash.h> @@ -4852,6 +4779,7 @@ Make and install the module:+make sudo insmod cryptosha256.ko dmesg @@ -4867,19 +4795,21 @@ Finally, remove the test module:+sudo rmmod cryptosha256-Symmetric key encryption
-++-Symmetric key encryption
+Here is an example of symmetrically encrypting a string using the AES algorithm and a password.
+#include <crypto/internal/skcipher.h> #include <linux/module.h> #include <linux/crypto.h> @@ -5065,14 +4995,15 @@ MODULE_LICENSE("GPL");-Standardising the interfaces: The Device Model
-++-Standardising the interfaces: The Device Model
+Up to this point we've seen all kinds of modules doing all kinds of things, but there was no consistency in their interfaces with the rest of the kernel. To impose some consistency such that there is at minimum a standardised way to start, suspend and resume a device a device model was added. An example is show below, and you can use this as a template to add your own suspend, resume or other interface functions.
+#include <linux/kernel.h> #include <linux/module.h> #include <linux/platform_device.h> @@ -5173,13 +5104,12 @@ module_exit(devicemodel_exit);-Optimisations
----Likely and Unlikely conditions
-++Optimisations
+++-Likely and Unlikely conditions
+Sometimes you might want your code to run as quickly as possible, especially if it's handling an interrupt or doing something which might cause noticible latency. If your code contains boolean conditions and if you know that the conditions are almost always likely to evaluate as either true or false, then you can allow the compiler to optimise for this using the likely and unlikely macros.
@@ -5189,6 +5119,7 @@ For example, when allocating memory you're almost always expecting this to succe+bvl = bvec_alloc(gfp_mask, nr_iovecs, &idx); if (unlikely(!bvl)) { mempool_free(bio, bio_pool); @@ -5204,35 +5135,35 @@ When the unlikely macro is used the compiler alters its machine instructi-Common Pitfalls
-++Common Pitfalls
+-Before I send you on your way to go out into the world and write kernel modules, there are a few things I need to warn you about. If I fail to warn you and something bad happens, please report the problem to me for a full refund of the amount I was paid for your copy of the book.
-Using standard libraries
-++-Using standard libraries
+You can't do that. In a kernel module you can only use kernel functions, which are the functions you can see in /proc/kallsyms.
-Disabling interrupts
-++-Disabling interrupts
+You might need to do this for a short time and that is OK, but if you don't enable them afterwards, your system will be stuck and you'll have to power it off.
-Sticking your head inside a large carnivore
-++-Sticking your head inside a large carnivore
+I probably don't have to warn you about this, but I figured I will anyway, just in case.
@@ -5240,9 +5171,9 @@ I probably don't have to warn you about this, but I figured I will anyway, just-Where To Go From Here?
-++Where To Go From Here?
+I could easily have squeezed a few more chapters into this book. I could have added a chapter about creating new file systems, or about adding new protocol stacks (as if there's a need for that – you'd have to dig underground to find a protocol stack not supported by Linux). I could have added explanations of the kernel mechanisms we haven't touched upon, such as bootstrapping or the disk interface.
diff --git a/4.15.2/LKMPG-4.15.2.md b/4.15.2/LKMPG-4.15.2.md index eb1259e..f7d8ef6 100644 --- a/4.15.2/LKMPG-4.15.2.md +++ b/4.15.2/LKMPG-4.15.2.md @@ -8,83 +8,83 @@ Table of Contents ----------------- ::: {#text-table-of-contents} -- [Introduction](#org1785162) - - [Authorship](#org63a1b37) - - [Versioning and Notes](#org10863e7) - - [Acknowledgements](#org49d4e59) - - [What Is A Kernel Module?](#org0a5ce13) - - [Kernel module package](#org1cb0938) - - [What Modules are in my Kernel?](#org878e43e) - - [Do I need to download and compile the kernel?](#orgb530dfd) - - [Before We Begin](#org2583533) -- [Headers](#org46d2b5f) -- [Examples](#orgdd7dd5d) -- [Hello World](#org92f8539) - - [The Simplest Module](#org5ae5562) - - [Hello and Goodbye](#org5c5dd06) - - [The \_\_init and \_\_exit Macros](#org7fd362f) - - [Licensing and Module Documentation](#orga52c864) - - [Passing Command Line Arguments to a Module](#org6976bc8) - - [Modules Spanning Multiple Files](#org5a97220) - - [Building modules for a precompiled kernel](#org65d6819) -- [Preliminaries](#orgb5adf2b) - - [How modules begin and end](#org4cf9365) - - [Functions available to modules](#orgcb5e2da) - - [User Space vs Kernel Space](#orgab9f1d1) - - [Name Space](#org9d3f3fc) - - [Code space](#orga3691e9) - - [Device Drivers](#org382352c) -- [Character Device drivers](#orgf1744e4) - - [The file\_operations Structure](#org07ed5b3) - - [The file structure](#orgf283636) - - [Registering A Device](#orgab1c63f) - - [Unregistering A Device](#org85bc5ea) - - [chardev.c](#orgdbd100e) - - [Writing Modules for Multiple Kernel Versions](#org130321b) -- [The /proc File System](#org44e86f2) - - [Read and Write a /proc File](#org330f61f) - - [Manage /proc file with standard filesystem](#orgaae8b16) - - [Manage /proc file with seq\_file](#org688df35) -- [sysfs: Interacting with your module](#orgbd8ce69) -- [Talking To Device Files](#orga1b8e31) -- [System Calls](#orgdf0ab54) -- [Blocking Processes and threads](#org83613ec) - - [Sleep](#org36ce43a) - - [Completions](#orgcfa6577) -- [Avoiding Collisions and Deadlocks](#org3ffdc76) - - [Mutex](#org89ec2e6) - - [Spinlocks](#org9889db8) - - [Read and write locks](#orge2dc3e1) - - [Atomic operations](#org1316854) -- [Replacing Print Macros](#orgbdec3ca) - - [Replacement](#org4579c68) - - [Flashing keyboard LEDs](#orgc9bd69e) -- [Scheduling Tasks](#orgf1f1faf) - - [Tasklets](#orgd815b3b) - - [Work queues](#org9b5b18e) -- [Interrupt Handlers](#org1cdf533) - - [Interrupt Handlers](#org5db45ad) - - [Detecting button presses](#org9ca1a6c) - - [Bottom Half](#org575eebf) -- [Crypto](#org004956f) - - [Hash functions](#org8f45455) - - [Symmetric key encryption](#orgcf62e2d) -- [Standardising the interfaces: The Device Model](#org161334f) -- [Optimisations](#org8639895) - - [Likely and Unlikely conditions](#orgb775e92) -- [Common Pitfalls](#org1cf3372) - - [Using standard libraries](#org8e71ecd) - - [Disabling interrupts](#org9ff7def) - - [Sticking your head inside a large carnivore](#orgdd651cd) -- [Where To Go From Here?](#orgcf6e3aa) +- [Introduction](#sec-1) + - [Authorship](#sec-1-1) + - [Versioning and Notes](#sec-1-2) + - [Acknowledgements](#sec-1-3) + - [What Is A Kernel Module?](#sec-1-4) + - [Kernel module package](#sec-1-5) + - [What Modules are in my Kernel?](#sec-1-6) + - [Do I need to download and compile the kernel?](#sec-1-7) + - [Before We Begin](#sec-1-8) +- [Headers](#sec-2) +- [Examples](#sec-3) +- [Hello World](#sec-4) + - [The Simplest Module](#sec-4-1) + - [Hello and Goodbye](#sec-4-2) + - [The \_\_init and \_\_exit Macros](#sec-4-3) + - [Licensing and Module Documentation](#sec-4-4) + - [Passing Command Line Arguments to a Module](#sec-4-5) + - [Modules Spanning Multiple Files](#sec-4-6) + - [Building modules for a precompiled kernel](#sec-4-7) +- [Preliminaries](#sec-5) + - [How modules begin and end](#sec-5-1) + - [Functions available to modules](#sec-5-2) + - [User Space vs Kernel Space](#sec-5-3) + - [Name Space](#sec-5-4) + - [Code space](#sec-5-5) + - [Device Drivers](#sec-5-6) +- [Character Device drivers](#sec-6) + - [The file\_operations Structure](#sec-6-1) + - [The file structure](#sec-6-2) + - [Registering A Device](#sec-6-3) + - [Unregistering A Device](#sec-6-4) + - [chardev.c](#sec-6-5) + - [Writing Modules for Multiple Kernel Versions](#sec-6-6) +- [The /proc File System](#sec-7) + - [Read and Write a /proc File](#sec-7-1) + - [Manage /proc file with standard filesystem](#sec-7-2) + - [Manage /proc file with seq\_file](#sec-7-3) +- [sysfs: Interacting with your module](#sec-8) +- [Talking To Device Files](#sec-9) +- [System Calls](#sec-10) +- [Blocking Processes and threads](#sec-11) + - [Sleep](#sec-11-1) + - [Completions](#sec-11-2) +- [Avoiding Collisions and Deadlocks](#sec-12) + - [Mutex](#sec-12-1) + - [Spinlocks](#sec-12-2) + - [Read and write locks](#sec-12-3) + - [Atomic operations](#sec-12-4) +- [Replacing Print Macros](#sec-13) + - [Replacement](#sec-13-1) + - [Flashing keyboard LEDs](#sec-13-2) +- [Scheduling Tasks](#sec-14) + - [Tasklets](#sec-14-1) + - [Work queues](#sec-14-2) +- [Interrupt Handlers](#sec-15) + - [Interrupt Handlers](#sec-15-1) + - [Detecting button presses](#sec-15-2) + - [Bottom Half](#sec-15-3) +- [Crypto](#sec-16) + - [Hash functions](#sec-16-1) + - [Symmetric key encryption](#sec-16-2) +- [Standardising the interfaces: The Device Model](#sec-17) +- [Optimisations](#sec-18) + - [Likely and Unlikely conditions](#sec-18-1) +- [Common Pitfalls](#sec-19) + - [Using standard libraries](#sec-19-1) + - [Disabling interrupts](#sec-19-2) + - [Sticking your head inside a large carnivore](#sec-19-3) +- [Where To Go From Here?](#sec-20) ::: ::: -::: {#outline-container-org1785162 .outline-2} -Introduction {#org1785162} +::: {#outline-container-sec-1 .outline-2} +Introduction {#sec-1} ------------ -::: {#text-org1785162 .outline-text-2} +::: {#text-1 .outline-text-2} The Linux Kernel Module Programming Guide is a free book; you may reproduce and/or modify it under the terms of the Open Software License, version 3.0. @@ -105,7 +105,7 @@ the Open Software License, and the original copyright notice must remain intact. If you have contributed new material to this book, you must make the material and source code available for your revisions. Please make revisions and updates available directly to the document maintainer, -Peter Jay Salzman \. This will allow for the merging of +Peter Jay Salzman \
. This will allow for the merging of updates and provide consistent revisions to the Linux community. If you publish or distribute this book commercially, donations, @@ -116,10 +116,10 @@ LDP. If you have questions or comments, please contact the address above. ::: -::: {#outline-container-org63a1b37 .outline-3} -### Authorship {#org63a1b37} +::: {#outline-container-sec-1-1 .outline-3} +### Authorship {#sec-1-1} -::: {#text-org63a1b37 .outline-text-3} +::: {#text-1-1 .outline-text-3} The Linux Kernel Module Programming Guide was originally written for the 2.2 kernels by Ori Pomerantz. Eventually, Ori no longer had time to maintain the document. After all, the Linux kernel is a fast moving @@ -132,10 +132,10 @@ other chapters. ::: ::: -::: {#outline-container-org10863e7 .outline-3} -### Versioning and Notes {#org10863e7} +::: {#outline-container-sec-1-2 .outline-3} +### Versioning and Notes {#sec-1-2} -::: {#text-org10863e7 .outline-text-3} +::: {#text-1-2 .outline-text-3} The Linux kernel is a moving target. There has always been a question whether the LKMPG should remove deprecated information or keep it around for historical sake. Michael Burian and I decided to create a new branch @@ -150,20 +150,20 @@ I can\'t promise anything. ::: ::: -::: {#outline-container-org49d4e59 .outline-3} -### Acknowledgements {#org49d4e59} +::: {#outline-container-sec-1-3 .outline-3} +### Acknowledgements {#sec-1-3} -::: {#text-org49d4e59 .outline-text-3} +::: {#text-1-3 .outline-text-3} The following people have contributed corrections or good suggestions: Ignacio Martin, David Porter, Daniele Paolo Scarpazza, Dimo Velev, Francois Audeon, Horst Schirmeier, Bob Mottram and Roman Lakeev. ::: ::: -::: {#outline-container-org0a5ce13 .outline-3} -### What Is A Kernel Module? {#org0a5ce13} +::: {#outline-container-sec-1-4 .outline-3} +### What Is A Kernel Module? {#sec-1-4} -::: {#text-org0a5ce13 .outline-text-3} +::: {#text-1-4 .outline-text-3} So, you want to write a kernel module. You know C, you\'ve written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your @@ -181,10 +181,10 @@ time we want new functionality. ::: ::: -::: {#outline-container-org1cb0938 .outline-3} -### Kernel module package {#org1cb0938} +::: {#outline-container-sec-1-5 .outline-3} +### Kernel module package {#sec-1-5} -::: {#text-org1cb0938 .outline-text-3} +::: {#text-1-5 .outline-text-3} Linux distros provide the commands *modprobe*, *insmod* and *depmod* within a package. @@ -206,10 +206,10 @@ sudo pacman -S gcc kmod ::: ::: -::: {#outline-container-org878e43e .outline-3} -### What Modules are in my Kernel? {#org878e43e} +::: {#outline-container-sec-1-6 .outline-3} +### What Modules are in my Kernel? {#sec-1-6} -::: {#text-org878e43e .outline-text-3} +::: {#text-1-6 .outline-text-3} To discover what modules are already loaded within your current kernel use the command **lsmod**. @@ -239,10 +239,10 @@ sudo lsmod | grep fat ::: ::: -::: {#outline-container-orgb530dfd .outline-3} -### Do I need to download and compile the kernel? {#orgb530dfd} +::: {#outline-container-sec-1-7 .outline-3} +### Do I need to download and compile the kernel? {#sec-1-7} -::: {#text-orgb530dfd .outline-text-3} +::: {#text-1-7 .outline-text-3} For the purposes of following this guide you don\'t necessarily need to do that. However, it would be wise to run the examples within a test distro running on a virtual machine in order to avoid any possibility of @@ -250,10 +250,10 @@ messing up your system. ::: ::: -::: {#outline-container-org2583533 .outline-3} -### Before We Begin {#org2583533} +::: {#outline-container-sec-1-8 .outline-3} +### Before We Begin {#sec-1-8} -::: {#text-org2583533 .outline-text-3} +::: {#text-1-8 .outline-text-3} Before we delve into code, there are a few issues we need to cover. Everyone\'s system is different and everyone has their own groove. Getting your first \"hello world\" program to compile and load correctly @@ -262,8 +262,8 @@ hurdle of doing it for the first time, it will be smooth sailing thereafter. ::: -- []{#org4517450}Modversioning\ - ::: {#text-org4517450 .outline-text-5} +- []{#sec-1-8-0-1}Modversioning\ + ::: {#text-1-8-0-1 .outline-text-5} A module compiled for one kernel won\'t load if you boot a different kernel unless you enable CONFIG\_MODVERSIONS in the kernel. We won\'t go into module versioning until later in this guide. Until we @@ -274,8 +274,8 @@ thereafter. kernel with modversioning turned off. ::: -- []{#orgdc404d5}Using X\ - ::: {#text-orgdc404d5 .outline-text-5} +- []{#sec-1-8-0-2}Using X\ + ::: {#text-1-8-0-2 .outline-text-5} It is highly recommended that you type in, compile and load all the examples this guide discusses. It\'s also highly recommended you do this from a console. You should not be working on this stuff in X. @@ -291,11 +291,11 @@ thereafter. ::: ::: -::: {#outline-container-org46d2b5f .outline-2} -Headers {#org46d2b5f} +::: {#outline-container-sec-2 .outline-2} +Headers {#sec-2} ------- -::: {#text-org46d2b5f .outline-text-2} +::: {#text-2 .outline-text-2} Before you can build anything you\'ll need to install the header files for your kernel. On Parabola GNU/Linux: @@ -325,11 +325,11 @@ sudo apt-get install kmod linux-headers-4.15.2-1-amd64 ::: ::: -::: {#outline-container-orgdd7dd5d .outline-2} -Examples {#orgdd7dd5d} +::: {#outline-container-sec-3 .outline-2} +Examples {#sec-3} -------- -::: {#text-orgdd7dd5d .outline-text-2} +::: {#text-3 .outline-text-2} All the examples from this document are available within the *examples* subdirectory. To test that they compile: @@ -345,17 +345,17 @@ version or need to install the corresponding kernel header files. ::: ::: -::: {#outline-container-org92f8539 .outline-2} -Hello World {#org92f8539} +::: {#outline-container-sec-4 .outline-2} +Hello World {#sec-4} ----------- -::: {#text-org92f8539 .outline-text-2} +::: {#text-4 .outline-text-2} ::: -::: {#outline-container-org5ae5562 .outline-3} -### The Simplest Module {#org5ae5562} +::: {#outline-container-sec-4-1 .outline-3} +### The Simplest Module {#sec-4-1} -::: {#text-org5ae5562 .outline-text-3} +::: {#text-4-1 .outline-text-3} Most people learning programming start out with some sort of \"*hello world*\" example. I don\'t know what happens to people who break with this tradition, but I think it\'s safer not to find out. We\'ll start @@ -498,8 +498,8 @@ to include **linux/kernel.h** only for the macro expansion for the pr\_alert() log level, which you\'ll learn about in Section 2.1.1. ::: -- []{#org8e886ba}A point about coding style\ - ::: {#text-org8e886ba .outline-text-5} +- []{#sec-4-1-0-1}A point about coding style\ + ::: {#text-4-1-0-1 .outline-text-5} Another thing which may not be immediately obvious to anyone getting started with kernel programming is that indentation within your code should be using **tabs** and **not spaces**. It\'s one of the coding @@ -507,8 +507,8 @@ pr\_alert() log level, which you\'ll learn about in Section 2.1.1. get used to it if you ever submit a patch upstream. ::: -- []{#org912c90e}Introducing print macros\ - ::: {#text-org912c90e .outline-text-5} +- []{#sec-4-1-0-2}Introducing print macros\ + ::: {#text-4-1-0-2 .outline-text-5} In the beginning there was **printk**, usually followed by a priority such as KERN\_INFO or KERN\_DEBUG. More recently this can also be expressed in abbreviated form using a set of print macros, @@ -518,8 +518,8 @@ pr\_alert() log level, which you\'ll learn about in Section 2.1.1. priority macros. ::: -- []{#org9ba89c7}About Compiling\ - ::: {#text-org9ba89c7 .outline-text-5} +- []{#sec-4-1-0-3}About Compiling\ + ::: {#text-4-1-0-3 .outline-text-5} Kernel modules need to be compiled a bit differently from regular userspace apps. Former kernel versions required us to care much about these settings, which are usually stored in Makefiles. @@ -545,10 +545,10 @@ pr\_alert() log level, which you\'ll learn about in Section 2.1.1. ::: ::: -::: {#outline-container-org5c5dd06 .outline-3} -### Hello and Goodbye {#org5c5dd06} +::: {#outline-container-sec-4-2 .outline-3} +### Hello and Goodbye {#sec-4-2} -::: {#text-org5c5dd06 .outline-text-3} +::: {#text-4-2 .outline-text-3} In early kernel versions you had to use the **init\_module** and **cleanup\_module** functions, as in the first hello world example, but these days you can name those anything you want by using the @@ -611,10 +611,10 @@ something like that. ::: ::: -::: {#outline-container-org7fd362f .outline-3} -### The \_\_init and \_\_exit Macros {#org7fd362f} +::: {#outline-container-sec-4-3 .outline-3} +### The \_\_init and \_\_exit Macros {#sec-4-3} -::: {#text-org7fd362f .outline-text-3} +::: {#text-4-3 .outline-text-3} This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The **\_\_init** macro causes the init function to be discarded and its memory freed once @@ -664,10 +664,10 @@ module_exit(hello_3_exit); ::: ::: -::: {#outline-container-orga52c864 .outline-3} -### Licensing and Module Documentation {#orga52c864} +::: {#outline-container-sec-4-4 .outline-3} +### Licensing and Module Documentation {#sec-4-4} -::: {#text-orga52c864 .outline-text-3} +::: {#text-4-4 .outline-text-3} Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this: @@ -721,10 +721,10 @@ module_exit(cleanup_hello_4); ::: ::: -::: {#outline-container-org6976bc8 .outline-3} -### Passing Command Line Arguments to a Module {#org6976bc8} +::: {#outline-container-sec-4-5 .outline-3} +### Passing Command Line Arguments to a Module {#sec-4-5} -::: {#text-org6976bc8 .outline-text-3} +::: {#text-4-5 .outline-text-3} Modules can take command line arguments, but not with the argc/argv you might be used to. @@ -763,7 +763,7 @@ module_param_array(myintarray, int, NULL, 0); /* not interested in count */ short myshortarray[4]; int count; -module_parm_array(myshortarray, short, &count, 0); /* put count into "count" variable */ +module_param_array(myshortarray, short, &count, 0); /* put count into "count" variable */ ``` ::: @@ -886,10 +886,10 @@ hello-5.o: invalid argument syntax for mylong: 'h' ::: ::: -::: {#outline-container-org5a97220 .outline-3} -### Modules Spanning Multiple Files {#org5a97220} +::: {#outline-container-sec-4-6 .outline-3} +### Modules Spanning Multiple Files {#sec-4-6} -::: {#text-org5a97220 .outline-text-3} +::: {#text-4-6 .outline-text-3} Sometimes it makes sense to divide a kernel module between several source files. @@ -957,10 +957,10 @@ module, second we tell make what object files are part of that module. ::: ::: -::: {#outline-container-org65d6819 .outline-3} -### Building modules for a precompiled kernel {#org65d6819} +::: {#outline-container-sec-4-7 .outline-3} +### Building modules for a precompiled kernel {#sec-4-7} -::: {#text-org65d6819 .outline-text-3} +::: {#text-4-7 .outline-text-3} Obviously, we strongly suggest you to recompile your kernel, so that you can enable a number of useful debugging features, such as forced module unloading (**MODULE\_FORCE\_UNLOAD**): when this option is enabled, you @@ -1094,17 +1094,17 @@ any errors. ::: ::: -::: {#outline-container-orgb5adf2b .outline-2} -Preliminaries {#orgb5adf2b} +::: {#outline-container-sec-5 .outline-2} +Preliminaries {#sec-5} ------------- -::: {#text-orgb5adf2b .outline-text-2} +::: {#text-5 .outline-text-2} ::: -::: {#outline-container-org4cf9365 .outline-3} -### How modules begin and end {#org4cf9365} +::: {#outline-container-sec-5-1 .outline-3} +### How modules begin and end {#sec-5-1} -::: {#text-org4cf9365 .outline-text-3} +::: {#text-5-1 .outline-text-3} A program usually begins with a **main()** function, executes a bunch of instructions and terminates upon completion of those instructions. Kernel modules work a bit differently. A module always begin with either @@ -1128,10 +1128,10 @@ cleanup\_module, I think you\'ll know what I mean. ::: ::: -::: {#outline-container-orgcb5e2da .outline-3} -### Functions available to modules {#orgcb5e2da} +::: {#outline-container-sec-5-2 .outline-3} +### Functions available to modules {#sec-5-2} -::: {#text-orgcb5e2da .outline-text-3} +::: {#text-5-2 .outline-text-3} Programmers use functions they don\'t define all the time. A prime example of this is **printf()**. You use these library functions which are provided by the standard C library, libc. The definitions for these @@ -1196,10 +1196,10 @@ everytime someone tries to delete a file on your system. ::: ::: -::: {#outline-container-orgab9f1d1 .outline-3} -### User Space vs Kernel Space {#orgab9f1d1} +::: {#outline-container-sec-5-3 .outline-3} +### User Space vs Kernel Space {#sec-5-3} -::: {#text-orgab9f1d1 .outline-text-3} +::: {#text-5-3 .outline-text-3} A kernel is all about access to resources, whether the resource in question happens to be a video card, a hard drive or even memory. Programs often compete for the same resource. As I just saved this @@ -1222,10 +1222,10 @@ returns and execution gets transfered back to user mode. ::: ::: -::: {#outline-container-org9d3f3fc .outline-3} -### Name Space {#org9d3f3fc} +::: {#outline-container-sec-5-4 .outline-3} +### Name Space {#sec-5-4} -::: {#text-org9d3f3fc .outline-text-3} +::: {#text-5-4 .outline-text-3} When you write a small C program, you use variables which are convenient and make sense to the reader. If, on the other hand, you\'re writing routines which will be part of a bigger problem, any global variables @@ -1250,10 +1250,10 @@ share the kernel\'s codespace. ::: ::: -::: {#outline-container-orga3691e9 .outline-3} -### Code space {#orga3691e9} +::: {#outline-container-sec-5-5 .outline-3} +### Code space {#sec-5-5} -::: {#text-orga3691e9 .outline-text-3} +::: {#text-5-5 .outline-text-3} Memory management is a very complicated subject and the majority of O\'Reilly\'s \"*Understanding The Linux Kernel*\" exclusively covers memory management! We\'re not setting out to be experts on memory @@ -1285,17 +1285,17 @@ worse than it sounds, so try your best to be careful. By the way, I would like to point out that the above discussion is true for any operating system which uses a monolithic kernel. This isn\'t -quite the same thing as *\"building all your modules into the kernel\"*, +quite the same thing as /\"building all your modules into the kernel\"/, although the idea is the same. There are things called microkernels which have modules which get their own codespace. The GNU Hurd and the Magenta kernel of Google Fuchsia are two examples of a microkernel. ::: ::: -::: {#outline-container-org382352c .outline-3} -### Device Drivers {#org382352c} +::: {#outline-container-sec-5-6 .outline-3} +### Device Drivers {#sec-5-6} -::: {#text-org382352c .outline-text-3} +::: {#text-5-6 .outline-text-3} One class of module is the device driver, which provides functionality for hardware like a serial port. On unix, each piece of hardware is represented by a file located in /dev named a device file which provides @@ -1306,8 +1306,8 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use /dev/sound without ever knowing what kind of sound card is installed. ::: -- []{#org4e199a6}Major and Minor Numbers\ - ::: {#text-org4e199a6 .outline-text-5} +- []{#sec-5-6-0-1}Major and Minor Numbers\ + ::: {#text-5-6-0-1 .outline-text-5} Let\'s look at some device files. Here are device files which represent the first three partitions on the primary master IDE hard drive: @@ -1384,7 +1384,7 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use thing that cares about the minor number. It uses the minor number to distinguish between different pieces of hardware. - By the way, when I say *\"hardware\"*, I mean something a bit more + By the way, when I say /\"hardware\"/, I mean something a bit more abstract than a PCI card that you can hold in your hand. Look at these two device files: @@ -1406,17 +1406,17 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use ::: ::: -::: {#outline-container-orgf1744e4 .outline-2} -Character Device drivers {#orgf1744e4} +::: {#outline-container-sec-6 .outline-2} +Character Device drivers {#sec-6} ------------------------ -::: {#text-orgf1744e4 .outline-text-2} +::: {#text-6 .outline-text-2} ::: -::: {#outline-container-org07ed5b3 .outline-3} -### The file\_operations Structure {#org07ed5b3} +::: {#outline-container-sec-6-1 .outline-3} +### The file\_operations Structure {#sec-6-1} -::: {#text-org07ed5b3 .outline-text-3} +::: {#text-6-1 .outline-text-3} The file\_operations structure is defined in **/usr/include/linux/fs.h**, and holds pointers to functions defined by the driver that perform various operations on the device. Each field of @@ -1511,10 +1511,10 @@ named fops. ::: ::: -::: {#outline-container-orgf283636 .outline-3} -### The file structure {#orgf283636} +::: {#outline-container-sec-6-2 .outline-3} +### The file structure {#sec-6-2} -::: {#text-orgf283636 .outline-text-3} +::: {#text-6-2 .outline-text-3} Each device is represented in the kernel by a file structure, which is defined in **linux/fs.h**. Be aware that a file is a kernel level structure and never appears in a user space program. It\'s not the same @@ -1533,10 +1533,10 @@ only use structures contained in file which are created elsewhere. ::: ::: -::: {#outline-container-orgab1c63f .outline-3} -### Registering A Device {#orgab1c63f} +::: {#outline-container-sec-6-3 .outline-3} +### Registering A Device {#sec-6-3} -::: {#text-orgab1c63f .outline-text-3} +::: {#text-6-3 .outline-text-3} As discussed earlier, char devices are accessed through device files, usually located in /dev. This is by convention. When writing a driver, it\'s OK to put the device file in your current directory. Just make @@ -1585,10 +1585,10 @@ registration and **device\_destroy** during the call to cleanup\_module. ::: ::: -::: {#outline-container-org85bc5ea .outline-3} -### Unregistering A Device {#org85bc5ea} +::: {#outline-container-sec-6-4 .outline-3} +### Unregistering A Device {#sec-6-4} -::: {#text-org85bc5ea .outline-text-3} +::: {#text-6-4 .outline-text-3} We can\'t allow the kernel module to be rmmod\'ed whenever root feels like it. If the device file is opened by a process and then we remove the kernel module, using the file would cause a call to the memory @@ -1622,10 +1622,10 @@ sooner or later during a module\'s development. ::: ::: -::: {#outline-container-orgdbd100e .outline-3} -### chardev.c {#orgdbd100e} +::: {#outline-container-sec-6-5 .outline-3} +### chardev.c {#sec-6-5} -::: {#text-orgdbd100e .outline-text-3} +::: {#text-6-5 .outline-text-3} The next code sample creates a char driver named chardev. You can cat its device file. @@ -1831,10 +1831,10 @@ static ssize_t device_write(struct file *filp, ::: ::: -::: {#outline-container-org130321b .outline-3} -### Writing Modules for Multiple Kernel Versions {#org130321b} +::: {#outline-container-sec-6-6 .outline-3} +### Writing Modules for Multiple Kernel Versions {#sec-6-6} -::: {#text-org130321b .outline-text-3} +::: {#text-6-6 .outline-text-3} The system calls, which are the major interface the kernel shows to the processes, generally stay the same across versions. A new system call may be added, but usually the old ones will behave exactly like they @@ -1882,11 +1882,11 @@ archives if you\'re interested in the full story. ::: ::: -::: {#outline-container-org44e86f2 .outline-2} -The /proc File System {#org44e86f2} +::: {#outline-container-sec-7 .outline-2} +The /proc File System {#sec-7} --------------------- -::: {#text-org44e86f2 .outline-text-2} +::: {#text-7 .outline-text-2} In Linux, there is an additional mechanism for the kernel and kernel modules to send information to processes --- the **/proc** file system. Originally designed to allow easy access to information about processes @@ -1999,10 +1999,10 @@ void cleanup_module() ::: ::: -::: {#outline-container-org330f61f .outline-3} -### Read and Write a /proc File {#org330f61f} +::: {#outline-container-sec-7-1 .outline-3} +### Read and Write a /proc File {#sec-7-1} -::: {#text-org330f61f .outline-text-3} +::: {#text-7-1 .outline-text-3} We have seen a very simple example for a /proc file where we only read the file /proc/helloworld. It\'s also possible to write in a /proc file. It works the same way as read, a function is called when the /proc file @@ -2136,10 +2136,10 @@ void cleanup_module() ::: ::: -::: {#outline-container-orgaae8b16 .outline-3} -### Manage /proc file with standard filesystem {#orgaae8b16} +::: {#outline-container-sec-7-2 .outline-3} +### Manage /proc file with standard filesystem {#sec-7-2} -::: {#text-orgaae8b16 .outline-text-3} +::: {#text-7-2 .outline-text-3} We have seen how to read and write a /proc file with the /proc interface. But it\'s also possible to manage /proc file with inodes. The main concern is to use advanced functions, like permissions. @@ -2276,10 +2276,10 @@ you want to document something kernel related yourself. ::: ::: -::: {#outline-container-org688df35 .outline-3} -### Manage /proc file with seq\_file {#org688df35} +::: {#outline-container-sec-7-3 .outline-3} +### Manage /proc file with seq\_file {#sec-7-3} -::: {#text-org688df35 .outline-text-3} +::: {#text-7-3 .outline-text-3} As we have seen, writing a /proc file may be quite \"complex\". So to help people writting /proc file, there is an API named seq\_file that helps formating a /proc file for output. It\'s based on sequence, which @@ -2456,11 +2456,11 @@ You can also read the code of fs/seq\_file.c in the linux kernel. ::: ::: -::: {#outline-container-orgbd8ce69 .outline-2} -sysfs: Interacting with your module {#orgbd8ce69} +::: {#outline-container-sec-8 .outline-2} +sysfs: Interacting with your module {#sec-8} ----------------------------------- -::: {#text-orgbd8ce69 .outline-text-2} +::: {#text-8 .outline-text-2} *sysfs* allows you to interact with the running kernel from userspace by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for applications or scripts. @@ -2477,74 +2477,8 @@ An example of a hello world module which includes the creation of a variable accessible via sysfs is given below. ::: {.org-src-container} -``` {.src .src-c} -/* - * hello-sysfs.c sysfs example - */ - -#include
-#include -#include -#include -#include -#include - -MODULE_LICENSE("GPL"); -MODULE_AUTHOR("Bob Mottram"); - -static struct kobject *mymodule; - -/* the variable you want to be able to change */ -static int myvariable = 0; - -static ssize_t myvariable_show(struct kobject *kobj, - struct kobj_attribute *attr, - char *buf) -{ - return sprintf(buf, "%d\n", myvariable); -} - -static ssize_t myvariable_store(struct kobject *kobj, - struct kobj_attribute *attr, - char *buf, size_t count) -{ - sscanf(buf, "%du", &myvariable); - return count; -} - - -static struct kobj_attribute myvariable_attribute = - __ATTR(myvariable, 0660, myvariable_show, - (void*)myvariable_store); - -static int __init mymodule_init (void) -{ - int error = 0; - - pr_info("mymodule: initialised\n"); - - mymodule = - kobject_create_and_add("mymodule", kernel_kobj); - if (!mymodule) - return -ENOMEM; - - error = sysfs_create_file(mymodule, &myvariable_attribute.attr); - if (error) { - pr_info("failed to create the myvariable file " \ - "in /sys/kernel/mymodule\n"); - } - - return error; -} - -static void __exit mymodule_exit (void) -{ - pr_info("mymodule: Exit success\n"); - kobject_put(mymodule); -} - -module_init(mymodule_init); -module_exit(mymodule_exit); +``` {.src .src- hello-sysfs.c"lang"hello-sysfs.c"switches"hello-sysfs.c"flags"=""} +"hello-sysfs.c"body ``` ::: @@ -2592,11 +2526,11 @@ sudo rmmod hello_sysfs ::: ::: -::: {#outline-container-orga1b8e31 .outline-2} -Talking To Device Files {#orga1b8e31} +::: {#outline-container-sec-9 .outline-2} +Talking To Device Files {#sec-9} ----------------------- -::: {#text-orga1b8e31 .outline-text-2} +::: {#text-9 .outline-text-2} Device files are supposed to represent physical devices. Most physical devices are used for output as well as input, so there has to be some mechanism for device drivers in the kernel to get the output to send to @@ -3118,11 +3052,11 @@ int main() ::: ::: -::: {#outline-container-orgdf0ab54 .outline-2} -System Calls {#orgdf0ab54} +::: {#outline-container-sec-10 .outline-2} +System Calls {#sec-10} ------------ -::: {#text-orgdf0ab54 .outline-text-2} +::: {#text-10 .outline-text-2} So far, the only thing we\'ve done was to use well defined kernel mechanisms to register **/proc** files and device handlers. This is fine if you want to do something the kernel programmers thought you\'d want, @@ -3396,17 +3330,17 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-org83613ec .outline-2} -Blocking Processes and threads {#org83613ec} +::: {#outline-container-sec-11 .outline-2} +Blocking Processes and threads {#sec-11} ------------------------------ -::: {#text-org83613ec .outline-text-2} +::: {#text-11 .outline-text-2} ::: -::: {#outline-container-org36ce43a .outline-3} -### Sleep {#org36ce43a} +::: {#outline-container-sec-11-1 .outline-3} +### Sleep {#sec-11-1} -::: {#text-org36ce43a .outline-text-3} +::: {#text-11-1 .outline-text-3} What do you do when somebody asks you for something you can\'t do right away? If you\'re a human being and you\'re bothered by a human being, the only thing you can say is: \"*Not right now, I\'m busy. Go away!*\". @@ -3850,10 +3784,10 @@ int main(int argc, char *argv[]) ::: ::: -::: {#outline-container-orgcfa6577 .outline-3} -### Completions {#orgcfa6577} +::: {#outline-container-sec-11-2 .outline-3} +### Completions {#sec-11-2} -::: {#text-orgcfa6577 .outline-text-3} +::: {#text-11-2 .outline-text-3} Sometimes one thing should happen before another within a module having multiple threads. Rather than using **/proc/sleep** commands the kernel has another way to do this which allows timeouts or interrupts to also @@ -3960,11 +3894,11 @@ enough for many common situations without adding a lot of complexity. ::: ::: -::: {#outline-container-org3ffdc76 .outline-2} -Avoiding Collisions and Deadlocks {#org3ffdc76} +::: {#outline-container-sec-12 .outline-2} +Avoiding Collisions and Deadlocks {#sec-12} --------------------------------- -::: {#text-org3ffdc76 .outline-text-2} +::: {#text-12 .outline-text-2} If processes running on different CPUs or in different threads try to access the same memory then it\'s possible that strange things can happen or your system can lock up. To avoid this various types of mutual @@ -3973,10 +3907,10 @@ code is \"locked\" or \"unlocked\" so that simultaneous attempts to run it can\'t happen. ::: -::: {#outline-container-org89ec2e6 .outline-3} -### Mutex {#org89ec2e6} +::: {#outline-container-sec-12-1 .outline-3} +### Mutex {#sec-12-1} -::: {#text-org89ec2e6 .outline-text-3} +::: {#text-12-1 .outline-text-3} You can use kernel mutexes (mutual exclusions) in much the same manner that you might deploy them in userland. This may be all that\'s needed to avoid collisions in most cases. @@ -4028,17 +3962,17 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-org9889db8 .outline-3} -### Spinlocks {#org9889db8} +::: {#outline-container-sec-12-2 .outline-3} +### Spinlocks {#sec-12-2} -::: {#text-org9889db8 .outline-text-3} +::: {#text-12-2 .outline-text-3} As the name suggests, spinlocks lock up the CPU that the code is running on, taking 100% of its resources. Because of this you should only use the spinlock mechanism around code which is likely to take no more than a few milliseconds to run and so won\'t noticably slow anything down from the user\'s point of view. -The example here is *\"irq safe\"* in that if interrupts happen during +The example here is /\"irq safe\"/ in that if interrupts happen during the lock then they won\'t be forgotten and will activate when the unlock happens, using the *flags* variable to retain their state. @@ -4112,10 +4046,10 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-orge2dc3e1 .outline-3} -### Read and write locks {#orge2dc3e1} +::: {#outline-container-sec-12-3 .outline-3} +### Read and write locks {#sec-12-3} -::: {#text-orge2dc3e1 .outline-text-3} +::: {#text-12-3 .outline-text-3} Read and write locks are specialised kinds of spinlocks so that you can exclusively read from something or write to something. Like the earlier spinlocks example the one below shows an \"irq safe\" situation in which @@ -4190,10 +4124,10 @@ corresponding write functions. ::: ::: -::: {#outline-container-org1316854 .outline-3} -### Atomic operations {#org1316854} +::: {#outline-container-sec-12-4 .outline-3} +### Atomic operations {#sec-12-4} -::: {#text-org1316854 .outline-text-3} +::: {#text-12-4 .outline-text-3} If you\'re doing simple arithmetic: adding, subtracting or bitwise operations then there\'s another way in the multi-CPU and multi-hyperthreaded world to stop other parts of the system from messing @@ -4285,17 +4219,17 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-orgbdec3ca .outline-2} -Replacing Print Macros {#orgbdec3ca} +::: {#outline-container-sec-13 .outline-2} +Replacing Print Macros {#sec-13} ---------------------- -::: {#text-orgbdec3ca .outline-text-2} +::: {#text-13 .outline-text-2} ::: -::: {#outline-container-org4579c68 .outline-3} -### Replacement {#org4579c68} +::: {#outline-container-sec-13-1 .outline-3} +### Replacement {#sec-13-1} -::: {#text-org4579c68 .outline-text-3} +::: {#text-13-1 .outline-text-3} In Section 1.2.1.2, I said that X and kernel module programming don\'t mix. That\'s true for developing kernel modules, but in actual use, you want to be able to send messages to whichever tty the command to load @@ -4427,10 +4361,10 @@ module_exit(print_string_exit); ::: ::: -::: {#outline-container-orgc9bd69e .outline-3} -### Flashing keyboard LEDs {#orgc9bd69e} +::: {#outline-container-sec-13-2 .outline-3} +### Flashing keyboard LEDs {#sec-13-2} -::: {#text-orgc9bd69e .outline-text-3} +::: {#text-13-2 .outline-text-3} In certain conditions, you may desire a simpler and more direct way to communicate to the external world. Flashing keyboard LEDs can be such a solution: It is an immediate way to attract attention or to display a @@ -4559,11 +4493,11 @@ minimum and make sure it does not show up in production code. ::: ::: -::: {#outline-container-orgf1f1faf .outline-2} -Scheduling Tasks {#orgf1f1faf} +::: {#outline-container-sec-14 .outline-2} +Scheduling Tasks {#sec-14} ---------------- -::: {#text-orgf1f1faf .outline-text-2} +::: {#text-14 .outline-text-2} There are two main ways of running tasks: tasklets and work queues. Tasklets are a quick and easy way of scheduling a single function to be run, for example when triggered from an interrupt, whereas work queues @@ -4571,10 +4505,10 @@ are more complicated but also better suited to running multiple things in a sequence. ::: -::: {#outline-container-orgd815b3b .outline-3} -### Tasklets {#orgd815b3b} +::: {#outline-container-sec-14-1 .outline-3} +### Tasklets {#sec-14-1} -::: {#text-orgd815b3b .outline-text-3} +::: {#text-14-1 .outline-text-3} Here\'s an example tasklet module. The *tasklet\_fn* function runs for a few seconds and in the mean time execution of the *example\_tasklet\_init* function continues to the exit point. @@ -4632,10 +4566,10 @@ Example tasklet ends ::: ::: -::: {#outline-container-org9b5b18e .outline-3} -### Work queues {#org9b5b18e} +::: {#outline-container-sec-14-2 .outline-3} +### Work queues {#sec-14-2} -::: {#text-org9b5b18e .outline-text-3} +::: {#text-14-2 .outline-text-3} To add a task to the scheduler we can use a workqueue. The kernel then uses the Completely Fair Scheduler (CFS) to execute work within the queue. @@ -4678,17 +4612,17 @@ MODULE_DESCRIPTION("Workqueue example"); ::: ::: -::: {#outline-container-org1cdf533 .outline-2} -Interrupt Handlers {#org1cdf533} +::: {#outline-container-sec-15 .outline-2} +Interrupt Handlers {#sec-15} ------------------ -::: {#text-org1cdf533 .outline-text-2} +::: {#text-15 .outline-text-2} ::: -::: {#outline-container-org5db45ad .outline-3} -### Interrupt Handlers {#org5db45ad} +::: {#outline-container-sec-15-1 .outline-3} +### Interrupt Handlers {#sec-15-1} -::: {#text-org5db45ad .outline-text-3} +::: {#text-15-1 .outline-text-3} Except for the last chapter, everything we did in the kernel so far we\'ve done as a response to a process asking for it, either by dealing with a special file, sending an ioctl(), or issuing a system call. But @@ -4755,10 +4689,10 @@ you\'re both willing to share. ::: ::: -::: {#outline-container-org9ca1a6c .outline-3} -### Detecting button presses {#org9ca1a6c} +::: {#outline-container-sec-15-2 .outline-3} +### Detecting button presses {#sec-15-2} -::: {#text-org9ca1a6c .outline-text-3} +::: {#text-15-2 .outline-text-3} Many popular single board computers, such as Raspberry Pis or Beagleboards, have a bunch of GPIO pins. Attaching buttons to those and then having a button press do something is a classic case in which you @@ -4929,10 +4863,10 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts"); ::: ::: -::: {#outline-container-org575eebf .outline-3} -### Bottom Half {#org575eebf} +::: {#outline-container-sec-15-3 .outline-3} +### Bottom Half {#sec-15-3} -::: {#text-org575eebf .outline-text-3} +::: {#text-15-3 .outline-text-3} Suppose you want to do a bunch of stuff inside of an interrupt routine. A common way to do that without rendering the interrupt unavailable for a significant duration is to combine it with a tasklet. This pushes the @@ -5114,11 +5048,11 @@ MODULE_DESCRIPTION("Interrupt with top and bottom half"); ::: ::: -::: {#outline-container-org004956f .outline-2} -Crypto {#org004956f} +::: {#outline-container-sec-16 .outline-2} +Crypto {#sec-16} ------ -::: {#text-org004956f .outline-text-2} +::: {#text-16 .outline-text-2} At the dawn of the internet everybody trusted everybody completely...but that didn\'t work out so well. When this guide was originally written it was a more innocent era in which almost nobody actually gave a damn @@ -5128,10 +5062,10 @@ enabling common methods of encryption, decryption and your favourite hash functions. ::: -::: {#outline-container-org8f45455 .outline-3} -### Hash functions {#org8f45455} +::: {#outline-container-sec-16-1 .outline-3} +### Hash functions {#sec-16-1} -::: {#text-org8f45455 .outline-text-3} +::: {#text-16-1 .outline-text-3} Calculating and checking the hashes of things is a common operation. Here is a demonstration of how to calculate a sha256 hash within a kernel module. @@ -5227,10 +5161,10 @@ sudo rmmod cryptosha256 ::: ::: -::: {#outline-container-orgcf62e2d .outline-3} -### Symmetric key encryption {#orgcf62e2d} +::: {#outline-container-sec-16-2 .outline-3} +### Symmetric key encryption {#sec-16-2} -::: {#text-orgcf62e2d .outline-text-3} +::: {#text-16-2 .outline-text-3} Here is an example of symmetrically encrypting a string using the AES algorithm and a password. @@ -5422,11 +5356,11 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-org161334f .outline-2} -Standardising the interfaces: The Device Model {#org161334f} +::: {#outline-container-sec-17 .outline-2} +Standardising the interfaces: The Device Model {#sec-17} ---------------------------------------------- -::: {#text-org161334f .outline-text-2} +::: {#text-17 .outline-text-2} Up to this point we\'ve seen all kinds of modules doing all kinds of things, but there was no consistency in their interfaces with the rest of the kernel. To impose some consistency such that there is at minimum @@ -5537,17 +5471,17 @@ module_exit(devicemodel_exit); ::: ::: -::: {#outline-container-org8639895 .outline-2} -Optimisations {#org8639895} +::: {#outline-container-sec-18 .outline-2} +Optimisations {#sec-18} ------------- -::: {#text-org8639895 .outline-text-2} +::: {#text-18 .outline-text-2} ::: -::: {#outline-container-orgb775e92 .outline-3} -### Likely and Unlikely conditions {#orgb775e92} +::: {#outline-container-sec-18-1 .outline-3} +### Likely and Unlikely conditions {#sec-18-1} -::: {#text-orgb775e92 .outline-text-3} +::: {#text-18-1 .outline-text-3} Sometimes you might want your code to run as quickly as possible, especially if it\'s handling an interrupt or doing something which might cause noticible latency. If your code contains boolean conditions and if @@ -5577,51 +5511,51 @@ pipeline. The opposite happens if you use the *likely* macro. ::: ::: -::: {#outline-container-org1cf3372 .outline-2} -Common Pitfalls {#org1cf3372} +::: {#outline-container-sec-19 .outline-2} +Common Pitfalls {#sec-19} --------------- -::: {#text-org1cf3372 .outline-text-2} +::: {#text-19 .outline-text-2} Before I send you on your way to go out into the world and write kernel modules, there are a few things I need to warn you about. If I fail to warn you and something bad happens, please report the problem to me for a full refund of the amount I was paid for your copy of the book. ::: -::: {#outline-container-org8e71ecd .outline-3} -### Using standard libraries {#org8e71ecd} +::: {#outline-container-sec-19-1 .outline-3} +### Using standard libraries {#sec-19-1} -::: {#text-org8e71ecd .outline-text-3} +::: {#text-19-1 .outline-text-3} You can\'t do that. In a kernel module you can only use kernel functions, which are the functions you can see in /proc/kallsyms. ::: ::: -::: {#outline-container-org9ff7def .outline-3} -### Disabling interrupts {#org9ff7def} +::: {#outline-container-sec-19-2 .outline-3} +### Disabling interrupts {#sec-19-2} -::: {#text-org9ff7def .outline-text-3} +::: {#text-19-2 .outline-text-3} You might need to do this for a short time and that is OK, but if you don\'t enable them afterwards, your system will be stuck and you\'ll have to power it off. ::: ::: -::: {#outline-container-orgdd651cd .outline-3} -### Sticking your head inside a large carnivore {#orgdd651cd} +::: {#outline-container-sec-19-3 .outline-3} +### Sticking your head inside a large carnivore {#sec-19-3} -::: {#text-orgdd651cd .outline-text-3} +::: {#text-19-3 .outline-text-3} I probably don\'t have to warn you about this, but I figured I will anyway, just in case. ::: ::: ::: -::: {#outline-container-orgcf6e3aa .outline-2} -Where To Go From Here? {#orgcf6e3aa} +::: {#outline-container-sec-20 .outline-2} +Where To Go From Here? {#sec-20} ---------------------- -::: {#text-orgcf6e3aa .outline-text-2} +::: {#text-20 .outline-text-2} I could easily have squeezed a few more chapters into this book. I could have added a chapter about creating new file systems, or about adding new protocol stacks (as if there\'s a need for that -- you\'d have to diff --git a/4.15.2/LKMPG-4.15.2.rst b/4.15.2/LKMPG-4.15.2.rst index 69adb40..27a0d4d 100644 --- a/4.15.2/LKMPG-4.15.2.rst +++ b/4.15.2/LKMPG-4.15.2.rst @@ -21,101 +21,101 @@ -- `Introduction <#org1785162>`__ +- `Introduction <#sec-1>`__ - - `Authorship <#org63a1b37>`__ - - `Versioning and Notes <#org10863e7>`__ - - `Acknowledgements <#org49d4e59>`__ - - `What Is A Kernel Module? <#org0a5ce13>`__ - - `Kernel module package <#org1cb0938>`__ - - `What Modules are in my Kernel? <#org878e43e>`__ - - `Do I need to download and compile the kernel? <#orgb530dfd>`__ - - `Before We Begin <#org2583533>`__ + - `Authorship <#sec-1-1>`__ + - `Versioning and Notes <#sec-1-2>`__ + - `Acknowledgements <#sec-1-3>`__ + - `What Is A Kernel Module? <#sec-1-4>`__ + - `Kernel module package <#sec-1-5>`__ + - `What Modules are in my Kernel? <#sec-1-6>`__ + - `Do I need to download and compile the kernel? <#sec-1-7>`__ + - `Before We Begin <#sec-1-8>`__ -- `Headers <#org46d2b5f>`__ -- `Examples <#orgdd7dd5d>`__ -- `Hello World <#org92f8539>`__ +- `Headers <#sec-2>`__ +- `Examples <#sec-3>`__ +- `Hello World <#sec-4>`__ - - `The Simplest Module <#org5ae5562>`__ - - `Hello and Goodbye <#org5c5dd06>`__ - - `The \__init and \__exit Macros <#org7fd362f>`__ - - `Licensing and Module Documentation <#orga52c864>`__ - - `Passing Command Line Arguments to a Module <#org6976bc8>`__ - - `Modules Spanning Multiple Files <#org5a97220>`__ - - `Building modules for a precompiled kernel <#org65d6819>`__ + - `The Simplest Module <#sec-4-1>`__ + - `Hello and Goodbye <#sec-4-2>`__ + - `The \__init and \__exit Macros <#sec-4-3>`__ + - `Licensing and Module Documentation <#sec-4-4>`__ + - `Passing Command Line Arguments to a Module <#sec-4-5>`__ + - `Modules Spanning Multiple Files <#sec-4-6>`__ + - `Building modules for a precompiled kernel <#sec-4-7>`__ -- `Preliminaries <#orgb5adf2b>`__ +- `Preliminaries <#sec-5>`__ - - `How modules begin and end <#org4cf9365>`__ - - `Functions available to modules <#orgcb5e2da>`__ - - `User Space vs Kernel Space <#orgab9f1d1>`__ - - `Name Space <#org9d3f3fc>`__ - - `Code space <#orga3691e9>`__ - - `Device Drivers <#org382352c>`__ + - `How modules begin and end <#sec-5-1>`__ + - `Functions available to modules <#sec-5-2>`__ + - `User Space vs Kernel Space <#sec-5-3>`__ + - `Name Space <#sec-5-4>`__ + - `Code space <#sec-5-5>`__ + - `Device Drivers <#sec-5-6>`__ -- `Character Device drivers <#orgf1744e4>`__ +- `Character Device drivers <#sec-6>`__ - - `The file_operations Structure <#org07ed5b3>`__ - - `The file structure <#orgf283636>`__ - - `Registering A Device <#orgab1c63f>`__ - - `Unregistering A Device <#org85bc5ea>`__ - - `chardev.c <#orgdbd100e>`__ - - `Writing Modules for Multiple Kernel Versions <#org130321b>`__ + - `The file_operations Structure <#sec-6-1>`__ + - `The file structure <#sec-6-2>`__ + - `Registering A Device <#sec-6-3>`__ + - `Unregistering A Device <#sec-6-4>`__ + - `chardev.c <#sec-6-5>`__ + - `Writing Modules for Multiple Kernel Versions <#sec-6-6>`__ -- `The /proc File System <#org44e86f2>`__ +- `The /proc File System <#sec-7>`__ - - `Read and Write a /proc File <#org330f61f>`__ - - `Manage /proc file with standard filesystem <#orgaae8b16>`__ - - `Manage /proc file with seq_file <#org688df35>`__ + - `Read and Write a /proc File <#sec-7-1>`__ + - `Manage /proc file with standard filesystem <#sec-7-2>`__ + - `Manage /proc file with seq_file <#sec-7-3>`__ -- `sysfs: Interacting with your module <#orgbd8ce69>`__ -- `Talking To Device Files <#orga1b8e31>`__ -- `System Calls <#orgdf0ab54>`__ -- `Blocking Processes and threads <#org83613ec>`__ +- `sysfs: Interacting with your module <#sec-8>`__ +- `Talking To Device Files <#sec-9>`__ +- `System Calls <#sec-10>`__ +- `Blocking Processes and threads <#sec-11>`__ - - `Sleep <#org36ce43a>`__ - - `Completions <#orgcfa6577>`__ + - `Sleep <#sec-11-1>`__ + - `Completions <#sec-11-2>`__ -- `Avoiding Collisions and Deadlocks <#org3ffdc76>`__ +- `Avoiding Collisions and Deadlocks <#sec-12>`__ - - `Mutex <#org89ec2e6>`__ - - `Spinlocks <#org9889db8>`__ - - `Read and write locks <#orge2dc3e1>`__ - - `Atomic operations <#org1316854>`__ + - `Mutex <#sec-12-1>`__ + - `Spinlocks <#sec-12-2>`__ + - `Read and write locks <#sec-12-3>`__ + - `Atomic operations <#sec-12-4>`__ -- `Replacing Print Macros <#orgbdec3ca>`__ +- `Replacing Print Macros <#sec-13>`__ - - `Replacement <#org4579c68>`__ - - `Flashing keyboard LEDs <#orgc9bd69e>`__ + - `Replacement <#sec-13-1>`__ + - `Flashing keyboard LEDs <#sec-13-2>`__ -- `Scheduling Tasks <#orgf1f1faf>`__ +- `Scheduling Tasks <#sec-14>`__ - - `Tasklets <#orgd815b3b>`__ - - `Work queues <#org9b5b18e>`__ + - `Tasklets <#sec-14-1>`__ + - `Work queues <#sec-14-2>`__ -- `Interrupt Handlers <#org1cdf533>`__ +- `Interrupt Handlers <#sec-15>`__ - - `Interrupt Handlers <#org5db45ad>`__ - - `Detecting button presses <#org9ca1a6c>`__ - - `Bottom Half <#org575eebf>`__ + - `Interrupt Handlers <#sec-15-1>`__ + - `Detecting button presses <#sec-15-2>`__ + - `Bottom Half <#sec-15-3>`__ -- `Crypto <#org004956f>`__ +- `Crypto <#sec-16>`__ - - `Hash functions <#org8f45455>`__ - - `Symmetric key encryption <#orgcf62e2d>`__ + - `Hash functions <#sec-16-1>`__ + - `Symmetric key encryption <#sec-16-2>`__ -- `Standardising the interfaces: The Device Model <#org161334f>`__ -- `Optimisations <#org8639895>`__ +- `Standardising the interfaces: The Device Model <#sec-17>`__ +- `Optimisations <#sec-18>`__ - - `Likely and Unlikely conditions <#orgb775e92>`__ + - `Likely and Unlikely conditions <#sec-18-1>`__ -- `Common Pitfalls <#org1cf3372>`__ +- `Common Pitfalls <#sec-19>`__ - - `Using standard libraries <#org8e71ecd>`__ - - `Disabling interrupts <#org9ff7def>`__ - - `Sticking your head inside a large carnivore <#orgdd651cd>`__ + - `Using standard libraries <#sec-19-1>`__ + - `Disabling interrupts <#sec-19-2>`__ + - `Sticking your head inside a large carnivore <#sec-19-3>`__ -- `Where To Go From Here? <#orgcf6e3aa>`__ +- `Where To Go From Here? <#sec-20>`__ .. raw:: html @@ -127,14 +127,14 @@ .. raw:: html -+.. rubric:: Introduction - :name: org1785162 + :name: sec-1 .. raw:: html -+The Linux Kernel Module Programming Guide is a free book; you may reproduce and/or modify it under the terms of the Open Software License, @@ -172,14 +172,14 @@ above. .. raw:: html -+.. rubric:: Authorship - :name: org63a1b37 + :name: sec-1-1 .. raw:: html -+The Linux Kernel Module Programming Guide was originally written for the 2.2 kernels by Ori Pomerantz. Eventually, Ori no longer had time to @@ -201,14 +201,14 @@ other chapters. .. raw:: html -+.. rubric:: Versioning and Notes - :name: org10863e7 + :name: sec-1-2 .. raw:: html -+The Linux kernel is a moving target. There has always been a question whether the LKMPG should remove deprecated information or keep it around @@ -232,14 +232,14 @@ I can't promise anything. .. raw:: html -+.. rubric:: Acknowledgements - :name: org49d4e59 + :name: sec-1-3 .. raw:: html -+The following people have contributed corrections or good suggestions: Ignacio Martin, David Porter, Daniele Paolo Scarpazza, Dimo Velev, @@ -255,14 +255,14 @@ Francois Audeon, Horst Schirmeier, Bob Mottram and Roman Lakeev. .. raw:: html -+.. rubric:: What Is A Kernel Module? - :name: org0a5ce13 + :name: sec-1-4 .. raw:: html -+So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where @@ -289,14 +289,14 @@ time we want new functionality. .. raw:: html -+.. rubric:: Kernel module package - :name: org1cb0938 + :name: sec-1-5 .. raw:: html -+Linux distros provide the commands *modprobe*, *insmod* and *depmod* within a package. @@ -309,7 +309,7 @@ On Debian: .. code:: src - sudo apt-get install build-essential kmod + sudo apt-get install build-essential kmod .. raw:: html @@ -323,7 +323,7 @@ On Parabola: .. code:: src - sudo pacman -S gcc kmod + sudo pacman -S gcc kmod .. raw:: html @@ -339,14 +339,14 @@ On Parabola: .. raw:: html -+.. rubric:: What Modules are in my Kernel? - :name: org878e43e + :name: sec-1-6 .. raw:: html -+To discover what modules are already loaded within your current kernel use the command **lsmod**. @@ -357,7 +357,7 @@ use the command **lsmod**. .. code:: src - sudo lsmod + sudo lsmod .. raw:: html @@ -372,7 +372,7 @@ them with: .. code:: src - sudo cat /proc/modules + sudo cat /proc/modules .. raw:: html @@ -387,7 +387,7 @@ particular. To search for the *fat* module: .. code:: src - sudo lsmod | grep fat + sudo lsmod | grep fat .. raw:: html @@ -403,14 +403,14 @@ particular. To search for the *fat* module: .. raw:: html -+.. rubric:: Do I need to download and compile the kernel? - :name: orgb530dfd + :name: sec-1-7 .. raw:: html -+For the purposes of following this guide you don't necessarily need to do that. However, it would be wise to run the examples within a test @@ -427,14 +427,14 @@ messing up your system. .. raw:: html -+.. rubric:: Before We Begin - :name: org2583533 + :name: sec-1-8 .. raw:: html -+Before we delve into code, there are a few issues we need to cover. Everyone's system is different and everyone has their own groove. @@ -448,7 +448,7 @@ thereafter.- | Modversioning - | ::: {#text-org4517450 .outline-text-5} A module compiled for one + | ::: {#text-1-8-0-1 .outline-text-5} A module compiled for one kernel won't load if you boot a different kernel unless you enable CONFIG_MODVERSIONS in the kernel. We won't go into module versioning until later in this guide. Until we cover modversions, @@ -461,8 +461,8 @@ thereafter. ::: - | Using X - | ::: {#text-orgdc404d5 .outline-text-5} It is highly recommended - that you type in, compile and load all the examples this guide + | ::: {#text-1-8-0-2 .outline-text-5} It is highly recommended that + you type in, compile and load all the examples this guide discusses. It's also highly recommended you do this from a console. You should not be working on this stuff in X. @@ -486,14 +486,14 @@ thereafter. .. raw:: html -+.. rubric:: Headers - :name: org46d2b5f + :name: sec-2 .. raw:: html -