<<linux device driver,third edition>> Chapter 2: Building and Running Modules

Linux内核模块编程要点解析

Kernel Modules Versus Applications

Kernel modules programming is similar to event driven programming.

the task of the module's initialization function is to perpare for later invocation of the module's functions;it's as though the module were saying,"Here I am,and this is what I can do."The module's exit function gets invoked just before the module is unloaded.it should tell the kernel,"I'm not there anymore;don't ask me to do anything else".

A module,is linked only to the kernel,and the only functions it can call are the ones exported by the kernel

Concurrency in the kernel

  • interrupt handlers
  • symmetric multiprocessor(SMP)
  • kernel code has been made preemptible

As a result,Linux kernel code,including driver code,must be reentrant.

A Few Other Details

Kernel code cannot do floating point arithmetic.

Kernel has very small stack.

Often,as you look at the kernel API,you will encounter function name starting with a double undersore(__).Essentially,it says to the programmer:"if you call this function,be sure you know what you are doing"

Compiling Modules

obj-m := hello.o

hello-objs := main.o add.o

all:
    make -C /usr/src/XXX/ M=`pwd` modules

clean:
    make -C /usr/src/XXX/ M=`pwd` clean

Loading and Unloading Modules

insmod

The program loads the module code and data into the kernel,which,in turn,performs a function similar to the of ld,in that it links any unresolved symbol in the module to the symbol table of the kernel

modprobe

like insmod,load a module into the kernel.It differs in that it will look at the module to be loaded to see whether it references any symbols that are not currently defined in the kernel.If any such references are found,modprobe looks for other modules in the current module search path that define the relevant symbols.When modprobe finds those modules,it loads them into the kernel as well.

The kernel symbol table

EXPORT_SYMBOL(name)
EXPORT_SYMBOL_GPL(name)

Module Parameters

insmod hello.ko howmany=10 whom="Mom"

Parameters are declared with the module_param macro,which is defined in moduleparam.h .module_param takes three parameters: the name of the variable,its type , and a permissions mask to be used for an accompanying sysfs entry.
eg:

static char *whom="world";
static int howmany=1;
module_param(howmany,int,S_IRUGO);
module_param(whom,charp,S_IRUGO);

转载于:https://www.cnblogs.com/r1ng0/p/9537591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值