Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example Analysis of Linux Kernel Foundation of Linux Kernel device driver

Shulou Source: shulou.com Published: 2022-06-01 18:19:35 09月17日 Update

This article mainly introduces the example analysis of the Linux kernel foundation of the Linux kernel device driver, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. Linux kernel driver module mechanism

Static loading, the driver module is programmed into the kernel and loaded when the kernel starts

Dynamic loading, compiling the driver module as ko, and loading when needed after the kernel starts.

two。 Write kernel drivers

# include # include static int _ _ init test_init (void) {return 0; / / return 0 indicates success. Add a negative number to exit the loading module} / / _ init when the kernel initializes the driver, release the code instruction space of this function static void _ _ exit test_exit (void) {....} / / _ exit specify that this function is used only when the driver is unloaded, and release module_init (test_init) after use / / specify test_init as the module initialization function module_exit (test_exit); / / specify test_exit as the uninstall function MODULE_LICENSE ("GPL") when the module exits; / / specify the supported protocol MODULE_AUTHOR ("author"); MODULE_DESCRIPTION ("description"); MODULE_VERSION ("version") # define _ _ init _ _ section (.init.text) # define _ _ initdata _ _ section (.init.data) char _ _ initdata buf [] = "hello world"; # define _ _ exitdata _ section (.exit.data) # define _ _ exit _ section (.exit.text) /

Modinfo test.ko views the module's information

Cat / proc/modules View the dynamic loading module of the current system is equivalent to lsmod

Test 1768 0-Live 0xbf03c000

Module name, memory size used, number of calls, valid, memory address where the module is located

Ls / sys/module view all modules

3. Makefile of driver module

Obj-m + = test.o / / Source file is test.c

Modules:make-C kernel source code directory M = modules where the driver code is located

Modules install:make-C kernel source code directory M = directory where the driver code is located modules_install INSTALL_MOD_PATH=/ file system path

Clean:make-C kernel source code directory M = modules clean where the driver code is located

4. View the message of the driver output

Cat / var/log/messagestail / var/log/messages

5. Level control of printk

/ usr/src/kernels/2.6.18-194.el5-i686/include/linux/kernel.h

# define KERN_EMERG "/ * system is unusable * / # define KERN_ALERT" / * action must be taken immediately * / # define KERN_CRIT "/ * critical conditions * / # define KERN_ERR" / * error conditions * / # define KERN_WARNING "/ * warning conditions * / # define KERN_NOTICE" / * normal but significant condition * / # define KERN_INFO "/ * informational * / # define KERN_DEBUG" / * debug-level messages * /

The default level is KERN_WARNING ""

Use: printk (KERN_INFO "content")

View the output level cat / proc/sys/kernel/printk of the current kernel

7 4 1 7

7:console_loglevel

4:default_message_loglevel

1:minimum_console_loglevel

7:default_console_loglevel

When the printk function uses a level less than the current console_loglevel level, it can export, otherwise it will not

Modify level output echo 8 > / proc/sys/kernel/printk

Thank you for reading this article carefully. I hope the article "sample Analysis of the Linux Kernel basis of Linux Kernel device drivers" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Kernel driver module directory level function output code location source code article foundation example device device driver analysis memory dynamic file system Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Huawei Shulou Technology Apple OPPO Reno