Compiling and Loading
The rest of this chapter is devoted to writing a complete, though typeless, module. That is, the module will not belong to any of the classes listed in Section 1.3 in Chapter 1. The sample driver shown in this chapter is called skull, short for Simple Kernel Utility for Loading Localities. You can reuse the skull source to load your own local code to the kernel, after removing the sample functionality it offers.[8]
Before we deal with the roles of init_module and cleanup_module, however, we’ll write a makefile that builds object code that the kernel can load.
First, we need to define the
__KERNEL__ symbol in the
preprocessor before we include any headers. As mentioned earlier, much
of the kernel-specific content in the kernel headers is unavailable
without this symbol.
Another important symbol is MODULE, which must be
defined before including <linux/module.h>
(except for drivers that are linked directly into the kernel). This
book does not cover directly linked modules; thus, the
MODULE symbol is always defined in our examples.
If you are compiling for an SMP machine, you also need to define
__SMP__ before including the kernel
headers. In version 2.2, the “multiprocessor or uniprocessor” choice
was promoted to a proper configuration item, so using these lines as
the very first lines of your modules will do the task:
#include <linux/config.h> #ifdef CONFIG_SMP # define __SMP__ #endif
A module writer must also specify the -O flag to the compiler, ...