linux内核模块编程.pdf
《linux内核模块编程.pdf》由会员分享,可在线阅读,更多相关《linux内核模块编程.pdf(80页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、The Linux Kernel Module Programming GuidePeter Jay SalzmanMichael BurianOri PomerantzCopyright 2001 Peter Jay Salzman20050526 ver 2.6.1The Linux Kernel Module Programming Guide is a free book;you may reproduce and/or modify it under theterms of the Open Software License,version 1.1.You can obtain a
2、copy of this license athttp:/opensource.org/licenses/osl.php.This book is distributed in the hope it will be useful,but without any warranty,without even the impliedwarranty of merchantability or fitness for a particular purpose.The author encourages wide distribution of this book for personal or co
3、mmercial use,provided the abovecopyright notice remains intact and the method adheres to the provisions of the Open Software License.Insummary,you may copy and distribute this book free of charge or for a profit.No explicit permission isrequired from the author for reproduction of this book in any m
4、edium,physical or electronic.Derivative works and translations of this document must be placed under the Open Software License,and theoriginal copyright notice must remain intact.If you have contributed new material to this book,you mustmake the material and source code available for your revisions.
5、Please make revisions and updates availabledirectly to the document maintainer,Peter Jay Salzman.This will allow for the merging ofupdates and provide consistent revisions to the Linux community.If you publish or distribute this book commercially,donations,royalties,and/or printed copies are greatly
6、appreciated by the author and the Linux Documentation Project(LDP).Contributing in this way shows yoursupport for free software and the LDP.If you have questions or comments,please contact the address above.Table of ContentsForeword.11.Authorship.12.Versioning and Notes.13.Acknowledgements.1Chapter
7、1.Introduction.21.1.What Is A Kernel Module?.21.2.How Do Modules Get Into The Kernel?.21.2.1.Before We Begin.3Chapter 2.Hello World.52.1.Hello,World(part 1):The Simplest Module.52.1.1.Introducing printk().62.2.Compiling Kernel Modules.62.3.Hello World(part 2).72.4.Hello World(part 3):The _init and _
8、exit Macros.82.5.Hello World(part 4):Licensing and Module Documentation.92.6.Passing Command Line Arguments to a Module.112.7.Modules Spanning Multiple Files.142.8.Building modules for a precompiled kernel.15Chapter 3.Preliminaries.173.1.Modules vs Programs.173.1.1.How modules begin and end.173.1.2.
9、Functions available to modules.173.1.3.User Space vs Kernel Space.183.1.4.Name Space.183.1.5.Code space.193.1.6.Device Drivers.19Chapter 4.Character Device Files.214.1.Character Device Drivers.214.1.1.The file_operations Structure.214.1.2.The file structure.224.1.3.Registering A Device.224.1.4.Unreg
10、istering A Device.234.1.5.chardev.c.234.1.6.Writing Modules for Multiple Kernel Versions.26Chapter 5.The/proc File System.285.1.The/proc File System.285.2.Read and Write a/proc File.305.3.Manage/proc file with standard filesystem.33Chapter 6.Using/proc For Input.386.1.TODO:Write a chapter about sysf
11、s.38The Linux Kernel Module Programming GuideiTable of ContentsChapter 7.Talking To Device Files.397.1.Talking to Device Files(writes and IOCTLs).39Chapter 8.System Calls.488.1.System Calls.48Chapter 9.Blocking Processes.539.1.Blocking Processes.53Chapter 10.Replacing Printks.6110.1.Replacing printk
12、.6110.2.Flashing keyboard LEDs.63Chapter 11.Scheduling Tasks.6611.1.Scheduling Tasks.66Chapter 12.Interrupt Handlers.7012.1.Interrupt Handlers.7012.1.1.Interrupt Handlers.7012.1.2.Keyboards on the Intel Architecture.70Chapter 13.Symmetric Multi Processing.7413.1.Symmetrical MultiProcessing.74Chapter
13、 14.Common Pitfalls.7514.1.Common Pitfalls.75Appendix A.Changes:2.0 To 2.2.76A.1.Changes between 2.4 and 2.6.76A.1.1.Changes between 2.4 and 2.6.76Appendix B.Where To Go From Here.77B.1.Where From Here?.77The Linux Kernel Module Programming GuideiiForeword1.AuthorshipThe Linux Kernel Module Programm
14、ing Guide was originally written for the 2.2 kernels by Ori Pomerantz.Eventually,Ori no longer had time to maintain the document.After all,the Linux kernel is a fast movingtarget.Peter Jay Salzman took over maintenance and updated it for the 2.4 kernels.Eventually,Peter nolonger had time to follow d
15、evelopments with the 2.6 kernel,so Michael Burian became a comaintainer toupdate the document for the 2.6 kernels.2.Versioning and NotesThe Linux kernel is a moving target.There has always been a question whether the LKMPG should removedeprecated information or keep it around for historical sake.Mic
16、hael Burian and I decided to create a newbranch of the LKMPG for each new stable kernel version.So version LKMPG 2.4.x will address Linux kernel2.4 and LKMPG 2.6.x will address Linux kernel 2.6.No attempt will be made to archive historicalinformation;a person wishing this information should read the
17、 appropriately versioned LKMPG.The source code and discussions should apply to most architectures,but I cant promise anything.Oneexception is Chapter 12,Interrupt Handlers,which should not work on any architecture except for x86.3.AcknowledgementsThe following people have contributed corrections or
18、good suggestions:Ignacio Martin,David Porter,DanielePaolo Scarpazza,Dimo Velev and Francois AudeonForeword1Chapter 1.Introduction1.1.What Is A Kernel Module?So,you want to write a kernel module.You know C,youve written a few normal programs to run asprocesses,and now you want to get to where the rea
19、l action is,to where a single wild pointer can wipe outyour file system and a core dump means a reboot.What exactly is a kernel module?Modules are pieces of code that can be loaded and unloaded into the kernelupon demand.They extend the functionality of the kernel without the need to reboot the syst
20、em.For example,one type of module is the device driver,which allows the kernel to access hardware connected to the system.Without modules,we would have to build monolithic kernels and add new functionality directly into thekernel image.Besides having larger kernels,this has the disadvantage of requi
21、ring us to rebuild and reboot thekernel every time we want new functionality.1.2.How Do Modules Get Into The Kernel?You can see what modules are already loaded into the kernel by running lsmod,which gets its information byreading the file/proc/modules.How do these modules find their way into the ker
22、nel?When the kernel needs a feature that is not resident inthe kernel,the kernel module daemon kmod1 execs modprobe to load the module in.modprobe is passed astring in one of two forms:A module name like softdog or ppp.A more generic identifier like charmajor1030.If modprobe is handed a generic iden
23、tifier,it first looks for that string in the file/etc/modprobe.conf.2 If it finds an alias line like:alias charmajor1030 softdogit knows that the generic identifier refers to the module softdog.ko.Next,modprobe looks through the file/lib/modules/version/modules.dep,to see if othermodules must be loa
24、ded before the requested module may be loaded.This file is created by depmod a andcontains module dependencies.For example,msdos.ko requires the fat.ko module to be already loadedinto the kernel.The requested module has a dependancy on another module if the other module definessymbols(variables or f
25、unctions)that the requested module uses.Lastly,modprobe uses insmod to first load any prerequisite modules into the kernel,and then the requestedmodule.modprobe directs insmod to/lib/modules/version/3,the standard directory for modules.insmod is intended to be fairly dumb about the location of modul
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- linux 内核 模块 编程
限制150内