笔记整理—linux驱动开发部分(2)模块信息与编译

news/2024/10/27 16:48:37/

        对于linux而言,.ko文件为驱动文件,在终端可以使用lsmod列出已经安装的模块,使用insmod xxx.ko安装所需要的模块,modinfo xxx.ko打印某个模块提供的信息,rmmod xxx卸载某个不需要的模块。

        insmod与module_init宏。在源代码中,使用宏声明了一个函数,使用module_init()宏对宏内的函数进行insmod的绑定,insmod中会去执行宏所绑定的函数。dmsg指令可看见函数内打印。

        module_exit宏与rmmod。也是一种module_exit()宏对函数的绑定与rmmod对其内函数的执行。

        insmod的version magic与zImage中的version magic因一模一样,否则会导致驱动模块安装不了,这是一种安全措施。

        如何保证内核源代码树与模块内核源代码一样(version magic)保证两个用的是同一个内核源码树即可。

        模块的常用宏可用modinfo查看。

        MODULE_xxx宏用于添加模块描述信息,MODULE_LICENSE("GPL")为许可证信息为GPL,GPL是指内核为GPL的内核;MODULE_AUTHOR()为作者描述;MODULE_DESCRIPTION()为模块描述(作用);MODULE_ALIAS()模块别名。

        函数修饰符:

static int __init chrdev_init(void);
static void __exit chrdev_exit(void);

        __init是一个宏,一个内核宏,与段相关,放入.init.text段默认为.text段。__exit也是一个宏把这个函数.exit.text段默认为.text段。所有__init的函数是被统一存在特定地方的,因为安装是一次性的,所以安装完成后这个段会被释放,__exit同理。

        printk()只能在内核范围使用,printf的调试信息一般为全开或全关,但在内核中的整体调试时,要善用打印级别做调试信息打印,可以靠更改打印级别控制可见信息输出。

        <linux/module.h>里存放了module_init;和module_exit。 <linux/init.h>存放了__init;和__exit。驱动头文件与应用层头文件不同,应用层头文件由编译器提供,驱动由内核提供(内核源码)/include/linux/xxx/xxx.h。

        MAKEFILE分析,KERN_DIR= 这是内核源码树目录,obj-m+=xxx.o编译目标xxx.c对应,-m是将其编译为一个模块。

make -c $(KERN_DIR) M='pwd' modules

        本质上是make modules,进入内核源码树中用内核模块规则进行模块编译,完成后把文件移动回来。KERN_DIR=/Root/driver/kernel要精准到kernel的位置就行。

        内核启动后会打印释放init段的内存大小。

        嵌入式系统工作原理:应用层——>API——>设备驱动——>硬件。

        API指write、read、open、close等,驱动源码中应该提供这些API实体。

        flie_operations结构体:

struct file_operations {struct module *owner;loff_t (*llseek) (struct file *, loff_t, int);ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);int (*readdir) (struct file *, void *, filldir_t);unsigned int (*poll) (struct file *, struct poll_table_struct *);int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);long (*compat_ioctl) (struct file *, unsigned int, unsigned long);int (*mmap) (struct file *, struct vm_area_struct *);int (*open) (struct inode *, struct file *);int (*flush) (struct file *, fl_owner_t id);int (*release) (struct inode *, struct file *);int (*fsync) (struct file *, int datasync);int (*aio_fsync) (struct kiocb *, int datasync);int (*fasync) (int, struct file *, int);int (*lock) (struct file *, int, struct file_lock *);ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);int (*check_flags)(int);int (*flock) (struct file *, int, struct file_lock *);ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);int (*setlease)(struct file *, long, struct file_lock **);
};

        包装了多种函数指针,使驱动接口与API相接,挂接实体函数;设备驱动向内核注册时提供该结构体类型变量;每个驱动都有这样的一个结构体。

        注册函数是由内核提供的,注册前驱动是用不了的,注册是向内核进行注册,注册后驱动就有了登记可以使用。


http://www.ppmy.cn/news/1542377.html

相关文章

PgSQL常用SQL语句

PgSQL常用SQL语句 这是我在这个网站整理的笔记,有错误的地方请指出&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;神的孩子都在歌唱 PgSQL是一种开源的关系型数据库管理系统&#xff0c;它是PostgreSQL的一种实现。本文将介绍一些常用的PgSQL SQL语句&a…

Kafka 解决消息丢失、乱序与重复消费

一、引言 在分布式系统中&#xff0c;Apache Kafka 作为一种高吞吐量的分布式发布订阅消息系统&#xff0c;被广泛应用于日志收集、流式处理、消息队列等场景。然而&#xff0c;在实际使用过程中&#xff0c;可能会遇到消息丢失、乱序、重复消费等问题&#xff0c;这些问题可能…

深入解析 Jenkins 自动化任务链:三大方法实现任务间依赖与状态控制

文章目录 前言1. 使用 “Build Trigger”&#xff08;构建触发器&#xff09;2. 使用 Jenkins Pipeline 实现任务触发3. 使用 Jenkins 的 “Parameterized Trigger Plugin” 插件例子1&#xff1a;任务 A 成功后自动执行任务 B例子2&#xff1a;任务 A 成功后自动执行 Pipeline…

模板语法(1)

一、文本&#xff1a; 在html中通过{{}}&#xff08;双大括号&#xff09;中可以把Vue对象中的数据插入到网页中。并且只要Vue对象上对应的值发生改变了&#xff0c;那么html中双大括号中的值也会立马改变。 <script setup name"App">import { ref } from vu…

【ArcGIS Pro实操第4期】绘制三维地图

【ArcGIS Pro实操第4期】绘制三维地图 ArcGIS Pro绘制三维地图-以DEM高程为例参考 如何使用ArcGIS Pro将栅格数据用三维的形式进行表达&#xff1f;在ArcGIS里可以使用ArcScene来实现&#xff0c;ArcGIS Pro实现原理跟ArcScene一致。由于Esri未来将不再对ArcGIS更新&#xff0c…

基于SpringBoot的酒店管理系统的设计与实现

摘要 酒店管理系统的设计旨在提供快捷高效的住宿资源管理方案&#xff0c;帮助管理员实现对酒店内房间、客户信息、订单的全方位管理&#xff0c;同时为用户提供便捷的预订和查询功能。本系统基于Spring Boot框架&#xff0c;结合前端框架和数据库设计&#xff0c;构建一个用户…

Linux TCP CC状态机

万字详文&#xff1a;TCP 拥塞控制详解 - 知乎bcc/tools/tcpcong.py at master iovisor/bccbcc/tools/tcpcong_example.txt at master iovisor/bcc 1.状态机 2.tcp map

Git process for submit and download

git init git add . git status git commit -m “commits more again” git remote add origin https://gitee.com/gong-dadian/full-stack-demos.git (first time?!) or git remote add origin gitgitee.com:gong-dadian/full-stack-demos.git git push origin master o…