文档介绍:动态模块与设备驱动
3/12/2018
1
西安交通大学计算机系
Linux的动态模块
3/12/2018
2
西安交通大学计算机系
动态模块的组成
模块的组织
#include <linux/> // 说明是个内核功能
#include <linux/> // 说明是个模块
// 其他header信息
int init_module() // 声明是一个模块
{
... // 加载时,初始化模块的编码
}
... // 期望实现的其它功能,如read()、ioctl()等函数
void cleanup_module()
{
... // 卸载时,注销模块的编码
}
3/12/2018
3
西安交通大学计算机系
模块编译与加卸载
模块的编译
#gcc −O2 −g −Wall −DMODULE −D __KERNEL__ −c
//
模块的加载
insmod命令
模块的查看
lsmod
more /proc/modules
dmesg ——查看日志(printk)
模块的卸载
rmmod命令
3/12/2018
4
西安交通大学计算机系
模块编程_示例1
#define MODULE
#include <linux/>
#include <linux/>
#if CONFIG_MODVERSION==1
#define MODVERSIONS
#include <linux/>
#endif
int init_module()
{ printk("Hello! This is a testing module!\n"); return 0;
}
void cleanup_module()
{ printk("Sorry! The testing module is unloading now!\n");
}
3/12/2018
5
西安交通大学计算机系
模块编程
模块的编译、加载与卸载
编译
# gcc −O2 −g −Wall −DMODULE −D__KERNEL__−c
//
加载
#insmod –f
查看
#lsmod
#cat /proc/modules
#dmesg
卸载
#rmmod testmodule
3/12/2018
6
西安交通大学计算机系
模块编程—演示
/home/yms/
/home/yms/
3/12/2018
7
西安交通大学计算机系
模块编程_示例2
系统调用的篡改
模块程序
用户程序 可执行程序 myuser
3/12/2018
8
西安交通大学计算机系
Linux的设备驱动
3/12/2018
9
西安交通大学计算机系
实验目的
了解Linux操作系统中的设备驱动程序的组成
编写简单的字符设备驱动程序并进行测试
理解Linux操作系统的设备管理机制
设备种类繁多,但具有通用性
采用通用接口,屏蔽实现细节
设备驱动程序
控制设备的操作
为内核提供统一的操作接口
3/12/2018
10
西安交通大学计算机系