开发环境: ubuntu 16.04
一、hello.c源码:
/*
* $Id: hello.c,v 1.5 2004/10/26 03:32:21 corbet Exp $
*/
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
三、hello.c分析:
1、内核需要自己单独打印输出函数,是因为它在运行时不能依赖c库。
2、模块能够调用printk是因为在insmod函数装入模块后,模块就链接到内核了。
3、module_init()与module_exit()宏定义, 声明一个模块的初始化和清理函数.
三、Makefile源码:
KERNELDIR ?= /lib/modules/`uname -r`/build
hello_world-objs := hello.o
obj-m := hello_world.o
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules;
clean:
$(MAKE) -C $(KERNELDIR) SUBDIRS=$(PWD) clean;
rm -f *.ko;
.PHONY: modules modules_install clean
四、Makefile分析:
1、先来分析第一句 :KERNELDIR ?= /lib/modules/
'uname -r’/build
这句是对KERNELDIR进行赋值,这个变量是后面我们用到的指代内核源码目录用的。
uname -r
表示内核版本号(可以在命令行输入看看)
/lib/modules/'uname -r'/build
就是完整的内核库所在路径
2、分析: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules;
这句是Makefile的规则:这里的$(MAKE)就相当于make,-C 选项的作用是指将当前工作目录转移到你所指定的位置。“M=”选项的作用是,当用户需要以某个内核为基础编译一个外部模块的话,需要在make modules 命令中加入“M=dir”,程序会自动到你所指定的dir目录中查找模块源码,将其编译,生成KO文件。
3、分析: .PHONY: modules modules_install clean
这句话是的作用是保证modules,modules_install,clean这三个命令能正常完成。.PHONY 这是一个特殊目标名称,还有其它的,如:
.SUFFIXES,.DEFAULT,.PRECIOUS,.INTERMEDIATE,.SECONDARY,.SECONDEXPANSION,.DELETE_ON_ERROR,.IGNORE .LOW_RESOLUTION_TIME .SILENT .EXPORT_ALL_VARIABLES .NOTPARALLEL
它们的具体用法可以参考GNU手册中的Special Built-in Target Names章节。
.PHONY目标的具体意思是如果在Makefile的工作目录中有名如:modules,modules_install,clean等文件时命令会出错。它是防止这出错的方式。
五、编译:
# make
make -C /lib/modules/`uname -r`/build M=/home/yang/yfwfile/linux_driver/source_code/misc-modules/hello modules;
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-166-generic'
CC [M] /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello.o
LD [M] /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello_world.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello_world.mod.o
LD [M] /home/yang/yfwfile/linux_driver/source_code/misc-modules/hello/hello_world.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-166-generic'
# ls
hello.c hellop.c hello_world.mod.c hello_world.o modules.order
hello.o hello_world.ko hello_world.mod.o Makefile Module.symvers
六、安装模块
insmod、rmmod 安装与卸载驱动的命令
如果没有输出"Hello,world"可能是因为你在终端模拟器下运行的,因为在终端模拟器下时会把内核消息输出到日志文件/var/log/kern.log中。如果在字符终端运行的话就会直接打出Hello,world
使用 tail /var/log/kernel.log查看
在字符终端下(如下图):
能正常看到输出。