# can't load example device driver?



## mcqueenorama (Mar 3, 2018)

I'm running 11.1-RELEASE

I've done two example hello_world type device drivers from both the Handbook and the Intrepid book.

It compiles fine.  In fact I compiled the kernel without incident, so the system seems to be setup.

After I compile the example driver I try to load the hello.ko with:

sudo kldload ./hello.ko

It complains that it can't load the module because its already loaded in the kernel.

I've renamed it to a stupid name and it still happens.


I've checked the list of installed kernel modules, and my module names are not in that list.

What am I missing?  I can find nothing about this in a google search either.  Seems odd to me.


----------



## mcqueenorama (Mar 3, 2018)

I just learned about kern.securelevel, which seemed like a suspect, but its -1, so that's not it:


```
$ sudo sysctl -n kern.securelevel
-1
$
```

What else might be preventing me from loading the kernel module?


----------



## Phishfry (Mar 3, 2018)

mcqueenorama said:


> What else might be preventing me from loading the kernel module?


Nothing is preventing the module from loading. The way it sounds you have compiled the module into your kernel.
This is known as static method. Module is built into the kernel via KERNCONF.
With the dynamic method you would have to load the module up.
A kernel driver can be either static or dynamic depending on the code options you give the module.
Dynamic kernel modules reside in the /boot/kernel directory and have a .ko file extension.
To see if your static module is compiled into the kernel use this: `kldstat -v`


----------



## Phishfry (Mar 3, 2018)

I hate to give this advice but I see your using sudo. Things like loading a kernel module have to be done as root.
Many development features are going to require root as well.


----------



## mcqueenorama (Mar 3, 2018)

Thanks for the advice, but neither of those seem to be the problem.  Here's some output showing some basic facts.  I still don't know what it might be.  I wonder if dynamic loading is a feature that needs to be enabled.  Can anybody think of any other things I should check?  Its likely something fundamental that I've missed.


```
root@q:/usr/home/self/hello # kldstat
Id Refs Address    Size     Name
 1   18 0xc0400000 183fe94  kernel
 2    1 0xc9208000 6000     ng_ubt.ko
 3    5 0xc91f4000 d000     netgraph.ko
 4    1 0xc944e000 c000     ng_hci.ko
 5    3 0xc945f000 2000     ng_bluetooth.ko
 6    1 0xc9226000 f000     ng_l2cap.ko
 7    1 0xc953e000 20000    ng_btsocket.ko
 8    1 0xc9563000 4000     ng_socket.ko
 9    1 0xc95b2000 a000     tmpfs.ko
 
#check all kernel modules for hello
root@q:/usr/home/self/hello # kldstat -v | grep hello

#try to load it as root
root@q:/usr/home/self/hello # kldload ./hello.ko
kldload: can't load ./hello.ko: module already loaded or in kernel
root@q:/usr/home/self/hello #

#show hello in the source file and Makefile
root@q:/usr/home/self/hello # grep hello hello.c
static int hello_modevent(module_t mod_unused, int event, void *arg_unused) {
    uprintf("hello world\n");
static moduledata_t hello_mod = {
  "hello",
  hello_modevent,
DECLARE_MODULE(hello, hello_mod, SI_SUB_KLD, SI_ORDER_ANY);
root@q:/usr/home/self/hello # grep hello Makefile
KMOD = hello
SRCS = hello.c
root@q:/usr/home/self/hello #
```


----------



## strider2009 (Jul 7, 2019)

mcqueenorama said:


> Thanks for the advice, but neither of those seem to be the problem.  Here's some output showing some basic facts.  I still don't know what it might be.  I wonder if dynamic loading is a feature that needs to be enabled.  Can anybody think of any other things I should check?  Its likely something fundamental that I've missed.
> 
> 
> ```
> ...


did you figure out what the problem was?


----------



## strider2009 (Jul 11, 2019)

Its not a fix, but I only ran into this issue when running on the Raspberry Pi. Moving to an amd64 laptop and everything works fine. 
Not a fix, but maybe a workaround for anyone who stumbles across this in the future.


----------

