當?shù)谝粋€穴居的原始人程序員在墻上鑿出第一個“洞穴計算機”的程序時,那是一個打印出用羚羊角上的圖案表示的“Hello world”的程序。羅馬編程教科書上是以“Salut, Mundi”的程序開始的。我不知道如果人們打破這個傳統(tǒng)后會有什么后果,但我認為還是不要去發(fā)現(xiàn)這個后果比較安全。
一個內(nèi)核模塊至少包括兩個函數(shù):init_module,在這個模塊插入內(nèi)核時調(diào)用;cleanup_module,在模塊被移出時調(diào)用。典型情況下,init_module為內(nèi)核中的某些東西注冊一個句柄,或者把內(nèi)核中的程序提換成它自己的代碼(通常是進行一些工作以后再調(diào)用原來工作的代碼)。Clean_module模塊要求撤銷init_module進行的所有處理工作,使得模塊可以被安全的卸載。
/* hello.c
* Copyright (C) 1998 by Ori Pomerantz
*
* "Hello, world" - the kernel module version.
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h> /* We‘re doing kernel work */
#include <linux/module.h> /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* can‘t be loaded */
return 0;
}
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
一個內(nèi)核模塊不是一個可以獨立執(zhí)行的文件,而是需要在運行時刻連接入內(nèi)核的目標文件。所以,它們需要用-c選項進行編譯。而且,所有的內(nèi)核模塊都必須包含特定的標志:
l __KERNEL__——這個標志告訴頭文件此代碼將在內(nèi)核模塊中運行,而不是作為用戶進程。
l MODULE——這個標志告訴頭文件要給出適當?shù)膬?nèi)核模塊的定義。
l LINUX——從技術(shù)上講,這個標志不是必要的。但是,如果你希望寫一個比較正規(guī)的內(nèi)核模塊,在多個操作系統(tǒng)上編譯,這個標志將會使你感到方便。它可以允許你在獨立于操作系統(tǒng)的部分進行常規(guī)的編譯。
還有其它的一些可被選擇包含標志,取決于編譯模塊是的選項。如果你不能明確內(nèi)核怎樣被編譯,可以在in/usr/include/linux/config.h中查到。
l __SMP__——對稱多線程。在內(nèi)核被編譯成支持對稱多線程(盡管在一臺處理機上運行)是必須定義。如果是這樣,還需要做一些別的事情(參見第12章)。
l CONFIG_MODVERSIONS——如果CONFIG_MODVERSIONS被激活,你需要在編譯是定義它并且包含文件/usr/include/linux/modversions.h。這可以有代碼自動完成。
ex Makefile
# Makefile for a basic kernel module
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX
hello.o: hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
echo insmod hello.o to turn it on
echo rmmod hello to turn if off
echo
echo X and kernel programming do not mix.
echo Do the insmod and rmmod from outside
所以,并不是剩下的事情就是root(你沒有把它編譯成root,而是在邊緣(注1.1)。對嗎?),然后就在你的核心內(nèi)容里插入或移出hello。當你這樣做的時候,要注意到你的新模塊在/proc/modules里。
而且,編譯文件不推薦從X下插入的原因是內(nèi)核有一條需要用printk打印的消息,它把它送給了控制臺。如果你不使用X,它就送到了你使用的虛擬終端(你用Alt-F<n>選擇的哪個)并且你可以看到。相反的,如果你使用了X,就有兩種可能性。如果用xterm –C打開了一個控制臺,輸出將被送到哪里。如果沒有,輸出將被送到虛擬終端7——被X“覆蓋”的那個。
如果你的內(nèi)核變得不穩(wěn)定,你可以在沒有X的情況下得到調(diào)試消息。在X外,printk可以直接從內(nèi)核中輸出到控制臺。而如果在X里,printk輸出到一個用戶態(tài)的進程(xterm –C)。當進程接收到CPU時間,它會將其送到X服務器進程。然后,當X服務器進程接收到CPU時間,它將會顯示,但是一個不穩(wěn)定的內(nèi)核意味著系統(tǒng)將會崩潰或重起,所以你不希望顯示錯誤的消息,然后可能被解釋給你什么發(fā)生了錯誤,但是超出了正確的時間。
有些時候在幾個源文件之間分出一個內(nèi)核模塊是很有意義的。在這種情況下,你需要做下面的事情:
1. 在除了一個以外的所有源文件中,增加一行#define __NO_VERSION__。這是很重要的,因為module.h一般包括kernel_version的定義,這是一個全局變量,包含模塊編譯的內(nèi)核版本。如果你需要version.h,你需要把自己把它包含進去,因為如果有__NO_VERSION__的話module.h不會自動包含。
2. 象通常一樣編譯源文件。
3. 把所有目標文件聯(lián)編成一個。在X86下,用ld –m elf_i386 –r –o <name of module>.o <1st source file>
這里給出一個這樣的內(nèi)核模塊的例子。
ex start.c
/* start.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - the kernel module version.
* This file includes just the start routine
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h> /* We‘re doing kernel work */
#include <linux/module.h> /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Initialize the module */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* can‘t be loaded */
return 0;
}
ex stop.c
/* stop.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - the kernel module version. This
* file includes just the stop routine.
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h> /* We‘re doing kernel work */
#define __NO_VERSION__ /* This isn‘t "the" file
* of the kernel module */
#include <linux/module.h> /* Specifically, a module */
#include <linux/version.h> /* Not included by
* module.h because
* of the __NO_VERSION__ */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
ex Makefile
# Makefile for a multifile kernel module
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX
hello.o: start.o stop.o
ld -m elf_i386 -r -o hello.o start.o stop.o
start.o: start.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c start.c
stop.o: stop.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c stop.c