Creating simple kernel module - linux-kernel

I've just started learning linux kernel modules and trying to write simple Hello world program.
So mymod.c:
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("\"Hello, world!\" minimal module");
MODULE_VERSION("printk");
int init_module(void)
{
printk("<1>Hello world 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}
Makefile:
obj-m += mymod.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make outout:
make -C /lib/modules/3.2.0-23-generic-pae/build M=/root modules
make[1]: Entering directory `/usr/src/linux-3.2.42'
WARNING: Symbol version dump /usr/src/linux-3.2.42/Module.symvers
is missing; modules will have no dependencies and modversions.
Building modules, stage 2.
MODPOST 1 modules
make[1]: Leaving directory `/usr/src/linux-3.2.42'
So it creates files I needed, but when I try to install this by
insmod mymod.ko
I get next output:
insmod: error inserting 'mymod.ko': -1 Invalid module format
So I'd like to know what's the problem?
PS. OS - Ubuntu Server 12.04. Kernel - linux 3.2.0-23 pae
UPDATE:
I've downloaded from kernel.org kernel 3.2.42 and put it in /usr/src and did 'make defconfig && make prepare', 'make modules_prepare'. Also I've created link in /lib/modules/3.2.0-23-generic-pae/build.

Is this the source tree for the running kernel? If not, it should fail.
Install the kernel-devel (or similarly named) package for your distribution, it adds enough machinery to build modules against it.

You missed the module_init and module_cleanup declaration,
module_init (module_init);
module_exit (cleanup_module);
Otherwise it would have no entry point defined, and it wouldn't load.

Because this task requires so many details, and small files to be coordinated it is best to use UML (user mode linux) so that the kprintf (kernel printf) always outputs to the terminal even in a graphical environment.

Related

not able to remove loadable kernel module(LKM) using rmmod

I am trying to write kernel module for ubuntu 12.04 LTS OS.
kernel version is 3.4.0-030400-generic-pae
I am able to compile it & load it to kernel.
For loading I use
sudo insmod nmod_main.ko
Now, If I try to remove it using
sudo rmmod nmod_main.ko
it gives me bellow error.
ERROR: Removing 'nmod_main': Device or resource busy
lsmod gives bellow information:
Module Size Used by
nmod_main 12394 0 [permanent]
Why this module shows permanent?
Bellow is the C-code for this module.
/********** Start of code ************/
#define __KERNEL__
#define MODULE
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_INFO "init_module() called\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "cleanup_module() called\n");
}
/********** End of code ************/
I am getting some compilation warnings as bellow:
Building with KERNELRELEASE = 3.4.0-030400-generic-pae
CC [M] ../src/nmod_main.o
../src/nmod_main.c:1:0: warning: "KERNEL" redefined [enabled by default]
:0:0: note: this is the location of the previous definition
../src/nmod_main.c:2:0: warning: "MODULE" redefined [enabled by default]
:0:0: note: this is the location of the previous definition
Building modules, stage 2.
Building with KERNELRELEASE = 3.4.0-030400-generic-pae
Please can anybody help me in this regard.
I am using bellow makefile for building this module:
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
.PHONY: build clean
build:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf ../src/*.o ../src/*~ core ../src/.depend ../src/.*.cmd ../src/*.ko ../src/*.mod.c
else
$(info Building with KERNELRELEASE = ${KERNELRELEASE})
obj-m := ../src/nmod_main.o
endi
This problem is solved. Yes, it is problem related to toolchain. I downloaded .deb files to install Linux Kernel from url.
But I was not sure about the toolchain used to generate these .deb files. So I finally downloaded Linux kernel source from url, compiled & installed on my laptop. Then compiled my module. Problem disappeared.
Thanks #avd for providing me valuable clue.
You need to add module entry points which will allow kernel to load or unload module.Without module_init and module_exit kernel does'nt know how to unload the module and module became permanent. BUt dont know to fix i need solution How to remove this permanent module.

Compile Warning - ARM Cross Compiler

I have been trying to cross compile a loadable kernel module and I have been getting warning message that I would like to get help.
Below is the warning message
make[1]: Entering directory `/home/userid/rowboat-android/kernel'
Building modules, stage 2.
MODPOST 1 modules
WARNING: "omap_device_build" [/home/userid/myfiles/lcdc_load_device.ko] undefined!
WARNING: "omap_hwmod_lookup" [/home/userid/myfiles/lcdc_load_device.ko] undefined!
make[1]: Leaving directory `/home/userid/rowboat-android/kernel'
Below is the Makefile
obj-m :=lcdc_load_device.o
lcdc_load_device-m := ../rowboat-android/kernel/arch/arm/plat-omap/omap_device.o
lcdc_load_device-m += ../rowboat-android/kernel/arch/arm/mach-omap2/omap_hwmod.o
ccflags-m := -I$(src)/../rowboat-android/kernel/arch/arm/plat-omap/include/plat/
KDIR := /home/userid/rowboat-android/kernel/
PWD := $(shell pwd)
default:
$(MAKE) ARCH=arm CROSS_COMPILE=/home/userid/rowboat-android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
I am suspecting that the makefile is finding the header file for "omap_device_build" and "omap_hwmod_lookup" functions.
Appreciate your help, and thank you advance.
Thank you to mbratch and artless noise for their comments
To address the WARNINGS, the symbols need to be exported. For the above question place
EXPORT_SYMBOL(omap_device_build) in omap_device.c
EXPORT_SYMBOL(omap_hwmod_lookup) in omap_hwmod.c
and compile the kernel. Then compile the loadable kernel module against the compiled kernel. Perform the following to verify if the symbols have been exported
grep omap_device_build /proc/kallsyms
or
grep omap_device_build Module.symvers
For more details reference the following links
Kernel Symbols: What's available to Your Module
Building and Running Modules
Comments are welcome
Thank you

How to build a kernel module

I am trying to compile a hello world module given over here
I have followed the following step.
Downloaded Linux kernel 2.6.35-rc5
extracted to directory /general/source/linux
Complied the entire kernel.
created a dir test in the linux folder.
Created and complied a hello world module as mentioned there.
when I run the insmod command, I get this error
insmod: error inserting 'hello.ko': -1 Invalid module format
How do I sort out this error?
Regards,
Ok the mistake that you are making is the kernel version.
First try
uname -r
You would get the kernel version. The downloaded version mostly likely won't be the kernel version of your system.
So change the make file to
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
.PHONY: build clean
build:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c
else
$(info Building with KERNELRELEASE = ${KERNELRELEASE})
obj-m := hello.o
endif
Make sure the tabs are in the order as mentioned in the above script.
Your kernel module must match the running kernel. If you want to install this specific module, for example, you'd need to also install the kernel that you've built.
Normally, you'd not build the kernel on your own and use a pre-built version that matches your distribution's kernel. Look for a kernel-headers package in your distribution's repository.

User Mode Linux - Installing a module error

I am trying to run 'make' on a module in User Mode Linux to install a simple makefile. Here is my make file:
obj-m := hello.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
When I run this in User Mode Linux I get the following error:
make[1]: Entering directory `/lib/modules/2.6.28/build'
make[1]: *** No rule to make target `modules'. Stop.
make[1]: Leaving directory `/lib/modules/2.6.28/build'
make: *** [default] Error 2
The problem is that no files are present under /lib/modules/. There's no directory for 2.6.28 or build. From what I've read, these should be symlinks to /usr/src, but under /usr/src, I don't see any files under that either.
Sources and headers of your UML kernel must be used to compile module for it.
You can compile it either inside UML or just in main system, but you must to use UML's kernel's headers and build scripts
You need to build and install the version of the kernel you are compiling for. Get the source from kernel.org, configure (I think make menuconfig picks the config up from the running kernel), build, and install it. You can do the build in your home directory under regular user, then of course you would need root to install it.
Edit:
Just in case you missed this - here's User Mode Linux HOWTO. It contains specific items for building and installing kernel and modules. Hope this helps.

Add insmod Kernel HID module

I made certain modifications in hid module.
I can make it and load (insmod) it on kernel v 2.6.27.14 sucessfully
Now I am willing to load the same kernel module on kernel v 2.6.27.11
As there is no differance in the kerbel source files for both the kernel versions
I can make it sucessfully, but I cannot add / insmod in this .11 kernel version
**
ERROR: Module myhid does not exist in /proc/modules
insmod: error inserting 'myhid.ko': -1 Invalid module format
**
Regards,
You can't load a module compiled for another kernel version. That the source code of the module did not change does not mean that the binary will be the same for another kernel version. Any interface change of kernel internal APIs (even when not directly visible) will break the module...
Therefore, the kernel stays on the safe side by disallowing loading of modules that were built for another kernel version. Alternatively, you can set the MODVERSIONS configuration option when building your kernel. This will embed version information into all symbols used by your module and with luck you can load it on another kernel version.
If any interface used by your module changed, the result will be the same though.
see what "modinfo" tells you about your module:
Check that it's compiled properly, linked to the right kernel.
$ modinfo hid
filename: /lib/modules/2.6.27.7/kernel/drivers/hid/hid.ko
license: GPL
depends:
vermagic: 2.6.27.7 mod_unload 486
parm: pb_fnmode:Mode of fn key on Apple keyboards (0 = disabled, 1 = fkeyslast, 2 = fkeysfirst) (int)
When you compile/install modules, don't forget that you have to run "depmod" (as root) to rebuild the modules dependancies before running insmod/modprobe.
Thanks ! Here is the make file . I dowload all the dependent source files for HID.O and rename them
MAKEFILE
obj-m := myhid.o
myhid-objs := my-hiddraw.o my-hid-core.o my-hid-input.o my-hid-input-quirk.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) M=$(PWD) modules

Resources