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 get the width and height of the image in WeChat applet

origin Recently, I am working on requirement A, i...

The main idea of ​​​​dynamically setting routing permissions in Vue

I have seen some dynamic routing settings on the ...

How to elegantly back up MySQL account information

Preface: I recently encountered the problem of in...

How to manually install MySQL 5.7 on CentOS 7.4

MySQL database is widely used, especially for JAV...

How to install MySQL under Linux (yum and source code compilation)

Here are two ways to install MySQL under Linux: y...

Use of Linux tr command

1. Introduction tr is used to convert or delete a...

Detailed explanation of vue keepAlive cache clearing problem case

Keepalive is often used for caching in Vue projec...

How to Find the Execution Time of a Command or Process in Linux

On Unix-like systems, you may know when a command...

Nginx reverse proxy learning example tutorial

Table of contents 1. Reverse proxy preparation 1....

A detailed introduction to the Linux directory structure

When you first start learning Linux, you first ne...

Reduce memory and CPU usage by optimizing web pages

Some web pages may not look large but may be very ...

Summary of uncommon js operation operators

Table of contents 2. Comma operator 3. JavaScript...

MySQL data table partitioning strategy and advantages and disadvantages analysis

Table of contents Why do we need partitions? Part...

Analysis of MySQL's planned tasks and event scheduling examples

This article uses examples to describe MySQL'...

How to solve the problem of ERROR 2003 (HY000) when starting mysql

1. Problem Description When starting MYSQL, a pro...