Building Kernel Module for LTPS

Before running this example, please make sure you have correctly set up the SDK environment.

Write a Simple Module

[dv@dvh tmp]$ mkdir kmod-hello_world
[dv@dvh tmp]$ cd kmod-hello_world/
[dv@dvh kmod-hello_world]$ touch ./mhello.c
  • Create mhello.c file with the following contents:
#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>       /* Needed for KERN_INFO */

int init_module( void) {
  printk( KERN_INFO "Repent for Coming!\n");
  return( 0);  }

void cleanup_module( void) {
  printk( KERN_INFO "Goodbye cruel world\n");
  return;  }
  • Now create a standard Makefile for our simple kernel module:
[dv@dvh kmod-hello_world]$ touch ./Makefile
[dv@dvh kmod-hello_world]$ vi ./Makefile
  • Makefile contents:

There are two tabs (not spaces!) at the beginning of each indented line.

obj-m += mhello.o

hello-objs := mhello.c

KERNEL_SRC ?= /lib/modules/$(shell uname -r)/build/

       make -C $(KERNEL_SRC) M=$(PWD) modules

       make -C $(KERNEL_SRC) M=$(PWD) modules_install

       make -C $(KERNEL_SRC) M=$(PWD) clean

If you're not familiar with the syntax, please read the Proper makefile setup for external kernel modules discussion at StackOverflow or full Linux kernel module build howto manual at Kernel.Org

Make sure you did not skip the Prepare SDK for Kernel Modules step before building the module.

Build your Module with SDK

  • Run the make command in the same terminal emulator window where you previously ran LTPS SDK environment setup script.
[dv@localhost kmod-hello_world]$ make KERNEL_SRC=${SDKTARGETSYSROOT}/usr/src/kernel      
make -C /home/dv/tpssdk/sysroots/cortexa8hf-neon-tps-linux-gnueabi/usr/src/kernel M=/home/dv/work/Tibbo/kmod-hello_world modules
make[1]: Entering directory '/home/dv/tpssdk/sysroots/cortexa8hf-neon-tps-linux-gnueabi/usr/src/kernel'
  CC [M]  /home/dv/work/Tibbo/kmod-hello_world/mhello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/dv/work/Tibbo/kmod-hello_world/mhello.mod.o
  LD [M]  /home/dv/work/Tibbo/kmod-hello_world/mhello.ko
make[1]: Leaving directory '/home/dv/tpssdk/sysroots/cortexa8hf-neon-tps-linux-gnueabi/usr/src/kernel'
[dv@localhost kmod-hello_world]$

We got module 'mhello.ko' built in our directory.

Now, (if you are building on a host system for a host system) it's time to run "make modules_install" to put module on its place at /lib/modules/.... But we're on the host PC building for another platform so just put it into LTPS manually...

[dv@localhost kmod-hello_world]$ scp ./mhello.ko root@
root@'s password: 
mhello.ko                                     100%   51KB   5.0MB/s   00:00    
[dv@localhost kmod-hello_world]$

And rebuild the module index:

[dv@localhost kmod-hello_world]$ ssh root@ 
root@'s password: 
X11 forwarding request failed on channel 0
root@tpp:~# depmod -A

Test our module by loading and unloading it. See the system log for module messages.

root@tpp:~# modprobe mhello
root@tpp:~# lsmod | grep mhello
mhello                   943  0
root@tpp:~# rmmod mhello
root@tpp:~# journalctl -l -n 10 | grep kernel
Sep 22 13:48:40 tpp kernel: mhello: module license 'unspecified' taints kernel.
Sep 22 13:48:40 tpp kernel: Disabling lock debugging due to kernel taint
Sep 22 13:48:40 tpp kernel: Repent for Coming!
Sep 22 13:49:58 tpp kernel: Goodbye cruel world