1.應用層code
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#define DATA_NUM (64)
int main() {
int fd, i;
int r_len, w_len;
fd_set fdset;
char buf[DATA_NUM] = "hello world";
memset(buf, 0, DATA_NUM);
fd = open("/dev/hello", O_RDWR);
printf("%d\n", fd);
if (fd == -1) {
printf("open file error\n");
return -1;
}
else {
printf("open success\n");
}
w_len = write(fd, buf, DATA_NUM);
r_len = read(fd, buf, DATA_NUM);
printf("w_len %d r_len %d\n", w_len, r_len);
return 0;
}
2.設備文件
代碼中的open的“/dev/hello”是個設備文件
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
ls: 無法訪問'/dev/hello': 沒有那個文件或目錄
現(xiàn)在還沒有創(chuàng)建這個設備文件
大部分驅動可以自動創(chuàng)建設備文件
也可以手動的創(chuàng)建設備文件
可以使用mknod命令創(chuàng)建設備文件
mknod [OPTION]... NAME TYPE [MAJOR MINOR]
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo mknod /dev/hello c 232 0
[sudo] zhaoxr 的密碼:
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
crw-r--r-- 1 root root 232, 0 4月 24 22:39 /dev/hello
查看驅動 lsmod
lsmod查看的是insmod的.ko文件
這個.ko文件的名字來自makefile文件的驅動名稱
cat /proc/devices
查看列出字符和塊設備的主設備號,以及分配到這些設備號的設備名稱。
這個設備名稱來自register_chrdev_region的第三個參數(shù)
3.應用層程序調用驅動過程
3.1 編寫驅動helloDev.c
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/slab.h>
#define BUFFER_MAX (10)
#define OK (0)
#define ERROR (-1)
struct cdev *gDev = NULL;
struct file_operations *gFile = NULL;
dev_t devNum;
unsigned int subDevNum = 1;
int reg_major= 232;
int reg_minor = 0;
char *buffer = NULL;
int flag = 0;
int hello_open(struct inode *p, struct file *f) {
printk(KERN_EMERG "hello_open\r\n");
return 0;
}
ssize_t hello_write(struct file *f, const char __user *u, size_t s, loff_t *l) {
printk(KERN_EMERG "hello_write\r\n");
return 0;
}
ssize_t hello_read(struct file *f, char __user *u, size_t s, loff_t *l) {
printk(KERN_EMERG "hello_read\r\n");
return 0;
}
int hello_init(void) {
devNum = MKDEV(reg_major, reg_minor);
if (OK == register_chrdev_region(devNum, subDevNum, "helloworld")) {
printk(KERN_EMERG "register_chrdev_region ok\n");
}
else {
printk(KERN_EMERG "register_chrdev_region error\n");
return ERROR;
}
printk(KERN_EMERG "hello driver init\n");
gDev = kzalloc(sizeof(struct cdev), GFP_KERNEL);
gFile = kzalloc(sizeof(struct file_operations), GFP_KERNEL);
gFile->open = hello_open;
gFile->read = hello_read;
gFile->write = hello_write;
gFile->owner = THIS_MODULE;
cdev_init(gDev, gFile);
cdev_add(gDev, devNum, 3);
return 0;
}
void __exit hello_exit(void) {
cdev_del(gDev);
unregister_chrdev_region(devNum, subDevNum);
return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
3.2 編寫驅動的makefile
ifneq ($(KERNELRELEASE),)
obj-m := helloDev.o
else
PWD := $(shell pwd)
$(info "$(PWD)")
KDIR := /lib/modules/$(shell uname -r)/build
$(info "$(KDIR)")
all:
make -C $(KDIR) M=$(PWD)
clean:
rm -rf *.o *.ko *.mod.c *.symvers *.c~ *~ *.mod *.order
endif
3.3 編寫應用層程序main.c
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#define DATA_NUM (64)
int main() {
int fd, i;
int r_len, w_len;
fd_set fdset;
char buf[DATA_NUM] = "hello world";
memset(buf, 0, DATA_NUM);
fd = open("/dev/hello", O_RDWR);
printf("%d\n", fd);
if (fd == -1) {
printf("open file error\n");
return -1;
}
else {
printf("open success\n");
}
w_len = write(fd, buf, DATA_NUM);
r_len = read(fd, buf, DATA_NUM);
printf("w_len %d r_len %d\n", w_len, r_len);
return 0;
}
3.4 編寫應用層makefile
out : main.o
gcc -o out main.o
main.o : main.c
gcc -c main.c
clean :
rm -rf *.o out
3.5編譯驅動和加載驅動
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ make
"/home/zhaoxr/xiangrui.zhao/linux_kernel"
"/lib/modules/5.15.0-69-generic/build"
make -C /lib/modules/5.15.0-69-generic/build M=/home/zhaoxr/xiangrui.zhao/linux_kernel
make[1]: 進入目錄“/usr/src/linux-headers-5.15.0-69-generic”
CC [M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.o
MODPOST /home/zhaoxr/xiangrui.zhao/linux_kernel/Module.symvers
CC [M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.mod.o
LD [M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.ko
BTF [M] /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.ko
Skipping BTF generation for /home/zhaoxr/xiangrui.zhao/linux_kernel/helloDev.ko due to unavailability of vmlinux
make[1]: 離開目錄“/usr/src/linux-headers-5.15.0-69-generic”
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls
helloDev.c helloDev.ko helloDev.mod helloDev.mod.c helloDev.mod.o helloDev.o Makefile modules.order Module.symvers user
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo insmod helloDev.ko
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22:47:40 ...
kernel:[169201.833551] register_chrdev_region ok
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22:47:40 ...
kernel:[169201.833556] hello driver init
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ lsmod
Module Size Used by
helloDev 16384 0
rfcomm 81920 4
ccm 20480 6
3.6 創(chuàng)建驅動對應的設備文件
利用主次設備號,創(chuàng)建驅動對應的設備文件,并設置權限,以方便應用層訪問
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo mknod /dev/hello c 232 0
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
crw-r--r-- 1 root root 232, 0 4月 25 22:52 /dev/hello
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ sudo chmod 777 /dev/hello
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel$ ls -lh /dev/hello
crwxrwxrwx 1 root root 232, 0 4月 25 22:52 /dev/hello
3.7 編譯應用層程序并執(zhí)行
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$ make
gcc -c main.c
main.c: In function ‘main’:
main.c:28:13: warning: implicit declaration of function ‘write’; did you mean ‘fwrite’? [-Wimplicit-function-declaration]
28 | w_len = write(fd, buf, DATA_NUM);
| ^~~~~
| fwrite
main.c:29:13: warning: implicit declaration of function ‘read’; did you mean ‘fread’? [-Wimplicit-function-declaration]
29 | r_len = read(fd, buf, DATA_NUM);
| ^~~~
| fread
gcc -o out main.o
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$ ls
main.c main.o Makefile out
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$ ./out
3
open success
w_len 0 r_len 0
zhaoxr@zhaoxr-ThinkPad-E450:~/xiangrui.zhao/linux_kernel/user$
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22:54:55 ...
kernel:[169637.294848] hello_open
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22:54:55 ...
kernel:[169637.295049] hello_write
Message from syslogd@zhaoxr-ThinkPad-E450 at Apr 25 22:54:55 ...
kernel:[169637.295056] hello_read
4.write的調用過程
應用層調用write,首先會調用C庫函數(shù),然后通過系統(tǒng)調用進入到內核里面。
內核中關于write的系統(tǒng)調用
在linux源代碼的fs目錄下有read_write.c文件
首先是SYSCALL_DEFINE
SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
size_t, count)
{
struct fd f = fdget_pos(fd);
ssize_t ret = -EBADF;
if (f.file) {
loff_t pos = file_pos_read(f.file);
ret = vfs_write(f.file, buf, count, &pos);
if (ret >= 0)
file_pos_write(f.file, pos);
fdput_pos(f);
}
return ret;
}
然后是vfs_write
ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
{
ssize_t ret;
if (!(file->f_mode & FMODE_WRITE))
return -EBADF;
if (!(file->f_mode & FMODE_CAN_WRITE))
return -EINVAL;
if (unlikely(!access_ok(VERIFY_READ, buf, count)))
return -EFAULT;
ret = rw_verify_area(WRITE, file, pos, count);
if (!ret) {
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
file_start_write(file);
ret = __vfs_write(file, buf, count, pos);
if (ret > 0) {
fsnotify_modify(file);
add_wchar(current, ret);
}
inc_syscw(current);
file_end_write(file);
}
return ret;
}
然后是__vfs_write
ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
loff_t *pos)
{
if (file->f_op->write)
return file->f_op->write(file, p, count, pos);
else if (file->f_op->write_iter)
return new_sync_write(file, p, count, pos);
else
return -EINVAL;
}
由于fd對應的file->f_op->write存在,因此會調用到我們自己驅動編寫的hello_write
文章來源地址http://www.zghlxwxcb.cn/news/detail-426422.html
文章來源:http://www.zghlxwxcb.cn/news/detail-426422.html
到了這里,關于linux內核開發(fā)第6講:應用層的write怎么樣調用到驅動里的write的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!