Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4729832
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: C/C++

2008-11-11 14:13:14

内核模块编译

以一个非常简单的程序hello.c为例:

/*hello.c*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL"); //General Public License

 
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
 
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye world\n");
}
 
module_init(hello_init);
module_exit(hello_exit);
 

Makefile文件的内容为:

obj-m := hello.o
KERNELDIR := /usr/src/kernels/linux-2.6.24/
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions

命今:
make 编译
insmod hello.ko 插入模块
lsmod 查看载入模块
rmmod hello 删除模块
dmesg 查看日志
[编辑] 虚拟地址到物理地址转换

我们可以通过objdump命令获得一个进程中每条指令的虚地址,要求通过内核的页目录和页表,编写模块,求出用户程序的任一个虚地址对应的物理地址。关于分页机制对应的内核函数的功能,在内核之旅http:// 下载《Understanding THe Linux Kernel》中第二章的内容。

[编辑] 简单的字符设备驱动

一个简单的字符设备驱动开发的例子
主要功能是将内核中的数据取到用户空间,所以程序分为两部分-内核部分(驱动)和用户部分

/*chardev.c*/
#include <linux/kernel.h>
#include <linux/fs.h> /*for file-f_op*/
#include <linux/module.h>
#include <asm/uaccess.h> /*for copy_to_user()*/
#include <linux/cdev.h> /*for cdev ,cdev_init,cdev_add....*/

MODULE_LICENSE("GPL");^M
MODULE_AUTHOR("Helight");

#define DP_MAJOR 250 /*the major number of the chardev*/
#define DP_MINOR 0 /*the minor number of the chardev*/
static int char_read(struct file *filp,char __user *buffer,size_t,loff_t *); /*the read operation of the chardev----read the data from kernel*/
static int char_open(struct inode *,struct file *); /*open the chardev*/
static int char_write(struct file *filp,const char __user *buffer,size_t ,loff_t*); /*write data to kernel*/
static int char_release(struct inode *,struct file *); /*release the chardev*/

static int chropen; /*the chardev open or not*/
struct cdev *chardev; /*define a char device*/
static int len;

static const struct file_operations char_ops = {
        .read = char_read,
        .write = char_write,
        .open = char_open,
        .release = char_release,
};

static int __init char_init(void)
{
        dev_t dev;
        printk(KERN_ALERT"Initing......\n");
        dev=MKDEV(DP_MAJOR,DP_MINOR);
        chardev = cdev_alloc( );

        if(chardev==NULL){
                return -1;
        }
        if(register_chrdev_region(dev,10,"chardev")<0){
                printk(KERN_ALERT"Register char dev error\n");
                return -1;
        }
        chropen=0;
        len=0;
        cdev_init(chardev,&char_ops);
        if(cdev_add(chardev,dev,1)<0)
    {
                printk(KERN_ALERT"Add char dev error\n");
    }

        return 0;
}

static int char_open(struct inode *inode,struct file *file)
{
        if(chropen==0)
                chropen++;
        else{
                printk(KERN_ALERT"Another process open the char device\n");
                return -1;
        }
        try_module_get(THIS_MODULE);
        return 0;
}

static int char_release(struct inode *inode,struct file *file)
{
        chropen--;
        module_put(THIS_MODULE);
        return 0;
}

static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
{
    if(length<strlen("hello world!")){
        if(copy_to_user(buffer,"hello world!",length))
        {
            return 0;
        }
    }else
    {
        if(copy_to_user(buffer,"hello world!",strlen("hello world!")))
        {
            return 0;
        }
    }
        return -1;;
}

static int char_write(struct file *filp,const char __user *buffer,size_t length,loff_t *offset)
{
        return 0;
}


static void __exit module_close(void)
{
        len=0;
        printk(KERN_ALERT"Unloading..........\n");
        kfree(arr);
        unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
        cdev_del(chardev);
}

module_init(char_init);
module_exit(module_close);


用户程序:

/*main.c*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>


int main(void)
{
    int testdev;
    int i,rf=0;
    char buf[15];

    memset(buf, 0, sizeof(buf));
    testdev = open("/dev/chardev0",O_RDWR);
    if ( testdev == -1 )
    {
        perror("open\n");
    exit(0);
    }

    rf=read(testdev,buf,12);
    if(rf<0)
      perror("read error\n");
    printf("R:%s\n",buf);
    close(testdev);
    return 0;
}


编译加载和使用:
<1>程序chardev.c是字符驱动程序,是以内核模块的形式插入内核的,所以编译方法和内核模块的编译方法一致。
<2>模块的加载和卸载也和上面所述的内核模块的加载和卸载方法一致。
<3>设备节点的创建,mknod /dev/chardev0 c 250 0
命令解释:
mknod是建立设备节点的命令;
/dev/chardev0:在/dev/目录下建立chardev0这样一个节点。
c:这个节点是指向一个字符设备的节点
250:这个设备的主设备号
0:次设备号
<3>编译用户程序gcc -o main main.c
<4>运行chmod 666 /dev/chardev0 使其它用户也可以对这个设备进行读写操作,否则只有root用户可以对它进行读写。
<5>运行main,如果没有什么问题的话应该要输出这几个字符。
R:hello


阅读(894) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~