第六期 基于QEMU进行Linux内核模块实验 《虚拟机就是开发板》

        对于Linux内核的学习,多数都是从调试运行内核模块开始的,这一期我们来总结一下用模拟开发板调试运行内核模块的一般方法。
        首先写一个内核模块的helloworld源文件,包括hello.c 和相应的Makefile:
hello.c
/*
 * A simple module for helloworld
 *
 * Copyright (C) 2017 aggresss (aggresss.163.com)
 *
 * Licensed under GPLv2 or later.
 */

// Defining __KERNEL__ and MODULE allows us to access kernel-level code not usually available to userspace programs.
#undef __KERNEL__
#define __KERNEL__

#undef MODULE
#define MODULE

#include <linux/module.h>    // included for all kernel modules
#include <linux/kernel.h>    // included for KERN_INFO
#include <linux/init.h>        // included for __init and __exit macros

static int __init hello_init(void)
{
    printk(KERN_INFO "Hello world!\n");
    return 0;    // Non-zero return means that the module couldn't be loaded.
}

static void __exit hello_exit(void)
{
    printk(KERN_INFO "Exit  module.\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("aggresss <aggresss@163.com>");
MODULE_LICENSE("GPL v2");
Makefile
#
#  A simple module for helloworld
#
#  Copyright (C) 2017 aggresss (aggresss.163.com)
#
#  Licensed under GPLv2 or later.
# 

ifneq ($(KERNELRELEASE),)

MODULE_NAME := test_module
$(MODULE_NAME)-objs := hello.o
obj-m += $(MODULE_NAME).o

else

KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build

all:
	$(MAKE) -C $(KDIR) M=$(CURDIR) modules
clean:
	@rm -rf .tmp_* .*o.cmd *.o *.ko *.mod.c *.order *.symvers

endif
        上面的文件可以在 https://github.com/aggresss/LKDemo 的hello_module 目录中下载。
        关于内核模块的编译如果是在本平台编译还是比较简单的,直接在内核模块对应的源文件目录下输入make 就可以编译了,但这样编译出来的模块是在本平台下运行的,因为它使用了当前操作系统的/lib/moudules/$(uname -r)/build 目录来生成当前的模块,并且编译器也是同平台下的。如果想编译在模拟开发板上运行的模块,就需要采用另一种方法,利用Linux 源文件根目录下Makefile 的 SUBDIRS 参数,下面来说明一下操作步骤:
        1. 进入到已经编译生成Linux源文件根目录下 例如: linux-4.1.38,前提是已经利用这个目录编译生成过内核目标文件 ;
        2. 执行 export ARCH=arm
                    export CROSS_COMPILE=arm-linux-gnueabi-
        3. make modules SUBDIRS= xxxx/hello_module    (xxxx为需要编译的模块所在目录)
        完成上面的操作后便可以将生成的 *.ko 文件导入模拟开发板中运行,通过 insmod 命令装载模块,lsmod 命令查看模块,rmmod 命令卸载模块,dmesg 命令查看内核调试信息,下面是演示截图:    
        
        这里需要说明一下:
        1.在目标开发板上执行  rmmod 时会提示 rmmod: can't change directory to '/lib/modules': No such file or directory这个错误,是由于没有 /lib/modules/4.1.38 这个目录导致的,在目标开发板上建立一下这个目录即可,是空目录,不需要任何文件。
        2.Makefile中的模块名称和依赖文件不要重名,也就是MODULE_NAME 不能 (MODULE_NAME)-objs 依赖的源文件的名字重复,这样容易造成循环依赖, 导致make会出现如下错误信息:
        make[2]: Circular /root/develop/kernel_module/helloworld/hello.o <- /root/develop/kernel_module/helloworld/hello.o dependency dropped. 
        更好的方法就是给模块命名的末尾加上 _module 后缀。
        更多内核模块编译相关的知识可以参考:
        http://www.ibm.com/developerworks/cn/linux/l-cn-kernelmodules/
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值