Search code examples
unixmodulekernelsolariskernel-module

how to make loadable kernel module on solaris? no linux


1. how to create loadable kernel module on solaris 11?

  • simple loadable kernel module (hello world).
  • I searched, but only showed how to create a Linux kernel module.
  • in linux, header linux/kernel.h, but not included header on solaris

2. how to compile loadable kernel module on solaris 11?

  • gcc -D_KERNEL -m64 -c cpluscplus.cpp
  • Is it appropriate to compile as above?
  • 64bit, x86

Solution

  • Here's the minimal hello world kernel module I can come up with:

    #include <sys/modctl.h>
    #include <sys/cmn_err.h>
    
    /*
     * Module linkage information for the kernel.
     */
    static struct modlmisc modlmisc = {
            &mod_miscops, "test module"
    };
    
    static struct modlinkage modlinkage = {
            MODREV_1, (void *)&modlmisc, NULL
    };
    
    
    int
    _init(void)
    {
            return (mod_install(&modlinkage));
    }
    
    int
    _fini(void)
    {
            return (mod_remove(&modlinkage));
    }
    
    int
    _info(struct modinfo *modinfop)
    {
            cmn_err(CE_NOTE, "hello kernel");
            return (mod_info(&modlinkage, modinfop));
    }
    

    Compiling this as 64-bit binary with Oracle Developer Studio 12.6 and the Solaris linker like so:

    cc -D_KERNEL -I include -m64 -c foomod.c
    ld -64 -z type=kmod -znodefs -o foomod foomod.o
    

    For GCC you will likely need a distinct set of options.

    Then load it with:

    modload ./foomod
    

    This will complain about signature verification. This is innocuous unless you are running the system with Verified Boot enabled.

    Check that module is loaded:

    # modinfo -i foomod
    ID  LOADADDR         SIZE   INFO REV NAMEDESC
    312 fffffffff7a8ddc0 268    --   1   foomod (test module)
    # dmesg | tail -1
    Mar 16 12:22:57 ST091 foomod: [ID 548715 kern.notice] NOTICE: hello kernel
    

    This works on Solaris 11.4 SRU 33 running on x86 machine (VirtualBox instance in fact).