linux 核心模組中的宏

來源:互聯網
上載者:User

Author-------Dansen-----xzd2734@163.com

編寫模組時一般都需要包含標頭檔<linux/module.h>,而在module.h中定義了符號__module_kernel_version
static const char __module_kernel_version[] __attribute__((section(".modinfo"))) =
"kernel_version=" UTS_RELEASE;
#define UTS_RELEASE "2.4.18-rmk7-pxa1"  //include/linux/version.h
符號__module_kernel_version在編譯後被放在.o檔案的.modinfo段,insmod使用它檢查模組和當前核心版本是否匹配.
看到《linux裝置驅動程式2》中所說的如果把模組代碼分為兩塊,則需要在另一個檔案包含module.h前定義__NO_VERSION__ 使得不再重複定義__module_kernel_version,不過在我的原始碼裡並沒什麼用,在我的module.h並沒有這種條件包含,而且編譯也不會出錯,只是用objdump工具察看時,確實在.modinfo出現了兩個__module_kernel_version符號。objdump -s hello.o

下面是一個Makefile檔案

CC = /opt/host/armv4l/bin/armv4l-unknown-linux-gcc
LD = /opt/host/armv4l/bin/armv4l-unknown-linux-ld
CFLAGS = -D__KERNEL__ -DMODULE -I/HHARM2410-R3/kernel/include -Wall -Wstrict-prototypes -Wno-trigraphs -Os -mapcs -fno-strict-aliasing -fno-common -fno-common -pipe -mapcs-32 -march=armv4 -mtune=arm9tdmi -mshort-load-bytes -msoft-float -I/opt/host/armv4l/src/linux/include

all: hello.o
hello.o: start.o stop.o
 $(LD) -r $^ -o $@

.PHONY: clean
clean:
 -rm -f *.o

當然是我的ARM平台的,注意CFLAGS,-D__KERNEL__ -DMODULE就相當於在每個檔案中define了__KERNEL__和MODULE這兩個宏,還要用-I包含核心源碼的include目錄.

另外來分析一下模組經常用到的一些宏
第一個是 THIS_MODULE,一開始就知道是直向模組自身,但是還是深入研究下吧
在module.h中尋找THIS_MODULE的定義
#if defined(MODULE) && !defined(__GENKSYMS__)
extern struct module __this_module;
#define THIS_MODULE (&__this_module)
#else
#ifndef __GENKSYMS__
#define THIS_MODULE  NULL
#endif
#endif
一般不會定義__GENKSYMS__,而編寫模組都會定義MODULE,
所以THIS_MODULE是一個struct module類型的指標。
我們把模組編譯成一個.o目標檔案,其實並沒有進行其中的調用進行連結,顯然真正的連結過程發生在我們把模組insmod的核心時,而在insmod時核心會建立一個struct module結構的結構體來包容從使用者空間load進來的模組,在連結的時候就會把這個指標給THIS_MODULE.
下面這些宏就是在.modinfo段定義一個字串,沒什麼可說的。
#define MODULE_AUTHOR(name)         /
const char __module_author[] __attribute__((section(".modinfo"))) =     /
"author=" name

#define MODULE_DESCRIPTION(desc)        /
const char __module_description[] __attribute__((section(".modinfo"))) =   /
"description=" desc

下面幾個宏會比較複雜
#define MOD_INC_USE_COUNT __MOD_INC_USE_COUNT(THIS_MODULE)
#define MOD_DEC_USE_COUNT __MOD_DEC_USE_COUNT(THIS_MODULE)
#define MOD_IN_USE  __MOD_IN_USE(THIS_MODULE)

#define __MOD_INC_USE_COUNT(mod)     /
 (atomic_inc(&(mod)->uc.usecount), (mod)->flags |= MOD_VISITED|MOD_USED_ONCE)
#define __MOD_DEC_USE_COUNT(mod)     /
 (atomic_dec(&(mod)->uc.usecount), (mod)->flags |= MOD_VISITED)
#define __MOD_IN_USE(mod)      /
 (mod_member_present((mod), can_unload) && (mod)->can_unload /
  ? (mod)->can_unload() : atomic_read(&(mod)->uc.usecount))

不過理解了THIS_MODULE看起來就是比較簡單了,顯然struct module中有對模組使用進行計數的一個變數,我們可以編寫簡單的模組來列印當前模組結構變數的值
 union
 {
  atomic_t usecount;
  long pad;
 } uc; 
MOD_INC_USE_COUNT就是讓usecount加1,MOD_DEC_USE_COUNT讓usecount減1
MOD_IN_USE挺複雜,想繼續理解就自己繼續學習吧。。。編寫簡單的模組測試比較eazy吧 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.