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

    技术2022-05-11  157

    linux 内核模块 驱动程序的编写(1)

    作者: sunmoon

    <http://os.silversand.net>

    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

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


    最新回复(0)