One, the concept of kernel modules
Often seen in the kernel driver code like Fs_init () such as driver initialization function, then this and module_init () function difference where the macro definition __define_initcall (LEVEL,FN) for the initialization of the kernel is very important, He instructs the compiler to place the starting address value of a series of initialization functions in a section in a certain order at compile time. In the kernel initialization segment, Do_initcalls () takes the starting address of these functions in order from the section as a function pointer to complete the corresponding initialization in turn. Initialization of some parts of the kernel relies on the completion of initialization of some other part, so this sequence is often important
Click to open link
where Module_init () corresponds to the following Device_initcall (FN)
[CPP]View Plaincopy
- 189 #define Core_initcall (FN) __define_initcall ("1", fn,1)
- #define CORE_INITCALL_SYNC (FN) __define_initcall ("1s", fn,1s)
- 191 #define Postcore_initcall (FN) __define_initcall ("2", fn,2)
- 192 #define POSTCORE_INITCALL_SYNC (FN) __define_initcall ("2s", fn,2s)
- 193 #define Arch_initcall (FN) __define_initcall ("3", fn,3)
- 194 #define ARCH_INITCALL_SYNC (FN) __define_initcall ("3s", fn,3s)
- 195 #define Subsys_initcall (FN) __define_initcall ("4", fn,4)
- 196 #define SUBSYS_INITCALL_SYNC (FN) __define_initcall ("4s", fn,4s)
- 197 #define Fs_initcall (FN) __define_initcall ("5", fn,5)
- 198 #define FS_INITCALL_SYNC (FN) __define_initcall ("5s", fn,5s)
- 199 #define Rootfs_initcall (FN) __define_initcall ("Rootfs", Fn,rootfs)
- #define Device_initcall (FN) __define_initcall ("6", fn,6)
- 201 #define DEVICE_INITCALL_SYNC (FN) __define_initcall ("6s", fn,6s)
- 202 #define Late_initcall (FN) __define_initcall ("7", fn,7)
- 203 #define LATE_INITCALL_SYNC (FN) __define_initcall ("7s", fn,7s)
- __define_initcall #define (level,fn,id) \
- 171 Static initcall_t __initcall_# #fn # #id __used \
- 172 __attribute__ ((__section__ (". Initcall" Level ". Init"))) = fn
Second, the difference between kernel modules and applications
1, applications can use some library functions, while kernel modules can only use some of the functions exported by other kernel modules
2, the way to handle errors is different.
3, the application performs a single task from beginning to end, and the module registers itself in advance to serve a future request.
Third, build the kernel module
Building the Hello.ko module obj-m: = hello.o
Build the Module.ko module and generate it from two source files (file1.c and file2.c)
[CPP]View Plaincopy
- Obj-m: = MODULE.O
- MODULE-OBJS: = FILE1.O file2.o
Four, module operation-related commands
Insmod: Loading a module
Modprobe: Load a module, while checking whether the module refers to some of the current kernel does not exist, if there is, Modprobe will look in the current module search path to define the other modules that meet and load
Rmmod, uninstall a module.
Five, references between modules
Export a function or variable of a module so that other modules can use the
[CPP]View Plaincopy
- Export_symbol (name);
- EXPORT_SYMBOL_GPL (name); //exported modules can only be used by modules under the GPL license
VI, module parameters
Insmod Hello howmany=10 whom= "Mom"
In the driver for hello.c
[CPP]View Plaincopy
- Static char*whom="World"
- static int howmany=1;
- Module_param (Howmany,Int,s_irugo);
- Module_param (Whom,charp,s_irugo)
[Linux Drive]linux driver module