没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Porting device drivers to the 2.6 kernel
Porting device drivers to the 2.6
kernel
说明:本文档内容来源于 lwn.net 上的 Linux2.6 驱动移植系列文章, 由晏渭川收集整理,请
勿用做任何商业用途。该文档比较适合用做参考手册,在 lwn.net 上还有针对于每个章节的
Comment & Reply , 该文档中没有收录这些 Comment & Reply,如有需要可到 lwn.net 上查
阅。你可以通过 [email protected] 和晏渭川联系。
1
Porting device drivers to the 2.6 kernel
The 2.6 kernel contains a long list of changes which affect device driver writers. As part of the
task of porting the
Linux Device Drivers sample code to 2.6, your humble LWN Kernel Page
author is producing a set of articles describing the changes which must be made. The articles are
Kernel Page as they are written; they will also be collected here. With luck, this page will be a
useful reference for those who must port drivers to the new kernel.
The creation of these articles is funded by LWN.net subscribers. If you find this material useful,
please consider
subscribing to LWN to help ensure that more of it gets written.
Except when otherwise specified, all of the articles below are written by LWN editor Jonathan
Corbet. The date and kernel version attached to each article notes when the article was last
updated.
2
Porting device drivers to the 2.6 kernel
1. Getting started
1.1 Driver porting: hello world
Porting ‘hello world’ (February, 2003); which covers the changes required to update the simplest
possible module to the 2.5 kernel.
Your editor is currently in the middle of porting the example source from
Linux Device Drivers,
Second Edition
to the 2.5 kernel. This work is, of course, just the beginning of the rather larger job
of updating the whole book. This article is the first in what will, hopefully, be a series describing
what is required to make this code work again. The series will thus, with luck, be useful as a guide
to how to port drivers to the new kernel API.
The obvious place to start in this sort of exercise, of course, is the classic "hello world" program,
which, in this context, is implemented as a kernel module. The 2.4 version of this module looked
like:
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "Hello, world\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye cruel world\n");
}
One would not expect that something this simple and useless would require much in the way of
changes, but, in fact, this module will not quite work in a 2.5 kernel. So what do we have to do to
fix it up?
The first change is relatively insignificant; the first line:
#define MODULE
is no longer necessary, since the kernel build system (which you really should use now, see the
next article) defines it for you.
The biggest problem with this module, however, is that you have to explicitly declare your
initialization and cleanup functions with
module_init and module_exit, which are found in
<linux/init.h>. You really should have done that for 2.4 as well, but you could get away without it
as long as you used the names
init_module and cleanup_module. You can still sort of get
away with it (though you may have to ignore some compiler warnings), but the new module code
broke this way of doing things once, and could do so again. It's really time to bite the bullet and do
3
Porting device drivers to the 2.6 kernel
things right.
With these changes, "hello world" now looks like:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
This module will now work - the "Hello, world" message shows up in the system log file. What
also shows up there, however, is a message reading "hello: module license 'unspecified' taints
kernel." "Tainting" of the kernel is (usually) a way of indicating that a proprietary module has
been inserted, which is not really the case here. What's missing is a declaration of the license used
by the module:
MODULE_LICENSE("Dual BSD/GPL");
MODULE_LICENSE is not exactly new; it was added to the 2.4.10 kernel. Some older code may
still lack
MODULE_LICENSE calls, however. They are worth adding; in addition to avoiding the
"taints kernel" message, a license declaration gives your module access to GPL-only kernel
symbols. Assuming, of course, that the module is GPL-licensed.
With these changes, "hello world" works as desired. At least, once you have succeeded in building
it properly; that is the subject of the next article.
1.2 Driver porting: compiling external modules
Compiling external modules (November, 2003; 2.6.0-test9); how to build modules with the new
module loader and kernel build scheme.
The 2.5 development series saw extensive changes to the kernel build mechanism and the
complete replacement of the module loading code. One result of these changes is that compiling
loadable modules has gotten a bit more complicated. In the 2.4 days, a makefile for an external
module could be put together in just about any old way; typically a form like the following was
used:
4
Porting device drivers to the 2.6 kernel
KERNELDIR = /usr/src/linux
CFLAGS = -D__KERNEL__ -DMODULE -I$(KERNELDIR)/include -O
all: module.o
Real-world makefiles, of course, tended to be a bit more complicated, but the job of creating a
loadable module was handled in a single, simple compilation step. All you really needed was a
handy set of kernel headers to compile against.
With the 2.6 kernel, you still need those headers. You also, however, need a configured kernel
source tree and a set of makefile rules describing how modules are built. There's a few reasons for
this:
The new module loader needs to have some extra symbols defined at compilation time. Among
other things, it needs to have the
KBUILD_BASENAME and KBUILD_MODNAME symbols
defined.
All loadable modules now need to go through a linking step - even those which are built from a
single source file. The link brings in
init/vermagic.o from the kernel source tree; this object
creates a special section in the loadable module describing the environment in which it was built.
It includes the compiler version used, whether the kernel was built for SMP, whether kernel
preemption is enabled, the architecture which was compiled for, and, of course, the kernel version.
A difference in any of these parameters can render a module incompatible with a given running
kernel; rather than fail in mysterious ways, the new module loader opts to detect these
compatibilities and refuse to load the module.
As of this writing (2.5.59), the "vermagic" scheme is fallible in that it assumes a match between
the kernel's
vermagic.o file and the way the module is being built. That will normally be the
case, but people who change compiler versions or perform some sort of compilation trickery could
get burned.
The new symbol versioning scheme ("modversions") requires a separate post-compile processing
step and yet another linkable object to hold the symbol checksums.
One could certainly, with some effort, write a new, standalone makefile which would handle the
above issues. But that solution, along with being a pain, is also brittle; as soon as the module build
process changes again, the makefile will break. Eventually that process will stabilize, but, for a
while, further changes are almost guaranteed.
So, now that you are convinced that you want to use the kernel build system for external modules,
how is that to be done? The first step is to learn how kernel makefiles work in general;
makefiles.txt from a recent kernel's Documentation/kbuild directory is recommended reading.
The makefile magic needed for a simple kernel module is minimal, however. In fact, for a
single-file module, a single-line makefile will suffice:
obj-m := module.o
(where
module is replaced with the actual name of the resulting module, of course). The kernel
build system, on seeing that declaration, will compile
module.o from module.c, link it with
vermagic.o, and leave the result in module.ko, which can then be loaded into the kernel.
A multi-file module is almost as easy:
obj-m := module.o
module-objs := file1.o file2.o
In this case,
file1.c and file2.c will be compiled, then linked into module.ko.
5
剩余97页未读,继续阅读
资源评论
gyj0754
- 粉丝: 9
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
- 092300108.cpp
- 车类型数据集6000张VOC+YOLO格式.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功