Chinaunix首页 | 论坛 | 博客

分类: BSD

2012-04-10 14:06:39

9.IO Kit及驱动开发
You may be think that writing your first kernel module is going to be a long, painful, grueling task, but really it is only slightly harder than writing your first program, Hello World!, in C or C++.

Lets break this into nice easy steps.

It is assumed you have installed FreeBSD already. If not, follow the instructions below with one change: Install the FreeBSD source during the “Choose Distribution” section.

How do I install FreeBSD?

Step 1 – Create a basic module file

A FreeBSD kernel module is written in C. This is going to be only a slightly harder than writing hello world in C. We are going to create a single text file with a .c extension and put some C code in it.

    1.Create a folder to hold you project. I used this directory:
    2.Create the file to hold your code. I named my file this:
    3.Edit the file with you favorite editor, vi, vim, emac, ee. I used easy editor (ee):
    ee hello_world_kmod.c
    4.Add the following code to the file.I have broken the code of the simplest kernel module into four parts or steps:
        Add four required #include statements.
        Create the kernel load/unload event handler.
        Create a struct to name the module and point to the event handler function.
        Call the DECLARE_MODULE macro.


  1. /*
  2.  * Step 1 - Add the four needed libraries to include
  3.  */
  4. #include <sys/param.h>
  5. #include <sys/module.h>
  6. #include <sys/kernel.h>
  7. #include <sys/systm.h>
  9. /*
  10.  * Step 2 - Handle the load/unload event
  11.  */
  12. static int EventHandler(struct module *inModule, int inEvent, void *inArg)
  13. {
  14.         // Set return code to 0
  15.         int returnCode = 0;
  17.         switch (inEvent)
  18.         {
  19.           case MOD_LOAD:
  20.                 uprintf("Hello, World! \n");
  21.                 break;
  22.           case MOD_UNLOAD:
  23.                 uprintf("Bye, World! \n");
  24.                 break;
  25.           default:
  26.                 returnCode = EOPNOTSUPP;
  27.                 break;
  28.         }
  30.         return(returnCode);
  31. }
  33. /*
  34.  * Step 3 - Name the module and the event hander function
  35.  * This is done using a struct of type moduledata_T
  36.  */
  37. static moduledata_t moduleData = {
  38.         "hello_world_kmod", // Module Name
  39.         EventHandler, // Event handler function name
  40.         NULL // Extra data
  41. };
  43. /*
  44.  * Step 4 - Declare the module
  45.  * This is done with the DECLARE_MODULE macro
  46.  */
  47. DECLARE_MODULE(hello_world_kmod, moduleData, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
5.Save and close the file.

Step 2 – Create a Makefile

Creating a Makefile to build a kernel module is quite easy because almost all the work is done for you. FreeBSD has a file that you include, /usr/src/share/mk/, that does most of the work for you and all you have to do is include it.

  1. In the same directory where you put your .c file, create a new text file called Makefile.
  2. There are three basic parts to the kernel module Makefile:
    1. Module name
    2. Source files
    3. Include of


  1. # Module Name
  2. KMOD = hello_world_kmod

  3. # Source files

  4. SRCS = hello_world_kmod.c

  5. # Include
  6. .include
3. Save and close the file.

Step 3 – Run make to build the module In the command prompt, in the directory where you have created your code and make file, run make.


  1. > cd /usr/home/jared/code/kernel/hwm
  2. > make
  3. Warning: Object directory not changed from original /usr/home/jared/code/kernel/hwm
  4. @ -> /usr/src/sys
  5. machine -> /usr/src/sys/amd64/include
  6. x86 -> /usr/src/sys/x86/include
  7. cc -O2 -pipe -fno-strict-aliasing -Werror -D_KERNEL -DKLD_MODULE -nostdinc -I. -I@ -I@/contrib/altq -finline-limit=8000 --param inline-unit-growth=100 --param large-function-growth=1000 -fno-common -fno-omit-frame-pointer -mno-sse -mcmodel=kernel -mno-red-zone -mno-mmx -msoft-float -fno-asynchronous-unwind-tables -ffreestanding -fstack-protector -std=iso9899:1999 -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual -Wundef -Wno-pointer-sign -fformat-extensions -Wmissing-include-dirs -fdiagnostics-show-option -c hello_world_kmod.c
  8. ld -d -warn-common -r -d -o hello_world_kmod.ko hello_world_kmod.o
  9. :> export_syms
  10. awk -f /sys/conf/kmod_syms.awk hello_world_kmod.ko export_syms | xargs -J% objcopy % hello_world_kmod.ko
  11. objcopy --strip-debug hello_world_kmod.ko
  12. >

Step 4 – Test loading an unloading the module

Loading and unloading kernel modules must be done as root. If you have sudo installed, use it, otherwise install it () or su to root.

Use kldload to load the module and kldunload to unload the module.


  1. > sudo kldload ./hello_world_kmod.ko
  2. Hello, World!
  3. > sudo kldunload ./hello_world_kmod.ko
  4. Bye

You have now performed the Hello World version of a FreeBSD kernel module.

阅读(5315) | 评论(0) | 转发(0) |


下一篇:2. Mac OS 内核开发,Hello World