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

来源:互联网 发布:旅行团乐队知乎 编辑:程序博客网 时间:2024/06/04 22:47
        对于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 macrosstatic 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.oobj-m += $(MODULE_NAME).oelseKVER := $(shell uname -r)KDIR := /lib/modules/$(KVER)/buildall:$(MAKE) -C $(KDIR) M=$(CURDIR) modulesclean:@rm -rf .tmp_* .*o.cmd *.o *.ko *.mod.c *.order *.symversendif
        上面的文件可以在 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/
1 1
原创粉丝点击