(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]#
- (LDD3实践)Chapter-2:建立和运行模块
- (LDD3读书记录)Chapter-2:建立和运行模块
- LDD3学习笔记(2):建立和运行模块
- LDD3 读书笔记 之 第 2 章 建立和运行模块
- LDD3读书笔记(第2章 建立和运行模块)
- LDD3读书笔记--建立和运行模块
- 2建立和运行模块
- (LDD3实践)Chapter-3:字符驱动
- Linux device drives chapter 2(构造和运行模块)
- LDD3笔记:第二章 构造和运行模块
- 建立和运行一个简单的模块
- ldd3第二章-----建立内核源码树,编译模块
- Linux设备驱动程序代码 第2章 建立和运行模块
- 《LINUX设备驱动程序》第2章(建立和运行模块)学习笔记
- 1_建立和运行一个简单的模块
- ldd3 读书笔记 —— scull 2 运行
- LDD3 hello world 模块
- LDD3 sleepy 模块
- PostgreSQL数据库压力测试工具pgbench简单应用
- [OpenGL]混合 Blend
- 窗口之间传递数据
- cocos2d-x的三种label:CCLabelTTF,CCLabelAtlas,CClabelBMFont
- client 无密码登陆 server
- (LDD3实践)Chapter-2:建立和运行模块
- Java中Error和Exception的区别
- 贴个自定义dialog中显示ExpandableListView 的代码
- 在莫奈姆瓦夏寻找爱的出口
- 查看修改Oracle数据库字符集
- TabActivity底部菜单栏
- linux daemon进程为什么要fork 2次?
- Gallery 3D+倒影 滑动切换图片示例
- 第7章:函数