(LDD3实践)Chapter-2:建立和运行模块

来源:互联网 发布:辽宁体育频道网络电视 编辑:程序博客网 时间:2024/04/29 12:11

X86-PC:

环境是:
[root@zx chap2_hello]# uname -aLinux zx 2.6.27.5-117.fc10.i686 #1 SMP Tue Nov 18 12:19:59 EST 2008 i686 i686 i386 GNU/Linux

源码(hello.c):

#include <linux/init.h>#include <linux/module.h>#include <linux/moduleparam.h> MODULE_LICENSE("Dual BSD/GPL");/* 初始化值 */static char *whom = "world";   static int howmany = 1;   module_param(howmany, int, S_IRUGO);   module_param(whom, charp, S_IRUGO);static __init int hello_init(void){int tmp = 0;for (; tmp < howmany; tmp++){printk(KERN_ALERT"Hello, %s\n", whom);}return 0;}static __exit void hello_exit(void){printk(KERN_ALERT"Goodbye, cruel world\n");}module_init(hello_init);module_exit(hello_exit);
Makefile:

# To build modules outside of the kernel tree, we run "make"# in the kernel source tree; the Makefile these then includes this# Makefile once again.# This conditional selects whether we are being included from the# kernel Makefile or not.ifeq ($(KERNELRELEASE),)    # Assume the source tree is where the running kernel was built    # You should set KERNELDIR in the environment if it's elsewhere    KERNELDIR ?= /lib/modules/$(shell uname -r)/build    #KERNELDIR ?= /share/my_kernel/linux-2.6.30.4    # The current directory is passed to sub-makes as argument    PWD := $(shell pwd)modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesmodules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.bak.PHONY: modules modules_install cleanelse    # called from kernel build system: just declare what our modules are    obj-m := hello.oendif
运行结果:

正如LDD3所述:“如果你从一个终端模拟器或者在窗口系统中运行insmod 和 rmmod, 你不会在你的屏幕上看到任何东西. 消息进入了其中一个系统日志文件中, 例如 /var/log/messages”,所以此处使用dmesg命令查看内核消息:

[root@zx chap2_hello]# dmesg -c[root@zx chap2_hello]# make cleanrm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.bak[root@zx chap2_hello]# makemake -C /lib/modules/2.6.27.5-117.fc10.i686/build M=/share/LDD3_code/my_module/chap2_hello modulesmake[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'  CC [M]  /share/LDD3_code/my_module/chap2_hello/hello.o  Building modules, stage 2.  MODPOST 1 modules  CC      /share/LDD3_code/my_module/chap2_hello/hello.mod.o  LD [M]  /share/LDD3_code/my_module/chap2_hello/hello.komake[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'[root@zx chap2_hello]# insmod hello.ko[root@zx chap2_hello]# rmmod hello[root@zx chap2_hello]# dmesg -cHello, worldGoodbye, cruel world[root@zx chap2_hello]# insmod hello.ko howmany=10 whom="Mom"[root@zx chap2_hello]# rmmod hello[root@zx chap2_hello]# dmesg -cHello, MomHello, MomHello, MomHello, MomHello, MomHello, MomHello, MomHello, MomHello, MomHello, MomGoodbye, cruel world[root@zx chap2_hello]# 
lsmod命令:

[root@zx chap2_hello]# insmod hello.ko [root@zx chap2_hello]# lsmodModule                  Size  Used byhello                   5760  0 nls_utf8                5632  1 
/proc/modules & /sys/module

[root@zx chap2_hello]# cat /proc/modules hello 5760 0 - Live 0xe095a000nls_utf8 5632 1 - Live 0xe095d000EmbedSky_irq.c  hello.ko  hello.mod.o  Makefile  Module.markers  Module.symvers[root@zx chap2_hello]# ls /sys/module/8250             bridge        hello   

ARM9-TQ2440:

环境是:
[root@EmbedSky /modules_test]# uname -aLinux EmbedSky 2.6.30.4-EmbedSky #8 Mon May 31 10:57:52 CST 2010 armv4tl GNU/Linux
源码和之前的一样,而Makefile则需要改变:
(关于这个Makefile参考了http://www.cnblogs.com/xmphoenix/archive/2011/10/25/2224237.html)
#hello_makefile ARMobj-m :=arm_hello.oKRNELDIR :=/share/my_kernel/linux-2.6.30.4-tq2440CROSS_COMPILE =arm-linux-CC :=$(CROSS_COMPILE)gccLD :=$(CROSS_COMPILE)ldPWD :=$(shell pwd)all:make -C $(KRNELDIR) M=$(PWD) modules  .PHONY :cleanclean:rm -rf *.o *ko
测试结果:
[root@EmbedSky /]# cd modules_test/[root@EmbedSky /modules_test]# ls[root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# rx arm_hello.koCCStarting xmodem transfer.  Press Ctrl+C to cancel.Transferring arm_hello.ko...  100%       3 KB    0 KB/s 00:00:04       0 Errorsroot@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# lsarm_hello.ko[root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# chmod 777 ./*[root@EmbedSky /modules_test]# insmod arm_hello.ko Hello, world[root@EmbedSky /modules_test]# rmmod arm_hello    Goodbye, cruel world[root@EmbedSky /modules_test]# rmmod arm_hello howmany=10rmmod: can't unload 'arm_hello': unknown symbol in module, or unknown parameter[root@EmbedSky /modules_test]# insmod arm_hello howmany=10insmod: can't insert 'arm_hello': No such file or directory[root@EmbedSky /modules_test]# insmod arm_hello.ko howmany=10Hello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, worldHello, world[root@EmbedSky /modules_test]# rmmod arm_helloGoodbye, cruel world[root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]# [root@EmbedSky /modules_test]#