(LDD3实践)Chapter-2:建立和运行模块

X86-PC:

环境是:
[root@zx chap2_hello]# uname -a
Linux zx 2.6.27.5-117.fc10.i686 #1 SMP Tue Nov 18 12:19:59 EST 2008 i686 i686 i386 GNU/Linux

源码(hello.c):

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h> 
MODULE_LICENSE("Dual BSD/GPL");

/* 初始化值 */
static char *whom = "world";   
static int howmany = 1;   

module_param(howmany, int, S_IRUGO);   
module_param(whom, charp, S_IRUGO);

static __init int hello_init(void)
{
	int tmp = 0;
	for (; tmp < howmany; tmp++)
	{
		printk(KERN_ALERT"Hello, %s\n", whom);
	}
	return 0;
}

static __exit void hello_exit(void)
{
	printk(KERN_ALERT"Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
Makefile:

# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    #KERNELDIR ?= /share/my_kernel/linux-2.6.30.4
    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.bak

.PHONY: modules modules_install clean

else
    # called from kernel build system: just declare what our modules are
    obj-m := hello.o
endif
运行结果:

正如LDD3所述:“如果你从一个终端模拟器或者在窗口系统中运行insmod 和 rmmod, 你不会在你的屏幕上看到任何东西. 消息进入了其中一个系统日志文件中, 例如 /var/log/messages”,所以此处使用dmesg命令查看内核消息:

[root@zx chap2_hello]# dmesg -c
[root@zx chap2_hello]# make clean
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions *.bak
[root@zx chap2_hello]# make
make -C /lib/modules/2.6.27.5-117.fc10.i686/build M=/share/LDD3_code/my_module/chap2_hello modules
make[1]: Entering directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
  CC [M]  /share/LDD3_code/my_module/chap2_hello/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /share/LDD3_code/my_module/chap2_hello/hello.mod.o
  LD [M]  /share/LDD3_code/my_module/chap2_hello/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.27.5-117.fc10.i686'
[root@zx chap2_hello]# insmod hello.ko
[root@zx chap2_hello]# rmmod hello
[root@zx chap2_hello]# dmesg -c
Hello, world
Goodbye, cruel world
[root@zx chap2_hello]# insmod hello.ko howmany=10 whom="Mom"
[root@zx chap2_hello]# rmmod hello
[root@zx chap2_hello]# dmesg -c
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Hello, Mom
Goodbye, cruel world
[root@zx chap2_hello]# 
lsmod命令:

[root@zx chap2_hello]# insmod hello.ko 
[root@zx chap2_hello]# lsmod
Module                  Size  Used by
hello                   5760  0 
nls_utf8                5632  1 
/proc/modules & /sys/module

[root@zx chap2_hello]# cat /proc/modules 
hello 5760 0 - Live 0xe095a000
nls_utf8 5632 1 - Live 0xe095d000

EmbedSky_irq.c  hello.ko  hello.mod.o  Makefile  Module.markers  Module.symvers
[root@zx chap2_hello]# ls /sys/module/
8250             bridge        hello   

ARM9-TQ2440:

环境是:
[root@EmbedSky /modules_test]# uname -a
Linux EmbedSky 2.6.30.4-EmbedSky #8 Mon May 31 10:57:52 CST 2010 armv4tl GNU/Linux
源码和之前的一样,而Makefile则需要改变:
(关于这个Makefile参考了 http://www.cnblogs.com/xmphoenix/archive/2011/10/25/2224237.html)
#hello_makefile ARM
obj-m :=arm_hello.o
KRNELDIR :=/share/my_kernel/linux-2.6.30.4-tq2440
CROSS_COMPILE =arm-linux-
CC :=$(CROSS_COMPILE)gcc
LD :=$(CROSS_COMPILE)ld
PWD :=$(shell pwd)
all:
	make -C $(KRNELDIR) M=$(PWD) modules  
	
.PHONY :clean
clean:
	rm -rf *.o *ko
测试结果:
[root@EmbedSky /]# cd modules_test/
[root@EmbedSky /modules_test]# ls
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# rx arm_hello.ko
CC
Starting xmodem transfer.  Press Ctrl+C to cancel.
Transferring arm_hello.ko...
  100%       3 KB    0 KB/s 00:00:04       0 Errors

root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# ls
arm_hello.ko
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# chmod 777 ./*
[root@EmbedSky /modules_test]# insmod arm_hello.ko 
Hello, world
[root@EmbedSky /modules_test]# rmmod arm_hello    
Goodbye, cruel world
[root@EmbedSky /modules_test]# rmmod arm_hello howmany=10
rmmod: can't unload 'arm_hello': unknown symbol in module, or unknown parameter
[root@EmbedSky /modules_test]# insmod arm_hello howmany=10
insmod: can't insert 'arm_hello': No such file or directory
[root@EmbedSky /modules_test]# insmod arm_hello.ko howmany=10
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
[root@EmbedSky /modules_test]# rmmod arm_hello
Goodbye, cruel world
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# 
[root@EmbedSky /modules_test]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值