据我所知,这个站点还没有关于如何编写可加载内核模块的信息(尽管有很多人询问如何做到这一点)。 经过一番查找,我发现 modules-2.0.0.tar.gz 包中的 insmod/modules.doc 和 insmod/HOWTO-modularize 文件相当好地描述了编写内核模块时需要做的一些事情。 该包中的 insmod/drv_hello.c 和 insmod/Makefile 文件提供了一个可以作为模块构建的字符设备驱动程序示例。 如果这些文件(或相关内容)可以在某个时候被纳入 KHG 就太好了。 总而言之,看起来模块应该使用以下编译器选项构建(至少,这是 drv_hello.o 的 Makefile 的方式)
-O6 -pipe -fomit-frame-pointer -Wall -DMODULE -D__KERNEL__ -DLINUX
在以上选项中,关键参数似乎是-DMODULE -D__KERNEL__以及可能的-O6实际上是需要的。 如果你想使用版本控制支持构建模块,请添加以下选项
-DMODVERSIONS -include /usr/include/linux/modversions.h
从示例和文档来看,模块应该采用以下形式
#include /* must be first! */
#include /* optional? */
#include /* ? */
/* More includes and the body of the driver go here. */
/* Note that any time a function returns a resource to the kernel
* (for example, after a open),
* call the MOD_INC_USE_COUNT; macro. Whenever the
* kernel releases the resource, call MOD_DEC_USE_COUNT;.
* This prevents the module from getting removed
* while other parts of the kernel still have
* references to its resources.
*/
#ifdef MODULE /* section containing module-specific code */
int
init_module(void)
{
/* Module initialization code.
* Registers drivers, symbols, handlers, etc. */
return 0; /* on success */
}
void
cleanup_module(void)
{
/* Do cleanup and unregister anything that was
* registered in init_module. */
}
#endif
再次强调,有关更详细的信息,请参阅 modules-2.0.0 包(以及可能还有更新的 modutils-2.1.x 包)中分散的文档。 |