驱动名 memdev.c #include <linux/module.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/cdev.h> #include <asm/uaccess.h> int dev1_registers[5]; int dev2_registers[5]; struct cdev cdev; dev_t devno; /*文件打开函数*/ int mem_open(struct inode *inode, struct file *filp) { /*获取次设备号*/ int num = MINOR(inode->i_rdev); if (num==0) filp->private_data = dev1_registers; else if(num == 1) filp->private_data = dev2_registers; else return -ENODEV; //无效的次设备号 return 0; } /*文件释放函数*/ int mem_release(struct inode *inode, struct file *filp) { return 0; } /*读函数*/ static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; int *register_addr = filp->private_data; /*获取设备的寄存器基地址*/ /*判断读位置是否有效*/ if (p >= 5*sizeof(int)) return 0; if (count > 5*sizeof(int) - p) count = 5*sizeof(int) - p; /*读数据到用户空间*/ if (copy_to_user(buf, register_addr+p, count)) { ret = -EFAULT; } else { *ppos += count; ret = count; } return ret; } /*写函数*/ static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; int *register_addr = filp->private_data; /*获取设备的寄存器地址*/ /*分析和获取有效的写长度*/ if (p >= 5*sizeof(int)) return 0; if (count > 5*sizeof(int) - p) count = 5*sizeof(int) - p; /*从用户空间写入数据*/ if (copy_from_user(register_addr + p, buf, count)) ret = -EFAULT; else { *ppos += count; ret = count; } return ret; } /* seek文件定位函数 */ static loff_t mem_llseek(struct file *filp, loff_t offset, int whence) { loff_t newpos; switch(whence) { case SEEK_SET: newpos = offset; break; case SEEK_CUR: newpos = filp->f_pos + offset; break; case SEEK_END: newpos = 5*sizeof(int)-1 + offset; break; default: return -EINVAL; } if ((newpos<0) || (newpos>5*sizeof(int))) return -EINVAL; filp->f_pos = newpos; return newpos; } /*文件操作结构体*/ static const struct file_operations mem_fops = { .llseek = mem_llseek, .read = mem_read, .write = mem_write, .open = mem_open, .release = mem_release, }; /*设备驱动模块加载函数*/ static int memdev_init(void) { /*初始化cdev结构*/ cdev_init(&cdev, &mem_fops); /* 注册字符设备 */ alloc_chrdev_region(&devno, 0, 2, "memdev"); cdev_add(&cdev, devno, 2); } /*模块卸载函数*/ static void memdev_exit(void) { cdev_del(&cdev); /*注销设备*/ unregister_chrdev_region(devno, 2); /*释放设备号*/ } MODULE_LICENSE("GPL"); module_init(memdev_init); module_exit(memdev_exit); Makefile文件: obj-m := memdev.o KDIR := /home/S5-driver/lesson7/TQ210/linux all: make -C $(KDIR) M=$(PWD) modules CROSS_COMPILE=arm-linux- ARCH=arm clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order insmod memdev.ko 应用程序: mem_write.c #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd = 0; int src = 2013; /*打开设备文件*/ fd = open("/dev/memdev0",O_RDWR); /*写入数据*/ write(fd, &src, sizeof(int)); /*关闭设备*/ close(fd); return 0; } arm-linux-readelf -d mem_write.c 检查动态链接库 一般采用静态编译 arm-linux-gcc -static mem_write.c -o mem_write 应用程序: mem_read.c #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd = 0; int dst = 0; /*打开设备文件*/ fd = open("/dev/memdev0",O_RDWR); /*写入数据*/ read(fd, &dst, sizeof(int)); printf("dst is %d\n",dst); /*关闭设备*/ close(fd); return 0; }
/* * NOTE: * all file operations except setlease can be called without * the big kernel lock held in all filesystems. */ struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); 重定位读写指针,响应lseek系统调用 ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); 从设备读取数据,响应read系统调用 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); 向设备写入数据,响应write系统调用 ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); 打开设备,响应open系统调用 int (*flush) (struct file *, fl_owner_t id); int (*release) (struct inode *, struct file *); 关闭设备,响应close系统调用 int (*fsync) (struct file *, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long, struct file_lock **); long (*fallocate)(struct file *file, int mode, loff_t offset, loff_t len); };
struct inode { /* RCU path lookup touches following: */ umode_t i_mode; uid_t i_uid; gid_t i_gid; const struct inode_operations *i_op; struct super_block *i_sb; spinlock_t i_lock; /* i_blocks, i_bytes, maybe i_size */ unsigned int i_flags; struct mutex i_mutex; unsigned long i_state; unsigned long dirtied_when; /* jiffies of first dirtying */ struct hlist_node i_hash; struct list_head i_wb_list; /* backing dev IO list */ struct list_head i_lru; /* inode LRU list */ struct list_head i_sb_list; union { struct list_head i_dentry; struct rcu_head i_rcu; }; unsigned long i_ino; atomic_t i_count; unsigned int i_nlink; dev_t i_rdev; unsigned int i_blkbits; u64 i_version; loff_t i_size; #ifdef __NEED_I_SIZE_ORDERED seqcount_t i_size_seqcount; #endif struct timespec i_atime; struct timespec i_mtime; struct timespec i_ctime; blkcnt_t i_blocks; unsigned short i_bytes; struct rw_semaphore i_alloc_sem; const struct file_operations *i_fop; /* former ->i_op->default_file_ops */ struct file_lock *i_flock; struct address_space *i_mapping; struct address_space i_data; #ifdef CONFIG_QUOTA struct dquot *i_dquot[MAXQUOTAS]; #endif struct list_head i_devices; union { struct pipe_inode_info *i_pipe; struct block_device *i_bdev; struct cdev *i_cdev; }; __u32 i_generation; #ifdef CONFIG_FSNOTIFY __u32 i_fsnotify_mask; /* all events this inode cares about */ struct hlist_head i_fsnotify_marks; #endif #ifdef CONFIG_IMA atomic_t i_readcount; /* struct files open RO */ #endif atomic_t i_writecount; #ifdef CONFIG_SECURITY void *i_security; #endif #ifdef CONFIG_FS_POSIX_ACL struct posix_acl *i_acl; struct posix_acl *i_default_acl; #endif void *i_private; /* fs or device private pointer */ }; struct file { /* * fu_list becomes invalid after file_free is called and queued via * fu_rcuhead for RCU freeing */ union { struct list_head fu_list; struct rcu_head fu_rcuhead; } f_u; struct path f_path; #define f_dentry f_path.dentry #define f_vfsmnt f_path.mnt const struct file_operations *f_op; spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */ #ifdef CONFIG_SMP int f_sb_list_cpu; #endif atomic_long_t f_count; unsigned int f_flags; fmode_t f_mode; loff_t f_pos; struct fown_struct f_owner; const struct cred *f_cred; struct file_ra_state f_ra; u64 f_version; #ifdef CONFIG_SECURITY void *f_security; #endif /* needed for tty driver, and maybe others */ void *private_data; #ifdef CONFIG_EPOLL /* Used by fs/eventpoll.c to link all the hooks to this file */ struct list_head f_ep_links; #endif /* #ifdef CONFIG_EPOLL */ struct address_space *f_mapping; #ifdef CONFIG_DEBUG_WRITECOUNT unsigned long f_mnt_write_state; #endif };
驱动程序和应用程序的桥梁到底怎么建立的?
例如:应用程序: mem_read.c #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int fd = 0; int dst = 0; /*打开设备文件*/ fd = open("/dev/memdev0",O_RDWR); /*写入数据*/ read(fd, &dst, sizeof(int)); printf("dst is %d\n",dst); /*关闭设备*/ close(fd); return 0; } 首先:我们静态编译 arm-linux-gcc -static -g read_mem.c -o read_mem 接着反汇编:arm-linux-objdump -D -S read_mem > tmp read_mem: file format elf32-littlearm Disassembly of section .note.ABI-tag: 000080f4 <.note.ABI-tag>: 80f4: 00000004 .word 0x00000004 80f8: 00000010 .word 0x00000010 80fc: 00000001 .word 0x00000001 8100: 00554e47 .word 0x00554e47 8104: 00000000 .word 0x00000000 8108: 00000002 .word 0x00000002 810c: 00000006 .word 0x00000006 8110: 0000000e .word 0x0000000e Disassembly of section .init: 00008228 <main>: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { 8228: e92d4800 push {fp, lr} 822c: e28db004 add fp, sp, #4 ; 0x4 8230: e24dd008 sub sp, sp, #8 ; 0x8 int fd = 0; 8234: e3a03000 mov r3, #0 ; 0x0 8238: e50b3008 str r3, [fp, #-8] int dst = 0; 823c: e3a03000 mov r3, #0 ; 0x0 8240: e50b300c str r3, [fp, #-12] /*打开设备文件*/ fd = open("/dev/memdev0",O_RDWR); 8244: e59f004c ldr r0, [pc, #76] ; 8298 <main+0x70> 8248: e3a01002 mov r1, #2 ; 0x2 824c: eb0028a3 bl 124e0 <__libc_open> 8250: e1a03000 mov r3, r0 8254: e50b3008 str r3, [fp, #-8] /*写入数据*/ read(fd, &dst, sizeof(int)); 8258: e24b300c sub r3, fp, #12 ; 0xc 825c: e51b0008 ldr r0, [fp, #-8] 8260: e1a01003 mov r1, r3 8264: e3a02004 mov r2, #4 ; 0x4 8268: eb0028c0 bl 12570<libc_read> 参数个数小于4 采用通用寄存器。 r0,r1,r2,r3 00012600 <__libc_read>: 12600: e51fc028 ldr ip, [pc, #-40] ; 125e0 <__libc_close+0x70> 12604: e79fc00c ldr ip, [pc, ip] 12608: e33c0000 teq ip, #0 ; 0x0 1260c: 1a000006 bne 1262c <__libc_read+0x2c> 12610: e1a0c007 mov ip, r7 12614: e3a07003 mov r7, #3 ; 0x3 12618: ef000000 svc 0x00000000 1261c: e1a0700c mov r7, ip 在应用程序中r7 = 3 svc是系统调用指令 ----> 用户空间进入内核空间。 1.入口: 在arch\arm\kernel\entry-common.S 中的vector_swi 2.取NO. 3.查表(依据是NO.) ENTRY(vector_swi) sub sp, sp, #S_FRAME_SIZE stmia sp, {r0 - r12} @ Calling r0 - r12 ARM( add r8, sp, #S_PC ) ARM( stmdb r8, {sp, lr}^ ) @ Calling sp, lr THUMB( mov r8, sp ) THUMB( store_user_sp_lr r8, r10, S_SP ) @ calling sp, lr mrs r8, spsr @ called from non-FIQ mode, so ok. str lr, [sp, #S_PC] @ Save calling PC str r8, [sp, #S_PSR] @ Save CPSR str r0, [sp, #S_OLD_R0] @ Save OLD_R0 zero_fp /* * Get the system call number. */ /* * If the swi argument is zero, this is an EABI call and we do nothing. * * If this is an old ABI call, get the syscall number into scno and * get the old ABI syscall table address. */ bics r10, r10, #0xff000000 eorne scno, r10, #__NR_OABI_SYSCALL_BASE ldrne tbl, =sys_oabi_call_table /* * If the swi argument is zero, this is an EABI call and we do nothing. * * If this is an old ABI call, get the syscall number into scno and * get the old ABI syscall table address. */ bics r10, r10, #0xff000000 eorne scno, r10, #__NR_OABI_SYSCALL_BASE ldrne tbl, =sys_oabi_call_table ENTRY(sys_call_table) #include "calls.S" #undef ABI #undef OBSOLETE /*============================================================================ * Special system call wrappers */ @ r0 = syscall number @ r8 = syscall table sys_syscall: * linux/arch/arm/kernel/calls.S * * Copyright (C) 1995-2005 Russell King * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This file is included thrice in entry-common.S */ /* 0 */ CALL(sys_restart_syscall) CALL(sys_exit) CALL(sys_fork_wrapper) CALL(sys_read) CALL(sys_write) /* 5 */ CALL(sys_open) CALL(sys_close) CALL(sys_ni_syscall) /* was sys_waitpid */ CALL(sys_creat) CALL(sys_link) /* 10 */ CALL(sys_unlink) CALL(sys_execve_wrapper) CALL(sys_chdir) CALL(OBSOLETE(sys_time)) /* used by libc4 */ CALL(sys_mknod) /* 15 */ CALL(sys_chmod) CALL(sys_lchown16) CALL(sys_ni_syscall) /* was sys_break */ CALL(sys_ni_syscall) /* was sys_stat */ CALL(sys_lseek) /* 20 */ CALL(sys_getpid) CALL(sys_mount) CALL(OBSOLETE(sys_oldumount)) /* used by libc4 */ 找到 sys_read 函数。 asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count); SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count) { struct file *file; ssize_t ret = -EBADF; int fput_needed; file = fget_light(fd, &fput_needed); if (file) { loff_t pos = file_pos_read(file); ret = vfs_read(file, buf, count, &pos); file_pos_write(file, pos); fput_light(file, fput_needed); } return ret; } 在read_write.c中 ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { ssize_t ret; if (!(file->f_mode & FMODE_READ)) return -EBADF; if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read)) return -EINVAL; if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) return -EFAULT; ret = rw_verify_area(READ, file, pos, count); if (ret >= 0) { count = ret; if (file->f_op->read) ret = file->f_op->read(file, buf, count, pos); else ret = do_sync_read(file, buf, count, pos); if (ret > 0) { fsnotify_access(file); add_rchar(current, ret); } inc_syscr(current); } return ret; }