




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
内核编程实验:hello.cLinux设备驱动程序 内核编程入门,就以最为简单的hello.c为例。 环境:Redhat 9.0,内核版本2.4.20-8。 虽然现在2.6.x的内核很早就就发布了,但是毕竟很多公司还在使用2.4.x的内核。作为新手,从2.4.x的内核入手是可行的。原因有如下几条: (1)2.4.x比较成熟。可能你遇到的绝大多数问题,网上都有解决方案。在这个过程中,你可以节省大量的时间,同时还可以对比网上的解决方案,加深认识,总结解决问题的方法,调整自己的学习方法和思路。 (2)事物的发展总不可能是一蹴而就的。了解发展的历程,对深入理解问题有很大的好处。所以在2.4.x的内核的基础上学习2.6.x的内核,就能够体会到2.6.x的内核在哪些方面要出色,或者为什么要采取这种改进技术。相信理论清晰了,即时2.6.x的内核也会容易上手。 下面总结了第一个内核程序hello.c的学习过程。(一)第一阶段:尽量简单/* hello.c*/#define MODULE#include int init_module(void)printk(Hello World!n);return 0;void cleanup_module(void)printk(Goodbye!n);执行,出现错误一:rootlqm drivers# gcc -c hello.crootlqm drivers# insmod hello.ohello.o: kernel-module version mismatchhello.o was compiled for kernel version 2.4.20while this kernel is version 2.4.20-8. 这是因为内核源代码版本和编译器版本不一致造成的。(1)编译器版本/usr/include/linux/version.h#define UTS_RELEASE 2.4.20#define LINUX_VERSION_CODE 132116#define KERNEL_VERSION(a,b,c) (a) 16) + (b) 8) + (c)(2)内核源代码版本/usr/src/linux-2.4.20-8/include/linux/version.h/usr/src/linux-2.4.20-8/include/linuxrootlqm linux# cat version.h#include #if defined(_module_smp)#define UTS_RELEASE 2.4.20-8smp#elif defined(_module_BOOT)#define UTS_RELEASE 2.4.20-8BOOT#elif defined(_module_bigmem)#define UTS_RELEASE 2.4.20-8bigmem#else#define UTS_RELEASE 2.4.20-8#endif#define LINUX_VERSION_CODE 132116#define KERNEL_VERSION(a,b,c) (a) 16) + (b) 8) + (c) 可以采取修改编译器版本号与内核源代码版本号一致的办法来解决这个问题,即修改/usr/include/linux/version.h中 #define UTS_RELEASE 2.4.20为 #define UTS_RELEASE 2.4.20-8执行,出现错误二:rootlqm drivers# gcc -c hello.crootlqm drivers# insmod hello.oWarning: loading hello.o will taint the kernel: no licenseSee /lkml/#export-tainted for information about tainted modulesModule hello loaded, with warningsrootlqm drivers# tail -n 1 /var/log/messagesJan 30 12:02:08 lqm kernel: Hello 也就是说出现了no license的警告。GNU的软件需要有GPL,所以修改源代码如下:/* hello.c*/#define MODULE#include MODULE_LICENSE(GPL);int init_module(void)printk(Hello World!n);return 0;void cleanup_module(void)printk(Goodbye!n); 这时没有错误了。写了一个脚本,测试流程自动化:#!/bin/bashgcc -c hello.csleep 1insmod hello.o & echo -e Instal module - hello.onsleep 1tail -n 1 /var/log/messageslsmod | grep hello & echo -e Module hello has instalednrmmod hello & echo -e Remove module - hellonsleep 1tail -n 1 /var/log/messageslsmod | grep hello | echo Module hello has removed执行结果如下:rootlqm hello# ./runInstal module - hello.o Jan 30 13:31:29 lqm kernel: Hello World! hello 748 0 (unused) Module hello has instaled Remove module - hello Jan 30 13:31:30 lqm kernel: Goodbye! Module hello has removed(二)第二阶段:完善,深入一点/* hello.c*/#ifndef _KERNEL_#define _KERNEL_#endif#ifndef MODULE#define MODULE#endif#include /*printk*/#include MODULE_LICENSE(GPL);static int init_module(void)printk(Hello, world!n);return 0;static void cleanup_module(void)printk(Goodbye!n);写Makefile文件如下:# Kernel Programming# Shandong University, Linqingmin# The path of kernel source codeINCLUDEDIR = /usr/src/linux-2.4.20-8/include/# CompilerCC = gcc# OptionsCFLAGS = -D_KERNEL_ -DMODULE -O -Wall -I$(INCLUDEDIR)# TargetOBJS = hello.oall: $(OBJS)$(OBJS): hello.c /usr/include/linux/version.h$(CC) $(CFLAGS) -c $install:insmod $(OBJS)uninstall:rmmod hello.PHONY: cleanclean:rm -f *.o 写Makefile时应该注意,不要用空格来代替。否则会出现错误:missing separator. Stop.修改执行脚本run:#!/bin/bash# The first stepmake & make install & echo -e Instal module - hello.onsleep 1tail -n 1 /var/log/messageslsmod | grep hello & echo -e Module hello has instaledn# The second stepmake uninstall & echo -e Remove module - hellonsleep 1tail -n 1 /var/log/messageslsmod | grep hello | echo Module hello has removed# The last stepmake clean执行结果如下:rootlqm hello# ./rungcc -D_KERNEL_ -DMODULE -O -Wall -I/usr/src/linux-2.4.20-8/include/ -c hello.chello.c:18: warning: init_module defined but not usedhello.c:25: warning: cleanup_module defined but not usedinsmod hello.oInstal module - hello.oJan 31 13:40:23 lqm kernel: Hello, hello 728 0 (unused)Module hello has instaledrmmod helloRemove module - helloJan 31 13:40:24 lqm kernel: Module hello has removedrm -f *.o(三)第三阶段:总结1、一个内核模块至少应该包括两个函数:(1)init_module:模块插入内核时调用(2)cleanup_module:模块移除时调用 这个简单的程序就是只实现了这两个函数,而且只做了打印信息的工作,没有使用价值。典型情况下,init_module为内核中的某些东西注册一个句柄,相当于模块初始化的工作。cleanup_module则是撤销模块前期的处理工作,使模块得以安全卸载。2、insmod实现动态加载模块。在当前OS上,动态加载模块以测试硬件等,避免了繁琐的工作。但是,在这种情况下,会出现版本不匹配的情况。另外,要分清楚内核源代码路径和编译器路径的不同,知道在编译时该指定那个路径。第二阶段开始出现过几个错误都是因为默认的路径是编译器路径,而不是内核源代码路径。体会内核模块化带来的好处!3、应用Make工具来管理项目。即使小,也要训练。在2.4内核和2.6内核下,Makefile的编写会有所不同。只是语法形式的不同,先深入掌握一种,另一种注意一下应该可以避免犯错误。 继续努力!2007/02/01补记/* Kernel Programming: hello.c*/*define _KERNEL_ MODULE*/ #ifndef _KERNEL_#define _KERNEL_#endif#ifndef MODULE#define MODULE#endif#include /*printk*/#include #include /*module_init module_exit*/MODULE_LICENSE(GPL); /*declaration license*/static int _init hello_init(void)printk(Hello, world!n);return 0;static void _exit hello_exit(void)printk(Goodbye!n);module_init(hello_init);module_exit(hello_exit);# Kernel Programming# Shandong University, Linqingmin# The path of kernel source codeKERNELDIR = /usr/src/linux-2.4.20-8# CompilerCC = gcc# OptionsCFLAGS = -D_KERNEL_ -DMODULE -I$(KERNELDIR)/include -O -Wallifdef CONFIG_SMPCFLAGS += -D_SMP_ -DSMPendif# OBJSOBJS = hello.c# TargetTARGET = hello.o$(TARGET): hello.c$(CC) $(CFLAGS) -c $install:insmod $(TARGET)uninstall:rmmod hello.PHONY: cleanclean:rm -f *.o#!/bin/bash# The first stepmake & make install & echo -e Instal module - hello.onsleep 1lsmod | grep hello & echo -e Module hello
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 六一活动便利店活动方案
- 六一活动啦啦队活动方案
- 六一活动室内活动方案
- 六一活动透明伞活动方案
- 六一社区趣味活动方案
- 六一策划活动方案
- 六一舞蹈推广活动方案
- 六一节超市活动方案
- 六一进超市活动方案
- 六一麦当劳活动策划方案
- 湖北省华中师大第一附中2024届物理高二第二学期期末达标检测试题含解析
- 经空气传播疾病医院感染预防与控制规范课件
- 2024年四川广安爱众股份有限公司招聘笔试参考题库含答案解析
- 冠心病合并糖尿病血脂管理
- PDCA循环在我院静脉用药调配中心用药错误管理中的应用静配中心质量持续改进案例
- 精神病患者攻击行为预防
- 《议程设置理论》课件
- 二单元税率利率复习课
- GB/Z 43281-2023即时检验(POCT)设备监督员和操作员指南
- 安全防护及文明施工措施
- 衣柜全屋定制家具施工方案
评论
0/150
提交评论