linux 模块编程

Download Linux 模块编程

If you can't read please download the document

Upload: patty

Post on 09-Jan-2016

134 views

Category:

Documents


9 download

DESCRIPTION

Linux 模块编程. 主要内容. 1. 2. Linux 内核模块机制介绍. 内核编程. Linux 内核模块机制介绍. 1. 微内核与单内核 Linux 的模块机制. 先来区分进程的两种运行模式: 用户模式 (user mode) 运行在用户空间,实现用户的功能 内核模式 (kernel mode 运行在内核空间,一般是实现系统相关的功能。 进程在自己空间中实现用户功能时,如果想使用系统功能,必须将运行级别从用户态提升到核心态才能访问内核空间,实现相关操作。这个过程是通过系统调用实现的, 而用户态切换到核心态过程不可避免的存在一定开销 。. - PowerPoint PPT Presentation

TRANSCRIPT

  • Linux

  • Linux

  • LinuxLinux

  • (user mode)(kernel mode

  • 1.Micro kernel Monolithic kernelMacro kernel

  • 2.LinuxLinux(Module)

  • Clibclinuxlinux

  • 1.

  • Cstatic

  • API

  • Linux2.4.10MODULE_LICENSEmodule license unspecified taints kernel.

    GPL"Dual BSD/GPL" MODULE_LICENSE("GPL");MODULE_LICENSE("Dual BSD/GPL");

  • Linuxmodule_param(name, type, perm);nametypepermsysfsS_IfooS_IRUGO|S_IWUSR:static char *user_name=username;module_param(user_name,charp,S_IRUGO);insmod module.ko user_name=book_user1

    typebyteshortushortintuintlongulongcharpbool

  • Linux2.4

    Linux2.6

  • modulemodule1module2module

  • printkprintkprintf()1024

    printk DEFAULT_ MESSAGE_LOGLEVEL4console_loglevelinclude / linux / kernel.h

  • #include #include MODULE_LICENSE("Dual BSD/GPL");static int __init mod_init_modtest(void);static void __exit mod_exit_modtest(void);module_init(mod_init_modtest);module_exit(mod_exit_modtest);int sum_op(int numdata);int factorial_op(int N);//int mod_init_modtest(void){ printk(KERN_INFO"--------Module_export_symbol init !---------\n"); return 0;}void mod_exit_modtest(void){ printk(KERN_INFO"-----Module_export_symbol was deleted!-----\n");}//===EXPORT_SYMBOL(sum_op);EXPORT_SYMBOL(factorial_op);MODULE_AUTHOR("book author");MODULE_DESCRIPTION("module1:Module_export_symbol --sum_op--factorial_op--");MODULE_VERSION("Ver 1.0");int sum_op(int numdata)//{char i = 0;char ret = 0;printk(KERN_INFO"sum operation\n");while(i
  • #include #include MODULE_LICENSE("Dual BSD/GPL");static int __init mod_init_modtest1(void);static void __exit mod_exit_modtest1(void);module_init(mod_init_modtest1);module_exit(mod_exit_modtest1);static char *user_name = "book_user";static int num_operator = 0;extern int sum_op(int);int mod_init_modtest1(void){int result = 0;printk(KERN_INFO"Hello,I am module 1 !\n");printk(KERN_INFO"%s,Welcome to use this sum_op!\n",user_name);result = sum_op(num_operator);printk(KERN_INFO"1 +..+ %d = %d\n",num_operator,result);return 0;}void mod_exit_modtest1(void){printk(KERN_INFO"Module 1 : Goodbye %s\n",user_name); }module_param(user_name,charp,S_IRUGO);module_param(num_operator,int,S_IRUGO);

    MODULE_AUTHOR("book author");MODULE_DESCRIPTION("Simple Module 1 ,used to sum_op");MODULE_VERSION("Ver 1.0");

    module1.c KERN_ALERT module1modulemakefileKBUILD_EXTRA_SYMBOLS=../module/Module.symvers

  • #include #include MODULE_LICENSE("Dual BSD/GPL");static int __init mod_init_modtest2(void);static void __exit mod_exit_modtest2(void);module_init(mod_init_modtest2);module_exit(mod_exit_modtest2);static char *user_name = "book_user";static int num_operator = 0;extern int factorial_op(int);int mod_init_modtest2(void){int result = 0;printk(KERN_INFO"Hello,I am module 2 !\n");printk(KERN_INFO"%s,Welcome to use this factorial_op!\n",user_name);result = factorial_op(num_operator);printk(KERN_INFO" %d! = %d\n",num_operator,result);return 0;}void mod_exit_modtest2(void){printk(KERN_INFO"Module 2 : Goodbye %s\n",user_name); }module_param(user_name,charp,S_IRUGO);module_param(num_operator,int,S_IRUGO);

    MODULE_AUTHOR("book author");MODULE_DESCRIPTION("Simple Module 2 ,used to factorial_op");MODULE_VERSION("Ver 1.0");

    module2.c

  • makemodule.cmodule1.cmodule2.c

  • lsmodmodule1module20modulemodule1module22

  • dmesg dmesg | tail -10

  • Linux kernel2.6.26module1.kosum_opmodulemodule1module.koModule.symversmodule1module1

  • 2..komakefile5linux2.6

  • LinuxMakefileKernel Makefile Kernel MakefileLinux Top MakefileLinux KernelvmlinuxmoduleARCH Makefile ARCH MakefileARCH/$(ARCH)/MakefileMakefileKernel Top MakefileKbuild Makefile KbuildKbuild MakefileKernel MakefileKbuildKbuild MakefileKbuild MakefileKbuild Makefile

  • Kbuild Makefileifneq ($(KERNELRELEASE),)module-objs=module*.oobj-m += module1.oelsePWD := $(shell pwd)KVER := $(shell uname -r)KDIR := /lib/modules/$(KVER)/buildall:$(MAKE) -C $(KDIR) M=$(PWD)clean:rm -rf *.o *.mod.c *.ko *.symvers *.order *.markersendifmakefile -C $(a) amakefileM=$(b) bmakefilemakemakefilemakefileKERNELRELEASE -objs=obj-m += obj-m makefile

  • makefile

    obj-$(CONFIG_EXT2_FS) += ext2.oext2-y := balloc.o bitmap.oext2-$(CONFIG_EXT2_FS_XATTR) += xattr.oext2ifneqballoc.obitmap.oext2.oext2.koxattr.oCONFIG_EXT2_FSkbuild Makefileext2.c/ext2.s

  • 3.root1insmodinsmod .ko2lsmod3dmesg4rmmodrmmod 0

  • 12Linux3makefile4mainmod.clenmod.csummod.cmainmodsummodsummodlenmod5

    printk 8printkinclude/linux/kernel.h #define KERN_EMERG 0/**/ #define KERN_ALERT 1/**/ #define KERN_CRIT 2/**/ #define KERN_ERR 3/*KERN_ERR*/ #define KERN_WARNING 4/**/ #define KERN_NOTICE 5/**/ #define KERN_INFO 6/**/ #define KERN_DEBUG 7/**/ Makefile.config Makefilevmlinux()() MakefileMakefilearch/$(ARCH)/ MakefileMakefileMakefile KbuildMakefile KbuildMakefile.configKbuild scripts/Makefile.*kbuild Makefile(**

    Makefile4 **"makemenuconfig""make" Makefile ** KbuildMakefile MakefileKbuild **sparcia64 kbuildMakefileMakefile *Kbuild*KbuildMakefile obj-mobj-y