设备管理与模块机制(1).ppt_第1页
设备管理与模块机制(1).ppt_第2页
设备管理与模块机制(1).ppt_第3页
设备管理与模块机制(1).ppt_第4页
设备管理与模块机制(1).ppt_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

LinuxDevice Module 设备管理与模块机制 基本概念传统方式的设备注册与管理devfs注册与管理块设备的请求队列网络设备模块机制 LinuxDevice Module 基本概念 字符设备 块设备 网络设备字符设备以字节为单位进行数据处理 通常只允许按顺序访问块设备将数据按可寻址的块为单位进行处理 可以随机访问 利用缓冲技术网络设备是一类特殊的设备 每块网卡有名字但没有设备文件与之对应查看系统中的设备 proc devices主设备号和次设备号majornumber 相同的设备使用相同的驱动程序minornumber 用来区分具体设备的实例查看设备及其类型 ls l dev 设备文件系统devfs dev目录过于庞大 很多设备文件没有对应系统中的设备devfs根据系统中的实际设备构建设备文件 并按目录存放 如 dev disk dev pts LinuxDevice Module 基本概念 LinuxDevice Module 基本概念 建立设备 mknod dev dev nametypemajor numberminor number LinuxDevice Module VFS中的文件 include linux fs hstructfile structfile operations f op structfile operations loff t llseek structfile loff t int ssize t read structfile char size t loff t ssize t write structfile constchar size t loff t int ioctl structinode structfile unsignedint unsignedlong int mmap structfile structvm area struct int open structinode structfile int release structinode structfile int fsync structfile structdentry intdatasync int fasync int structfile int LinuxDevice Module 1 llseek file offset whence 修改文件的读写指针 2 read file buf count offset 从设备文件的offset处开始读出count个字节 然后增加 offset的值 3 write file buf count offset 从设备文件的offset处写入count个字节 然后增加 offset的值 4 ioctl inode file cmd arg 向一个硬件设备发命令 对设备进行控制 5 mmap file vma 将设备空间映射到进程地址空间 6 open inode file 打开并初始化设备 7 release inode file 关闭设备并释放资源 8 fsync file dentry 实现内存与设备之间的同步通信 9 fasync file on 实现内存与设备之间的异步通信 LinuxDevice Module fs devices cstructdevice struct constchar name structfile operations fops staticstructdevice structchrdevs MAX CHRDEV 注册与注销函数 intregister chrdev unsignedintmajor constchar name structfile operations fops intunregister chrdev unsignedintmajor constchar name 注 major即设备的主设备号 注册后就是访问数组chrdevs的索引 下标 字符设备的注册与管理 LinuxDevice Module PCI设备 驱动实现见word文档 Linux内核启动时会对所有PCI设备进行扫描 登录和分配资源等初始化操作 建立起系统中所有PCI设备的拓扑结构此后当内核欲初始化某设备时 调用module init加载该设备的驱动程序 LinuxDevice Module 块设备 fs block dev cstaticstruct constchar name structblock device operations bdops blkdevs MAX BLKDEV LinuxDevice Module 块设备注册 fs block dev cregister blkdev unsignedintmajor constchar name structblock device operations bdops intunregister blkdev unsignedintmajor constchar name LinuxDevice Module 块设备的操作block device operations structblock device operations int open structinode structfile int release structinode structfile int ioctl structinode structfile unsigned unsignedlong int check media change kdev t int revalidate kdev t structmodule owner LinuxDevice Module block device operations 并不能完全提供file operations结构中的所必需的主要函数 例如read write 所以内核实际上是采用def blk fops变量对相关的file operations 变量进行了赋值 structfile operationsdef blk fops 除了open release等函数利用了设备注册时提供的block device operations 结构中的成员变量之外 其他函数都是采用所有块设备通用的操作函数 def blk fops 块设备的缺省操作def blk fops LinuxDevice Module fs block dev cstructfile operationsdef blk fops open blkdev open release blkdev close llseek block llseek read generic file read write generic file write mmap generic file mmap fsync block fsync ioctl blkdev ioctl 块设备的缺省操作def blk fops LinuxDevice Module block read与block write等函数是设备相关的块设备注册时一个重要的任务就是提供这个设备相关的操作函数给内核 LinuxDevice Module devfs注册与管理 fs devfs base cregister chrdev 停止使用 改为devfs register chrdev register blkdev 停止使用 改为devfs register blkdev intdevfs register chrdev unsignedintmajor constchar name structfile operations fops intdevfs register blkdev unsignedintmajor constchar name structblock device operations bdops intdevfs unregister chrdev unsignedintmajor constchar name intdevfs unregister blkdev unsignedintmajor constchar name LinuxDevice Module 块设备的请求队列 当系统对块设备进行读操作时 仅仅是通过块设备通用的读操作函数block read 将这一个请求发送给对应的设备 并保存在该设备的操作请求队列 requestqueue 中 然后调用这个块设备的底层处理函数 对请求队列中的操作请求进行逐一的执行structblk dev struct include linux blkdev h request queue trequest queue queue proc queue void data structblk dev structblk dev MAX BLKDEV LinuxDevice Module block read 流程 LinuxDevice Module Linux网络协议栈 LinuxDevice Module 重要的数据结构 以socket文件描述符作为参数 系统调用从用户空间切换到内核空间 从而进入到BSDSocket层的操作 操作的对象是socket 结构 每一个这样的结构对应的是一个网络连接通过网络地址族的不同来判断是否应该进入到INETSocket层 这一层的数据存放在msghdr 结构的变量中在INETSocket层中 分成面向连接和面向无连接两种类型 区分TCP和UDP协议 在这一层中的操作对象是sock 类型的数据 而数据存放在sk buff 结构中 LinuxDevice Module 模块机制 Module Linux的单块结构 monolithic 使得其可扩展性较差模块机制 LinuxKernelModule LKM 提高了linux内核的可扩展性利用linux源码编译生成内核时 如某功能允许 m 选项 其他为 y n 说明可以以模块形式存在多数设备驱动程序以模块的方式挂接到内核系统启动时已将若干模块挂入了内核用户只要有权限 就可以编写模块挂入内核模块的缺点 增加了内核管理代价 LinuxDevice Module 模块的设计 EveryLKMconsistsoftwobasicfunctions minimum intinit module void usedforallinitializationstuff voidcleanup module void usedforacleanshutdown 安装模块命令 insmodmodule o modprobemodule o卸载模块命令 rmmodmodule o查询系统中已装入的模块 lsmod LinuxDevice Module 模块的设计 例子hello c defineMODULE includeintinit module void printk Hello world n return0 voidcleanup module void printk Goodbyecruelworld n 编译模块 gcc chello c DMODULE D KERNEL DLINUX Wall O2 I usr src linux 2 4 include安装 卸载模块 insmodhello oHelloworld rmmodhelloGoodbyecruelworld LinuxDevice Module 模块设计注意事项 模块设计与应用程序设计模块是装入内核的 运行时CPU处于核心态应用程序运行时CPU处于用户态编译模块设计应用程序使用的include文件 usr include设计内核模块使用的include文件 usr src linux

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论