linux内核编程-2

2 篇文章 0 订阅

原子上下文就是内核不能休眠的时候就称作原子上下文 ;估计这些操作 需要快速处理吧,不能休眠;  对于spinlock那种,如果我睡眠了。。别的cpu在获得锁的时候就要白白浪费cpu啊,他也要求spinlock的临界区尽量短  


jian@jian-X550VC:~/linux$ sudo modinfo arch/x86/kvm/kvm.ko
filename:       arch/x86/kvm/kvm.ko
license:        GPL
author:         Qumranet
srcversion:     002068E364A9603557F5236
depends:        
intree:         Y
vermagic:       3.14.0-rc5+ SMP mod_unload modversions
parm:           ignore_msrs:bool
parm:           min_timer_period_us:uint
parm:           tsc_tolerance_ppm:uint
parm:           allow_unsafe_assigned_interrupts:Enable device assignment on platforms without interrupt remapping support. (bool)

param ignore_msrs

在源代码里面可以找到

mmu.c

在search ignore_msrs

x86.c

module_param(name,type,access authority)


unsigned int min_timer_period_us = 500;
module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);

我们自己试一试加个参数

修改main.c

unsigned int testpar= 0;
module_param(testpar, uint, S_IRUGO | S_IWUSR);

static __init int minit(void)
{
    printk("testpar= %#x.\n",testpar);
    printk("call %s.\n",__FUNCTION__);
    other_function();
    return 0;
}


sudo make

sudo modinfo tmain.ko

apuser@jianzhangubtnb:~/mymodule$ sudo rmmod tmain.ko
ERROR: Module tmain does not exist in /proc/modules
apuser@jianzhangubtnb:~/mymodule$ sudo insmod tmain.ko
apuser@jianzhangubtnb:~/mymodule$ sudo dmesg
[83733.816253] testpar= 0x0.
[83733.816257] call minit.
[83733.816259] call other_function.
apuser@jianzhangubtnb:~/mymodule$ sudo rmmod tmain.ko
apuser@jianzhangubtnb:~/mymodule$ sudo dmesg
[83733.816253] testpar= 0x0.
[83733.816257] call minit.
[83733.816259] call other_function.
[83750.851620] call mexit.
apuser@jianzhangubtnb:~/mymodule$ sudo dmesg -c
[83733.816253] testpar= 0x0.
[83733.816257] call minit.
[83733.816259] call other_function.
[83750.851620] call mexit.
apuser@jianzhangubtnb:~/mymodule$ sudo insmod tmain.ko testpar=100
apuser@jianzhangubtnb:~/mymodule$ sudo dmesg
[83764.976980] testpar= 0x64.
[83764.976985] call minit.
[83764.976987] call other_function.

初始值是0,在insmod里面可以带参数变了-》100

param 除了bool,uint还可以有其他类型;

看一下实现好了

moduleparam.h

 * Standard types are:
 *    byte, short, ushort, int, uint, long, ulong
 *    charp: a character pointer
 *    bool: a bool, values 0/1, y/n, Y/N.
 *    invbool: the above, only sense-reversed (N = true).
 */
#define module_param(name, type, perm)                \
    module_param_named(name, name, type, perm)
/

编写与编译module之间的依赖

把原先的代码都放到m1目录下面

cp m1 m2 -r

m1/src/other.c

导出一个函数

EXPORT_SYMBOL(other_function);

在m2中使用他

cd ../m2

sudo rm src -rf

修改Makefile
obj-m=t2main.o

t2main-y= main.o

all:
    make -C /lib/modules/`uname -r`/build EXTRA_CFLAGS=-I$(shell pwd)/include M=`pwd`   
    ~/Downloads/linux-3.12/linux/scripts/sign-file sha512 ~/Downloads/linux-3.12/linux/signing_key.priv ~/Downloads/linux-3.12/linux/signing_key.x509 t2main.ko


sudo make

报错了,因为现在找不到other_function

怎么才能够在m2中找的到m1中的那个函数呢?

先编译m1

然后拷贝符合表到m2

   sudo cp Module.symvers ../m2

然后编译m2

apuser@jianzhangubtnb:~/mymodule/m2$ sudo insmod t2main.ko
insmod: error inserting 't2main.ko': -1 Unknown symbol in module

未知符号,所以需要先加载m1,再加载m2

sudo rmmod tmain.ko

sudo dmesg

sudo dmesg -c

sudo insmod tmain.ko

sudo iinsmod tmain2.ko

apuser@jianzhangubtnb:~/mymodule/m2$ sudo dmesg
[85295.749134] testpar= 0x0.
[85295.749139] call minit.
[85295.749141] call other_function.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值