File:  [NetBSD Developer Wiki] / wikisrc / projects / project / userland_pci.mdwn
Revision 1.1: download - view: text, annotated - select for diffs
Tue Feb 23 17:29:23 2016 UTC (3 years, 9 months ago) by riastradh
Branches: MAIN
CVS tags: HEAD
Add userland PCI drivers project.

[[!template id=project

title="Userland PCI drivers"

contact="""
[tech-kern](mailto:tech-kern@NetBSD.org)
"""

mentors="""
[Taylor R Campbell](mailto:riastradh@NetBSD.org)
"""

category="kernel"
difficulty="medium"
duration="2-3 months"

description="""
When developing device drivers inside the kernel, mistakes will usually
 cause the whole kernel to crash unrecoverably and require a reboot.
But device drivers need not run inside the kernel: with
 [rump](http://rumpkernel.org/), device driver code can be run as a
 process inside userland.

However, userland code has only limited access to the hardware
 registers needed to control devices: currently, NetBSD supports only
 USB device drivers in userland, via
 [ugen(4)](http://netbsd.gw.com/cgi-bin/man-cgi?ugen++NetBSD-current).
NetBSD should additionally support developing PCI drivers in userland
 with rump -- at least one driver,
 [iwm(4)](http://netbsd.gw.com/cgi-bin/man-cgi?iwm++NetBSD-current), was
 developed using rump, but on a Linux host!

There are several milestones to this project:

1. Implement enough of the
 [bus_space(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current)
 and
 [pci_mapreg(9)](http://netbsd.gw.com/cgi-bin/man-cgi?pci+9+NetBSD-current)
 APIs to map device registers from PCI BARs, using a
 [/dev/pciN](http://netbsd.gw.com/cgi-bin/man-cgi?pci+4+NetBSD-current)
 device.
A first approximation can be done using
 [pci(3)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current)
 and simply mmapping from
 [/dev/mem](http://netbsd.gw.com/cgi-bin/man-cgi?mem+4+NetBSD-current),
 but it would be better to cooperate with the kernel so that the
 kernel can limit the user to mapping ranges listed in PCI BARs without
 granting privileges to read all physical pages in the system.
Cooperation with the kernel will also be necessary to implement port
 I/O instead of memory-mapped I/O, without raising the I/O privilege
 level of the userland process, on x86 systems.

2. Expose PCI interrupts as events that can be read from a
 [/dev/pciN](http://netbsd.gw.com/cgi-bin/man-cgi?pci+4+NetBSD-current)
 device instance, and use that to implement the
 [pci_intr(9)](http://netbsd.gw.com/cgi-bin/man-cgi?pci_intr++NetBSD-current)
 API in userland.
For many devices, this may require a small device-specific shim in the
 kernel to acknowledge interrupts while they are masked -- but that is
 a small price to pay for rapidly iterating driver development in
 userland.

3. Devise a scheme for userland allocate and map memory for DMA in
 order to implement
 [bus_dma(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_dma++NetBSD-current).
Again, a first approximation can be done by simply wiring pages with
 [mlock(3)](http://netbsd.gw.com/cgi-bin/man-cgi?mlock++NetBSD-current)
 and then asking the kernel for a virtual-to-physical translation to
 program hardware DMA registers.
However, this will not work on any machines with an IOMMU, which would
 help to prevent certain classes of catastrophic memory corruption in
 the case of a buggy driver.
Cooperation with the kernel, and maintaining a kernel-side mirror of
 each `bus_dmamem` allocation and each `bus_dmamap`.

Inspiration may be found in the Linux
 [uio](https://www.kernel.org/doc/htmldocs/uio-howto/about.html)
 framework.
This project is not necessarily PCI-specific -- ideally, most of the
 code to manage
 [bus_space(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current),
 [bus_dma(9)](http://netbsd.gw.com/cgi-bin/man-cgi?bus_space++NetBSD-current),
 and interrupt event delivery should be generic.
The focus is PCI because it is widely used and would be applicable to
 many drivers and devices for which someone has yet to write drivers.
"""
]]

[[!tag gsoc]]

CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb