linux内核编程(一)

来源:百度文库 编辑:神马文学网 时间:2024/04/26 10:39:14
Linux内核编程(一) 2005.08.04  来自:CSDN
.Hello, world
当第一个穴居的原始人程序员在墙上凿出第一个“洞穴计算机”的程序时,那是一个打印出用羚羊角上的图案表示的“Hello world”的程序。罗马编程教科书上是以“Salut, Mundi”的程序开始的。我不知道如果人们打破这个传统后会有什么后果,但我认为还是不要去发现这个后果比较安全。
一个内核模块至少包括两个函数:init_module,在这个模块插入内核时调用;cleanup_module,在模块被移出时调用。典型情况下,init_module为内核中的某些东西注册一个句柄,或者把内核中的程序提换成它自己的代码(通常是进行一些工作以后再调用原来工作的代码)。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    /* We‘re doing kernel work */
#include    /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#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");}
.1内核模块的编译文件
一个内核模块不是一个可以独立执行的文件,而是需要在运行时刻连接入内核的目标文件。所以,它们需要用-c选项进行编译。而且,所有的内核模块都必须包含特定的标志:
l         __KERNEL__——这个标志告诉头文件此代码将在内核模块中运行,而不是作为用户进程。
l         MODULE——这个标志告诉头文件要给出适当的内核模块的定义。
l         LINUX——从技术上讲,这个标志不是必要的。但是,如果你希望写一个比较正规的内核模块,在多个操作系统上编译,这个标志将会使你感到方便。它可以允许你在独立于操作系统的部分进行常规的编译。
还有其它的一些可被选择包含标志,取决于编译模块是的选项。如果你不能明确内核怎样被编译,可以在in/usr/include/linux/config.h中查到。
l         __SMP__——对称多线程。在内核被编译成支持对称多线程(尽管在一台处理机上运行)是必须定义。如果是这样,还需要做一些别的事情(参见第12章)。
l         CONFIG_MODVERSIONS——如果CONFIG_MODVERSIONS被激活,你需要在编译是定义它并且包含文件/usr/include/linux/modversions.h。这可以有代码自动完成。
ex Makefile
# Makefile for a basic kernel module CC=gccMODCFLAGS := -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)。对吗?),然后就在你的核心内容里插入或移出hello。当你这样做的时候,要注意到你的新模块在/proc/modules里。
而且,编译文件不推荐从X下插入的原因是内核有一条需要用printk打印的消息,它把它送给了控制台。如果你不使用X,它就送到了你使用的虚拟终端(你用Alt-F选择的哪个)并且你可以看到。相反的,如果你使用了X,就有两种可能性。如果用xterm –C打开了一个控制台,输出将被送到哪里。如果没有,输出将被送到虚拟终端7——被X“覆盖”的那个。
如果你的内核变得不稳定,你可以在没有X的情况下得到调试消息。在X外,printk可以直接从内核中输出到控制台。而如果在X里,printk输出到一个用户态的进程(xterm –C)。当进程接收到CPU时间,它会将其送到X服务器进程。然后,当X服务器进程接收到CPU时间,它将会显示,但是一个不稳定的内核意味着系统将会崩溃或重起,所以你不希望显示错误的消息,然后可能被解释给你什么发生了错误,但是超出了正确的时间。
多文件内核模块
有些时候在几个源文件之间分出一个内核模块是很有意义的。在这种情况下,你需要做下面的事情:
1.       在除了一个以外的所有源文件中,增加一行#define __NO_VERSION__。这是很重要的,因为module.h一般包括kernel_version的定义,这是一个全局变量,包含模块编译的内核版本。如果你需要version.h,你需要把自己把它包含进去,因为如果有__NO_VERSION__的话module.h不会自动包含。
2.       象通常一样编译源文件。
3.       把所有目标文件联编成一个。在X86下,用ld –m elf_i386 –r –o .o <1st source file>
这里给出一个这样的内核模块的例子。
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    /* We‘re doing kernel work */
#include    /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#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    /* We‘re doing kernel work */
#define __NO_VERSION__      /* This isn‘t "the" file
* of the kernel module */
#include    /* Specifically, a module */
#include    /* Not included by
* module.h because
* of the __NO_VERSION__ */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include
#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