Linux kernel device driver Linux kernel basic notes summary

Linux kernel device driver Linux kernel basic notes summary

1. Linux kernel driver module mechanism

Static loading, compile the driver module into the kernel and load it when the kernel starts Dynamic loading, compile the driver module as ko, and load it when the kernel starts

2. Write kernel driver

#include <linux/module.h>
#include <linux/init.h>
static int __init test_init(void) 
{
return 0; //Return 0 to indicate success, return a negative number to exit loading module}
//__init After the kernel initializes the driver, release the code instruction space of this function static void __exit test_exit(void)
{
....
}
//__exit specifies that this function is only used when the driver is uninstalled and is released after usemodule_init(test_init); //Specify test_init as the module initialization functionmodule_exit(test_exit); //Specify test_exit as the module exit uninstall functionMODULE_LICENSE("GPL"); //Specify the supported protocolsMODULE_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 to view module information

cat /proc/modules View the dynamic loading modules of the current system, which is equivalent to lsmod

test 1768 0 - Live 0xbf03c000

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

ls /sys/module to view all modules

3. Makefile of driver module

  • obj-m += test.o //The source code file is test.c
  • modules:make -C kernel source directory M = driver code directory modules
  • modules install:make -C kernel source directory M=driver code directory modules_install INSTALL_MOD_PATH=/file system path
  • clean:make -C kernel source directory M=driver code directory modules clean

4. Check the driver output message

cat /var/log/messages
tail /var/log/messages

5. Printk level control

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

<linux/kernel.h>
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
#define KERN_WARNING "<4>" /* warning conditions */
#define KERN_NOTICE "<5>" /* normal but significant condition */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */

The default level is KERN_WARNING "<4>"

Use: printk(KERN_INFO"內容");

View the output level of the current kernel cat /proc/sys/kernel/printk
7 4 1 7
7:console_loglevel
4:default_message_loglevel
1:minimum_console_loglevel
7:default_console_loglevel

When the level used by the printk function is lower than the current console_loglevel level, it can be output, otherwise it will not be output

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

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • A picture to show the operation principle of Linux kernel
  • Explore how an LED can get you started with the Linux kernel

<<:  Detailed explanation of replace into example in mysql

>>:  Vue routing to implement login interception

Recommend

How to install pip package in Linux

1. Download the pip installation package accordin...

Comparison of storage engines supported by MySQL database

Table of contents Storage Engine Storage engines ...

Nginx improves access speed based on gzip compression

1. Why does nginx use gzip? 1. The role of compre...

Using puppeteer to implement webpage screenshot function on linux (centos)

You may encounter the following problems when ins...

Detailed graphic and text instructions for installing MySQL 5.7.20 on Mac OS

Installing MySQL 5.7 from TAR.GZ on Mac OS X Comp...

How to operate json fields in MySQL

MySQL 5.7.8 introduced the json field. This type ...

How to install php7 + nginx environment under centos6.6

This article describes how to install php7 + ngin...

Docker FAQ

Docker only maps ports to IPv6 but not to IPv4 St...

Building a selenium distributed environment based on docker

1. Download the image docker pull selenium/hub do...

SQL GROUP BY detailed explanation and simple example

The GROUP BY statement is used in conjunction wit...

Detailed explanation of setting Context Path in Web application

URL: http://hostname.com/contextPath/servletPath/...

Detailed explanation of the principle of Vue monitoring data

Table of contents 1. Introduction II. Monitoring ...