内核驱动 helloworld 内核模块

本文详细介绍了一个简单的ARM内核模块开发过程,包括C源码编写、Makefile配置及常见错误处理方法。此外,还提供了如何修改内核以适配不同架构的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1   .c源码

#include <linux/kernel.h>

  #include <linux/init.h>

  #include<linux/module.h>

   static int hello_init(void)

   {

       printk(KERN_INFO "[init] Can you feelmei?  I am ready to installarm_hello....\n");

       return 0;

   }

   

  static int hello_exit(void)

  {

    printk(KERN_INFO "[exit] Yes. I can feel you.....I am read to leavearm_hello......\n");

          return 0;

 }

 module_init(hello_init);

 module_exit(hello_exit);

 

 MODULE_AUTHOR("Tbao  -invt company-");

 MODULE_LICENSE("GPL");

 MODULE_DESCRIPTION("Asimple arm Hello World Module");

 MODULE_ALIAS("A simple arm module");

 

 

2.Makefile

在内核源码中./drivers/test/中Makefile,在该目录下单独编译内核模块。

Ti335x Armv7开发板下:

 obj-m += hello.o

 CURRENT_PATH :=$(shell pwd)

 LINUX_KERNEL :=$(shell uname -r)

 LINUX_KERNEL_PATH:=/usr/local/ti-sdk-am335x-evm/board-support/linux-3.2.0-psp04.06.00.11

 all:

      make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH)ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-  modules

 clean:

      make -C $(LINUX_KERNEL_PATH)M=$(CURRENT_PATH) clean

 

Ps:在单独编译内核的时候:出现以下报错

WARNING:Symboversion dump/usr/local/ti-sdk-am335x-evm/board-support/linux-3.2.0-psp04.06.00.11/

Module.symvers is missing; modules willhave no dependencies and modversions.

是需要先进行内核编译,在内核源码根目录下生成Module.symvers,在编辑内核模块就不错了。

在目标机上安装模块时:出错,是内核版本校验,内核版本不对。

root@am335x-evm:/mnt#insmod hello.ko

[9293.451572] hello: module_layout: kernel tainted.

[9293.456587] Disabling lock debugging due to kernel taint

[9293.462161] hello: version magic '3.2.0 mod_unload ARMv5 p2v8 'should be '3.2.0 mod_unload modversions ARMv7 p2v8 '

Error: couldnot insert module hello.ko: Invalid module formats

 

107.792110] hello: version magic ' 3.2.0mod_unload modversions ARMv7 p2v8 ' should be '3.2.0 mod_unload modversionsARMv7 p2v8 '

Error: couldnot insert module hello.ko: Invalid module format

 

3.改内核:

Include/linux中的Vermagic.h:  控制版本的是宏

 

#if 0

//-------Tbao----à>20170828

#define VERMAGIC_STRING                                                \

         UTS_RELEASE" "                                                             \

         MODULE_VERMAGIC_SMPMODULE_VERMAGIC_PREEMPT                        \

         MODULE_VERMAGIC_MODULE_UNLOADMODULE_VERMAGIC_MODVERSIONS      \

         MODULE_ARCH_VERMAGIC

//<<-------Tbao----------

#endif

/************添加*************下面符合开发板的****/

#define VERMAGIC_STRING    "3.2.0 mod_unload modversions ARMv7p2v8 "

注意不要有空格等。。。。。。。。

 

3.安装模块

dmesg | grep 可以查看加载模块中的信息。

insmod 加载模块

rmmod 卸载模块

lsmod 列出已经加载模块和信息

modinfo 查看模块信息

 cat   /proc/devices

 

 

 

再以一个内核模块为例: /drivers/net/can/usb/ems_usb.c

static unsigned int debug = 1;    //定义一个打印变量
module_param(debug,int,0644);     //在用户态下编程可以通过main()来传递命令行参数,而编写一个内核模块则可通过                                                                         module_param()来传递命令行参数. 
MODULE_PARM_DESC(debug,"debug == 0:close debug info print; debug == 1: open debug info print");

if(debug){

       printk("*********************\n");

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值