QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 991|回复: 2

内核模块问题集!

[复制链接]
发表于 2003-5-25 16:20:17 | 显示全部楼层 |阅读模式
这两天看了内核模块编程相关, 对于前面的讨论,还是有很多不明白的问题。我根据自己的疑惑都列出来,请大家一起回答,我自己也找答案。

模块编程基本要点参考http://www.linuxfans.org/nuke/mo ... ewtopic&t=23579
http://www.pantek.com/library/linux/HOWTO/Module-HOWTO.html

在开机时,自动加载内核模块的实现?
关键文件/boot/module-info,这通常是个软连接,指向内核版本对应的自动加载模块列表,比如:
[root@RHL24 boot]# ll module-info
lrwxrwxrwx 1 root  root           21 May 16 10:26 module-info -> module-info-2.4.18-14
而具体模块存于/lib/modules/2.4.18-14/kernel/目录下,当然你的版本是2.4.20,则将2.4.28-14改为2.4.20。
对于标准模块的编译加载参考:http://www.linuxfans.org/nuke/mo ... ewtopic&t=13928

疑问:
a. 谁能给一个自动加载自己编译的helloworld.o,要修改的相关文件过程?
b. 不知道/etc/modules.conf是什么用途?
c. 以pcnet32为例,内核怎么知道在/lib/modules/2.4.18-14/kernel/drivers/net下找pcnet32.o?
d. 我通过cat /proc/modules查看目前内核模块:(以ext3为例)
ext3                   70368   3
但是,我并没有在module-info中找到相关定义,这是为什么?
e. dragonfly以前解释内核按需(as need)自动加载模块,我还是不明白按需具体应该怎么理解?内核根据什么判断是需要的?
f. 假设模块是一个设备驱动,内核在什么地方通过什么函数加载该设备驱动?
g. 在http://www.pantek.com/library/linux/HOWTO/Module-HOWTO.html中,提及自动加载模块“You can cause an LKM to be loaded automatically when the kernel first needs it. You do this with either a kerneld daemon or with the more recent invention, the kernel module loader, which is part of Linux”,很奇怪,我的RH8,既没有kerneld命令,也没有man。这需要单独安装?

先写这么多吧,大家有什么问题也可以贴。争取能够把模块编程的概念性,可以实际操作的基本问题都搞明白,最后我来整理成一个FAQ,方便后来的初学者!
发表于 2003-5-26 00:45:07 | 显示全部楼层
0) in fact, i believe that module-info is only an info file for human read, not for kernel module load/unload/managemnet purpose. i never change it under redhat and i even do not have it under my gentoo. i am really busy now. so if i have time, i will install slackware, mdk, debian, bsd and see the difference.

a) for rh, u need modify the /etc/rc.sysinit, grep the modprobe in it to see how write a small piece of shell script in it to autoload what u need. for gentoo, u only need add u module name in /etc/modules.autoload; and need config under /etc/module.d

b) it is the key file for loading module. for example, my module.conf has such a line: alias sound-slot-0 es1371. u may have alias sound-slot-0 xxx as well. so for kernel, if someone requires a sound service, and kernel finds its operation pointers are null. then kernel will try to load it. the first problem need to be solve is to load which module, what kernel do is to load sound-slot-x, but whether sound-slot-x is es1371 or xxx is decided by reading this file. and some contents under /etc/module.d if u have. then the second problem is how to decide which es1371 u need load from /lib/modules/2.4.18-14 or 2.4.18-10... this is also u problem c

c) this is decided by modprobe code, i forget the detail, but basically it is decided by current loaded kernel version. there are some doc on it.

d) that is the module size and reference count, it is maintained in a kernel data structure, and this /proc read is served by
269 static int modules_read_proc(char *page, char **start, off_t off,
270                                  int count, int *eof, void *data)
271 {
272         int len = get_module_list(page);
273         return proc_calc_metrics(page, start, off, count, eof, len);
274 }
in fs/proc/proc_misc.c. and actual work is finished in get_module_list in kernel/module.c
回复

使用道具 举报

发表于 2003-5-26 01:21:49 | 显示全部楼层
e) check the code piece in soundcore_open in drivers/sound/sound_core.c as an example:
a judge here
499         if (!new_fops) {
to see if there are chr ops for this dev. if soundcore is linked into kernel by choose Y when config it, this is not NULL, then kernel can go ahead to use the fun pointers. but if not linked into kernel, then kernel use following code to load it.
510                 sprintf(mod, "sound-slot-%i", unit>>4);
511                 request_module(mod);
512                 sprintf(mod, "sound-service-%i-%i", unit>>4, chain);
513                 request_module(mod);
so key point here is to use request_module to load module. but how kernel do this. basically request_module will create a user space context, a thread, to exec the modprobe xxx. then the left work is still finished by modprobe. pls check the qjfx to know more about this.

f) if u can understand e, then this is easy to answer, u can check those global blk&chr arrays (see ldd2 book for detail). if the ops there is null, then u can sprintf a "char-major-xx" like string and call request_module to load it. that is why we can find "alias char-major-81     bttv" like string in module.conf. if u need 81th chr dev, u check the array, if null, u request it, then left work is finished by modprobe using these config files.

g) the auto load is like what i describled. kerneld is an old one i believe, u need not it anymore. read Documentation/kmod.txt for more info.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-6-16 06:17 , Processed in 0.090145 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表