FreeBSD Architecture Handbook
Prev | Chapter 9 Writing FreeBSD Device Drivers | Next

# 9.2 Dynamic Kernel Linker Facility - KLD

The kld interface allows system administrators to dynamically add and remove functionality from a running system. This allows device driver writers to load their new changes into a running kernel without constantly rebooting to test changes.

The kld interface is used through:

- `kldload` - loads a new kernel module

- `kldunload` - unloads a kernel module

- `kldstat` - lists loaded modules

Skeleton Layout of a kernel module

    /*
     * KLD Skeleton
     * Inspired by Andrew Reiter's Daemonnews article
     */

    #include <sys/types.h>
    #include <sys/module.h>
    #include <sys/systm.h>  /* uprintf */
    #include <sys/errno.h>
    #include <sys/param.h>  /* defines used in kernel.h */
    #include <sys/kernel.h> /* types used in module initialization */

    /*
     * Load handler that deals with the loading and unloading of a KLD.
     */

    static int
    skel_loader(struct module *m, int what, void *arg)
    {
      int err = 0;

      switch (what) {
      case MOD_LOAD:                /* kldload */
        uprintf("Skeleton KLD loaded.\n");
        break;
      case MOD_UNLOAD:
        uprintf("Skeleton KLD unloaded.\n");
        break;
      default:
        err = EOPNOTSUPP;
        break;
      }
      return(err);
    }

    /* Declare this module to the rest of the kernel */

    static moduledata_t skel_mod = {
      "skel",
      skel_loader,
      NULL
    };

    DECLARE_MODULE(skeleton, skel_mod, SI_SUB_KLD, SI_ORDER_ANY);

## 9.2.1 Makefile

FreeBSD provides a system makefile to simplify compiling a kernel module.

    SRCS=skeleton.c
    KMOD=skeleton

    .include <bsd.kmod.mk>

Running `make` with this makefile will create a file `skeleton.ko` that can be loaded into the kernel by typing:

    # kldload -v ./skeleton.ko

Prev | Home | Next
Writing FreeBSD Device Drivers | Up | Character Devices