一、基本理论
二、基本数据结构和实例
三 、总结
一、基本理论
虽然基本的linux内核是一个大的Image档,也可以当做是一个bin档,可执行文件。但是内核是模块化组成的,它允许在内核运行时向其中动态的插入或删除代码。
二、基本数据结构和实例
以下是module_init和module_exit宏函数
/* Each module must use one module_init(). */
#define module_init(initfn) \
static inline initcall_t __inittest(void) \
{ return initfn; } \
int init_module(void) __attribute__((alias(#initfn)));
/* This is only required if you want to be unloadable. */
#define module_exit(exitfn) \
static inline exitcall_t __exittest(void) \
{ return exitfn; } \
void cleanup_module(void) __attribute__((alias(#exitfn)));
这个是基本的内核或驱动的书籍都会提到的入门demo。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/*
* hello_init ---初始化函数,当模块装载时被调用,如果成功返回 0,否则返回非0值
*
*/
static int __init hello_init(void){
printk(KERN_ALERT"hello kernel.\n");
return 0;
}
/*
* hello_exit ---退出函数,当模块卸载时被调用
*
*/
static void __exit hello_exit(void){
printk(KERN_ALERT"exit kernel.\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("Dual BSD/GPL");
ifneq ($(KERNELRELEASE),)
obj-m :=hello.o
else
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
PWD :=$(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
rm -rf *.o hello.mod.c Module* module*
下面是测试的结果
三 、总结
.......