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).

    hello.c
    #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");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);
    

    Makefile
    obj-m += hello.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
    

  • 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.

2 comments:

Unknown said...

Try adding the following macros:

MODULE_LICENSE("GPL");
MODULE_AUTHOR("YOUR_NAME");
MODULE_DESCRIPTION("DESCRIPTION_OF_YOUR_MODULE");

HeatfanJohn said...

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

http://stackoverflow.com/questions/20167411/how-to-compile-a-kernel-module-for-raspberry-pi/23685353#23685353

These steps worked for me.