linux内核模块和驱动程序的编写

来源:互联网 发布:淘宝网围巾专卖 编辑:程序博客网 时间:2024/04/30 17:21
linux内核是一个整体是结构.因此向内核添加任何东西.或者删除某些功能 ,都十分困难.为了解决这个问题. 
引入了内核机制.从而可以动态的想内核中添加或者删除模块. 

模块不被编译在内核中,因而控制了内核的大小.然而模块一旦被插入内核,他就和内核其他部分一样.这样一来 
就会曾家一部分系统开销.同时,如果模块出现问题.,也许会带来系统的崩溃. 

1.1模块的实现机制: 

启动时,由函数 void inti_modules() 来初始化模块,.因为启动事很多时候没有模块.这个函数往往把内核自 
身当作一个虚模块. 

如由系统需要,则调用一系列以sys 开头的函数,对模块进行操作. 如:sys_creat_modules(),sys_inti_modules() , 
sys_deldte_modules()等等. 

这里会用到一些模块的数据就结构,在/usr/scr/linux/include/linux/module.h 中,有兴趣的朋友可以找出来一看 

块的加入有两种方法:一是手动加入:如:insmod modulename.另一种是根据需要,动态的加载模块.如你执行命令:  
$mount -t msdos /dev/hdd /mnt/d 时.系统便自动加载 FAT模块,以支持MSDOS的文件系统.

1.2 模块编程 

写一个模块,必须有一定的多进程编程基础.因为你变得程序不是以一个独立的程序的来运行的.另外,因为,模块需要 
在内核模式下运行,会遇到在内和空间和用户空间数据交换的问题.一般的数据复制函数无法完成这一个过程.因此系 
统已入了一些特殊的函数以用来完成内核空间和用户空间数据的交换. 

这些函数有:void put _user (type valude,type *u_addr) 

memcpy_tofs() 

等等,有兴趣的朋友可以仔细的看看所有的函数,以及他们的用法.需要说明的是.模块编程河内核的版本有很大的关系. 
如果版本不通可能造成,内核模块不能编译,或者.在运行这个模块时,出现不可测结果.如:系统崩溃等. 

明白了这些以后.你就可以尝试着编写内核模块了.对于每一个内核模块来说.必定包含两个函数 

int init_module() 这个函数在插入内核时启动,在内核中注册一定的功能函数,或者用他的代码代替内和中某些函数 
的内容(估计这些函数是空的).因此,内和可以安全的卸载.(个人猜测) 

int cleanup_module() 当内核模块谢载时,调用.将模块从内核中清除. 

同其他的程序设计教程一样 ,我们给出一个hello world 的例子 

/*hello.c a module programm*/ 

/* the program runing under kernel mod and it is a module*/ 

#include" linux/kernerl.h"  

#include"llinux/module.h" 

/* pross the CONFIG_MODVERSIONS*/ 

#if CONFIG_MODVERSIONS==1 

#define MODVERSIONS 

#include""linux/modversions.h" 

#end if 

  

/* the init function*/ 

int init_module() 



printk(" hello world !\n'); 

printd(" I have runing in a kerner mod@!!\n"); 

return 1; 



/* the distory function*/ 

int cleanup_module() 



printk(" I will shut down myself in kernerl mod /n)"; 

retutn 0; 



这样一个例子就完成了.我们也写一个makefile 的例子,以适于我们在大程序重的应用.一下是makfile 文件的内容 

# a makefile for a module 

CC=gcc 

MODCFLAGS:= -Wall _DMODULE -D_KERNEL_ -Dlinux 

hello.o hello.c /usr/inculde?linux/version.h 

CC $(MODCFLAGS) 0c hello.c 

echo the module is complie completely 

然后你运行make 命令 得到hello.o 这个模块.运行 

$insmod hello.o 

hello world! 

I will shut down myself in kernerl mod 

$lsmod 

hello (unused) 

…. 

$remmod 

I will shut down myself in kernerl mod 

这样你的模块就可以随意的插入和删除了. 






linux中的大部分驱动程序,是以模块的形式编写的.这些驱动程序源码可以修改到内核中,也可以把他们编译成模 
块形势,在需要的时候动态加载. 

一个典型的驱动程序,大体上可以分为这么几个部分: 

1,注册设备 

在系统初启,或者模块加载时候,必须将设备登记到相应的设备数组,并返回设备的主驱动号,例如:对快设备来说调 
用 refister_blkdec()将设备添加到数组blkdev中.并且获得该设备号.并利用这些设备号对此数组进行索引.对于 
字符驱动设备来说,要使用 module_register_chrdev()来获得祝设备的驱动号.然后对这个设备的所有调用都用这 
个设备号来实现 

2,定义功能函数 

对于每一个驱动函数来说.都有一些和此设备密切相关的功能函数.那最常用的块设备或者字符设备来说.都存在着 
诸如 open() read() write() ioctrol()这一类的操作.当系统社用这些调用时.将自动的使用驱动函数中特定的模 
块.来实现具体的操作.而对于特定的设备.上面的系统调用对应的函数是一定的. 

如:在块驱动设备中.当系统试图读取这个设备(即调用read()时),就会运行驱动程序中的block_read() 这个函数. 

打开新设备时会调用这个设备驱动程序的device_open() 这个函数. 

3,谢载模块 

在不用这个设备时,可以将他卸载.主要是从/proc 中取消这个设备的特殊文件.可用特定的函数实现. 

下面我们列举一个字符设备驱动程序的框架.来说明这个过程. 

/* a module of a character device */ 

  

/* some include files*/ 

#include"param.h" 

#include"user.h" 

#include"tty.h" 

#include"dir.h" 

#include”fs.h" 

  

/* the include files modules need*/ 

#include"linux/kernel.h" 

#include"linux/module.h" 

#if CONFIG_MODBERSIONS==1 

degine MODBERSIONS 

#include" linux.modversions.h" 

#endif 

#difine devicename mydevice 

/* the init funcion*/ 

int init_module() 



int tag=module_register_chrdev(0,mydevice,&Fops); 

if (tag<0) 



printk("the device init is erro!\n"); 

return 1; 



return 0; 



  

/*the funcion which the device will be used */ 

int device_open () 



……. 



int device_read () 



……. 



int device_write () 



……. 



int device_ioctl () 



……. 



…… 

/* the deltter function of this module*/ 

int cleanup_module() 



int re=module_unregister_chrdev(tag,mydevice); 

if( re<0) 



printk("erro unregister the module !!\n"); 

return 1; 



return 0; 

0 0
原创粉丝点击