内核实验(五):传统简单字符设备驱动
创始人
2025-05-30 12:41:50
0

文章目录

    • 一、篇头
    • 二、源码
      • 2.1 驱动关键部分
      • 2.2 APP:test\_3\_app.c
      • 2.3 驱动完整源码
      • 2.4 Makefile
    • 三、编译
      • 3.1 编译ko
      • 3.2 编译app
    • 四、测试
      • 4.1 部署
      • 4.2 测试
        • 4.2.1 启动qemu虚拟机
        • 4.2.2 运行测试文件
        • 4.2.3 查看设备情况
    • 五、篇尾

一、篇头

目标是把内核相关部分的实验都再做一次,虽然有现成源码,但从以往学习经历看,自己还是得多少做一些改动,对于要自虐的,可以从头打一遍,这样效果会好很多,练习完后,做个总结。实验简单,花的时间也不多,温故而知新。

二、源码

2.1 驱动关键部分

static int __init test_3_init(void)
{int ret;pr_info("test_3_init\n");ret = alloc_chrdev_region(&dev, 0, count, DEMO_NAME);if (ret) {pr_err("failed to allocate char device region");return ret;}test_3_cdev = cdev_alloc();if (!test_3_cdev) {pr_err("cdev_alloc failed\n");goto unregister_chrdev;}cdev_init(test_3_cdev, &test_3_fops);/*(1) 下面语句会创建 /proc/devices/test_3_dev, 但不会创建/dev下的设备节点*/ret = cdev_add(test_3_cdev, dev, count);if (ret) {pr_err("cdev_add failed\n");goto cdev_fail;}pr_err("succeeded register char device: %s\n", DEMO_NAME);pr_err("Major number = %d, minor number = %d\n",MAJOR(dev), MINOR(dev));
// …… 省略 …… return ret;
}

2.2 APP:test_3_app.c

  • 完成的linux app源码
#include 
#include 
#include #define MY_DEV_NAME "/dev/test_3_dev"int main()
{char buffer[64];int fd;fd = open(MY_DEV_NAME, O_RDONLY);if (fd < 0) {printf("open device %s failded\n", MY_DEV_NAME);return -1;}read(fd, buffer, 64);close(fd);return 0;
}

2.3 驱动完整源码

  • test_3.c
#include 
#include 
#include 
#include 
#include #define DEMO_NAME "test_3_dev"
static dev_t dev;
static struct cdev *test_3_cdev;
static signed count = 1;static int test_3_open(struct inode *inode, struct file *file)
{int major = MAJOR(inode->i_rdev);int minor = MINOR(inode->i_rdev);pr_info("%s: major=%d, minor=%d\n", __func__, major, minor);return 0;
}static int test_3_release(struct inode *inode, struct file *file)
{pr_info("%s \n", __func__);return 0;
}static ssize_t
test_3_read(struct file *file, char __user *buf, size_t lbuf, loff_t *ppos)
{pr_info("%s \n", __func__);return 0;
}static ssize_t
test_3_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
{pr_info("%s \n", __func__);return 0;}static const struct file_operations test_3_fops = {.owner = THIS_MODULE,.open = test_3_open,.release = test_3_release,.read = test_3_read,.write = test_3_write
};static int __init test_3_init(void)
{int ret;pr_info("test_3_init\n");ret = alloc_chrdev_region(&dev, 0, count, DEMO_NAME);if (ret) {pr_err("failed to allocate char device region");return ret;}test_3_cdev = cdev_alloc();if (!test_3_cdev) {pr_err("cdev_alloc failed\n");goto unregister_chrdev;}cdev_init(test_3_cdev, &test_3_fops);ret = cdev_add(test_3_cdev, dev, count);if (ret) {pr_err("cdev_add failed\n");goto cdev_fail;}pr_err("succeeded register char device: %s\n", DEMO_NAME);pr_err("Major number = %d, minor number = %d\n",MAJOR(dev), MINOR(dev));return 0;cdev_fail:cdev_del(test_3_cdev);
unregister_chrdev:unregister_chrdev_region(dev, count);return ret;
}static void __exit test_3_exit(void)
{pr_info("test_3_exit\n");if (test_3_cdev)cdev_del(test_3_cdev);unregister_chrdev_region(dev, count);
}module_init(test_3_init);
module_exit(test_3_exit);MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("szhou <66176468@qq.com>");
MODULE_DESCRIPTION("test_1, simple char kernel module");

2.4 Makefile

  • 实验和之前几篇文章有集成性,本文实验只需要在 obj-m 后面添加 test_3.o 即可
KDIR := /home/szhou/works/qemu_linux/linux-stableobj-m := test_1.o test_2.o test_3.oall :$(MAKE) -C $(KDIR) M=$(PWD) modulesclean:$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) cleanrm -f *.ko

三、编译

3.1 编译ko

szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ ls
Makefile  modules.order  Module.symvers  test_1.c  test_1.ko  test_1.mod  test_1.mod.c  test_1.mod.o  test_1.o  test_2.c  test_2.ko  test_2.mod  test_2.mod.c  test_2.mod.o  test_2.o  test_3_app.c  test_3.c
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ make
make -C /home/szhou/works/qemu_linux/linux-stable M=/home/szhou/works/qemu_linux/linux-stable/my_kmodules modules
make[1]: Entering directory '/home/szhou/works/qemu_linux/linux-stable'CC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_3.oMODPOST /home/szhou/works/qemu_linux/linux-stable/my_kmodules/Module.symversCC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_1.mod.oLD [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_1.koCC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_2.mod.oLD [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_2.koCC [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_3.mod.oLD [M]  /home/szhou/works/qemu_linux/linux-stable/my_kmodules/test_3.ko
make[1]: Leaving directory '/home/szhou/works/qemu_linux/linux-stable'
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$

3.2 编译app

  • 因为使用qemu模拟,而我们之前建立的mini Linux系统尚未添加 so 等各种依赖库,所以需要在编译app时候添加 --static标志,使用静态链接,这样就可以不依赖于so文件。
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ arm-linux-gnueabi-gcc test_3_app.c -o test_3_app --static
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ file test_3_app
test_3_app: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, BuildID[sha1]=1a479f0e5a7f3cba6fb0ea4121337f56623cb5a5, for GNU/Linux 3.2.0, not stripped
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ 

四、测试

4.1 部署

  • 透过NFS部署,不了解的,请参考之前的内核实验(四)
  • 将测试文件复制到nfs共享目录下
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ cp test_3.ko ~/works/nfs_share/
szhou@bc01:~/works/qemu_linux/linux-stable/my_kmodules$ cp test_3_app ~/works/nfs_share/

4.2 测试

4.2.1 启动qemu虚拟机

qemu-system-arm   -nographic  -M vexpress-a9 -m 1024M -kernel arch/arm/boot/zImage   -initrd ../busybox/rootfs.ext4.img.gz    -dtb arch/arm/boot/dts/vexpress-v2p-ca9.dtb 

4.2.2 运行测试文件

----------------------------------------
Welcome to szhou's tiny Linux
----------------------------------------Please press Enter to activate this console. 
~ # 
~ # 
~ #  mount -t nfs -o nolock 192.168.3.67:/home/szhou/works/nfs_share /mnt
~ # cd /mnt/
/mnt # ls
abc_client  bcd_server  test_3.ko
/mnt # insmod test_3.ko 
test_3: loading out-of-tree module taints kernel.
test_3_init
succeeded register char device: test_3_dev
Major number = 248, minor number = 0
/mnt # 
/mnt # mknod /dev/test_3_dev c 248 0
/mnt # ./test_3_app 
test_3_open: major=248, minor=0
test_3_read 
test_3_release 
/mnt # 
/mnt # rmmod  test_3.ko 
test_3_exit
/mnt # 

在这里插入图片描述

4.2.3 查看设备情况

  • 注意,此处尚未使用mknod手动创建设备文件
(1) insmod ko
/mnt # insmod test_3.ko 
test_3: loading out-of-tree module taints kernel.
test_3_init
succeeded register char device: test_3_dev
Major number = 248, minor number = 0(2)查看 /dev 可见无目标设备(3)查看 /proc/devices ,则创建了test_3_dev
/dev # cat /proc/devices 
Character devices:1 mem2 pty3 ttyp4 /dev/vc/04 tty5 /dev/tty5 /dev/console
// …… 省略 ……
248 test_3_dev
// …… 省略 ……Block devices:
// …… 省略 ……
/dev # 

五、篇尾

略……
用这个简单的实验, 再次检验了内核实验四,NFS环境的高效。

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
JAVA多线程知识整理 Java多线程基础 线程的创建和启动 继承Thread类来创建并启动 自定义Thread类的子类&#...
【洛谷 P1090】[NOIP... [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G ...
国民技术LPUART介绍 低功耗通用异步接收器(LPUART) 简介 低功耗通用异步收发器...
城乡供水一体化平台-助力乡村振... 城乡供水一体化管理系统建设方案 城乡供水一体化管理系统是运用云计算、大数据等信息化手段࿰...
程序的循环结构和random库...   第三个参数就是步长     引入文件时记得指明字符格式,否则读入不了 ...
中国版ChatGPT在哪些方面... 目录 一、中国巨大的市场需求 二、中国企业加速创新 三、中国的人工智能发展 四、企业愿景的推进 五、...
报名开启 | 共赴一场 Flu... 2023 年 1 月 25 日,Flutter Forward 大会在肯尼亚首都内罗毕...
汇编00-MASM 和 Vis... Qt源码解析 索引 汇编逆向--- MASM 和 Visual Studio入门 前提知识ÿ...
【简陋Web应用3】实现人脸比... 文章目录🍉 前情提要🌷 效果演示🥝 实现过程1. u...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
windows安装JDK步骤 一、 下载JDK安装包 下载地址:https://www.oracle.com/jav...
分治法实现合并排序(归并排序)... 🎊【数据结构与算法】专题正在持续更新中,各种数据结构的创建原理与运用✨...
在linux上安装配置node... 目录前言1,关于nodejs2,配置环境变量3,总结 前言...
Linux学习之端口、网络协议... 端口:设备与外界通讯交流的出口 网络协议:   网络协议是指计算机通信网...
Linux内核进程管理并发同步... 并发同步并发 是指在某一时间段内能够处理多个任务的能力,而 并行 是指同一时间能够处理...
opencv学习-HOG LO... 目录1. HOG(Histogram of Oriented Gradients,方向梯度直方图)1...
EEG微状态的功能意义 导读大脑的瞬时全局功能状态反映在其电场结构上。聚类分析方法一致地提取了四种头表面脑电场结构ÿ...
【Unity 手写PBR】Bu... 写在前面 前期积累: GAMES101作业7提高-实现微表面模型你需要了解的知识 【技...