1
0
mirror of https://github.com/bashrc/LKMPG.git synced 2018-06-11 03:06:54 +02:00

Update different formats

This commit is contained in:
Bob Mottram
2018-05-22 13:56:32 +01:00
parent 41f6bf0915
commit dadc4300bf
5 changed files with 3721 additions and 3992 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,29 +1,28 @@
% Created 2018-02-15 Thu 12:41
% Intended LaTeX compiler: pdflatex
% Created 2018-05-22 Tue 13:55
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\tolerance=1000
\author{Peter Jay Salzman, Michael Burian, Ori Pomerantz, Bob Mottram}
\date{\today}
\title{The Linux Kernel Module Programming Guide}
\hypersetup{
pdfauthor={Peter Jay Salzman, Michael Burian, Ori Pomerantz, Bob Mottram},
pdftitle={The Linux Kernel Module Programming Guide},
pdfkeywords={linux, kernel, kernel module, kernel programming},
pdfsubject={How to make kernel modules for Linux},
pdfcreator={Emacs 25.3.1 (Org mode 9.1.5)},
pdflang={English}}
pdfkeywords={linux, kernel, kernel module, kernel programming},
pdfsubject={How to make kernel modules for Linux},
pdfcreator={Emacs 25.3.1 (Org mode 8.2.3c)}}
\begin{document}
\maketitle
@@ -31,7 +30,7 @@
\section*{Introduction}
\label{sec:orgc321dcd}
\label{sec-1}
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.
This book is distributed in the hope it will be useful, but without any warranty, without even the implied warranty of merchantability or fitness for a particular purpose.
@@ -43,31 +42,31 @@ Derivative works and translations of this document must be placed under the Open
If you publish or distribute this book commercially, donations, royalties, and/or printed copies are greatly appreciated by the author and the \href{http://www.tldp.org}{Linux Documentation Project} (LDP). Contributing in this way shows your support for free software and the LDP. If you have questions or comments, please contact the address above.
\subsection*{Authorship}
\label{sec:orgb6d5857}
\label{sec-1-1}
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 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.
\subsection*{Versioning and Notes}
\label{sec:org02b5f61}
\label{sec-1-2}
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.
The source code and discussions should apply to most architectures, but I can't promise anything.
\subsection*{Acknowledgements}
\label{sec:orgd049ec8}
\label{sec-1-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.
\subsection*{What Is A Kernel Module?}
\label{sec:orgbf29314}
\label{sec-1-4}
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.
What exactly is a kernel module? Modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. Without modules, we would have to build monolithic kernels and add new functionality directly into the kernel image. Besides having larger kernels, this has the disadvantage of requiring us to rebuild and reboot the kernel every time we want new functionality.
\subsection*{Kernel module package}
\label{sec:orga12b1cc}
\label{sec-1-5}
Linux distros provide the commands \emph{modprobe}, \emph{insmod} and \emph{depmod} within a package.
@@ -84,7 +83,7 @@ sudo pacman -S gcc kmod
\end{verbatim}
\subsection*{What Modules are in my Kernel?}
\label{sec:org4043d5d}
\label{sec-1-6}
To discover what modules are already loaded within your current kernel use the command \textbf{lsmod}.
@@ -105,20 +104,20 @@ sudo lsmod | grep fat
\end{verbatim}
\subsection*{Do I need to download and compile the kernel?}
\label{sec:org5514142}
\label{sec-1-7}
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.
\subsection*{Before We Begin}
\label{sec:org64bf8fe}
\label{sec-1-8}
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.
\begin{itemize}
\item Modversioning
\label{sec:org626b2a3}
\label{sec-1-8-0-1}
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.
\item Using X
\label{sec:org90aa845}
\label{sec-1-8-0-2}
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.
@@ -126,7 +125,7 @@ Modules can't print to the screen like printf() can, but they can log informatio
\end{itemize}
\section*{Headers}
\label{sec:org5433425}
\label{sec-2}
Before you can build anything you'll need to install the header files for your kernel. On Parabola GNU/Linux:
\begin{verbatim}
@@ -146,7 +145,7 @@ 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
\end{verbatim}
\section*{Examples}
\label{sec:org6c6550e}
\label{sec-3}
All the examples from this document are available within the \emph{examples} subdirectory. To test that they compile:
\begin{verbatim}
@@ -156,9 +155,9 @@ make
If there are any compile errors then you might have a more recent kernel version or need to install the corresponding kernel header files.
\section*{Hello World}
\label{sec:org271d322}
\label{sec-4}
\subsection*{The Simplest Module}
\label{sec:orgd17fc17}
\label{sec-4-1}
Most people learning programming start out with some sort of "\emph{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.
Here's the simplest module possible.
@@ -260,15 +259,15 @@ Lastly, every kernel module needs to include linux/module.h. We needed to includ
\begin{itemize}
\item A point about coding style
\label{sec:org2ebe640}
\label{sec-4-1-0-1}
Another thing which may not be immediately obvious to anyone getting started with kernel programming is that indentation within your code should be using \textbf{tabs} and \textbf{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.
\item Introducing print macros
\label{sec:orge74bcce}
\label{sec-4-1-0-2}
In the beginning there was \textbf{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 \textbf{pr\_info} and \textbf{pr\_debug}. This just saves some mindless keyboard bashing and looks a bit neater. They can be found within \textbf{linux/printk.h}. Take time to read through the available priority macros.
\item About Compiling
\label{sec:org70e2fb5}
\label{sec-4-1-0-3}
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 \textbf{linux/Documentation/kbuild/modules.txt}.
@@ -280,7 +279,7 @@ Here's another exercise for the reader. See that comment above the return statem
\end{itemize}
\subsection*{Hello and Goodbye}
\label{sec:org34d97ca}
\label{sec-4-2}
In early kernel versions you had to use the \textbf{init\_module} and \textbf{cleanup\_module} functions, as in the first hello world example, but these days you can name those anything you want by using the \textbf{module\_init} and \textbf{module\_exit} macros. These macros are defined in \textbf{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:
\begin{verbatim}
@@ -323,7 +322,7 @@ clean:
Now have a look at linux/drivers/char/Makefile for a real world example. As you can see, some things get hardwired into the kernel (obj-y) but where are all those obj-m gone? Those familiar with shell scripts will easily be able to spot them. For those not, the obj-\$(CONFIG\_FOO) entries you see everywhere expand into obj-y or obj-m, depending on whether the CONFIG\_FOO variable has been set to y or m. While we are at it, those were exactly the kind of variables that you have set in the linux/.config file, the last time when you said make menuconfig or something like that.
\subsection*{The \_\_init and \_\_exit Macros}
\label{sec:org6c778f1}
\label{sec-4-3}
This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The \textbf{\_\_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.
There is also an \textbf{\_\_initdata} which works similarly to \textbf{\_\_init} but for init variables rather than functions.
@@ -358,7 +357,7 @@ module_exit(hello_3_exit);
\end{verbatim}
\subsection*{Licensing and Module Documentation}
\label{sec:org21ab1f3}
\label{sec-4-4}
Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this:
\begin{verbatim}
@@ -401,7 +400,7 @@ module_exit(cleanup_hello_4);
\end{verbatim}
\subsection*{Passing Command Line Arguments to a Module}
\label{sec:org3cd1129}
\label{sec-4-5}
Modules can take command line arguments, but not with the argc/argv you might be used to.
To allow arguments to be passed to your module, declare the variables that will take the values of the command line arguments as global and then use the module\_param() macro, (defined in linux/moduleparam.h) to set the mechanism up. At runtime, insmod will fill the variables with any command line arguments that are given, like ./insmod mymodule.ko myvariable=5. The variable declarations and macros should be placed at the beginning of the module for clarity. The example code should clear up my admittedly lousy explanation.
@@ -421,7 +420,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 */
\end{verbatim}
A good use for this is to have the module variable's default values set, like an port or IO address. If the variables contain the default values, then perform autodetection (explained elsewhere). Otherwise, keep the current value. This will be made clear later on.
@@ -532,7 +531,7 @@ hello-5.o: invalid argument syntax for mylong: 'h'
\end{verbatim}
\subsection*{Modules Spanning Multiple Files}
\label{sec:orge26ffb5}
\label{sec-4-6}
Sometimes it makes sense to divide a kernel module between several source files.
Here's an example of such a kernel module.
@@ -589,7 +588,7 @@ clean:
This is the complete makefile for all the examples we've seen so far. The first five lines are nothing special, but for the last example we'll need two lines. First we invent an object name for our combined module, second we tell make what object files are part of that module.
\subsection*{Building modules for a precompiled kernel}
\label{sec:org9159753}
\label{sec-4-7}
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 (\textbf{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 \textbf{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.
There are a number of cases in which you may want to load your module into a precompiled running kernel, such as the ones shipped with common Linux distributions, or a kernel you have compiled in the past. In certain circumstances you could require to compile and insert a module into a running kernel which you are not allowed to recompile, or on a machine that you prefer not to reboot. If you can't think of a case that will force you to use modules for a precompiled kernel you might want to skip this and treat the rest of this chapter as a big footnote.
@@ -652,9 +651,9 @@ CC scripts/empty.o
If you do not desire to actually compile the kernel, you can interrupt the build process (CTRL-C) just after the SPLIT line, because at that time, the files you need will be are ready. Now you can turn back to the directory of your module and compile it: It will be built exactly according your current kernel settings, and it will load into it without any errors.
\section*{Preliminaries}
\label{sec:org71a80b8}
\label{sec-5}
\subsection*{How modules begin and end}
\label{sec:orgf6f7093}
\label{sec-5-1}
A program usually begins with a \textbf{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.
All modules end by calling either \textbf{cleanup\_module} or the function you specify with the \textbf{module\_exit} call. This is the exit function for modules; it undoes whatever entry function did. It unregisters the functionality that the entry function registered.
@@ -662,7 +661,7 @@ All modules end by calling either \textbf{cleanup\_module} or the function you s
Every module must have an entry function and an exit function. Since there's more than one way to specify entry and exit functions, I'll try my best to use the terms `entry function' and `exit function', but if I slip and simply refer to them as init\_module and cleanup\_module, I think you'll know what I mean.
\subsection*{Functions available to modules}
\label{sec:org55630eb}
\label{sec-5-2}
Programmers use functions they don't define all the time. A prime example of this is \textbf{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.
Kernel modules are different here, too. In the hello world example, you might have noticed that we used a function, \textbf{pr\_info()} but didn't include a standard I/O library. That's because modules are object files whose symbols get resolved upon insmod'ing. The definition for the symbols comes from the kernel itself; the only external functions you can use are the ones provided by the kernel. If you're curious about what symbols have been exported by your kernel, take a look at \textbf{/proc/kallsyms}.
@@ -686,13 +685,13 @@ with \textbf{gcc -Wall -o hello hello.c}. Run the exectable with \textbf{strace
You can even write modules to replace the kernel's system calls, which we'll do shortly. Crackers often make use of this sort of thing for backdoors or trojans, but you can write your own modules to do more benign things, like have the kernel write Tee hee, that tickles! everytime someone tries to delete a file on your system.
\subsection*{User Space vs Kernel Space}
\label{sec:org65c3b2a}
\label{sec-5-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 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'.
Recall the discussion about library functions vs system calls. Typically, you use a library function in user mode. The library function calls one or more system calls, and these system calls execute on the library function's behalf, but do so in supervisor mode since they are part of the kernel itself. Once the system call completes its task, it returns and execution gets transfered back to user mode.
\subsection*{Name Space}
\label{sec:orgc59d839}
\label{sec-5-4}
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.
When writing kernel code, even the smallest module will be linked against the entire kernel, so this is definitely an issue. The best way to deal with this is to declare all your variables as static and to use a well-defined prefix for your symbols. By convention, all kernel prefixes are lowercase. If you don't want to declare everything as static, another option is to declare a symbol table and register it with a kernel. We'll get to this later.
@@ -700,22 +699,22 @@ When writing kernel code, even the smallest module will be linked against the en
The file \textbf{/proc/kallsyms} holds all the symbols that the kernel knows about and which are therefore accessible to your modules since they share the kernel's codespace.
\subsection*{Code space}
\label{sec:orga4e4f07}
\label{sec-5-5}
Memory management is a very complicated subject and the majority of O'Reilly's "\emph{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.
If you haven't thought about what a segfault really means, you may be surprised to hear that pointers don't actually point to memory locations. Not real ones, anyway. When a process is created, the kernel sets aside a portion of real physical memory and hands it to the process to use for its executing code, variables, stack, heap and other things which a computer scientist would know about. This memory begins with 0x00000000 and extends up to whatever it needs to be. Since the memory space for any two processes don't overlap, every process that can access a memory address, say 0xbffff978, would be accessing a different location in real physical memory! The processes would be accessing an index named 0xbffff978 which points to some kind of offset into the region of memory set aside for that particular process. For the most part, a process like our Hello, World program can't access the space of another process, although there are ways which we'll talk about later.
The kernel has its own space of memory as well. Since a module is code which can be dynamically inserted and removed in the kernel (as opposed to a semi-autonomous object), it shares the kernel's codespace rather than having its own. Therefore, if your module segfaults, the kernel segfaults. And if you start writing over data because of an off-by-one error, then you're trampling on kernel data (or code). This is even 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 \emph{"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.
\subsection*{Device Drivers}
\label{sec:orgc5d62fb}
\label{sec-5-6}
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.
\begin{itemize}
\item Major and Minor Numbers
\label{sec:org0b68ce3}
\label{sec-5-6-0-1}
Let's look at some device files. Here are device files which represent the first three partitions on the primary master IDE hard drive:
@@ -745,7 +744,7 @@ When the system was installed, all of those device files were created by the mkn
I would like to make a few last points which are implicit from the above discussion, but I'd like to make them explicit just in case. When a device file is accessed, the kernel uses the major number of the file to determine which driver should be used to handle the access. This means that the kernel doesn't really need to use or even know about the minor number. The driver itself is the only 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 \emph{"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:
\begin{verbatim}
% ls -l /dev/sda /dev/sdb
@@ -757,9 +756,9 @@ By now you can look at these two device files and know instantly that they are b
\end{itemize}
\section*{Character Device drivers}
\label{sec:orga656c47}
\label{sec-6}
\subsection*{The file\_operations Structure}
\label{sec:org274a1cc}
\label{sec-6-1}
The file\_operations structure is defined in \textbf{/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.
For example, every character driver needs to define a function that reads from the device. The file\_operations structure holds the address of the module's function that performs that operation. Here is what the definition looks like for kernel 3.0:
@@ -826,7 +825,7 @@ The meaning is clear, and you should be aware that any member of the structure w
An instance of struct file\_operations containing pointers to functions that are used to implement read, write, open, \ldots{} syscalls is commonly named fops.
\subsection*{The file structure}
\label{sec:org098d6fc}
\label{sec-6-2}
Each device is represented in the kernel by a file structure, which is defined in \textbf{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 \textbf{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.
@@ -835,7 +834,7 @@ An instance of struct file is commonly named filp. You'll also see it refered to
Go ahead and look at the definition of file. Most of the entries you see, like struct dentry aren't used by device drivers, and you can ignore them. This is because drivers don't fill file directly; they only use structures contained in file which are created elsewhere.
\subsection*{Registering A Device}
\label{sec:org754d088}
\label{sec-6-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 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.
Adding a driver to your system means registering it with the kernel. This is synonymous with assigning it a major number during the module's initialization. You do this by using the register\_chrdev function, defined by linux/fs.h.
@@ -851,7 +850,7 @@ Now the question is, how do you get a major number without hijacking one that's
If you pass a major number of 0 to register\_chrdev, the return value will be the dynamically allocated major number. The downside is that you can't make a device file in advance, since you don't know what the major number will be. There are a couple of ways to do this. First, the driver itself can print the newly assigned number and we can make the device file by hand. Second, the newly registered device will have an entry in \textbf{/proc/devices}, and we can either make the device file by hand or write a shell script to read the file in and make the device file. The third method is we can have our driver make the the device file using the \textbf{device\_create} function after a successful registration and \textbf{device\_destroy} during the call to cleanup\_module.
\subsection*{Unregistering A Device}
\label{sec:org9b8d9f7}
\label{sec-6-4}
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.
Normally, when you don't want to allow something, you return an error code (a negative number) from the function which is supposed to do it. With cleanup\_module that's impossible because it's a void function. However, there's a counter which keeps track of how many processes are using your module. You can see what it's value is by looking at the 3rd field of \textbf{/proc/modules}. If this number isn't zero, rmmod will fail. Note that you don't have to check the counter from within cleanup\_module because the check will be performed for you by the system call sys\_delete\_module, defined in \textbf{linux/module.c}. You shouldn't use this counter directly, but there are functions defined in \textbf{linux/module.h} which let you increase, decrease and display this counter:
@@ -864,7 +863,7 @@ Normally, when you don't want to allow something, you return an error code (a ne
It's important to keep the counter accurate; if you ever do lose track of the correct usage count, you'll never be able to unload the module; it's now reboot time, boys and girls. This is bound to happen to you sooner or later during a module's development.
\subsection*{chardev.c}
\label{sec:org3bbde96}
\label{sec-6-5}
The next code sample creates a char driver named chardev. You can cat its device file.
\begin{verbatim}
@@ -1058,19 +1057,19 @@ static ssize_t device_write(struct file *filp,
\end{verbatim}
\subsection*{Writing Modules for Multiple Kernel Versions}
\label{sec:orga0aa025}
\label{sec-6-6}
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.
The Linux kernel versions are divided between the stable versions (n.\$<\$even number\(>\).m) and the development versions (n.\$<\$odd number\(>\).m). The development versions include all the cool new ideas, including those which will be considered a mistake, or reimplemented, in the next version. As a result, you can't trust the interface to remain the same in those versions (which is why I don't bother to support them in this book, it's too much work and it would become dated too quickly). In the stable versions, on the other hand, we can expect the interface to remain the same regardless of the bug fix version (the m number).
The Linux kernel versions are divided between the stable versions (n.\$<\$even number$>$.m) and the development versions (n.\$<\$odd number$>$.m). The development versions include all the cool new ideas, including those which will be considered a mistake, or reimplemented, in the next version. As a result, you can't trust the interface to remain the same in those versions (which is why I don't bother to support them in this book, it's too much work and it would become dated too quickly). In the stable versions, on the other hand, we can expect the interface to remain the same regardless of the bug fix version (the m number).
There are differences between different kernel versions, and if you want to support multiple kernel versions, you'll find yourself having to code conditional compilation directives. The way to do this to compare the macro LINUX\_VERSION\_CODE to the macro KERNEL\_VERSION. In version a.b.c of the kernel, the value of this macro would be \(2^{16}a+2^{8}b+c\).
There are differences between different kernel versions, and if you want to support multiple kernel versions, you'll find yourself having to code conditional compilation directives. The way to do this to compare the macro LINUX\_VERSION\_CODE to the macro KERNEL\_VERSION. In version a.b.c of the kernel, the value of this macro would be $2^{16}a+2^{8}b+c$.
While previous versions of this guide showed how you can write backward compatible code with such constructs in great detail, we decided to break with this tradition for the better. People interested in doing such might now use a LKMPG with a version matching to their kernel. We decided to version the LKMPG like the kernel, at least as far as major and minor number are concerned. We use the patchlevel for our own versioning so use LKMPG version 2.4.x for kernels 2.4.x, use LKMPG version 2.6.x for kernels 2.6.x and so on. Also make sure that you always use current, up to date versions of both, kernel and guide.
You might already have noticed that recent kernels look different. In case you haven't they look like 2.6.x.y now. The meaning of the first three items basically stays the same, but a subpatchlevel has been added and will indicate security fixes till the next stable patchlevel is out. So people can choose between a stable tree with security updates and use the latest kernel as developer tree. Search the kernel mailing list archives if you're interested in the full story.
\section*{The /proc File System}
\label{sec:org34d8e48}
\label{sec-7}
In Linux, there is an additional mechanism for the kernel and kernel modules to send information to processes --- the \textbf{/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 \textbf{/proc/modules} which provides the list of modules and \textbf{/proc/meminfo} which stats memory usage statistics.
The method to use the proc file system is very similar to the one used with device drivers --- a structure is created with all the information needed for the \textbf{/proc} file, including pointers to any handler functions (in our case there is only one, the one called when somebody attempts to read from the \textbf{/proc} file). Then, init\_module registers the structure with the kernel and cleanup\_module unregisters it.
@@ -1144,7 +1143,7 @@ void cleanup_module()
\end{verbatim}
\subsection*{Read and Write a /proc File}
\label{sec:org72592c6}
\label{sec-7-1}
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)
The reason for copy\_from\_user or get\_user is that Linux memory (on Intel architecture, it may be different under some other processors) is segmented. This means that a pointer, by itself, does not reference a unique location in memory, only a location in a memory segment, and you need to know which memory segment it is to be able to use it. There is one memory segment for the kernel, and one for each of the processes.
@@ -1253,7 +1252,7 @@ void cleanup_module()
\end{verbatim}
\subsection*{Manage /proc file with standard filesystem}
\label{sec:orgfbdd2bd}
\label{sec-7-2}
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.
In Linux, there is a standard mechanism for file system registration. Since every file system has to have its own functions to handle inode and file operations, there is a special structure to hold pointers to all those functions, struct \textbf{inode\_operations}, which includes a pointer to struct file\_operations.
@@ -1355,7 +1354,7 @@ void cleanup_module()
Still hungry for procfs examples? Well, first of all keep in mind, there are rumors around, claiming that procfs is on it's way out, consider using sysfs instead. Second, if you really can't get enough, there's a highly recommendable bonus level for procfs below linux/Documentation/DocBook/ . Use make help in your toplevel kernel directory for instructions about how to convert it into your favourite format. Example: make htmldocs . Consider using this mechanism, in case you want to document something kernel related yourself.
\subsection*{Manage /proc file with seq\_file}
\label{sec:orgd4ccaa0}
\label{sec-7-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 is composed of 3 functions: start(), next(), and stop(). The seq\_file API starts a sequence when a user read the /proc file.
@@ -1365,9 +1364,7 @@ non NULL value, the function next() is called. This function is an iterator, the
BE CARREFUL: when a sequence is finished, another one starts. That means that at the end of function stop(), the function start() is called again. This loop finishes when the function start() returns NULL. You can see a scheme of this in the figure "How seq\_file works".
\begin{center}
\includegraphics[width=.9\linewidth]{img/seq_file.png}
\end{center}
\begin{center}
\begin{tabular}{}
@@ -1518,14 +1515,14 @@ If you want more information, you can read this web page:
\begin{itemize}
\item \url{http://lwn.net/Articles/22355/}
\item \url{http://www.kernelnewbies.org/documents/seq\_file\_howto.txt}
\item \url{http://www.kernelnewbies.org/documents/seq_file_howto.txt}
\end{itemize}
You can also read the code of fs/seq\_file.c in the linux kernel.
\section*{sysfs: Interacting with your module}
\label{sec:orga3389e2}
\label{sec-8}
\emph{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 \emph{sys} directory on your system.
\begin{verbatim}
@@ -1535,73 +1532,7 @@ ls -l /sys
An example of a hello world module which includes the creation of a variable accessible via sysfs is given below.
\begin{verbatim}
/*
* 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
\end{verbatim}
Make and install the module:
@@ -1637,7 +1568,7 @@ sudo rmmod hello_sysfs
\end{verbatim}
\section*{Talking To Device Files}
\label{sec:org4dcda87}
\label{sec-9}
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.
This is not always enough. Imagine you had a serial port connected to a modem (even if you have an internal modem, it is still implemented from the CPU's perspective as a serial port connected to a modem, so you don't have to tax your imagination too hard). The natural thing to do would be to use the device file to write things to the modem (either modem commands or data to be sent through the phone line) and read things from the modem (either responses for commands or the data received through the phone line). However, this leaves open the question of what to do when you need to talk to the serial port itself, for example to send the rate at which data is sent and received.
@@ -2117,7 +2048,7 @@ int main()
\end{verbatim}
\section*{System Calls}
\label{sec:orgb23484f}
\label{sec-10}
So far, the only thing we've done was to use well defined kernel mechanisms to register \textbf{/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.
If you're not being sensible and using a virtual machine then this is where kernel programming can become hazardous. While writing the example below, I killed the \textbf{open()} system call. This meant I couldn't open any files, I couldn't run any programs, and I couldn't shutdown the system. I had to restart the virtual machine. No important files got anihilated, but if I was doing this on some live mission critical system then that could have been a possible outcome. To ensure you don't lose any files, even within a test environment, please run \textbf{sync} right before you do the \textbf{insmod} and the \textbf{rmmod}.
@@ -2299,9 +2230,9 @@ MODULE_LICENSE("GPL");
\end{verbatim}
\section*{Blocking Processes and threads}
\label{sec:orge7d633e}
\label{sec-11}
\subsection*{Sleep}
\label{sec:orgf09dc03}
\label{sec-11-1}
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: "\emph{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).
This kernel module is an example of this. The file (called \textbf{/proc/sleep}) can only be opened by a single process at a time. If the file is already open, the kernel module calls wait\_event\_interruptible. The easiest way to keep a file open is to open it with:
@@ -2686,7 +2617,7 @@ int main(int argc, char *argv[])
\end{verbatim}
\subsection*{Completions}
\label{sec:org674a879}
\label{sec-11-2}
Sometimes one thing should happen before another within a module having multiple threads. Rather than using \textbf{/proc/sleep} commands the kernel has another way to do this which allows timeouts or interrupts to also happen.
In the following example two threads are started, but one needs to start before another.
@@ -2778,10 +2709,10 @@ So even though \emph{flywheel\_thread} is started first you should notice if you
There are other variations upon the \emph{wait\_for\_completion} function, which include timeouts or being interrupted, but this basic mechanism is enough for many common situations without adding a lot of complexity.
\section*{Avoiding Collisions and Deadlocks}
\label{sec:org3ee70af}
\label{sec-12}
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.
\subsection*{Mutex}
\label{sec:orgb6306b0}
\label{sec-12-1}
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.
\begin{verbatim}
@@ -2827,10 +2758,10 @@ MODULE_DESCRIPTION("Mutex example");
MODULE_LICENSE("GPL");
\end{verbatim}
\subsection*{Spinlocks}
\label{sec:org7588133}
\label{sec-12-2}
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 \emph{"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 \emph{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 \emph{flags} variable to retain their state.
\begin{verbatim}
#include <linux/kernel.h>
@@ -2899,7 +2830,7 @@ MODULE_LICENSE("GPL");
\end{verbatim}
\subsection*{Read and write locks}
\label{sec:org6f1791a}
\label{sec-12-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 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.
\begin{verbatim}
@@ -2960,7 +2891,7 @@ MODULE_LICENSE("GPL");
Of course if you know for sure that there are no functions triggered by irqs which could possibly interfere with your logic then you can use the simpler \emph{read\_lock(\&myrwlock)} and \emph{read\_unlock(\&myrwlock)} or the corresponding write functions.
\subsection*{Atomic operations}
\label{sec:org18753cc}
\label{sec-12-4}
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.
\begin{verbatim}
@@ -3042,9 +2973,9 @@ MODULE_DESCRIPTION("Atomic operations example");
MODULE_LICENSE("GPL");
\end{verbatim}
\section*{Replacing Print Macros}
\label{sec:org3904545}
\label{sec-13}
\subsection*{Replacement}
\label{sec:org8657a17}
\label{sec-13-1}
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.
"tty" is an abbreviation of \emph{teletype}: originally a combination keyboard-printer used to communicate with a Unix system, and today an abstraction for the text stream used for a Unix program, whether it's a physical terminal, an xterm on an X display, a network connection used with ssh, etc.
@@ -3163,7 +3094,7 @@ module_exit(print_string_exit);
\end{verbatim}
\subsection*{Flashing keyboard LEDs}
\label{sec:org20e7e12}
\label{sec-13-2}
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.
The following source code illustrates a minimal kernel module which, when loaded, starts blinking the keyboard LEDs until it is unloaded.
@@ -3269,11 +3200,11 @@ If none of the examples in this chapter fit your debugging needs there might yet
While you have seen lots of stuff that can be used to aid debugging here, there are some things to be aware of. Debugging is almost always intrusive. Adding debug code can change the situation enough to make the bug seem to dissappear. Thus you should try to keep debug code to a minimum and make sure it does not show up in production code.
\section*{Scheduling Tasks}
\label{sec:org22cd2ac}
\label{sec-14}
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.
\subsection*{Tasklets}
\label{sec:org61a92d7}
\label{sec-14-1}
Here's an example tasklet module. The \emph{tasklet\_fn} function runs for a few seconds and in the mean time execution of the \emph{example\_tasklet\_init} function continues to the exit point.
\begin{verbatim}
@@ -3323,7 +3254,7 @@ Example tasklet init continues...
Example tasklet ends
\end{verbatim}
\subsection*{Work queues}
\label{sec:org546f488}
\label{sec-14-2}
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.
\begin{verbatim}
@@ -3360,9 +3291,9 @@ MODULE_DESCRIPTION("Workqueue example");
\end{verbatim}
\section*{Interrupt Handlers}
\label{sec:org5c07d3a}
\label{sec-15}
\subsection*{Interrupt Handlers}
\label{sec:org4f9dad1}
\label{sec-15-1}
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.
There are two types of interaction between the CPU and the rest of the computer's hardware. The first type is when the CPU gives orders to the hardware, the other is when the hardware needs to tell the CPU something. The second, called interrupts, is much harder to implement because it has to be dealt with when convenient for the hardware, not the CPU. Hardware devices typically have a very small amount of RAM, and if you don't read their information when available, it is lost.
@@ -3378,7 +3309,7 @@ In practice IRQ handling can be a bit more complex. Hardware is often designed i
This function receives the IRQ number, the name of the function, flags, a name for /proc/interrupts and a parameter to pass to the interrupt handler. Usually there is a certain number of IRQs available. How many IRQs there are is hardware-dependent. The flags can include SA\_SHIRQ to indicate you're willing to share the IRQ with other interrupt handlers (usually because a number of hardware devices sit on the same IRQ) and SA\_INTERRUPT to indicate this is a fast interrupt. This function will only succeed if there isn't already a handler on this IRQ, or if you're both willing to share.
\subsection*{Detecting button presses}
\label{sec:org8aa69b8}
\label{sec-15-2}
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.
Here's an example where buttons are connected to GPIO numbers 17 and 18 and an LED is connected to GPIO 4. You can change those numbers to whatever is appropriate for your board.
@@ -3538,7 +3469,7 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts");
\end{verbatim}
\subsection*{Bottom Half}
\label{sec:org23c6161}
\label{sec-15-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 bulk of the work off into the scheduler.
The example below modifies the previous example to also run an additional task when an interrupt is triggered.
@@ -3711,11 +3642,11 @@ MODULE_AUTHOR("Bob Mottram");
MODULE_DESCRIPTION("Interrupt with top and bottom half");
\end{verbatim}
\section*{Crypto}
\label{sec:org4022507}
\label{sec-16}
At the dawn of the internet everybody trusted everybody completely\ldots{}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.
\subsection*{Hash functions}
\label{sec:orgc51232f}
\label{sec-16-1}
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.
@@ -3802,7 +3733,7 @@ Finally, remove the test module:
sudo rmmod cryptosha256
\end{verbatim}
\subsection*{Symmetric key encryption}
\label{sec:org68c3780}
\label{sec-16-2}
Here is an example of symmetrically encrypting a string using the AES algorithm and a password.
\begin{verbatim}
@@ -3988,7 +3919,7 @@ MODULE_DESCRIPTION("Symmetric key encryption example");
MODULE_LICENSE("GPL");
\end{verbatim}
\section*{Standardising the interfaces: The Device Model}
\label{sec:orgf49b112}
\label{sec-17}
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.
\begin{verbatim}
@@ -4090,9 +4021,9 @@ module_init(devicemodel_init);
module_exit(devicemodel_exit);
\end{verbatim}
\section*{Optimisations}
\label{sec:orga045cfd}
\label{sec-18}
\subsection*{Likely and Unlikely conditions}
\label{sec:org279fdc0}
\label{sec-18-1}
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 \emph{true} or \emph{false}, then you can allow the compiler to optimise for this using the \emph{likely} and \emph{unlikely} macros.
For example, when allocating memory you're almost always expecting this to succeed.
@@ -4108,23 +4039,23 @@ if (unlikely(!bvl)) {
When the \emph{unlikely} macro is used the compiler alters its machine instruction output so that it continues along the false branch and only jumps if the condition is true. That avoids flushing the processor pipeline. The opposite happens if you use the \emph{likely} macro.
\section*{Common Pitfalls}
\label{sec:org5911e2e}
\label{sec-19}
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.
\subsection*{Using standard libraries}
\label{sec:orgf4b2a9d}
\label{sec-19-1}
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.
\subsection*{Disabling interrupts}
\label{sec:org7bc9c92}
\label{sec-19-2}
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.
\subsection*{Sticking your head inside a large carnivore}
\label{sec:org5023a68}
\label{sec-19-3}
I probably don't have to warn you about this, but I figured I will anyway, just in case.
\section*{Where To Go From Here?}
\label{sec:org4bc2277}
\label{sec-20}
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.
However, I chose not to. My purpose in writing this book was to provide initiation into the mysteries of kernel module programming and to teach the common techniques for that purpose. For people seriously interested in kernel programming, I recommend \href{https://kernelnewbies.org}{kernelnewbies.org} and the \emph{Documentation} subdirectory within the kernel source code which isn't always easy to understand but can be a starting point for further investigation. Also, as Linus said, the best way to learn the kernel is to read the source code yourself.
@@ -4134,4 +4065,5 @@ If you're interested in more examples of short kernel modules then searching on
I hope I have helped you in your quest to become a better programmer, or at least to have fun through technology. And, if you do write useful kernel modules, I hope you publish them under the GPL, so I can use them too.
If you'd like to contribute to this guide, notice anything glaringly wrong, or just want to add extra sarcastic remarks perhaps involving monkeys or some other kind of animal then please file an issue or even better submit a pull request at \url{https://github.com/bashrc/LKMPG}.
% Emacs 25.3.1 (Org mode 8.2.3c)
\end{document}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB