Sunday, January 19, 2014

failed experiment: build "Hello World" kernel module for Raspberry Pi

It is a failed experiment to build "Hello World" kernel module for Raspberry Pi, follow the step:
  • Run with a new installed Raspbian, wheezy, 2014-01-07.
  • Download and transfer the source code of Raspberry Pi
  • Create hello.c and Makefile follow "The Linux Kernel Module Programming Guide", 2.3. Hello World (part 2).

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    static int __init hello_init(void)
     printk(KERN_INFO "Hello Raspberry Pi!\n");
     return 0;
    static void __exit hello_exit(void)
     printk(KERN_INFO "Bye Raspberry Pi!\n");

    obj-m += hello.o
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

  • Then make it, without error.
  • When I try to insert the module hello.ko, error of invalid module format reported.
    $ sudo insmod ./hello.ko
    Error: could not insert module ./hello.ko: Invalid module format

  • "disagrees about version of symbol module_layout" logged in /var/log/messages'. I assume it is something mis-match between the kernel version in my source and the running system.
  • Then I update Raspberry Pi to most update with:
    $ sudo rpi-update
    $ sudo apt-get update
    $ sudo apt-get upgrade
    and reboot
  • I try to insmod again, but fail with "Unable to handle NULL pointer dereference at virtual address 00000004", and the system hang-up.
    "Unable to handle NULL pointer dereference at virtual address 00000004"
I'm a newbies in Linux programming, and ask for help. Please leave me comment for any advice.


vu nguyen said...

Try adding the following macros:


HeatfanJohn said...

Try the steps listed in my answer to this problem on Stackoverflow:

These steps worked for me.