-
内核模块编译的makefile
https://www.cnblogs.com/downey-blog/p/10486907.html
路径:/lib/modules/$(shell uname -r)/build
M=$(PWD)
PWD说明应该在模块所在目录下执行make。 -
教程
https://blog.csdn.net/yeshennet/article/details/82315604 -
makefile中的 := 和 =
1、“=”
make会将整个makefile展开后,再决定变量的值。也就是说,变量的值将会是整个makefile中最后被指定的值。看例子:
x = foo
y = $(x) bar
x = abc
在上例中,y的值将会是 abc bar ,而不是 foo bar 。
2、“:=”
“:=”表示变量的值决定于它在makefile中的位置,而不是整个makefile展开后的最终值。
x := foo
y := $(x) bar
x := abc
在上例中,y的值将会是 foo bar ,而不是 abc bar 了。 -
obj-m obj-y
obj-m += module.o
obj-y += module.o
obj-m表示编译生成可加载模块
obj-y表示直接将模块编译进内核
定义了要编译的文件,所有的选项,以及到哪些子目录去执行递归操作。 最简单的Kbuild makefile 只包含一行:
例子:
obj-y += foo.o 该例子告诉Kbuild在当前目录里,有一个名为foo.o的目标文件。foo.o将从foo.c或foo.S文件编译得到。
如果foo.o要编译成一模块,那就要用obj-m了。
obj-y += /usr/kernel/ 表示该目录下的对应所有文件生成的 .o 目标文件。
obj-m += module.o
module-y += hello1.o hello2.o
可以一次编译多个模块。利用makefile的自动推导机制
或者
obj-m += hello-1.o
obj-m += hello-2.o
例子:
obj-$(CONFIG_FOO) += foo.o $(CONFIG_FOO)可以为y(编译进内核)
或m(编译成模块)。如果CONFIG_FOO不是y 和m,那么该文件就不会被编译联接了
除了y、m以外的obj-x 形式的目标都不会被编译。
- 模块参数
- __init __exit __initdata宏
- makefile中的组合模块,可以实现一次编译多个模块
obj-m += startstop.o
startstop-objs := start.o stop.o
为组合模块创建一个对象名称startstop,然后我们告诉make什么对象文件是该模块的一部分
- dmesg指令查看printk打印的信息
- 模块入口.
模块始终以init_module或您通过module_init调用指定的函数开头。 这是模块的入口功能; 它告诉内核模块提供了哪些功能,并设置内核以在需要时运行模块的功能。 一旦它执行此操作,入口函数返回,并且模块不执行任何操作,直到内核想要对模块提供的代码执行某些操作。
/ proc / kallsyms 包含内核知道的所有符号,因此它们可以访问模块,因为它们共享内核的代码空间。
设备驱动
11.
3,1
3,2
3,3
3是主要编号,代表驱动程序。1-3是次要编号,用于该驱动程序表示不同的设备。驱动在编写的时候要注册主设备号,即编写者应该明确写的这个驱动是为哪个设备服务的
- 字符设备©和块设备(b)
在/dev下用ls -l查看 - 创建设备文件
mknod /dev/hhh c 12 2
在/dev下创建一个名为hhh的字符设备,其编号为12,次要编号为2. - 注册字符设备
在init_module时调用
int register_chrdev(unsigned int major, const char *name, struct file_operations *fops);
功能:向系统添加驱动程序,注册到内核。
major:主编号,设为0表示分配动态主编号
name:设备名
fops:指向file_operations结构体的指针
返回值:<0失败
若major设为0,则返回值是动态分配的主编号
注册完成后,可以在/proc/devices中查看
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(*aio_read) (struct kiocb *, char __user *, size_t, loff_t);
ssize_t(*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t(*aio_write) (struct kiocb *, const char __user *, size_t,
loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int,
unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, 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(*readv) (struct file *, const struct iovec *, unsigned long,
loff_t *);
ssize_t(*writev) (struct file *, const struct iovec *, unsigned long,
loff_t *);
ssize_t(*sendfile) (struct file *, loff_t *, size_t, read_actor_t,
void __user *);
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);
};
简化版
struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
15.cat /proc/modules
可以看到每个模块的情况,第三个字段表示有多少进程使用该模块
try_module_get(THIS_MODULE)
:增加当前模块使用次数
module_put(THIS_MODULE)
:减少当前模块使用次数
-
取消注册字符设备
int unregister_chrdev(unsigned int major, const char *name )
返回值:<0表示失败 -
struct inode 和 struct file 在 linux/fs.h中定义
-
put_user(x,b)把内核空间的x拷贝到用户空间的b,仅限于int 和char类型。
get_user(x,b)把用户空间的x拷贝到内核空间的b -
在模块init的时候,注册字符设备,参数中的file_operations结构体很关键,因为其中指定了读写释放时应该调用的函数,相关读写释放函数也在该模块文件中定义。
-
MKDEV(), MAJOR, MINOR宏
位于<linux/cdev.h>
MKDEV(int major, int minor)宏用于创建设备编号,传入主编号和次编号。返回dev_t类型的设备编号,dev_t类型就是unsigned int,在<linux/kdev_t.h>
MAJOR宏表示主编号,MINOR宏是次编号
-
devfs_mk_cdev(MKDEV(major,minor),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME )
devfs_mk_cdev(MKDEV(major,minor),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME )
用于创建设备文件。
命令行下的mknod /dev/name c 12 2也用于创建设备文件
- 驱动总体编写框架
static struct file_operations xxx_fops = {
.owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
.open = xxx_open,
.release = xxx_close,
.read = xxx_read,
};
static int __init my_init(void)
{
//注册设备驱动
register_chrdev (unsigned int major, const char *name, struct file_operations *fops);
//创建设备文件
devfs_mk_cdev(MKDEV(BUTTON_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
}
static int __exit my_exit(void)
{
//移除设备文件
devfs_remove( DEVICE_NAME);
//注销设备驱动
unregister_chrdev( unsigned int major, const char *name );
}
module_init( my_init );
module_exit( my_exit );
注意:当程序打开设备文件open("/dev/devname", O_RDONLY)时,会触发xxx_open函数,同时open函数自身还是会执行。
不是说拿自定义的xxx_open替换open
- copy_to_user copy_from_user
#include <linux/uaccess.h>
unsigned long copy_from_user(void *to, const void *from, unsigned long n);
unsigned long copy_to_user(void *to, const void *from, unsigned long n)
成功返回0,否则返回负数
25.源文件
char_dev.c模块源文件
user_read.c源文件
user_write.c源文件
char_dev.c
user_read.c
user_write.c
makefile