diff --git a/4.15.2/LKMPG-4.15.2.html b/4.15.2/LKMPG-4.15.2.html index a52a914..352b943 100644 --- a/4.15.2/LKMPG-4.15.2.html +++ b/4.15.2/LKMPG-4.15.2.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + The Linux Kernel Module Programming Guide @@ -265,121 +265,121 @@ for the JavaScript code in this tag.

Table of Contents

-
-

Introduction

-
+
+

Introduction

+

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 +401,18 @@ If you publish or distribute this book commercially, donations, royalties, and/o

-
-

Authorship

-
+
+

Authorship

+

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.

-
-

Versioning and Notes

-
+
+

Versioning and Notes

+

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 +423,18 @@ The source code and discussions should apply to most architectures, but I can't
-
-

Acknowledgements

-
+
+

Acknowledgements

+

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.

-
-

What Is A Kernel Module?

-
+
+

What Is A Kernel Module?

+

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 +445,9 @@ What exactly is a kernel module? Modules are pieces of code that can be loaded a
-
-

Kernel module package

-
+
+

Kernel module package

+

Linux distros provide the commands modprobe, insmod and depmod within a package.

@@ -472,9 +472,9 @@ On Parabola:
-
-

What Modules are in my Kernel?

-
+
+

What Modules are in my Kernel?

+

To discover what modules are already loaded within your current kernel use the command lsmod.

@@ -504,33 +504,33 @@ This can be a long list, and you might prefer to search for something particular
-
-

Do I need to download and compile the kernel?

-
+
+

Do I need to download and compile the kernel?

+

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 Begin

-
+
+

Before We Begin

+

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.

    -
  • Modversioning
    -
    +
  • Modversioning
    +

    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.

  • -
  • Using X
    -
    +
  • Using X
    +

    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.

    @@ -544,9 +544,9 @@ Modules can't print to the screen like printf() can, but they can log informatio
-
-

Headers

-
+
+

Headers

+

Before you can build anything you'll need to install the header files for your kernel. On Parabola GNU/Linux:

@@ -576,9 +576,9 @@ This will tell you what kernel header files are available. Then for example:
-
-

Examples

-
+
+

Examples

+

All the examples from this document are available within the examples subdirectory. To test that they compile:

@@ -594,13 +594,13 @@ If there are any compile errors then you might have a more recent kernel version

-
-

Hello World

-
+
+

Hello World

+
-
-

The Simplest Module

-
+
+

The Simplest Module

+

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.

@@ -744,23 +744,23 @@ Lastly, every kernel module needs to include linux/module.h. We needed to includ
    -
  • A point about coding style
    -
    +
  • A point about coding style
    +

    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.

  • -
  • Introducing print macros
    -
    +
  • Introducing print macros
    +

    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.

  • -
  • About Compiling
    -
    +
  • About Compiling
    +

    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.

    @@ -779,9 +779,9 @@ Here's another exercise for the reader. See that comment above the return statem
-
-

Hello and Goodbye

-
+
+

Hello and Goodbye

+

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 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:

@@ -833,9 +833,9 @@ Now have a look at linux/drivers/char/Makefile for a real world example. As you
-
-

The __init and __exit Macros

-
+
+

The __init and __exit Macros

+

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.

@@ -880,9 +880,9 @@ module_exit(hello_3_exit);
-
-

Licensing and Module Documentation

-
+
+

Licensing and Module Documentation

+

Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this:

@@ -934,9 +934,9 @@ module_exit(cleanup_hello_4);
-
-

Passing Command Line Arguments to a Module

-
+
+

Passing Command Line Arguments to a Module

+

Modules can take command line arguments, but not with the argc/argv you might be used to.

@@ -1086,9 +1086,9 @@ hello-5.o: invalid argument syntax for mylong: 'h'
-
-

Modules Spanning Multiple Files

-
+
+

Modules Spanning Multiple Files

+

Sometimes it makes sense to divide a kernel module between several source files.

@@ -1159,9 +1159,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.

@@ -1253,13 +1253,13 @@ If you do not desire to actually compile the kernel, you can interrupt the build
-
-

Preliminaries

-
+
+

Preliminaries

+
-
-

How modules begin and end

-
+
+

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 +1274,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.

@@ -1314,9 +1314,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 +1327,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 +1344,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.

@@ -1365,17 +1365,17 @@ By the way, I would like to point out that the above discussion is true for any
-
-

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:

    @@ -1440,13 +1440,13 @@ By now you can look at these two device files and know instantly that they are b
-
-

Character Device drivers

-
+
+

Character Device drivers

+
-
-

The file_operations Structure

-
+
+

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.

@@ -1531,9 +1531,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 +1548,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.

@@ -1578,9 +1578,9 @@ If you pass a major number of 0 to register_chrdev, the return value will be the
-
-

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.

@@ -1600,9 +1600,9 @@ 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.

@@ -1803,9 +1803,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 +1829,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.

@@ -1921,9 +1921,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)

@@ -2040,9 +2040,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.

@@ -2160,9 +2160,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 @@ -2352,9 +2352,9 @@ 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.

@@ -2488,9 +2488,9 @@ Finally, remove the test module:
-
-

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.

@@ -2986,9 +2986,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.

@@ -3194,13 +3194,13 @@ MODULE_LICENSE("GPL");
-
-

Blocking Processes and threads

-
+
+

Blocking Processes and threads

+
-
-

Sleep

-
+
+

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).

@@ -3610,9 +3610,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.

@@ -3717,16 +3717,16 @@ 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.

@@ -3776,9 +3776,9 @@ 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.

@@ -3856,9 +3856,9 @@ 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.

@@ -3925,9 +3925,9 @@ 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.

@@ -4014,13 +4014,13 @@ MODULE_LICENSE("GPL");
-
-

Replacing Print Macros

-
+
+

Replacing Print Macros

+
-
-

Replacement

-
+
+

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.

@@ -4147,9 +4147,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.

@@ -4235,9 +4235,7 @@ MODULE_LICENSE("GPL"); /* * Set up the LED blink timer the first time */ - init_timer(&my_timer); - my_timer.function = my_timer_func; - my_timer.data = (unsigned long)&kbledstatus; + timer_setup(&my_timer, (void*)&my_timer_func, (unsigned long)&kbledstatus); my_timer.expires = jiffies + BLINK_DELAY; add_timer(&my_timer); @@ -4268,17 +4266,17 @@ 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.

@@ -4335,9 +4333,9 @@ 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.

@@ -4379,13 +4377,13 @@ 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.

@@ -4416,9 +4414,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.

@@ -4584,9 +4582,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.

@@ -4766,17 +4764,17 @@ 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.

@@ -4874,9 +4872,9 @@ Finally, remove the test module:
-
-

Symmetric key encryption

-
+
+

Symmetric key encryption

+

Here is an example of symmetrically encrypting a string using the AES algorithm and a password.

@@ -5067,9 +5065,9 @@ 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.

@@ -5175,13 +5173,13 @@ module_exit(devicemodel_exit);
-
-

Optimisations

-
+
+

Optimisations

+
-
-

Likely and Unlikely conditions

-
+
+

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.

@@ -5206,35 +5204,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.

