字符设备驱动(内核态用户态内存交互)

前言

内核驱动:运行在内核态的动态模块,遵循内核模块框架接口,更倾向于插件。
应用程序:运行在用户态的进程。
应用程序与内核驱动交互通过既定接口,内核态和用户态访问依然遵循内核既定接口。

环境搭建

系统:openEuler-20.03-LTS-SP3

yum install gcc kernel-devel

编写源码

  • char_module.c
#include 
#include 
#include 
#include 
#include  //下面这三个头文件是由于动态创建需要加的
#include 
#include 

MODULE_LICENSE("GPL");

#define DEVICE_NAME "char_module"
#define BUF_SIZE 32

static struct class *cdev_class;
dev_t dev_num = 0; // 这里是动态分配设备号和动态创建设备结点需要用到的
struct cdev dev_c;

static char context_buf[BUF_SIZE]={"this a test context buffer\0"};

static ssize_t read(struct file *, char *, size_t, loff_t *);
static ssize_t write(struct file *, const char *, size_t, loff_t *);
static int open(struct inode *, struct file *);
static int release(struct inode *, struct file *);

// 初始化字符设备驱动的 file_operations 结构体
struct file_operations fops = {
        .read = read,
        .write = write,
        .open = open,
        .release = release
};

static int __init demo_init(void)
{
        int ret, err;

        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);

        // 注册设备驱动
        ret = alloc_chrdev_region(&dev_num, 0, 1, DEVICE_NAME); // 动态分配设备号
        if (ret)
        {
                printk("demo_init register failure\n");
                unregister_chrdev_region(dev_num, 1);
                return ret;
        }
        printk("demo_init register success\n");

        // 初始化设备操作
        cdev_init(&dev_c, &fops);
        err = cdev_add(&dev_c, dev_num, 1);
        if (err)
        {
                printk(KERN_NOTICE "error %d adding cdev\n", err);
                unregister_chrdev_region(dev_num, 1);
                return err;
        }

        // 动态创建设备结点
        cdev_class = class_create(THIS_MODULE, DEVICE_NAME); 
        if (IS_ERR(cdev_class))
        {
                printk("ERR:cannot create a cdev_class\n");
                unregister_chrdev_region(dev_num, 1);
                return -1;
        }
        device_create(cdev_class, NULL, dev_num, 0, DEVICE_NAME);

        return ret;
}

static void __exit demo_exit(void)
{
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);

        // 注销设备驱动
        device_destroy(cdev_class, dev_num);
        class_destroy(cdev_class);
        unregister_chrdev_region(dev_num, 1);
}

static ssize_t read(struct file *filp, char *buf, size_t len, loff_t *off)
{
        // 内核空间到用户空间copy
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);
        if (raw_copy_to_user(buf, &context_buf, sizeof(context_buf)))
        {
                return -EFAULT;
        }
        printk(KERN_INFO "user space: %pF", buf);
        printk(KERN_INFO "read: %pF; size: %ld; data: %s", &context_buf, sizeof(context_buf), context_buf);
        return BUF_SIZE;
}

static ssize_t write (struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
        // 用户空间到内核空间copy
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);
        if (raw_copy_from_user(&context_buf, buf, sizeof(context_buf)))
        {
                return -EFAULT;
        }
        printk(KERN_INFO "user space: %pF", buf);
        printk(KERN_INFO "write: %pF; size: %ld; data: %s", &context_buf, sizeof(context_buf), context_buf);
        return BUF_SIZE;
}

static int open(struct inode *inodp, struct file *filp)
{
        printk(KERN_INFO "%s: %s", DEVICE_NAME , __func__);
        return 0;
}

static int release(struct inode *inodp, struct file *filp)
{
        printk(KERN_INFO "%s: %s", DEVICE_NAME, __func__);
        return 0;
}

module_init(demo_init);
module_exit(demo_exit);
  • Makefile
ifneq ($(KERNELRELEASE),)
obj-m := char_module.o

else
PWD  := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
        $(MAKE) -C $(KDIR) M=$(PWD) modules modules_install
clean:
        rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions modules.*  Module.*
endif

app.c

#include 
#include 
#include 
#include 
 
#define CHAR_DEV_NAME "/dev/char_module"

int main()
{
        int ret;
        int fd;
        char buf[32];
 
        fd = open(CHAR_DEV_NAME, O_RDWR | O_NDELAY);
        if(fd < 0)
        {
                printf("open failed!\n");
                return -1;
        }

        int size = read(fd, buf, 32);

        printf("read size: %d;\nbuffer:[%s]\n", size, buf);

        char *write_buf = "use a application wirte to driver buffer";

        int w_size = write(fd, write_buf, strlen(write_buf));

        printf("write size: %d;\nbuffer:[%s]\n", w_size, write_buf);

        close(fd);

        return 0;
}

构建并测试

  • 驱动构建
    make && insmod char_module.ko
    
  • 驱动信息确认
    在这里插入图片描述
  • 应用程序构建
    gcc app.c -o app
    ./app
    
  • 应用程序运行结果
    字符设备驱动(内核态用户态内存交互)_第1张图片
  • 查看驱动日志
    dmesg
    
    字符设备驱动(内核态用户态内存交互)_第2张图片

你可能感兴趣的:(交互)