Linux模块编程方法总结

一、编写一个基本的内核模块

1、编辑源文件,代码hello.c

#include <linux/init.h>

#include <linux/kernel.h>

#include <linux/module.h>

 

int init_hello_module(void)

{

       printk("init_hello_module\n");

 

       return0;

}

 

void exit_hello_module(void)

{

       printk("exit_hello_module\n");

}

 

 

MODULE_LICENSE("GPL");

module_init(init_hello_module);

module_exit(exit_hello_module);

 

Makefile文件

ifeq ($(KERNELRELEASE),)

$(info 1st)

all:

       make-C /lib/modules/$(shell uname -r)/build M=$(shell pwd)

clean:

       rm*.o *.ko *.mod.c modules.order

else

$(info 2nd)

obj-m:=hello.o

endif

 

2.编译

make

3. 测试sudo insmod hello.ko、sudo rmmod hello、dmesg

 

3. 原理:

 

Makefile原理

1、当用户在shell输入make 命令,会先在当前目录下找Makefile

2、解析的第一条语句是 ifeq ($(KERNELRELEASE),) 判断当前的Makefile中是否有KERNELRELEASE 变量因为当前Makefile中没有KERNELRELEASE,所以ifeq 条件成立

3、执行第一条规则语句 all: make -C path/to/kernel M=$(shell pwd) 调用了make命令,根据 -C 指定的内核顶层源码路径,去解析内核顶层源码对应的 Makefile

4、内核顶层源码makefile中根据 M=$(shell pwd)参数第二次进入当前目录,解析当前 Makefile,这时,因为是从内核顶层源码的Makefile 进入的,而KERNELRELEASE 在内核顶层源码Makefile已经定义,所以这一次ifeq条件不成立,进入 else分支

5、这时内核Makefile检查编译模块所依赖的.o文件(hello.o)是否存在,如果不存在,根据hello.c生成hello.o

6、内核Makefile会第三次进入当前目录,还是走else 分支,这一次因为hello.o已经存在会根据 hello.o生成hello.ko

 

module_init原理:https://www.cnblogs.com/chaozhu/p/6410271.html,补充

 

二、多个文件生成一个模块

在同一目录下增加show.c文件

#include <linux/kernel.h>

 

static int a;

int show_addr(void)

{

       printk("aaddr = %p\n",&a);

 

       return0;

}

 

Makefile改为

 

ifeq ($(KERNELRELEASE),)

$(info 1st)

all:

       make-C /lib/modules/$(shell uname -r)/build M=$(shell pwd)

clean:

       rm*.o *.ko *.mod.c modules.order

else

$(info 2nd)

obj-m:=helloshow.o

helloshow-y+=hello.o

helloshow-y+=show.o

endif

 

三、多个模块之间的函数调用

Show.c改为如下:

#include <linux/init.h>

#include <linux/kernel.h>

#include <linux/module.h>

 

static int a;

int show_addr(void)

{

       printk("aaddr = %p\n",&a);

 

       return0;

}

 

int init_show_module(void)

{

       printk("init_show_module\n");

       show_addr();

 

       return0;

}

 

void exit_show_module(void)

{

       printk("exit_show_module\n");

}

 

EXPORT_SYMBOL(show_addr);

MODULE_LICENSE("GPL");

module_init(init_show_module);

module_exit(exit_show_module);

Makefile改为如下:

ifeq ($(KERNELRELEASE),)

$(info 1st)

all:

       make-C /lib/modules/$(shell uname -r)/build M=$(shell pwd)

clean:

       rm*.o *.ko *.mod.c modules.order

else

$(info 2nd)

obj-m:=hello.o show.o

endif

 

四、模块传递参数

1. 在hello.c中增加模块参数定义

#include <linux/init.h>

#include <linux/kernel.h>

#include <linux/module.h>

 

static int count = 5;

static int a = 100;

static int b = 100;

static char *string = "helloworld";

 

int init_hello_module(void)

{

       printk("init_hello_module\n");

       for(;count-->0;)

       {

              printk("init:stringis %s\n",string);

       }

      

       return0;

}

 

void exit_hello_module(void)

{

       printk("exit_hello_module\n");

       for(;count-->0;)

       {

              printk("exit:stringis %s\n",string);

       }

}

 

 

MODULE_LICENSE("GPL");

module_param(count, int, 0644);

module_param(a, int, 0644);

module_param(b, int, 0644);

module_param(string, charp, 0644);

module_init(init_hello_module);

module_exit(exit_hello_module);

 

2. 编译测试模块参数传递

方法1,在加载模块的时候修改参数值

方法2,通过sysfs文件系统的节点修改模块参数值


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值