Userspace vs kernel space driver

22,482

Another consideration: it is far easier to debug user-space drivers. You can use gdb, valgrind, etc. Heck, you don't even have to write your driver in C.

There's a third option beyond just user space or kernel space drivers: some of both. You can do just the kernel-space-only stuff in a kernel driver and do everything else in user space. You might not even have to write the kernel space driver if you use the Linux UIO driver framework (see https://www.kernel.org/doc/html/latest/driver-api/uio-howto.html).

I've had luck writing a DMA-capable driver almost completely in user space. UIO provides the infrastructure so you can just read/select/epoll on a file to wait on an interrupt.

You should be cognizant of the security implications of programming the DMA descriptors from user space: unless you have some protection in the device itself or an IOMMU, the user space driver can cause the device to read from or write to any address in physical memory.

Share:
22,482
Katoch
Author by

Katoch

Updated on November 22, 2020

Comments

  • Katoch
    Katoch over 3 years

    I am looking to write a PWM driver. I know that there are two ways we can control a hardware driver:

    1. User space driver.
    2. Kernel space driver

    If in general (do not consider a PWM driver case) we have to make a decision whether to go for user space or kernel space driver. Then what factors we have to take into consideration apart from these?

    1. User space driver can directly mmap() /dev/mem memory to their virtual address space and need no context switching.
    2. Userspace driver cannot have interrupt handlers implemented (They have to poll for interrupt).
    3. Userspace driver cannot perform DMA (As DMA capable memory can be allocated from kernel space).