@@ -5242,9 +5240,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 62f5968..a6f02ef 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](#orgf3497c5) - - [Authorship](#org34e7977) - - [Versioning and Notes](#orgf6ef76f) - - [Acknowledgements](#orgc967c58) - - [What Is A Kernel Module?](#org0a581aa) - - [Kernel module package](#org029cb2f) - - [What Modules are in my Kernel?](#org5ef33ca) - - [Do I need to download and compile the kernel?](#org5b17e9b) - - [Before We Begin](#orgcfb9f7e) -- [Headers](#orge2b5ae3) -- [Examples](#org5a5f628) -- [Hello World](#org9c9913f) - - [The Simplest Module](#orgd6069e0) - - [Hello and Goodbye](#org1217bec) - - [The \_\_init and \_\_exit Macros](#org90d1270) - - [Licensing and Module Documentation](#orgc726252) - - [Passing Command Line Arguments to a Module](#orgf38f4b6) - - [Modules Spanning Multiple Files](#org1d8bb9c) - - [Building modules for a precompiled kernel](#org4210695) -- [Preliminaries](#org9b6e076) - - [How modules begin and end](#orga9647ed) - - [Functions available to modules](#org9bd06e9) - - [User Space vs Kernel Space](#org7dabaae) - - [Name Space](#org16acec4) - - [Code space](#orge85acf5) - - [Device Drivers](#orgb47a01a) -- [Character Device drivers](#org7882da3) - - [The file\_operations Structure](#orgb24fcc0) - - [The file structure](#org6e8ef01) - - [Registering A Device](#org68e914d) - - [Unregistering A Device](#org9dd4a7c) - - [chardev.c](#orgfd4ef85) - - [Writing Modules for Multiple Kernel Versions](#org2de85a6) -- [The /proc File System](#orgbd42731) - - [Read and Write a /proc File](#orgac2ac98) - - [Manage /proc file with standard filesystem](#orgb21d645) - - [Manage /proc file with seq\_file](#org3dd2bb9) -- [sysfs: Interacting with your module](#org714b89f) -- [Talking To Device Files](#orgb674dde) -- [System Calls](#org6add61c) -- [Blocking Processes and threads](#org3359a62) - - [Sleep](#org65a16ba) - - [Completions](#orgc4aa165) -- [Avoiding Collisions and Deadlocks](#org6a92beb) - - [Mutex](#org9e0e461) - - [Spinlocks](#org66f7c66) - - [Read and write locks](#orgedf3265) - - [Atomic operations](#org532aa9f) -- [Replacing Print Macros](#org2b469b3) - - [Replacement](#org725ae77) - - [Flashing keyboard LEDs](#org8a29832) -- [Scheduling Tasks](#org7cabe47) - - [Tasklets](#org0282886) - - [Work queues](#orgf7f8600) -- [Interrupt Handlers](#orgd551fb7) - - [Interrupt Handlers](#org64784c3) - - [Detecting button presses](#org74afed1) - - [Bottom Half](#orgb77ff34) -- [Crypto](#orgd3754e6) - - [Hash functions](#orge22ae95) - - [Symmetric key encryption](#orgc4f4be1) -- [Standardising the interfaces: The Device Model](#orgb192fca) -- [Optimisations](#orgd494e36) - - [Likely and Unlikely conditions](#orgfee6da3) -- [Common Pitfalls](#org57e70e3) - - [Using standard libraries](#orgb09decf) - - [Disabling interrupts](#org0cdb999) - - [Sticking your head inside a large carnivore](#org8882024) -- [Where To Go From Here?](#org195148f) +- [Introduction](#org0c1f452) + - [Authorship](#org795474a) + - [Versioning and Notes](#org4087c64) + - [Acknowledgements](#orgfee3c45) + - [What Is A Kernel Module?](#orgaa146fc) + - [Kernel module package](#orgb8dd50e) + - [What Modules are in my Kernel?](#org2c8e961) + - [Do I need to download and compile the kernel?](#org3a5069c) + - [Before We Begin](#org65deef9) +- [Headers](#org8ba4da8) +- [Examples](#org4259ce6) +- [Hello World](#orgfa484db) + - [The Simplest Module](#org495cb53) + - [Hello and Goodbye](#org7a50b41) + - [The \_\_init and \_\_exit Macros](#orgd5c7fb1) + - [Licensing and Module Documentation](#orgcc3ca30) + - [Passing Command Line Arguments to a Module](#org4e71571) + - [Modules Spanning Multiple Files](#org22bcf57) + - [Building modules for a precompiled kernel](#orge74dfca) +- [Preliminaries](#org8752231) + - [How modules begin and end](#orge94c71e) + - [Functions available to modules](#org1a9dc70) + - [User Space vs Kernel Space](#orgeaf5d21) + - [Name Space](#orgd3ea79f) + - [Code space](#org1595baf) + - [Device Drivers](#orgd0ff7c1) +- [Character Device drivers](#orgd16c37f) + - [The file\_operations Structure](#org079be2f) + - [The file structure](#org5161089) + - [Registering A Device](#org39d8806) + - [Unregistering A Device](#orgeadff50) + - [chardev.c](#orgdd7b4b0) + - [Writing Modules for Multiple Kernel Versions](#org46c702f) +- [The /proc File System](#orgfd453ce) + - [Read and Write a /proc File](#org4178ce8) + - [Manage /proc file with standard filesystem](#orgfaee38c) + - [Manage /proc file with seq\_file](#orgb5757af) +- [sysfs: Interacting with your module](#orge1358df) +- [Talking To Device Files](#org9458047) +- [System Calls](#org7676770) +- [Blocking Processes and threads](#org26b5f2e) + - [Sleep](#orge28c7d5) + - [Completions](#org4574a81) +- [Avoiding Collisions and Deadlocks](#org9949395) + - [Mutex](#orgaacad0a) + - [Spinlocks](#orgba2b451) + - [Read and write locks](#org78e399c) + - [Atomic operations](#orgb70376b) +- [Replacing Print Macros](#orgab764f9) + - [Replacement](#orgf8bbb15) + - [Flashing keyboard LEDs](#org331f665) +- [Scheduling Tasks](#org3ef4dd6) + - [Tasklets](#orgc6bcee8) + - [Work queues](#orgd4fe9d0) +- [Interrupt Handlers](#org3c40e87) + - [Interrupt Handlers](#orge9f7498) + - [Detecting button presses](#orgc89ace5) + - [Bottom Half](#orgfa48677) +- [Crypto](#orgc1f3763) + - [Hash functions](#org2808d46) + - [Symmetric key encryption](#org9ceede6) +- [Standardising the interfaces: The Device Model](#org0a78c70) +- [Optimisations](#org24da896) + - [Likely and Unlikely conditions](#org45afbf3) +- [Common Pitfalls](#org972b87c) + - [Using standard libraries](#orgc7bc4d9) + - [Disabling interrupts](#org8e4ee93) + - [Sticking your head inside a large carnivore](#orgb182bfa) +- [Where To Go From Here?](#org4bdbcf7) ::: ::: -::: {#outline-container-orgf3497c5 .outline-2} -Introduction {#orgf3497c5} +::: {#outline-container-org0c1f452 .outline-2} +Introduction {#org0c1f452} ------------ -::: {#text-orgf3497c5 .outline-text-2} +::: {#text-org0c1f452 .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. @@ -116,10 +116,10 @@ LDP. If you have questions or comments, please contact the address above. ::: -::: {#outline-container-org34e7977 .outline-3} -### Authorship {#org34e7977} +::: {#outline-container-org795474a .outline-3} +### Authorship {#org795474a} -::: {#text-org34e7977 .outline-text-3} +::: {#text-org795474a .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-orgf6ef76f .outline-3} -### Versioning and Notes {#orgf6ef76f} +::: {#outline-container-org4087c64 .outline-3} +### Versioning and Notes {#org4087c64} -::: {#text-orgf6ef76f .outline-text-3} +::: {#text-org4087c64 .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-orgc967c58 .outline-3} -### Acknowledgements {#orgc967c58} +::: {#outline-container-orgfee3c45 .outline-3} +### Acknowledgements {#orgfee3c45} -::: {#text-orgc967c58 .outline-text-3} +::: {#text-orgfee3c45 .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-org0a581aa .outline-3} -### What Is A Kernel Module? {#org0a581aa} +::: {#outline-container-orgaa146fc .outline-3} +### What Is A Kernel Module? {#orgaa146fc} -::: {#text-org0a581aa .outline-text-3} +::: {#text-orgaa146fc .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-org029cb2f .outline-3} -### Kernel module package {#org029cb2f} +::: {#outline-container-orgb8dd50e .outline-3} +### Kernel module package {#orgb8dd50e} -::: {#text-org029cb2f .outline-text-3} +::: {#text-orgb8dd50e .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-org5ef33ca .outline-3} -### What Modules are in my Kernel? {#org5ef33ca} +::: {#outline-container-org2c8e961 .outline-3} +### What Modules are in my Kernel? {#org2c8e961} -::: {#text-org5ef33ca .outline-text-3} +::: {#text-org2c8e961 .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-org5b17e9b .outline-3} -### Do I need to download and compile the kernel? {#org5b17e9b} +::: {#outline-container-org3a5069c .outline-3} +### Do I need to download and compile the kernel? {#org3a5069c} -::: {#text-org5b17e9b .outline-text-3} +::: {#text-org3a5069c .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-orgcfb9f7e .outline-3} -### Before We Begin {#orgcfb9f7e} +::: {#outline-container-org65deef9 .outline-3} +### Before We Begin {#org65deef9} -::: {#text-orgcfb9f7e .outline-text-3} +::: {#text-org65deef9 .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. ::: -- []{#org0622852}Modversioning\ - ::: {#text-org0622852 .outline-text-5} +- []{#orge72489f}Modversioning\ + ::: {#text-orge72489f .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. ::: -- []{#org6298e45}Using X\ - ::: {#text-org6298e45 .outline-text-5} +- []{#org7fc2c85}Using X\ + ::: {#text-org7fc2c85 .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-orge2b5ae3 .outline-2} -Headers {#orge2b5ae3} +::: {#outline-container-org8ba4da8 .outline-2} +Headers {#org8ba4da8} ------- -::: {#text-orge2b5ae3 .outline-text-2} +::: {#text-org8ba4da8 .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.14.8-1-amd64 ::: ::: -::: {#outline-container-org5a5f628 .outline-2} -Examples {#org5a5f628} +::: {#outline-container-org4259ce6 .outline-2} +Examples {#org4259ce6} -------- -::: {#text-org5a5f628 .outline-text-2} +::: {#text-org4259ce6 .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-org9c9913f .outline-2} -Hello World {#org9c9913f} +::: {#outline-container-orgfa484db .outline-2} +Hello World {#orgfa484db} ----------- -::: {#text-org9c9913f .outline-text-2} +::: {#text-orgfa484db .outline-text-2} ::: -::: {#outline-container-orgd6069e0 .outline-3} -### The Simplest Module {#orgd6069e0} +::: {#outline-container-org495cb53 .outline-3} +### The Simplest Module {#org495cb53} -::: {#text-orgd6069e0 .outline-text-3} +::: {#text-org495cb53 .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. ::: -- []{#org1239959}A point about coding style\ - ::: {#text-org1239959 .outline-text-5} +- []{#orge9f9010}A point about coding style\ + ::: {#text-orge9f9010 .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. ::: -- []{#orga621564}Introducing print macros\ - ::: {#text-orga621564 .outline-text-5} +- []{#org6c9563e}Introducing print macros\ + ::: {#text-org6c9563e .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. ::: -- []{#org8021c2d}About Compiling\ - ::: {#text-org8021c2d .outline-text-5} +- []{#orgaf1fbff}About Compiling\ + ::: {#text-orgaf1fbff .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-org1217bec .outline-3} -### Hello and Goodbye {#org1217bec} +::: {#outline-container-org7a50b41 .outline-3} +### Hello and Goodbye {#org7a50b41} -::: {#text-org1217bec .outline-text-3} +::: {#text-org7a50b41 .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-org90d1270 .outline-3} -### The \_\_init and \_\_exit Macros {#org90d1270} +::: {#outline-container-orgd5c7fb1 .outline-3} +### The \_\_init and \_\_exit Macros {#orgd5c7fb1} -::: {#text-org90d1270 .outline-text-3} +::: {#text-orgd5c7fb1 .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-orgc726252 .outline-3} -### Licensing and Module Documentation {#orgc726252} +::: {#outline-container-orgcc3ca30 .outline-3} +### Licensing and Module Documentation {#orgcc3ca30} -::: {#text-orgc726252 .outline-text-3} +::: {#text-orgcc3ca30 .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-orgf38f4b6 .outline-3} -### Passing Command Line Arguments to a Module {#orgf38f4b6} +::: {#outline-container-org4e71571 .outline-3} +### Passing Command Line Arguments to a Module {#org4e71571} -::: {#text-orgf38f4b6 .outline-text-3} +::: {#text-org4e71571 .outline-text-3} Modules can take command line arguments, but not with the argc/argv you might be used to. @@ -886,10 +886,10 @@ hello-5.o: invalid argument syntax for mylong: 'h' ::: ::: -::: {#outline-container-org1d8bb9c .outline-3} -### Modules Spanning Multiple Files {#org1d8bb9c} +::: {#outline-container-org22bcf57 .outline-3} +### Modules Spanning Multiple Files {#org22bcf57} -::: {#text-org1d8bb9c .outline-text-3} +::: {#text-org22bcf57 .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-org4210695 .outline-3} -### Building modules for a precompiled kernel {#org4210695} +::: {#outline-container-orge74dfca .outline-3} +### Building modules for a precompiled kernel {#orge74dfca} -::: {#text-org4210695 .outline-text-3} +::: {#text-orge74dfca .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-org9b6e076 .outline-2} -Preliminaries {#org9b6e076} +::: {#outline-container-org8752231 .outline-2} +Preliminaries {#org8752231} ------------- -::: {#text-org9b6e076 .outline-text-2} +::: {#text-org8752231 .outline-text-2} ::: -::: {#outline-container-orga9647ed .outline-3} -### How modules begin and end {#orga9647ed} +::: {#outline-container-orge94c71e .outline-3} +### How modules begin and end {#orge94c71e} -::: {#text-orga9647ed .outline-text-3} +::: {#text-orge94c71e .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-org9bd06e9 .outline-3} -### Functions available to modules {#org9bd06e9} +::: {#outline-container-org1a9dc70 .outline-3} +### Functions available to modules {#org1a9dc70} -::: {#text-org9bd06e9 .outline-text-3} +::: {#text-org1a9dc70 .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-org7dabaae .outline-3} -### User Space vs Kernel Space {#org7dabaae} +::: {#outline-container-orgeaf5d21 .outline-3} +### User Space vs Kernel Space {#orgeaf5d21} -::: {#text-org7dabaae .outline-text-3} +::: {#text-orgeaf5d21 .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-org16acec4 .outline-3} -### Name Space {#org16acec4} +::: {#outline-container-orgd3ea79f .outline-3} +### Name Space {#orgd3ea79f} -::: {#text-org16acec4 .outline-text-3} +::: {#text-orgd3ea79f .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-orge85acf5 .outline-3} -### Code space {#orge85acf5} +::: {#outline-container-org1595baf .outline-3} +### Code space {#org1595baf} -::: {#text-orge85acf5 .outline-text-3} +::: {#text-org1595baf .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 @@ -1292,10 +1292,10 @@ Magenta kernel of Google Fuchsia are two examples of a microkernel. ::: ::: -::: {#outline-container-orgb47a01a .outline-3} -### Device Drivers {#orgb47a01a} +::: {#outline-container-orgd0ff7c1 .outline-3} +### Device Drivers {#orgd0ff7c1} -::: {#text-orgb47a01a .outline-text-3} +::: {#text-orgd0ff7c1 .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. ::: -- []{#orgfad3092}Major and Minor Numbers\ - ::: {#text-orgfad3092 .outline-text-5} +- []{#org60beeae}Major and Minor Numbers\ + ::: {#text-org60beeae .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: @@ -1406,17 +1406,17 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use ::: ::: -::: {#outline-container-org7882da3 .outline-2} -Character Device drivers {#org7882da3} +::: {#outline-container-orgd16c37f .outline-2} +Character Device drivers {#orgd16c37f} ------------------------ -::: {#text-org7882da3 .outline-text-2} +::: {#text-orgd16c37f .outline-text-2} ::: -::: {#outline-container-orgb24fcc0 .outline-3} -### The file\_operations Structure {#orgb24fcc0} +::: {#outline-container-org079be2f .outline-3} +### The file\_operations Structure {#org079be2f} -::: {#text-orgb24fcc0 .outline-text-3} +::: {#text-org079be2f .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-org6e8ef01 .outline-3} -### The file structure {#org6e8ef01} +::: {#outline-container-org5161089 .outline-3} +### The file structure {#org5161089} -::: {#text-org6e8ef01 .outline-text-3} +::: {#text-org5161089 .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-org68e914d .outline-3} -### Registering A Device {#org68e914d} +::: {#outline-container-org39d8806 .outline-3} +### Registering A Device {#org39d8806} -::: {#text-org68e914d .outline-text-3} +::: {#text-org39d8806 .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-org9dd4a7c .outline-3} -### Unregistering A Device {#org9dd4a7c} +::: {#outline-container-orgeadff50 .outline-3} +### Unregistering A Device {#orgeadff50} -::: {#text-org9dd4a7c .outline-text-3} +::: {#text-orgeadff50 .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-orgfd4ef85 .outline-3} -### chardev.c {#orgfd4ef85} +::: {#outline-container-orgdd7b4b0 .outline-3} +### chardev.c {#orgdd7b4b0} -::: {#text-orgfd4ef85 .outline-text-3} +::: {#text-orgdd7b4b0 .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-org2de85a6 .outline-3} -### Writing Modules for Multiple Kernel Versions {#org2de85a6} +::: {#outline-container-org46c702f .outline-3} +### Writing Modules for Multiple Kernel Versions {#org46c702f} -::: {#text-org2de85a6 .outline-text-3} +::: {#text-org46c702f .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-orgbd42731 .outline-2} -The /proc File System {#orgbd42731} +::: {#outline-container-orgfd453ce .outline-2} +The /proc File System {#orgfd453ce} --------------------- -::: {#text-orgbd42731 .outline-text-2} +::: {#text-orgfd453ce .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-orgac2ac98 .outline-3} -### Read and Write a /proc File {#orgac2ac98} +::: {#outline-container-org4178ce8 .outline-3} +### Read and Write a /proc File {#org4178ce8} -::: {#text-orgac2ac98 .outline-text-3} +::: {#text-org4178ce8 .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-orgb21d645 .outline-3} -### Manage /proc file with standard filesystem {#orgb21d645} +::: {#outline-container-orgfaee38c .outline-3} +### Manage /proc file with standard filesystem {#orgfaee38c} -::: {#text-orgb21d645 .outline-text-3} +::: {#text-orgfaee38c .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-org3dd2bb9 .outline-3} -### Manage /proc file with seq\_file {#org3dd2bb9} +::: {#outline-container-orgb5757af .outline-3} +### Manage /proc file with seq\_file {#orgb5757af} -::: {#text-org3dd2bb9 .outline-text-3} +::: {#text-orgb5757af .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-org714b89f .outline-2} -sysfs: Interacting with your module {#org714b89f} +::: {#outline-container-orge1358df .outline-2} +sysfs: Interacting with your module {#orge1358df} ----------------------------------- -::: {#text-org714b89f .outline-text-2} +::: {#text-orge1358df .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. @@ -2592,11 +2592,11 @@ sudo rmmod hello_sysfs ::: ::: -::: {#outline-container-orgb674dde .outline-2} -Talking To Device Files {#orgb674dde} +::: {#outline-container-org9458047 .outline-2} +Talking To Device Files {#org9458047} ----------------------- -::: {#text-orgb674dde .outline-text-2} +::: {#text-org9458047 .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 +3118,11 @@ int main() ::: ::: -::: {#outline-container-org6add61c .outline-2} -System Calls {#org6add61c} +::: {#outline-container-org7676770 .outline-2} +System Calls {#org7676770} ------------ -::: {#text-org6add61c .outline-text-2} +::: {#text-org7676770 .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 +3396,17 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-org3359a62 .outline-2} -Blocking Processes and threads {#org3359a62} +::: {#outline-container-org26b5f2e .outline-2} +Blocking Processes and threads {#org26b5f2e} ------------------------------ -::: {#text-org3359a62 .outline-text-2} +::: {#text-org26b5f2e .outline-text-2} ::: -::: {#outline-container-org65a16ba .outline-3} -### Sleep {#org65a16ba} +::: {#outline-container-orge28c7d5 .outline-3} +### Sleep {#orge28c7d5} -::: {#text-org65a16ba .outline-text-3} +::: {#text-orge28c7d5 .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 +3850,10 @@ int main(int argc, char *argv[]) ::: ::: -::: {#outline-container-orgc4aa165 .outline-3} -### Completions {#orgc4aa165} +::: {#outline-container-org4574a81 .outline-3} +### Completions {#org4574a81} -::: {#text-orgc4aa165 .outline-text-3} +::: {#text-org4574a81 .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 +3960,11 @@ enough for many common situations without adding a lot of complexity. ::: ::: -::: {#outline-container-org6a92beb .outline-2} -Avoiding Collisions and Deadlocks {#org6a92beb} +::: {#outline-container-org9949395 .outline-2} +Avoiding Collisions and Deadlocks {#org9949395} --------------------------------- -::: {#text-org6a92beb .outline-text-2} +::: {#text-org9949395 .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 +3973,10 @@ code is \"locked\" or \"unlocked\" so that simultaneous attempts to run it can\'t happen. ::: -::: {#outline-container-org9e0e461 .outline-3} -### Mutex {#org9e0e461} +::: {#outline-container-orgaacad0a .outline-3} +### Mutex {#orgaacad0a} -::: {#text-org9e0e461 .outline-text-3} +::: {#text-orgaacad0a .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,10 +4028,10 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-org66f7c66 .outline-3} -### Spinlocks {#org66f7c66} +::: {#outline-container-orgba2b451 .outline-3} +### Spinlocks {#orgba2b451} -::: {#text-org66f7c66 .outline-text-3} +::: {#text-orgba2b451 .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 @@ -4112,10 +4112,10 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-orgedf3265 .outline-3} -### Read and write locks {#orgedf3265} +::: {#outline-container-org78e399c .outline-3} +### Read and write locks {#org78e399c} -::: {#text-orgedf3265 .outline-text-3} +::: {#text-org78e399c .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 +4190,10 @@ corresponding write functions. ::: ::: -::: {#outline-container-org532aa9f .outline-3} -### Atomic operations {#org532aa9f} +::: {#outline-container-orgb70376b .outline-3} +### Atomic operations {#orgb70376b} -::: {#text-org532aa9f .outline-text-3} +::: {#text-orgb70376b .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 +4285,17 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-org2b469b3 .outline-2} -Replacing Print Macros {#org2b469b3} +::: {#outline-container-orgab764f9 .outline-2} +Replacing Print Macros {#orgab764f9} ---------------------- -::: {#text-org2b469b3 .outline-text-2} +::: {#text-orgab764f9 .outline-text-2} ::: -::: {#outline-container-org725ae77 .outline-3} -### Replacement {#org725ae77} +::: {#outline-container-orgf8bbb15 .outline-3} +### Replacement {#orgf8bbb15} -::: {#text-org725ae77 .outline-text-3} +::: {#text-orgf8bbb15 .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 +4427,10 @@ module_exit(print_string_exit); ::: ::: -::: {#outline-container-org8a29832 .outline-3} -### Flashing keyboard LEDs {#org8a29832} +::: {#outline-container-org331f665 .outline-3} +### Flashing keyboard LEDs {#org331f665} -::: {#text-org8a29832 .outline-text-3} +::: {#text-org331f665 .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 @@ -4519,9 +4519,7 @@ static int __init kbleds_init(void) /* * Set up the LED blink timer the first time */ - init_timer(&my_timer); - my_timer.function = my_timer_func; - my_timer.data = (unsigned long)&kbledstatus; + timer_setup(&my_timer, (void*)&my_timer_func, (unsigned long)&kbledstatus); my_timer.expires = jiffies + BLINK_DELAY; add_timer(&my_timer); @@ -4561,11 +4559,11 @@ minimum and make sure it does not show up in production code. ::: ::: -::: {#outline-container-org7cabe47 .outline-2} -Scheduling Tasks {#org7cabe47} +::: {#outline-container-org3ef4dd6 .outline-2} +Scheduling Tasks {#org3ef4dd6} ---------------- -::: {#text-org7cabe47 .outline-text-2} +::: {#text-org3ef4dd6 .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 @@ -4573,10 +4571,10 @@ are more complicated but also better suited to running multiple things in a sequence. ::: -::: {#outline-container-org0282886 .outline-3} -### Tasklets {#org0282886} +::: {#outline-container-orgc6bcee8 .outline-3} +### Tasklets {#orgc6bcee8} -::: {#text-org0282886 .outline-text-3} +::: {#text-orgc6bcee8 .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. @@ -4634,10 +4632,10 @@ Example tasklet ends ::: ::: -::: {#outline-container-orgf7f8600 .outline-3} -### Work queues {#orgf7f8600} +::: {#outline-container-orgd4fe9d0 .outline-3} +### Work queues {#orgd4fe9d0} -::: {#text-orgf7f8600 .outline-text-3} +::: {#text-orgd4fe9d0 .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. @@ -4680,17 +4678,17 @@ MODULE_DESCRIPTION("Workqueue example"); ::: ::: -::: {#outline-container-orgd551fb7 .outline-2} -Interrupt Handlers {#orgd551fb7} +::: {#outline-container-org3c40e87 .outline-2} +Interrupt Handlers {#org3c40e87} ------------------ -::: {#text-orgd551fb7 .outline-text-2} +::: {#text-org3c40e87 .outline-text-2} ::: -::: {#outline-container-org64784c3 .outline-3} -### Interrupt Handlers {#org64784c3} +::: {#outline-container-orge9f7498 .outline-3} +### Interrupt Handlers {#orge9f7498} -::: {#text-org64784c3 .outline-text-3} +::: {#text-orge9f7498 .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 @@ -4757,10 +4755,10 @@ you\'re both willing to share. ::: ::: -::: {#outline-container-org74afed1 .outline-3} -### Detecting button presses {#org74afed1} +::: {#outline-container-orgc89ace5 .outline-3} +### Detecting button presses {#orgc89ace5} -::: {#text-org74afed1 .outline-text-3} +::: {#text-orgc89ace5 .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 @@ -4931,10 +4929,10 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts"); ::: ::: -::: {#outline-container-orgb77ff34 .outline-3} -### Bottom Half {#orgb77ff34} +::: {#outline-container-orgfa48677 .outline-3} +### Bottom Half {#orgfa48677} -::: {#text-orgb77ff34 .outline-text-3} +::: {#text-orgfa48677 .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 @@ -5116,11 +5114,11 @@ MODULE_DESCRIPTION("Interrupt with top and bottom half"); ::: ::: -::: {#outline-container-orgd3754e6 .outline-2} -Crypto {#orgd3754e6} +::: {#outline-container-orgc1f3763 .outline-2} +Crypto {#orgc1f3763} ------ -::: {#text-orgd3754e6 .outline-text-2} +::: {#text-orgc1f3763 .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 @@ -5130,10 +5128,10 @@ enabling common methods of encryption, decryption and your favourite hash functions. ::: -::: {#outline-container-orge22ae95 .outline-3} -### Hash functions {#orge22ae95} +::: {#outline-container-org2808d46 .outline-3} +### Hash functions {#org2808d46} -::: {#text-orge22ae95 .outline-text-3} +::: {#text-org2808d46 .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. @@ -5229,10 +5227,10 @@ sudo rmmod cryptosha256 ::: ::: -::: {#outline-container-orgc4f4be1 .outline-3} -### Symmetric key encryption {#orgc4f4be1} +::: {#outline-container-org9ceede6 .outline-3} +### Symmetric key encryption {#org9ceede6} -::: {#text-orgc4f4be1 .outline-text-3} +::: {#text-org9ceede6 .outline-text-3} Here is an example of symmetrically encrypting a string using the AES algorithm and a password. @@ -5424,11 +5422,11 @@ MODULE_LICENSE("GPL"); ::: ::: -::: {#outline-container-orgb192fca .outline-2} -Standardising the interfaces: The Device Model {#orgb192fca} +::: {#outline-container-org0a78c70 .outline-2} +Standardising the interfaces: The Device Model {#org0a78c70} ---------------------------------------------- -::: {#text-orgb192fca .outline-text-2} +::: {#text-org0a78c70 .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 @@ -5539,17 +5537,17 @@ module_exit(devicemodel_exit); ::: ::: -::: {#outline-container-orgd494e36 .outline-2} -Optimisations {#orgd494e36} +::: {#outline-container-org24da896 .outline-2} +Optimisations {#org24da896} ------------- -::: {#text-orgd494e36 .outline-text-2} +::: {#text-org24da896 .outline-text-2} ::: -::: {#outline-container-orgfee6da3 .outline-3} -### Likely and Unlikely conditions {#orgfee6da3} +::: {#outline-container-org45afbf3 .outline-3} +### Likely and Unlikely conditions {#org45afbf3} -::: {#text-orgfee6da3 .outline-text-3} +::: {#text-org45afbf3 .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 @@ -5579,51 +5577,51 @@ pipeline. The opposite happens if you use the *likely* macro. ::: ::: -::: {#outline-container-org57e70e3 .outline-2} -Common Pitfalls {#org57e70e3} +::: {#outline-container-org972b87c .outline-2} +Common Pitfalls {#org972b87c} --------------- -::: {#text-org57e70e3 .outline-text-2} +::: {#text-org972b87c .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-orgb09decf .outline-3} -### Using standard libraries {#orgb09decf} +::: {#outline-container-orgc7bc4d9 .outline-3} +### Using standard libraries {#orgc7bc4d9} -::: {#text-orgb09decf .outline-text-3} +::: {#text-orgc7bc4d9 .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-org0cdb999 .outline-3} -### Disabling interrupts {#org0cdb999} +::: {#outline-container-org8e4ee93 .outline-3} +### Disabling interrupts {#org8e4ee93} -::: {#text-org0cdb999 .outline-text-3} +::: {#text-org8e4ee93 .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-org8882024 .outline-3} -### Sticking your head inside a large carnivore {#org8882024} +::: {#outline-container-orgb182bfa .outline-3} +### Sticking your head inside a large carnivore {#orgb182bfa} -::: {#text-org8882024 .outline-text-3} +::: {#text-orgb182bfa .outline-text-3} I probably don\'t have to warn you about this, but I figured I will anyway, just in case. ::: ::: ::: -::: {#outline-container-org195148f .outline-2} -Where To Go From Here? {#org195148f} +::: {#outline-container-org4bdbcf7 .outline-2} +Where To Go From Here? {#org4bdbcf7} ---------------------- -::: {#text-org195148f .outline-text-2} +::: {#text-org4bdbcf7 .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.org b/4.15.2/LKMPG-4.15.2.org index 694e56c..bbe0a45 100644 --- a/4.15.2/LKMPG-4.15.2.org +++ b/4.15.2/LKMPG-4.15.2.org @@ -3168,9 +3168,7 @@ static int __init kbleds_init(void) /* * Set up the LED blink timer the first time */ - init_timer(&my_timer); - my_timer.function = my_timer_func; - my_timer.data = (unsigned long)&kbledstatus; + timer_setup(&my_timer, (void*)&my_timer_func, (unsigned long)&kbledstatus); my_timer.expires = jiffies + BLINK_DELAY; add_timer(&my_timer); diff --git a/4.15.2/LKMPG-4.15.2.rst b/4.15.2/LKMPG-4.15.2.rst index afd9dc1..fd394f9 100644 --- a/4.15.2/LKMPG-4.15.2.rst +++ b/4.15.2/LKMPG-4.15.2.rst @@ -21,101 +21,101 @@
-- `Introduction <#orgf3497c5>`__ +- `Introduction <#org0c1f452>`__ - - `Authorship <#org34e7977>`__ - - `Versioning and Notes <#orgf6ef76f>`__ - - `Acknowledgements <#orgc967c58>`__ - - `What Is A Kernel Module? <#org0a581aa>`__ - - `Kernel module package <#org029cb2f>`__ - - `What Modules are in my Kernel? <#org5ef33ca>`__ - - `Do I need to download and compile the kernel? <#org5b17e9b>`__ - - `Before We Begin <#orgcfb9f7e>`__ + - `Authorship <#org795474a>`__ + - `Versioning and Notes <#org4087c64>`__ + - `Acknowledgements <#orgfee3c45>`__ + - `What Is A Kernel Module? <#orgaa146fc>`__ + - `Kernel module package <#orgb8dd50e>`__ + - `What Modules are in my Kernel? <#org2c8e961>`__ + - `Do I need to download and compile the kernel? <#org3a5069c>`__ + - `Before We Begin <#org65deef9>`__ -- `Headers <#orge2b5ae3>`__ -- `Examples <#org5a5f628>`__ -- `Hello World <#org9c9913f>`__ +- `Headers <#org8ba4da8>`__ +- `Examples <#org4259ce6>`__ +- `Hello World <#orgfa484db>`__ - - `The Simplest Module <#orgd6069e0>`__ - - `Hello and Goodbye <#org1217bec>`__ - - `The \__init and \__exit Macros <#org90d1270>`__ - - `Licensing and Module Documentation <#orgc726252>`__ - - `Passing Command Line Arguments to a Module <#orgf38f4b6>`__ - - `Modules Spanning Multiple Files <#org1d8bb9c>`__ - - `Building modules for a precompiled kernel <#org4210695>`__ + - `The Simplest Module <#org495cb53>`__ + - `Hello and Goodbye <#org7a50b41>`__ + - `The \__init and \__exit Macros <#orgd5c7fb1>`__ + - `Licensing and Module Documentation <#orgcc3ca30>`__ + - `Passing Command Line Arguments to a Module <#org4e71571>`__ + - `Modules Spanning Multiple Files <#org22bcf57>`__ + - `Building modules for a precompiled kernel <#orge74dfca>`__ -- `Preliminaries <#org9b6e076>`__ +- `Preliminaries <#org8752231>`__ - - `How modules begin and end <#orga9647ed>`__ - - `Functions available to modules <#org9bd06e9>`__ - - `User Space vs Kernel Space <#org7dabaae>`__ - - `Name Space <#org16acec4>`__ - - `Code space <#orge85acf5>`__ - - `Device Drivers <#orgb47a01a>`__ + - `How modules begin and end <#orge94c71e>`__ + - `Functions available to modules <#org1a9dc70>`__ + - `User Space vs Kernel Space <#orgeaf5d21>`__ + - `Name Space <#orgd3ea79f>`__ + - `Code space <#org1595baf>`__ + - `Device Drivers <#orgd0ff7c1>`__ -- `Character Device drivers <#org7882da3>`__ +- `Character Device drivers <#orgd16c37f>`__ - - `The file_operations Structure <#orgb24fcc0>`__ - - `The file structure <#org6e8ef01>`__ - - `Registering A Device <#org68e914d>`__ - - `Unregistering A Device <#org9dd4a7c>`__ - - `chardev.c <#orgfd4ef85>`__ - - `Writing Modules for Multiple Kernel Versions <#org2de85a6>`__ + - `The file_operations Structure <#org079be2f>`__ + - `The file structure <#org5161089>`__ + - `Registering A Device <#org39d8806>`__ + - `Unregistering A Device <#orgeadff50>`__ + - `chardev.c <#orgdd7b4b0>`__ + - `Writing Modules for Multiple Kernel Versions <#org46c702f>`__ -- `The /proc File System <#orgbd42731>`__ +- `The /proc File System <#orgfd453ce>`__ - - `Read and Write a /proc File <#orgac2ac98>`__ - - `Manage /proc file with standard filesystem <#orgb21d645>`__ - - `Manage /proc file with seq_file <#org3dd2bb9>`__ + - `Read and Write a /proc File <#org4178ce8>`__ + - `Manage /proc file with standard filesystem <#orgfaee38c>`__ + - `Manage /proc file with seq_file <#orgb5757af>`__ -- `sysfs: Interacting with your module <#org714b89f>`__ -- `Talking To Device Files <#orgb674dde>`__ -- `System Calls <#org6add61c>`__ -- `Blocking Processes and threads <#org3359a62>`__ +- `sysfs: Interacting with your module <#orge1358df>`__ +- `Talking To Device Files <#org9458047>`__ +- `System Calls <#org7676770>`__ +- `Blocking Processes and threads <#org26b5f2e>`__ - - `Sleep <#org65a16ba>`__ - - `Completions <#orgc4aa165>`__ + - `Sleep <#orge28c7d5>`__ + - `Completions <#org4574a81>`__ -- `Avoiding Collisions and Deadlocks <#org6a92beb>`__ +- `Avoiding Collisions and Deadlocks <#org9949395>`__ - - `Mutex <#org9e0e461>`__ - - `Spinlocks <#org66f7c66>`__ - - `Read and write locks <#orgedf3265>`__ - - `Atomic operations <#org532aa9f>`__ + - `Mutex <#orgaacad0a>`__ + - `Spinlocks <#orgba2b451>`__ + - `Read and write locks <#org78e399c>`__ + - `Atomic operations <#orgb70376b>`__ -- `Replacing Print Macros <#org2b469b3>`__ +- `Replacing Print Macros <#orgab764f9>`__ - - `Replacement <#org725ae77>`__ - - `Flashing keyboard LEDs <#org8a29832>`__ + - `Replacement <#orgf8bbb15>`__ + - `Flashing keyboard LEDs <#org331f665>`__ -- `Scheduling Tasks <#org7cabe47>`__ +- `Scheduling Tasks <#org3ef4dd6>`__ - - `Tasklets <#org0282886>`__ - - `Work queues <#orgf7f8600>`__ + - `Tasklets <#orgc6bcee8>`__ + - `Work queues <#orgd4fe9d0>`__ -- `Interrupt Handlers <#orgd551fb7>`__ +- `Interrupt Handlers <#org3c40e87>`__ - - `Interrupt Handlers <#org64784c3>`__ - - `Detecting button presses <#org74afed1>`__ - - `Bottom Half <#orgb77ff34>`__ + - `Interrupt Handlers <#orge9f7498>`__ + - `Detecting button presses <#orgc89ace5>`__ + - `Bottom Half <#orgfa48677>`__ -- `Crypto <#orgd3754e6>`__ +- `Crypto <#orgc1f3763>`__ - - `Hash functions <#orge22ae95>`__ - - `Symmetric key encryption <#orgc4f4be1>`__ + - `Hash functions <#org2808d46>`__ + - `Symmetric key encryption <#org9ceede6>`__ -- `Standardising the interfaces: The Device Model <#orgb192fca>`__ -- `Optimisations <#orgd494e36>`__ +- `Standardising the interfaces: The Device Model <#org0a78c70>`__ +- `Optimisations <#org24da896>`__ - - `Likely and Unlikely conditions <#orgfee6da3>`__ + - `Likely and Unlikely conditions <#org45afbf3>`__ -- `Common Pitfalls <#org57e70e3>`__ +- `Common Pitfalls <#org972b87c>`__ - - `Using standard libraries <#orgb09decf>`__ - - `Disabling interrupts <#org0cdb999>`__ - - `Sticking your head inside a large carnivore <#org8882024>`__ + - `Using standard libraries <#orgc7bc4d9>`__ + - `Disabling interrupts <#org8e4ee93>`__ + - `Sticking your head inside a large carnivore <#orgb182bfa>`__ -- `Where To Go From Here? <#org195148f>`__ +- `Where To Go From Here? <#org4bdbcf7>`__ .. raw:: html @@ -127,14 +127,14 @@ .. raw:: html -
+
.. rubric:: Introduction - :name: orgf3497c5 + :name: org0c1f452 .. 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: org34e7977 + :name: org795474a .. 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: orgf6ef76f + :name: org4087c64 .. 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: orgc967c58 + :name: orgfee3c45 .. 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: org0a581aa + :name: orgaa146fc .. 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: org029cb2f + :name: orgb8dd50e .. raw:: html -
+
Linux distros provide the commands *modprobe*, *insmod* and *depmod* within a package. @@ -339,14 +339,14 @@ On Parabola: .. raw:: html -
+
.. rubric:: What Modules are in my Kernel? - :name: org5ef33ca + :name: org2c8e961 .. raw:: html -
+
To discover what modules are already loaded within your current kernel use the command **lsmod**. @@ -403,14 +403,14 @@ particular. To search for the *fat* module: .. raw:: html -
+
.. rubric:: Do I need to download and compile the kernel? - :name: org5b17e9b + :name: org3a5069c .. 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: orgcfb9f7e + :name: org65deef9 .. 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-org0622852 .outline-text-5} A module compiled for one + | ::: {#text-orge72489f .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,7 +461,7 @@ thereafter. ::: - | Using X - | ::: {#text-org6298e45 .outline-text-5} It is highly recommended + | ::: {#text-org7fc2c85 .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: orge2b5ae3 + :name: org8ba4da8 .. raw:: html -
+
Before you can build anything you'll need to install the header files for your kernel. On Parabola GNU/Linux: @@ -550,14 +550,14 @@ example: .. raw:: html -
+
.. rubric:: Examples - :name: org5a5f628 + :name: org4259ce6 .. raw:: html -
+
All the examples from this document are available within the *examples* subdirectory. To test that they compile: @@ -588,14 +588,14 @@ version or need to install the corresponding kernel header files. .. raw:: html -
+
.. rubric:: Hello World - :name: org9c9913f + :name: orgfa484db .. raw:: html -
+
.. raw:: html @@ -603,14 +603,14 @@ version or need to install the corresponding kernel header files. .. raw:: html -
+
.. rubric:: The Simplest Module - :name: orgd6069e0 + :name: org495cb53 .. raw:: html -
+
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 @@ -818,7 +818,7 @@ pr_alert() log level, which you'll learn about in Section 2.1.1.
- | A point about coding style - | ::: {#text-org1239959 .outline-text-5} Another thing which may not + | ::: {#text-orge9f9010 .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 conventions of @@ -828,7 +828,7 @@ pr_alert() log level, which you'll learn about in Section 2.1.1. ::: - | Introducing print macros - | ::: {#text-orga621564 .outline-text-5} In the beginning there was + | ::: {#text-org6c9563e .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, such as **pr_info** and @@ -839,7 +839,7 @@ pr_alert() log level, which you'll learn about in Section 2.1.1. ::: - | About Compiling - | ::: {#text-org8021c2d .outline-text-5} Kernel modules need to be + | ::: {#text-orgaf1fbff .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. Although hierarchically @@ -870,14 +870,14 @@ pr_alert() log level, which you'll learn about in Section 2.1.1. .. raw:: html -
+
.. rubric:: Hello and Goodbye - :name: org1217bec + :name: org7a50b41 .. raw:: html -
+
In early kernel versions you had to use the **init_module** and **cleanup_module** functions, as in the first hello world example, but @@ -961,14 +961,14 @@ something like that. .. raw:: html -
+
.. rubric:: The \__init and \__exit Macros - :name: org90d1270 + :name: orgd5c7fb1 .. raw:: html -
+
This demonstrates a feature of kernel 2.2 and later. Notice the change in the definitions of the init and cleanup functions. The **\__init** @@ -1033,14 +1033,14 @@ kernel memory: 236k freed, this is precisely what the kernel is freeing. .. raw:: html -
+
.. rubric:: Licensing and Module Documentation - :name: orgc726252 + :name: orgcc3ca30 .. raw:: html -
+
Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this: @@ -1115,14 +1115,14 @@ are illustrated in the below example. .. raw:: html -
+
.. rubric:: Passing Command Line Arguments to a Module - :name: orgf38f4b6 + :name: org4e71571 .. raw:: html -
+
Modules can take command line arguments, but not with the argc/argv you might be used to. @@ -1315,14 +1315,14 @@ I would recommend playing around with this code: .. raw:: html -
+
.. rubric:: Modules Spanning Multiple Files - :name: org1d8bb9c + :name: org22bcf57 .. raw:: html -
+
Sometimes it makes sense to divide a kernel module between several source files. @@ -1417,14 +1417,14 @@ second we tell make what object files are part of that module. .. raw:: html -
+
.. rubric:: Building modules for a precompiled kernel - :name: org4210695 + :name: orge74dfca .. raw:: html -
+
Obviously, we strongly suggest you to recompile your kernel, so that you can enable a number of useful debugging features, such as forced module @@ -1599,14 +1599,14 @@ any errors. .. raw:: html -
+
.. rubric:: Preliminaries - :name: org9b6e076 + :name: org8752231 .. raw:: html -
+
.. raw:: html @@ -1614,14 +1614,14 @@ any errors. .. raw:: html -
+
.. rubric:: How modules begin and end - :name: orga9647ed + :name: orge94c71e .. raw:: html -
+
A program usually begins with a **main()** function, executes a bunch of instructions and terminates upon completion of those instructions. @@ -1654,14 +1654,14 @@ think you'll know what I mean. .. raw:: html -
+
.. rubric:: Functions available to modules - :name: org9bd06e9 + :name: org1a9dc70 .. raw:: html -
+
Programmers use functions they don't define all the time. A prime example of this is **printf()**. You use these library functions which @@ -1741,14 +1741,14 @@ everytime someone tries to delete a file on your system. .. raw:: html -
+
.. rubric:: User Space vs Kernel Space - :name: org7dabaae + :name: orgeaf5d21 .. raw:: html -
+
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. @@ -1780,14 +1780,14 @@ returns and execution gets transfered back to user mode. .. raw:: html -
+
.. rubric:: Name Space - :name: org16acec4 + :name: orgd3ea79f .. raw:: html -
+
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 @@ -1821,14 +1821,14 @@ share the kernel's codespace. .. raw:: html -
+
.. rubric:: Code space - :name: orge85acf5 + :name: org1595baf .. raw:: html -
+
Memory management is a very complicated subject and the majority of O'Reilly's "*Understanding The Linux Kernel*" exclusively covers memory @@ -1876,14 +1876,14 @@ Magenta kernel of Google Fuchsia are two examples of a microkernel. .. raw:: html -
+
.. rubric:: Device Drivers - :name: orgb47a01a + :name: orgd0ff7c1 .. raw:: html -
+
One class of module is the device driver, which provides functionality for hardware like a serial port. On unix, each piece of hardware is @@ -1899,7 +1899,7 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use
- | Major and Minor Numbers - | ::: {#text-orgfad3092 .outline-text-5} Let's look at some device + | ::: {#text-org60beeae .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: @@ -2023,14 +2023,14 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use .. raw:: html -
+
.. rubric:: Character Device drivers - :name: org7882da3 + :name: orgd16c37f .. raw:: html -
+
.. raw:: html @@ -2038,14 +2038,14 @@ Ensoniq IS1370 sound card. A userspace program like mp3blaster can use .. raw:: html -
+
.. rubric:: The file_operations Structure - :name: orgb24fcc0 + :name: org079be2f .. raw:: html -
+
The file_operations structure is defined in **/usr/include/linux/fs.h**, and holds pointers to functions defined by the driver that perform @@ -2167,14 +2167,14 @@ named fops. .. raw:: html -
+
.. rubric:: The file structure - :name: org6e8ef01 + :name: org5161089 .. raw:: html -
+
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 @@ -2202,14 +2202,14 @@ use structures contained in file which are created elsewhere. .. raw:: html -
+
.. rubric:: Registering A Device - :name: org68e914d + :name: org39d8806 .. raw:: html -
+
As discussed earlier, char devices are accessed through device files, usually located in /dev. This is by convention. When writing a driver, @@ -2273,14 +2273,14 @@ the **device_create** function after a successful registration and .. raw:: html -
+
.. rubric:: Unregistering A Device - :name: org9dd4a7c + :name: orgeadff50 .. raw:: html -
+
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 @@ -2322,14 +2322,14 @@ sooner or later during a module's development. .. raw:: html -
+
.. rubric:: chardev.c - :name: orgfd4ef85 + :name: orgdd7b4b0 .. raw:: html -
+
The next code sample creates a char driver named chardev. You can cat its device file. @@ -2556,14 +2556,14 @@ acknowledging that we received it. .. raw:: html -
+
.. rubric:: Writing Modules for Multiple Kernel Versions - :name: org2de85a6 + :name: org46c702f .. raw:: html -
+
The system calls, which are the major interface the kernel shows to the processes, generally stay the same across versions. A new system call @@ -2621,14 +2621,14 @@ archives if you're interested in the full story. .. raw:: html -
+
.. rubric:: The /proc File System - :name: orgbd42731 + :name: orgfd453ce .. raw:: html -
+
In Linux, there is an additional mechanism for the kernel and kernel modules to send information to processes — the **/proc** file system. @@ -2759,14 +2759,14 @@ it never returns zero, the read function is called endlessly. .. raw:: html -
+
.. rubric:: Read and Write a /proc File - :name: orgac2ac98 + :name: org4178ce8 .. raw:: html -
+
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. @@ -2915,14 +2915,14 @@ function because data is already in kernel space. .. raw:: html -
+
.. rubric:: Manage /proc file with standard filesystem - :name: orgb21d645 + :name: orgfaee38c .. raw:: html -
+
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 @@ -3073,14 +3073,14 @@ you want to document something kernel related yourself. .. raw:: html -
+
.. rubric:: Manage /proc file with seq_file - :name: org3dd2bb9 + :name: orgb5757af .. raw:: html -
+
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 @@ -3284,14 +3284,14 @@ You can also read the code of fs/seq_file.c in the linux kernel. .. raw:: html -
+
.. rubric:: sysfs: Interacting with your module - :name: org714b89f + :name: orge1358df .. raw:: html -
+
*sysfs* allows you to interact with the running kernel from userspace by reading or setting variables inside of modules. This can be useful for @@ -3474,14 +3474,14 @@ Finally, remove the test module: .. raw:: html -
+
.. rubric:: Talking To Device Files - :name: orgb674dde + :name: org9458047 .. raw:: html -
+
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 @@ -4030,14 +4030,14 @@ Documentation/ioctl-number.txt. .. raw:: html -
+
.. rubric:: System Calls - :name: org6add61c + :name: org7676770 .. raw:: html -
+
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 @@ -4326,14 +4326,14 @@ hacker, this would be the first thing he'd try. ;) .. raw:: html -
+
.. rubric:: Blocking Processes and threads - :name: org3359a62 + :name: org26b5f2e .. raw:: html -
+
.. raw:: html @@ -4341,14 +4341,14 @@ hacker, this would be the first thing he'd try. ;) .. raw:: html -
+
.. rubric:: Sleep - :name: org65a16ba + :name: orge28c7d5 .. raw:: html -
+
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 @@ -4825,14 +4825,14 @@ a file with **O_NONBLOCK**. .. raw:: html -
+
.. rubric:: Completions - :name: orgc4aa165 + :name: org4574a81 .. raw:: html -
+
Sometimes one thing should happen before another within a module having multiple threads. Rather than using **/proc/sleep** commands the kernel @@ -4957,14 +4957,14 @@ enough for many common situations without adding a lot of complexity. .. raw:: html -
+
.. rubric:: Avoiding Collisions and Deadlocks - :name: org6a92beb + :name: org9949395 .. raw:: html -
+
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 @@ -4979,14 +4979,14 @@ can't happen. .. raw:: html -
+
.. rubric:: Mutex - :name: org9e0e461 + :name: orgaacad0a .. raw:: html -
+
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 @@ -5053,14 +5053,14 @@ avoid collisions in most cases. .. raw:: html -
+
.. rubric:: Spinlocks - :name: org66f7c66 + :name: orgba2b451 .. raw:: html -
+
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 @@ -5156,14 +5156,14 @@ happens, using the *flags* variable to retain their state. .. raw:: html -
+
.. rubric:: Read and write locks - :name: orgedf3265 + :name: org78e399c .. raw:: html -
+
Read and write locks are specialised kinds of spinlocks so that you can exclusively read from something or write to something. Like the earlier @@ -5253,14 +5253,14 @@ corresponding write functions. .. raw:: html -
+
.. rubric:: Atomic operations - :name: org532aa9f + :name: orgb70376b .. raw:: html -
+
If you're doing simple arithmetic: adding, subtracting or bitwise operations then there's another way in the multi-CPU and @@ -5370,14 +5370,14 @@ overwritten by some other shenanigans. An example is shown below. .. raw:: html -
+
.. rubric:: Replacing Print Macros - :name: org2b469b3 + :name: orgab764f9 .. raw:: html -
+
.. raw:: html @@ -5385,14 +5385,14 @@ overwritten by some other shenanigans. An example is shown below. .. raw:: html -
+
.. rubric:: Replacement - :name: org725ae77 + :name: orgf8bbb15 .. raw:: html -
+
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 @@ -5539,14 +5539,14 @@ which we use to write a string to the tty. .. raw:: html -
+
.. rubric:: Flashing keyboard LEDs - :name: org8a29832 + :name: org331f665 .. raw:: html -
+
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 @@ -5640,9 +5640,7 @@ when loaded, starts blinking the keyboard LEDs until it is unloaded. /* * Set up the LED blink timer the first time */ - init_timer(&my_timer); - my_timer.function = my_timer_func; - my_timer.data = (unsigned long)&kbledstatus; + timer_setup(&my_timer, (void*)&my_timer_func, (unsigned long)&kbledstatus); my_timer.expires = jiffies + BLINK_DELAY; add_timer(&my_timer); @@ -5695,14 +5693,14 @@ minimum and make sure it does not show up in production code. .. raw:: html -
+
.. rubric:: Scheduling Tasks - :name: org7cabe47 + :name: org3ef4dd6 .. raw:: html -
+
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 @@ -5716,14 +5714,14 @@ in a sequence. .. raw:: html -
+
.. rubric:: Tasklets - :name: org0282886 + :name: orgc6bcee8 .. raw:: html -
+
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* @@ -5802,14 +5800,14 @@ So with this example loaded *dmesg* should show: .. raw:: html -
+
.. rubric:: Work queues - :name: orgf7f8600 + :name: orgd4fe9d0 .. raw:: html -
+
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 @@ -5870,14 +5868,14 @@ queue. .. raw:: html -
+
.. rubric:: Interrupt Handlers - :name: orgd551fb7 + :name: org3c40e87 .. raw:: html -
+
.. raw:: html @@ -5885,14 +5883,14 @@ queue. .. raw:: html -
+
.. rubric:: Interrupt Handlers - :name: org64784c3 + :name: orge9f7498 .. raw:: html -
+
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 @@ -5968,14 +5966,14 @@ both willing to share. .. raw:: html -
+
.. rubric:: Detecting button presses - :name: org74afed1 + :name: orgc89ace5 .. raw:: html -
+
Many popular single board computers, such as Raspberry Pis or Beagleboards, have a bunch of GPIO pins. Attaching buttons to those and @@ -6161,14 +6159,14 @@ whatever is appropriate for your board. .. raw:: html -
+
.. rubric:: Bottom Half - :name: orgb77ff34 + :name: orgfa48677 .. raw:: html -
+
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 @@ -6368,14 +6366,14 @@ additional task when an interrupt is triggered. .. raw:: html -
+
.. rubric:: Crypto - :name: orgd3754e6 + :name: orgc1f3763 .. raw:: html -
+
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 @@ -6391,14 +6389,14 @@ hash functions. .. raw:: html -
+
.. rubric:: Hash functions - :name: orge22ae95 + :name: org2808d46 .. raw:: html -
+
Calculating and checking the hashes of things is a common operation. Here is a demonstration of how to calculate a sha256 hash within a @@ -6521,14 +6519,14 @@ Finally, remove the test module: .. raw:: html -
+
.. rubric:: Symmetric key encryption - :name: orgc4f4be1 + :name: org9ceede6 .. raw:: html -
+
Here is an example of symmetrically encrypting a string using the AES algorithm and a password. @@ -6738,14 +6736,14 @@ algorithm and a password. .. raw:: html -
+
.. rubric:: Standardising the interfaces: The Device Model - :name: orgb192fca + :name: org0a78c70 .. raw:: html -
+
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 @@ -6871,14 +6869,14 @@ to add your own suspend, resume or other interface functions. .. raw:: html -
+
.. rubric:: Optimisations - :name: orgd494e36 + :name: org24da896 .. raw:: html -
+
.. raw:: html @@ -6886,14 +6884,14 @@ to add your own suspend, resume or other interface functions. .. raw:: html -
+
.. rubric:: Likely and Unlikely conditions - :name: orgfee6da3 + :name: org45afbf3 .. raw:: html -
+
Sometimes you might want your code to run as quickly as possible, especially if it's handling an interrupt or doing something which might @@ -6941,14 +6939,14 @@ pipeline. The opposite happens if you use the *likely* macro. .. raw:: html -
+
.. rubric:: Common Pitfalls - :name: org57e70e3 + :name: org972b87c .. raw:: html -
+
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 @@ -6961,14 +6959,14 @@ a full refund of the amount I was paid for your copy of the book. .. raw:: html -
+
.. rubric:: Using standard libraries - :name: orgb09decf + :name: orgc7bc4d9 .. raw:: html -
+
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. @@ -6983,14 +6981,14 @@ which are the functions you can see in /proc/kallsyms. .. raw:: html -
+
.. rubric:: Disabling interrupts - :name: org0cdb999 + :name: org8e4ee93 .. raw:: html -
+
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 @@ -7006,14 +7004,14 @@ to power it off. .. raw:: html -
+
.. rubric:: Sticking your head inside a large carnivore - :name: org8882024 + :name: orgb182bfa .. raw:: html -
+
I probably don't have to warn you about this, but I figured I will anyway, just in case. @@ -7032,14 +7030,14 @@ anyway, just in case. .. raw:: html -
+
.. rubric:: Where To Go From Here? - :name: org195148f + :name: org4bdbcf7 .. raw:: html -
+
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 diff --git a/4.15.2/LKMPG-4.15.2.tex b/4.15.2/LKMPG-4.15.2.tex index be836c4..987d83a 100644 --- a/4.15.2/LKMPG-4.15.2.tex +++ b/4.15.2/LKMPG-4.15.2.tex @@ -1,4 +1,4 @@ -% Created 2018-01-03 Wed 17:35 +% Created 2018-02-15 Thu 12:39 % Intended LaTeX compiler: pdflatex \documentclass[11pt]{article} \usepackage[utf8]{inputenc} @@ -31,7 +31,7 @@ \section*{Introduction} -\label{sec:org58923ad} +\label{sec:org26872cf} 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 +43,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:org3c9fa66} +\label{sec:orgad37b39} 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:org6be956a} +\label{sec:org4af1d92} 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:org079f260} +\label{sec:org30c8b80} 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:orgf00e867} +\label{sec:orgea6332c} 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:orgc575527} +\label{sec:orgad4164b} Linux distros provide the commands \emph{modprobe}, \emph{insmod} and \emph{depmod} within a package. @@ -84,7 +84,7 @@ sudo pacman -S gcc kmod \end{verbatim} \subsection*{What Modules are in my Kernel?} -\label{sec:orgf1b45a5} +\label{sec:org921f89b} To discover what modules are already loaded within your current kernel use the command \textbf{lsmod}. @@ -105,20 +105,20 @@ sudo lsmod | grep fat \end{verbatim} \subsection*{Do I need to download and compile the kernel?} -\label{sec:org6fbe188} +\label{sec:org480c85e} 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:org8fd7aff} +\label{sec:org718d12c} 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:orge3b3039} +\label{sec:org486f233} 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:orgedf122e} +\label{sec:org4f3c4b2} 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 +126,7 @@ Modules can't print to the screen like printf() can, but they can log informatio \end{itemize} \section*{Headers} -\label{sec:orge6f432c} +\label{sec:org7aa387e} Before you can build anything you'll need to install the header files for your kernel. On Parabola GNU/Linux: \begin{verbatim} @@ -146,7 +146,7 @@ This will tell you what kernel header files are available. Then for example: sudo apt-get install kmod linux-headers-4.14.8-1-amd64 \end{verbatim} \section*{Examples} -\label{sec:org982837d} +\label{sec:orga0c3a1b} All the examples from this document are available within the \emph{examples} subdirectory. To test that they compile: \begin{verbatim} @@ -156,9 +156,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:org2f356ff} +\label{sec:org0f0d6ef} \subsection*{The Simplest Module} -\label{sec:orgc496296} +\label{sec:org2f87e7a} 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 +260,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:orgba54241} +\label{sec:orgb8f8deb} 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:org9331e15} +\label{sec:orga724f9c} 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:orgf4767b0} +\label{sec:org801f361} 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 +280,7 @@ Here's another exercise for the reader. See that comment above the return statem \end{itemize} \subsection*{Hello and Goodbye} -\label{sec:org530a207} +\label{sec:org9e51fc7} 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 +323,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:orgbf7bc3b} +\label{sec:orgfbdb444} 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 +358,7 @@ module_exit(hello_3_exit); \end{verbatim} \subsection*{Licensing and Module Documentation} -\label{sec:org37febfd} +\label{sec:org9b38471} Honestly, who loads or even cares about proprietary modules? If you do then you might have seen something like this: \begin{verbatim} @@ -401,7 +401,7 @@ module_exit(cleanup_hello_4); \end{verbatim} \subsection*{Passing Command Line Arguments to a Module} -\label{sec:org7533927} +\label{sec:orgbc34b7e} 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. @@ -532,7 +532,7 @@ hello-5.o: invalid argument syntax for mylong: 'h' \end{verbatim} \subsection*{Modules Spanning Multiple Files} -\label{sec:org6595d9c} +\label{sec:org2703947} Sometimes it makes sense to divide a kernel module between several source files. Here's an example of such a kernel module. @@ -589,7 +589,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:org6554f4c} +\label{sec:org6eb0191} 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 +652,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:orgb3efe20} +\label{sec:orgcc87f20} \subsection*{How modules begin and end} -\label{sec:org481504d} +\label{sec:org966b512} 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 +662,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:orgdda2552} +\label{sec:org9f375cd} 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 +686,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:org678d1dd} +\label{sec:org8fc856c} 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:org1f8b4d9} +\label{sec:org91871f3} 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,7 +700,7 @@ 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:orgbf83ca0} +\label{sec:orged414c8} 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. @@ -710,12 +710,12 @@ 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 \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. \subsection*{Device Drivers} -\label{sec:org75344d8} +\label{sec:org3cdf394} 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:org4b5efb6} +\label{sec:org3365022} Let's look at some device files. Here are device files which represent the first three partitions on the primary master IDE hard drive: @@ -757,9 +757,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:orge10b926} +\label{sec:org91c5d74} \subsection*{The file\_operations Structure} -\label{sec:orgcd67554} +\label{sec:org4211876} 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 +826,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:org3edaadf} +\label{sec:org7de2fa4} 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 +835,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:orge720278} +\label{sec:org0365401} 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 +851,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:orgc69529a} +\label{sec:orgfbb8f69} 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 +864,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:org074e4bf} +\label{sec:org9c8d49d} The next code sample creates a char driver named chardev. You can cat its device file. \begin{verbatim} @@ -1058,7 +1058,7 @@ static ssize_t device_write(struct file *filp, \end{verbatim} \subsection*{Writing Modules for Multiple Kernel Versions} -\label{sec:org7c5b605} +\label{sec:orgf6305b6} 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). @@ -1070,7 +1070,7 @@ While previous versions of this guide showed how you can write backward compatib 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:org60ea81e} +\label{sec:org4682c24} 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 +1144,7 @@ void cleanup_module() \end{verbatim} \subsection*{Read and Write a /proc File} -\label{sec:org90393b7} +\label{sec:org0858b50} 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 +1253,7 @@ void cleanup_module() \end{verbatim} \subsection*{Manage /proc file with standard filesystem} -\label{sec:orgab5df9a} +\label{sec:orga85c958} 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 +1355,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:orgf89c83b} +\label{sec:org3a43216} 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. @@ -1525,7 +1525,7 @@ If you want more information, you can read this web page: You can also read the code of fs/seq\_file.c in the linux kernel. \section*{sysfs: Interacting with your module} -\label{sec:orgca5c8d9} +\label{sec:org2aa3282} \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} @@ -1637,7 +1637,7 @@ sudo rmmod hello_sysfs \end{verbatim} \section*{Talking To Device Files} -\label{sec:org80eb1cf} +\label{sec:org599da10} 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 +2117,7 @@ int main() \end{verbatim} \section*{System Calls} -\label{sec:org68479ec} +\label{sec:org63b6332} 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 +2299,9 @@ MODULE_LICENSE("GPL"); \end{verbatim} \section*{Blocking Processes and threads} -\label{sec:orga99b92b} +\label{sec:orgd818037} \subsection*{Sleep} -\label{sec:org5428e82} +\label{sec:orgc038411} 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 +2686,7 @@ int main(int argc, char *argv[]) \end{verbatim} \subsection*{Completions} -\label{sec:org9abe94f} +\label{sec:org96a9d53} 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 +2778,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:org694d9ba} +\label{sec:orgf651ed7} 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:org2f84012} +\label{sec:org311a9ac} 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,7 +2827,7 @@ MODULE_DESCRIPTION("Mutex example"); MODULE_LICENSE("GPL"); \end{verbatim} \subsection*{Spinlocks} -\label{sec:orgdaf7eec} +\label{sec:orgf6e3dfb} 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. @@ -2899,7 +2899,7 @@ MODULE_LICENSE("GPL"); \end{verbatim} \subsection*{Read and write locks} -\label{sec:orgcafdffd} +\label{sec:org97efbd9} 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 +2960,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:org53e26b1} +\label{sec:org7539626} 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 +3042,9 @@ MODULE_DESCRIPTION("Atomic operations example"); MODULE_LICENSE("GPL"); \end{verbatim} \section*{Replacing Print Macros} -\label{sec:org85ac312} +\label{sec:orge929d19} \subsection*{Replacement} -\label{sec:orgec0d9a2} +\label{sec:org346ed60} 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 +3163,7 @@ module_exit(print_string_exit); \end{verbatim} \subsection*{Flashing keyboard LEDs} -\label{sec:orgdf21c40} +\label{sec:org3b076fb} 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. @@ -3245,9 +3245,7 @@ static int __init kbleds_init(void) /* * Set up the LED blink timer the first time */ - init_timer(&my_timer); - my_timer.function = my_timer_func; - my_timer.data = (unsigned long)&kbledstatus; + timer_setup(&my_timer, (void*)&my_timer_func, (unsigned long)&kbledstatus); my_timer.expires = jiffies + BLINK_DELAY; add_timer(&my_timer); @@ -3271,11 +3269,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:orgcfa5bbc} +\label{sec:org2e1e9ce} 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:orgc307748} +\label{sec:orga14aa91} 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} @@ -3325,7 +3323,7 @@ Example tasklet init continues... Example tasklet ends \end{verbatim} \subsection*{Work queues} -\label{sec:orgb0d2993} +\label{sec:org84172b2} 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} @@ -3362,9 +3360,9 @@ MODULE_DESCRIPTION("Workqueue example"); \end{verbatim} \section*{Interrupt Handlers} -\label{sec:orge7bc8ab} +\label{sec:org4ba4855} \subsection*{Interrupt Handlers} -\label{sec:org1851ae5} +\label{sec:org7554e19} 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. @@ -3380,7 +3378,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:orga6b0ec1} +\label{sec:org59f694f} 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. @@ -3540,7 +3538,7 @@ MODULE_DESCRIPTION("Handle some GPIO interrupts"); \end{verbatim} \subsection*{Bottom Half} -\label{sec:orgef0413c} +\label{sec:orge59e62b} 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. @@ -3713,11 +3711,11 @@ MODULE_AUTHOR("Bob Mottram"); MODULE_DESCRIPTION("Interrupt with top and bottom half"); \end{verbatim} \section*{Crypto} -\label{sec:org3cefa39} +\label{sec:org9233538} 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:orgd02b76a} +\label{sec:orgb86daae} 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. @@ -3804,7 +3802,7 @@ Finally, remove the test module: sudo rmmod cryptosha256 \end{verbatim} \subsection*{Symmetric key encryption} -\label{sec:org49fd0a0} +\label{sec:orgb6851ac} Here is an example of symmetrically encrypting a string using the AES algorithm and a password. \begin{verbatim} @@ -3990,7 +3988,7 @@ MODULE_DESCRIPTION("Symmetric key encryption example"); MODULE_LICENSE("GPL"); \end{verbatim} \section*{Standardising the interfaces: The Device Model} -\label{sec:org2c0e1c9} +\label{sec:org1a91102} 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} @@ -4092,9 +4090,9 @@ module_init(devicemodel_init); module_exit(devicemodel_exit); \end{verbatim} \section*{Optimisations} -\label{sec:orgfdaa465} +\label{sec:org81f28d1} \subsection*{Likely and Unlikely conditions} -\label{sec:org5b6194a} +\label{sec:orga22c3dc} 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. @@ -4110,23 +4108,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:orgb453ee2} +\label{sec:orgf2a7c7d} 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:org468618b} +\label{sec:org3bf678a} 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:org2ed2867} +\label{sec:org679059a} 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:org0aecc68} +\label{sec:orgd3f9988} 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:orgaf2b21d} +\label{sec:orgd3a38ee} 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.