The simplest kernel module
#include <linux/init.h>#include <linux/module.h>Static int__init Hello_init (void)//kernel initialization function{PRINTK ("Hello world!\n");//Print function, similar to Prinft return 0; }Static void__exit Hello_exit (void)//kernel cleanup function{PRINTK ("Good bye!\n"); } module_init (Hello_init);//Specify initialization functionModule_exit (Hello_exit);//Specify clear functionModule_license ("GPL");//Specify the license used by the codeModule_author ("Laowu");//Designated authorModule_version ("1.0");//Specify code revision number
Makefile of kernel modules
obj-m += hello.o KDIR:=~/linux-2.6make -C$(KDIRM=`pwd` modules
- Obj-m + = hello.o shows that there is a kernel module that needs to be constructed from hello.o, and the generated module name is Hello.ko.
If Hello.ko is generated by two source files, such as file1.c file2.c, it should be written as follows
Obj-m: = Hello.ko
HELLO-OBJS: = FILE1.O file2.0
- Make-c $ (kdir) m=
pwd
modules
First change the directory to the location specified by-C (that is, the kernel source code directory), which holds the top-level makefile file of the kernel, the m= option, let the makefile return to the module source code directory before constructing the modules target
Insertion of the kernel
Insmod Hello.ko
Kernel parameters
How to use the Module_param function:
Module_param (name, type, perm)
Name: Names of kernel parameters, defined by themselves;
Type: Types of kernel parameters, common types Byte, short, int, long, ULONG, BOOL, charp (character pointer);
Perm: The kernel parameter has permission S_irugo (Read access to module parameters). In fact, permissions and file permissions are almost
Methods for passing parameters when the kernel loads the module:
Use command: Insmod Xxx.ko a=1
After the module is loaded, and his permissions are not 0, the corresponding module parameters can be found in the/sys/module/xxx/parameter directory.
Export symbol table
Use Export_symbol to export functions for use by other modules, and the symbol table in the "/proc/kallsyms" file, which records the symbols of functions and the addresses of functions in memory.
Concepts related to Linux device driver (1) modules