模塊不被編譯在內(nèi)核中,因而控制了內(nèi)核的大小.然而模塊一旦被插入內(nèi)核,他就和內(nèi)核其他部分一樣.這樣一來就會(huì)曾家一部分系統(tǒng)開銷。同時(shí),如果模塊出現(xiàn)問題,也許會(huì)帶來系統(tǒng)的崩潰。
模塊的實(shí)現(xiàn)機(jī)制:
啟動(dòng)時(shí),由函數(shù) void inti_modules() 來初始化模塊,因?yàn)閱?dòng)事很多時(shí)候沒有模塊.這個(gè)函數(shù)往往把內(nèi)核自身當(dāng)作一個(gè)虛模塊。
如由系統(tǒng)需要,則調(diào)用一系列以sys 開頭的函數(shù),對(duì)模塊進(jìn)行操作. 如:
sys_creat_modules(),sys_inti_modules() ,
sys_deldte_modules()等等.
這里會(huì)用到一些模塊的數(shù)據(jù)就結(jié)構(gòu),在/usr/scr/Linux/include/Linux/module.h 中,有興趣的朋友可以找出來一看塊的加入有兩種方法:一是手動(dòng)加入:如:insmod modulename.另一種是根據(jù)需要,動(dòng)態(tài)的加載模塊:如你執(zhí)行命令:
$mount -t msdos /dev/hdd /mnt/d 時(shí).系統(tǒng)便自動(dòng)加載 FAT模塊,以支持MSDOS的文件系統(tǒng)。
1.模塊編程
寫一個(gè)模塊,必須有一定的多進(jìn)程編程基礎(chǔ),因?yàn)槟阕兊贸绦虿皇且砸粋€(gè)獨(dú)立的程序的來運(yùn)行的。另外,因?yàn)椋K需要在內(nèi)核模式下運(yùn)行,會(huì)遇到在內(nèi)和空間和用戶空間數(shù)據(jù)交換的問題.一般的數(shù)據(jù)復(fù)制函數(shù)無法完成這一個(gè)過程。因此系統(tǒng)已入了一些特殊的函數(shù)以用來完成內(nèi)核空間和用戶空間數(shù)據(jù)的交換/
這些函數(shù)有:void put _user (type valude,type *u_addr)
memcpy_tofs()
等等,有興趣的朋友可以仔細(xì)的看看所有的函數(shù),以及他們的用法.需要說明的是.模塊編程河內(nèi)核的版本有很大的關(guān)系。如果版本不通可能造成,內(nèi)核模塊不能編譯,或者.在運(yùn)行這個(gè)模塊時(shí),出現(xiàn)不可測(cè)結(jié)果。如:系統(tǒng)崩潰等。
明白了這些以后,你就可以嘗試著編寫內(nèi)核模塊了。對(duì)于每一個(gè)內(nèi)核模塊來說,必定包含兩個(gè)函數(shù)int init_module() 這個(gè)函數(shù)在插入內(nèi)核時(shí)啟動(dòng),在內(nèi)核中注冊(cè)一定的功能函數(shù),或者用他的代碼代替內(nèi)和中某些函數(shù)的內(nèi)容(估計(jì)這些函數(shù)是空的)。因此,內(nèi)和可以安全的卸載。
int cleanup_module() 當(dāng)內(nèi)核模塊謝載時(shí),調(diào)用.將模塊從內(nèi)核中清除.
同其他的程序設(shè)計(jì)教程一樣 ,我們給出一個(gè)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;
}
這樣一個(gè)例子就完成了.我們也寫一個(gè)makefile 的例子,以適于我們?cè)诖蟪绦蛑氐膽?yīng)用。一下是makfile 文件的內(nèi)容 。
# 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
然后你運(yùn)行make 命令 得到hello.o 這個(gè)模塊,運(yùn)行
$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中的大部分驅(qū)動(dòng)程序,是以模塊的形式編寫的,這些驅(qū)動(dòng)程序源碼可以修改到內(nèi)核中,也可以把他們編譯成模塊形勢(shì),在需要的時(shí)候動(dòng)態(tài)加載。
一個(gè)典型的驅(qū)動(dòng)程序,大體上可以分為這么幾個(gè)部分:
1.注冊(cè)設(shè)備
在系統(tǒng)初啟,或者模塊加載時(shí)候,必須將設(shè)備登記到相應(yīng)的設(shè)備數(shù)組,并返回設(shè)備的主驅(qū)動(dòng)號(hào),例如:對(duì)快設(shè)備來說調(diào)用 refister_blkdec()將設(shè)備添加到數(shù)組blkdev中,并且獲得該設(shè)備號(hào),并利用這些設(shè)備號(hào)對(duì)此數(shù)組進(jìn)行索引。對(duì)于字符驅(qū)動(dòng)設(shè)備來說,要使用 module_register_chrdev()來獲得祝設(shè)備的驅(qū)動(dòng)號(hào),然后對(duì)這個(gè)設(shè)備的所有調(diào)用都用這個(gè)設(shè)備號(hào)來實(shí)現(xiàn)。
2.定義功能函數(shù)
對(duì)于每一個(gè)驅(qū)動(dòng)函數(shù)來說,都有一些和此設(shè)備密切相關(guān)的功能函數(shù),那常用的塊設(shè)備或者字符設(shè)備來說,都存在著諸如 open() read() write() ioctrol()這一類的操作。當(dāng)系統(tǒng)社用這些調(diào)用時(shí),將自動(dòng)的使用驅(qū)動(dòng)函數(shù)中特定的模塊,來實(shí)現(xiàn)具體的操作。而對(duì)于特定的設(shè)備,上面的系統(tǒng)調(diào)用對(duì)應(yīng)的函數(shù)是一定的。
如:在塊驅(qū)動(dòng)設(shè)備中.當(dāng)系統(tǒng)試圖讀取這個(gè)設(shè)備(即調(diào)用read()時(shí)),就會(huì)運(yùn)行驅(qū)動(dòng)程序中的block_read() 這個(gè)函數(shù)。
打開新設(shè)備時(shí)會(huì)調(diào)用這個(gè)設(shè)備驅(qū)動(dòng)程序的device_open() 這個(gè)函數(shù).
3.謝載模塊
在不用這個(gè)設(shè)備時(shí),可以將他卸載,主要是從/proc 中取消這個(gè)設(shè)備的特殊文件,可用特定的函數(shù)實(shí)現(xiàn)。
下面我們列舉一個(gè)字符設(shè)備驅(qū)動(dòng)程序的框架.來說明這個(gè)過程.
/* 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;
}