LINUX Study Notes 21-kernel module

Source: Internet
Author: User

(1) Development of LINUX "kernel module:
 
1. How to use required components
 
A) compile all components into the kernel file:
 
I. Defect 1: The Kernel File is too large
 
Ii. defect 2: to add or delete a component, recompile the entire kernel.
 
B) use the "kernel module" mechanism:
 
I. The module itself is not compiled into the Kernel File
 
Ii. dynamically install or uninstall the kernel during running as needed.
 
2. program structure: for example, vi hello. c
 
A) # include <linux/init. h>
 
B) # include <linux/module. h> header file
 
C) static int hello_init (void)
 
D ){
 
E) printk (KERN_WARNING "Hello, world! \ N "); print the Function
 
F) return 0;
 
G )}
 
H) static void hello_exit (void)
 
I ){
 
J) printk (KERN_INFO "Goodbye, world \ n ");
 
K )}
 
L) module_init (hello_init); Module Loading Function (required)
 
M) module_exit (hello_exit); Module unload function (required)
 
3. Module Compilation: the basic format of Makefile is similar. GCC cannot be used here.
 
A) Write Makefile for a single file: note that M is capitalized.
 
Ifneq ($ (KERNELRELEASE),) if the variable is null for 1st times, run else;
 
 
 
Obj-m: = hello. o target name, which needs to be modified as needed
 
 
 
Else
 
 
 
KDIR: =/lib/modules/2.6.18-53. el5/build kernel source code path: Modify as needed
 
All:
 
Make-C $ (KDIR) M = $ (PWD) modules-C enter the directory, use makefile in the directory to compile the module code in the current directory, enter the code again, and execute if true
 
Clean:
 
Rm-f *. ko *. o *. mod. o *. mod. c *. symvers
 
Endif
 
I. Error: missing separator lacks a separator: Pay attention to the delimiter. The format is correct.
 
Ii. Error: The Makefile file is not found. Check whether M is in uppercase and whether the file name is correct.
 
B) Write Makefile for multiple files: You need to change the location
 
I. obj-m: = hello. o
 
Ii. hello-objs: = main. o add. o
 
4. Installation and uninstallation
 
A) Load insmod: insmod hello. ko.
 
B) uninstall rmmod: rmmod hello
 
C) view lsmod
 
D) Load modprobe (modprobe hello): modprobe is like insmod. It also loads a module to the kernel. The difference is that it depends on the file/lib/modules/<$ version>/modules. dep to check the module to be loaded and whether it depends on other modules. If yes, modprobe will first find these modules and load them to the kernel.
 
5. Note: The application executes tasks from the beginning (main) to the end, and disappears from the memory after execution. The kernel module registers itself in the kernel to serve a future request, and then its initialization function ends. At this time, the module still exists in the kernel until the unmount function is called, module disappears from the kernel.
 
6. Optional module information:
 
A) license statement: the macro MODULE_LICENSE is used to inform the kernel that the module has a license, which is commonly used as "GPL": MODULE_LICENSE ("GPL ");
 
B) Author's statement (optional): MODULE_AUTHOR ("Simon Li ");
 
C) Module description (optional): MODULE_DESCRIPTION ("Hello World Module ");
 
D) module version (optional): MODULE_VERSION ("V1.0 ");
 
E) module alias (optional): MODULE_ALIAS ("a simple module ");
 
F) module parameters: module parameters are used to pass parameters to the module during module loading.
 
I. module_param (name, type, perm): module_param (a, int, S_IRUGO );
 
Ii. name is the name of the module parameter,
 
Iii. type is the type of this parameter: bool: Boolean int: integer charp: string type
 
Iv. perm is the access permission for module parameters.
 
1) S_IRUGO: any user has read permission on the parameter displayed in/sys/module.
 
2) S_IWUSR: allows the root user to modify this parameter in/sys/module.
 
V. Example: static int age = 10;
 
Vi. module_param (age, int, S_IRUGO );
 
7. kernel symbol export:
 
A) Role: when multiple modules are dependent, You need to export the symbols used in the dependent modules so that other modules can use them.
 
B)/proc/kallsyms records the names and addresses of all exported symbols in the kernel.
 
C) Method: add the EXPORT_SYMBOL (symbol name) at the end of the file)
 
D) EXPORT_SYMBOL_GPL (symbol name): EXPORT_SYMBOL_GPL can only be used for modules that contain the GPL license.
 
8. FAQs: Version mismatch
 
A) Use modprobe -- force-modversion to forcibly insert
 
B) Make sure that the version of the kernel code on which the kernel module is compiled is the same as that of the currently running kernel.
 
C) * You can view the current kernel version through uname-r.
 
9. Comparison between Printk and Printf
 
A) Printk is used in the kernel, and Printf is used in applications.
 
B) Printk allows message classification by attaching different "Priority" based on the severity. The descending order of priority is:
 
I. Role: control under what circumstances to print. The smaller the number, the higher the level.
 
Ii. KERN_EMERG "<0>" is used for emergency messages, often pre-crash messages.
 
Iii. message that KERN_ALERT "<1>" requires immediate action.
 
Iv. KERN_CRIT "<2>" severe condition.
 
V. KERN_ERR "<3>" error.
 
Vi. KERN_WARNING "<4>" error warning
 
Vii. Normal KERN_NOTICE "<5>", but still worth noting
 
Viii. KERN_INFO "<6>" information message
 
Ix. KERN_DEBUG "<7>" is used for debugging messages.
 
X. The default DEFAULT_MESSAGE_LOGLEVEL priority is used by printk without a specified priority. It is an integer defined in kernel/printk. c. In kernel 2.6.29 # define DEFAULT_MESSAGE_LOGLEVEL 4
 
Xi. Console priority configuration: File/proc/sys/kernel/printk
 
The file has four numbers: 6 4 1 7, indicating the following priority:
 
Console_loglevel pure character console priority
 
Default_message_loglevel default message priority
 
Minimum_console_level
 
Default_console_loglevel
 
Messages can be printed to the console only when the message priority is higher than the console priority.
 
Find the unprinted statements: vi/var/log/messages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.