標籤:
一直都在耿耿於懷,這個THIS_MODULE到底是個什麼玩意,linux核心中無處不在的東西。今天上網搜了一下,算是基本明白了。網上牛人寫的已經比較詳細,另外目前暫時沒有時間往更深層次分析,所以直接貼過來得了。。。
轉帖網址:
http://blog.csdn.net/a954423389/archive/2010/12/27/6101369.aspx
源碼位置:
@ kernel/module.c
@ include/linux/module.h
結構體struct module在核心中代表一個核心模組,通過insmod(實際執行init_module系統調用)把自己編寫的核心模組插入核心時,模組便與一個 struct module結構體相關聯,並成為核心的一部分。下面是結構體struct module的完整定義,接下來會逐個解釋:
struct module
{
enum module_state state;
struct list_head list;
char name[MODULE_NAME_LEN];
struct module_kobject mkobj;
struct module_param_attrs *param_attrs;
const char *version;
const char *srcversion;
const struct kernel_symbol *syms;
unsigned int num_syms;
const unsigned long *crcs;
const struct kernel_symbol *gpl_syms;
unsigned int num_gpl_syms;
const unsigned long *gpl_crcs;
unsigned int num_exentries;
const struct exception_table_entry *extable;
int (*init)(void);
void *module_init;
void *module_core;
unsigned long init_size, core_size;
unsigned long init_text_size, core_text_size;
struct mod_arch_specific arch;
int unsafe;
int license_gplok;
#ifdef CONFIG_MODULE_UNLOAD
struct module_ref ref[NR_CPUS];
struct list_head modules_which_use_me;
struct task_struct *waiter;
void (*exit)(void);
#endif
#ifdef CONFIG_KALLSYMS
Elf_Sym *symtab;
unsigned long num_symtab;
char *strtab;
struct module_sect_attrs *sect_attrs;
#endif
void *percpu;
char *args;
};
我們插入一個核心模組,一般會使用工具insmod,該工具實際上調用了系統調用init_module,在該系統調用函數中,首先調用 load_module,把使用者空間傳入的整個核心模組檔案建立成一個核心模組,返回一個struct module結構體。核心中便以這個結構體代表這個核心模組。
state是模組當前的狀態。它是一個枚舉型變數,可取的值為:MODULE_STATE_LIVE,MODULE_STATE_COMING,MODULE_STATE_GOING。分別表示模組當前正常使用中(存活狀態),模組當前正在被載入,模組當前正在被卸載。load_module函數中完成模組的部分建立工作後,把狀態置為MODULE_STATE_COMING,sys_init_module函數中完成模組的全部初始化工作後(包括把模組加入全域的模組列表,調用模組本身的初始化函數),把模組狀態置為MODULE_STATE_LIVE,最後,使用rmmod工具卸載模組時,會調用系統調用delete_module,會把模組的狀態置為MODULE_STATE_GOING。這是模組內部維護的一個狀態。
list是作為一個列表的成員,所有的核心模組都被維護在一個全域鏈表中,鏈表頭是一個全域變數struct module *modules。任何一個新建立的模組,都會被加入到這個鏈表的頭部,通過modules->next即可引用到。
name是模組的名字,一般會拿模組檔案的檔案名稱作為模組名。它是這個模組的一個標識。
另外,還要介紹一下宏THIS_MODULE,它的定義如下是#define THIS_MODULE (&__this_module),__this_module是一個struct module變數,代表當前模組,跟current有幾分相似。可以通過THIS_MODULE宏來引用模組的struct module結構,試試下面的模組:
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
unsigned int cpu = get_cpu();
struct module *mod;
printk(KERN_ALERT "this module: %p==%p/n", &__this_module, THIS_MODULE );
printk(KERN_ALERT "module state: %d/n", THIS_MODULE->state );
printk(KERN_ALERT "module name: %s/n", THIS_MODULE->name );
list_for_each_entry(mod, *(&THIS_MODULE->list.prev), list )
printk(KERN_ALERT "module name: %s/n", mod->name );
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "module state: %d/n", THIS_MODULE->state );
}
module_init(hello_init);
module_exit(hello_exit);
owner是一個struct module *類型的結構體指標,現在告訴你的是每個struct module結構體在核心裡都代表了一個核心模組,就像十七大裡的每個代表都代表了一批人,至於代表了什麼人,選他們的人才知道,同樣,每個struct module結構體代表了什麼模組,對它進行初始化的模組才知道。當然,初始化這個結構不是寫驅動的人該做的事,是在剛才略過的那個從insmod或 modprobe到你驅動的xxx_init函數的曲折過程中做的事。insmod命令執行後,會調用kernel/module.c裡的一個系統調用init_module,它會調用load_module函數,將使用者空間傳入的整個核心模組檔案建立成一個核心模組,並返回一個struct module結構體,從此,核心中便以這個結構體代表這個核心模組。
再看看THIS_MODULE宏是什麼意思,它在include/linux/module.h裡的定義是
85 #define THIS_MODULE (&__this_module)
是一個struct module變數,代表當前模組,與那個著名的current有幾分相似,可以通過THIS_MODULE宏來引用模組的struct module結構,比如使用THIS_MODULE->state可以獲得當前模組的狀態。現在你應該明白為啥在那個歲月裡,你需要毫不猶豫毫不遲疑的將struct usb_driver結構裡的owner設定為THIS_MODULE了吧,這個owner指標指向的就是你的模組自己。那現在owner咋就說沒就沒了那?這個說來可就話長了,咱就長話短說吧。不知道那個時候你有沒有忘記過初始化owner,反正是很多人都會忘記,大家都把注意力集中到probe、 disconnect等等需要動腦子的角色上面了,這個不需要動腦子,只需要花個幾秒鐘指定一下的owner反倒常常被忽視,這個就是容易得到的往往不去珍惜,不容易得到的往往日日思量著去爭取。於是在2006年的春節前夕,在咱們都無心工作無心學習等著過春節的時候,Greg堅守一線,去掉了 owner,於是千千萬萬個寫usb驅動的人再也不用去時刻謹記初始化owner了。咱們是不用設定owner了,可core裡不能不設定,struct usb_driver結構裡不是沒有owner了麼,可它裡面嵌的那個struct device_driver結構裡還有啊,設定了它就可以了。於是Greg同時又增加了usb_register_driver()這麼一層,usb_register()可以通過將參數指定為THIS_MODULE去調用它,所有的事情都挪到它裡面去做。反正usb_register() 也是內聯的,並不會增加調用的開銷。
其餘相關網址,可供參考:
http://hi.baidu.com/_huangshuijing/blog/item/253f9a9516183f0c7bf480c5.html
http://www.embedu.org/Column/Column92.htm
以下均針對於核心2.6.18
在module.h 中 THIS_MODULE的定義如下:
extern struct module __this_module;#define THIS_MODULE (&__this_module)
即是儲存了__this_module這個對象的地址,那這個__this_module在哪裡定義呢?這就要從module的編譯說起啦,如果編譯過模組就會發現,會產生*.mod.c這樣的一個檔案,開啟這個檔案,就會發現,類似下面的定義:
struct module __this_module__attribute__((section(".gnu.linkonce.this_module"))) = { .name = KBUILD_MODNAME, .init = init_module,#ifdef CONFIG_MODULE_UNLOAD .exit = cleanup_module,#endif};
這個檔案是調用modpost產生的,modpost的main中有這樣一段代碼:
for (mod = modules; mod; mod = mod->next) { if (mod->skip) continue; buf.pos = 0; add_header(&buf, mod); add_versions(&buf, mod); add_depends(&buf, mod, modules); add_moddevtable(&buf, mod); add_srcversion(&buf, mod); sprintf(fname, "%s.mod.c", mod->name); write_if_changed(&buf, fname); }
其中的add_header就偷偷添加了__this_module 的定義
static void add_header(struct buffer *b, struct module *mod){ buf_printf(b, "#include <linux/module.h>\n"); buf_printf(b, "#include <linux/vermagic.h>\n"); buf_printf(b, "#include <linux/compiler.h>\n"); buf_printf(b, "\n"); buf_printf(b, "MODULE_INFO(vermagic, VERMAGIC_STRING);\n"); buf_printf(b, "\n"); buf_printf(b, "struct module __this_module\n"); buf_printf(b, "__attribute__((section(\".gnu.linkonce.this_module\"))) = {\n"); buf_printf(b, " .name = KBUILD_MODNAME,\n"); if (mod->has_init) buf_printf(b, " .init = init_module,\n"); if (mod->has_cleanup) buf_printf(b, "#ifdef CONFIG_MODULE_UNLOAD\n" " .exit = cleanup_module,\n" "#endif\n"); buf_printf(b, "};\n");}
Linux中THIS_MODULE宏定義詳解