如何在 Linux 中为我们的 “核弹发射器” 编写一个设备驱动程序?
内核模块:一段可以被内核动态加载执行的代码
struct file_operations
的对象file_operations
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
int (*mmap) (struct file *, struct vm_area_struct *);
unsigned long mmap_supported_flags;
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*flock) (struct file *, int, struct file_lock *);
...
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
unlocked_ioctl
: BKL (Big Kernel Lock) 时代的遗产ioctl
ioctl
执行时默认持有 BKLunlocked_ioctl
避免锁ioctl
从 struct file_operations
中移除compact_ioctl
: 机器字长的兼容性磁盘 (存储设备) 的访问特性
文件系统和磁盘设备之间的接口