Kernel Module 1 Hello World - FrankBau/meta-marsboard-bsp GitHub Wiki
My module version 1: "Hello world"
Kernel modules are mostly written in C, but the usual library functions like malloc, fopen, close, must not be used because their implementation is targeted to user space.
A working yocto development environment should be installed and the kernel already built (bitbake virtual/kernel).
Create a new, empty folder my_module
and remember the absolute path to this folder, something like /home/user/my_module
.
In that folder create two files:
- a source code file
my_module.c
- a
Makefile
my_module.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
static int __init my_init(void)
{
printk(KERN_INFO "hello, my module\n");
return 0;
}
static void __exit my_exit(void)
{
printk(KERN_INFO "good bye, my module\n" );
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("F.B.");
MODULE_DESCRIPTION("My First Driver");
Makefile
This can be a one liner:
obj-m += my_module.o
Build module
On the build host, setup yocto build environmentd and open a kernel development shell:
$ . ./setup-environment build
$ bitbake -c devshell virtual/kernel
The last command opens a new shell. This is the kernel development shell (devshell) which is used for the next steps. The kernel development shell has a number of environment variables set for cross-compilation. You can test this by invoking the gcc cross-compiler for the board:
root:kernel-source$ arm-poky-linux-gnueabi-gcc --version
arm-poky-linux-gnueabi-gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The module is now built "out-of-tree" in its own folder, say /home/user/my_module
as created above.
But, make is executed in the kernel source directory, so don't change the current devshell directory!
root:kernel-source$ make M=/home/user/my_module modules
The make command generates several files in the module directory.
The resulting kernel module is my_module.ko
(.ko = "kernel object").
Test the kernel module
Copy my_module.ko to the target board, see Exchanging files with MarS-Board.
On the target board, load the module into the currently running Linux kernel space. Useful commands:
| lsmod | list currently installed (loaded) modules | | modinfo | output module info | | insmod my_module.ko | install (load) module | init function will be executed | | rmmod my_module | uninstall (unload) module | exit function will be executed | | modprobe | convenience function, see man page |
Kernel Module 2 - Char Device shows how to communicate with the kernel module from a user mode process.