<p>通过《手把手教Linux驱动1-模块化编程》的学习,我们已经掌握了如何向内核加载一个模块,现在我们学习模块之间如何传递参数和如何导出模块的符号。</p><p><strong>给模块传递参数</strong></p><p>当我们加载一个模块到Linux内核的时候,Linux内核允许向这个模块传递一些参数。这样设计的好处就是,让我们的模块操作起来更灵活,我们可以通过给它传递不同的参数来完成不同的功能。</p><p/><p>例如:我们写一个模块程序,来完成硬件中断的操作。在Linux操作系统中,每个中断都有一个中断号。如果我们在模块里面将中断号写死,那我们的模块只能响应特定的中断了。如果我们把中断号作为参数传递给我们的模块,那么我们的模块就可以完成对不同的中断进行操作。</p><p>
</p><p>那怎么向模块传递参数呢?很简单,Linux内核都给我们做好了,我们只需调用相应的接口就可以了。</p><p>
</p><p>在模块里面,声明一个变量(全局变量),用来接收用户加载模块时传递的参数</p><p>文件位置如下:</p><pre>linux-3.14\include\linux Moduleparam.h</pre><p><strong>函数原型:</strong></p><pre>module_param(name,type,perm)
参数:
@name用来接收参数的变量名
@type参数的数据类型
bool :布尔类型
invbool:颠倒了值的bool类型;
charp :字符指针类型,内存为用户提供的字符串分配;
int :整型
long :长整型
short :短整型
uint :无符号整型
ulong :无符号长整型
ushort :无符号短整型
@perm 指定参数访问权限。</pre><p>
</p><p> 每个模块的参数,最后都会表现在sysfs文件系统中,也就是说最后会在系统/sys/module/模块名字/parameters/路径下看到以参数名命名的文件。</p><p> 这个文件的权限就是这里指定的权限。如果perm的值为0,则在sysfs文件系统中不会生成参数对应的文件。</p><p>
</p><p><strong>典型使用案列:</strong></p><p>(1)我们可以在模块(test.ko)里面写如下代码,接收用户加载模块时传递的参数</p><pre>static unsigned int var=0;
module_param(var,uint,0400);</pre><p>在加载模块的时候,传递参数:</p><pre>insmod test.ko var=100</pre><p>最后模块里面的全局变量var的值就为100了。</p><p>
</p><p>(2)如果我要传递一个字符串到模块里面,该怎么操作呢?</p><pre>static char *string;
module_param(string,charp,0400);</pre><p>在加载模块的时候,传递参数:</p><pre>insmod test.ko string="yikoulinux";</pre><p>有人可能会问,这段代码是不是有bug,因为你的string指针是一个野指针。</p><p>其实内核会自动给用户传递的字符串分配空间的,然后用string指针保存字符串所在内存的首地址。</p><p>(3)让模块内部变量的名字和加载模块时传递的参数名不同</p><p><strong>函数原型:</strong></p><pre>module_param_named(name_out,name_in,type,perm);
参数:
@name_out在加载模块时,参数的名字
@name_in模块内部变量的名字
@type 参数类型
@perm 访问权限</pre><p>
</p><p>典型使用案列:</p><pre>static int var =0;
module_param_named(var_out,var,int,0400);</pre><p>在加载模块的时候,传递参数:</p><pre>insmod test.ko var_out=100</pre><p>var_out就是模块变量var在外部的名字,此时var的值为100 </p><p>(4)加载模块的时候,传递字符串到模块的一个全局字符数组里面</p><p>函数原型:</p><pre>module_param_string(name,string,len,perm);
参数:
@name在加载模块时,参数的名字
@string模块内部字符数组的名字
@len 模块内部字符数组的大小
@perm 访问权限</pre><p>
</p><p><strong>典型使用案列:</strong></p><pre>static int buffer[LEN];
module_param_string(buffer_out,buffer,LEN,0400);</pre><p>在加载模块的时候,传递参数:</p><pre>insmod test.ko buffer_out="hello word"</pre><p>加载模块的时候,内核直接把"hello word"字符串口拷贝到buffer数组中。</p><p>(5)加载模块的时候,传递参数到模块的数组中</p><p>函数原型:</p><pre>module_param_arry(name,type,num_point,perm);
参数:
@name 模块的数组名,也是外部指定的参数名
@type模块数组的数据类型
@num_point用来获取用户在加载模块时传递的参数个数,NULL:不关心用户传递的参数个数
@perm 访问权限</pre><p>
</p><p>典型使用案列:</p><pre>static int my_arry[3];
int num;
module_param_arry(my_arry,int,&num,0400);</pre><p>在加载模块的时候,传递参数:(多个参数以”,“隔开)</p><pre>insmod test.ko my_arr=1,2,3</pre><p>注意:以上接口在调用的时候,perm指定的权限不能让普通用户具有写权限,否则编译会报错。 (6)给模块里面每个接收用户参数的变量指定一个描述信息函数原型:</p><pre>MODULE_PARM_DESC(name,describe);
参数
@name 变量名
@describe描述信息的字符串</pre><p>
</p><p><strong>案例演示</strong></p><p>说了这么多,下面我们一起来实现一个完整的代码吧:</p><p>
</p><pre><code>#include <linux/init.h></code><code>#include <linux/module.h></code><code>#include <linux/moduleparam.h></code><code>
</code><code>static int var1 = 0;</code><code>module_param(var1,int,0644);</code><code>MODULE_PARM_DESC(var1, "Get value from user.\n");</code><code>
</code><code>static int var2 = 0;</code><code>module_param_named(var2_out,var2,int,0644);</code><code>MODULE_PARM_DESC(var2, "Test var2 named var2_out.\n");</code><code>
</code><code>static char *string = NULL;</code><code>module_param(string,charp,0444);</code><code>MODULE_PARM_DESC(string, "Test module param string.\n");</code><code>
</code><code>static char buffer[10];</code><code>module_param_string(buffer,buffer,sizeof(buffer),0644);</code><code>MODULE_PARM_DESC(buffer, "Test module param string.\n");</code><code>
</code><code>static int myarray[3];</code><code>int num;</code><code>module_param_array(myarray,int,&num,0444);</code><code>MODULE_PARM_DESC(myarray, "Test module param array.\n");</code><code>
</code><code>static int __init hello_init(void)</code><code>{</code><code> int i = 0;</code><code> printk("-----------------------------\n");</code><code> printk("var1 : %d\n",var1);</code><code> printk("var2 : %d\n",var2);</code><code> printk("string : %s\n",string);</code><code> printk("buffer : %s\n",buffer);</code><code>
</code><code> for(i=0;i<num;i++)</code><code> {</code><code> printk("myarray[%d] : %d\n",i,myarray[i]);</code><code> }</code><code> printk("-----------------------------\n"); </code><code> return 0;</code><code>}</code><code>static void __exit hello_exit(void)</code><code>{</code><code> printk("hello_exit \n");</code><code> return;</code><code>}</code><code>MODULE_LICENSE("GPL");</code><code>MODULE_AUTHOR("yikoulinux");</code><code>module_init(hello_init);</code><code>module_exit(hello_exit);</code></pre><p>命令如下:
</p><ul><li><p>
</p></li></ul><pre>insmod hello.ko var1=100 var2_out=200 string="yikoulinux" buffer="yikoupeng" myarray=100,200,300</pre><p/><p>测试结果如下:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-a71cfb457dff0795.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>同时在目录/sys/module/hello/parameters下回生成对应的文件节点,文件权限与代码中定义的是一一对应的:
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-39f364aa0dd4043e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>所有节点的内容可以用cat命令查看
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-b4c04df108b21e34.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>切换到管理员模式,可以通过echo命令修改有W权限的文件:
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-e8a15d040689a294.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>[注意]操作之前一口君已经切换到管理员模式。</p><p>
</p><p>
</p><p><strong>模块符号导出</strong></p><p>
</p><p><strong>(1)什么是符号?</strong></p><p> 这里的符号主要指的是全局变量和函数。</p><p/><p><strong>(2)为什么要导出符号?</strong></p><p> Linux内核采用的是以模块化形式管理内核代码。内核中的每个模块相互之间是相互独立的,也就是说A模块的全局变量和函数,B模块是无法访问的。</p><p>有些时候,我们写一些模块代码的时候,发现部分函数功能别人已经实现了,此时我们就想如果我们可以调用他们已经实现好的函数接口就好了。那如何才能做到这点呢?符号导出了,也就是说你可以把你实现的函数接口和全局变量导出,以供其他模块使用。</p><p> 在Linux内核的世界里,如果一个模块已经以静态的方式编译进的内核,那么它导出的符号就会出现在全局的内核符号表中。在Ubuntu 14.04系统中,Linux内核的全局符号表在以下文件中存放:</p><ul><li><p>
</p></li></ul><pre>/usr/src/linux-headers-3.2.0-29-generic-pae/Module.symvers</pre><p/><p>如果打开这个文件,可以发现里面的内容就是:
</p><p>Addr------a符号名------a模块名------a导出符号的宏</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-0c075c0f0c932a83.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p/><p>(3)如何导出符号?
</p><p>Linux内核给我们提供了两个宏:</p><ul><li><p>
</p></li><li><p>
</p></li></ul><pre>EXPORT_SYMBOL(name);EXPORT_SYMBOL_GPL(name);</pre><p>上面宏定义的任一个使得给定的符号在模块外可用.GPL版本的宏定义只能使符号对GPL许可的模块可用.符号必须在模块文件的全局部分输出,在任何函数之外,因为宏定义扩展成一个特殊用途的并被期望是全局存取的变量的声明。</p><p/><p>(4)模块编译时,如何寻找使用的符号?</p><p>a.在本模块中符号表中,寻找符号(函数或变量实现)</p><p>b.在内核全局符号表中寻找</p><p>c.在模块目录下的Module.symvers文件中寻找</p><p>如果在这三个地方都没有找到,则编译保存</p><p>
</p><p><strong>案例演示</strong></p>
<p>本例实现功能:模块A导出全局变量global_var和函数show两个符号供模块B使用。</p><p>模块A代码:</p><pre><code>#include <linux/init.h></code><code>#include <linux/module.h></code><code>
</code><code>static int global_var = 100;</code><code>static void show(void)</code><code>{</code><code> printk("show(): global_var =%d \n",global_var);</code><code>}</code><code>static int hello_init(void)</code><code>{</code><code> printk("module b :global_var=%d\n",global_var);</code><code> return 0;</code><code>}</code><code>static void hello_exit(void)</code><code>{</code><code> printk("hello_exit \n");</code><code> return;</code><code>}</code><code>EXPORT_SYMBOL(global_var);</code><code>EXPORT_SYMBOL(show);</code><code>MODULE_AUTHOR("PENG");</code><code>MODULE_LICENSE("GPL");</code><code>module_init(hello_init);</code><code>module_exit(hello_exit);</code></pre><p>模块B</p><pre><code>#include <linux/init.h></code><code>#include <linux/module.h></code><code>
</code><code>extern int global_var;</code><code>extern void show(void);</code><code>static int hello_init(void)</code><code>{</code><code> printk("module a: global_var= %d\n",global_var);</code><code> show();</code><code> return 0;</code><code>}</code><code>static void hello_exit(void)</code><code>{</code><code> printk("hello_exit \n");</code><code> return;</code><code>}</code><code>MODULE_AUTHOR("PENG");</code><code>MODULE_LICENSE("GPL");</code><code>module_init(hello_init);</code><code>module_exit(hello_exit);</code></pre><p>
</p><p>调试步骤:</p><ol><li><p>编译模块A,然后加载模块A,在模块A编译好后,在它的当前目录会看到一个Module.symvers文件,这里存放的就是我们模块A导出的符号。</p></li><li><p>将模块A编译生成的Module.symvers文件拷贝到模块B目录下,然后编译模块B,加载模块B。</p></li><li><p>通过dmesg查看模块打印的信息。</p></li></ol><p>打印信息如下:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/23850874-41aea2df89ad6c08.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>
</p><p>
</p><p>
</p>