将设备内存映射到用户进程地址 space

Mapping device memory into user process address space

当我阅读 LDD3 书时,在第 15 章(内存映射和 dma)中,mmap 调用的介绍说:

mmap() system call allows mapping of device memory directly into user process address space.

混淆是关于地址 space。为什么要将设备内存映射到用户 space,因为内核只负责设备。为什么需要在用户 space 中映射它。如果设备内存映射到用户 space,为什么内核要管理它呢?如果设备位于用户地址 space 可能被错误使用怎么办?

我是新手,如有不妥请指正

谢谢

我能想到的用法很少:

  • 用户模式驱动程序——在这种情况下,内核驱动程序仅伪装成存根:用于将内存映射到用户 space、传递中断等(这对于专有驱动程序很常见) .

  • 某些用户 space 应用程序正在直接填充或读取 DMA 缓冲区,以避免在用户 space 和内核 space 之间复制它们。

此致,

马特乌斯

您所指的同一章节中有您问题的答案。

A definitive example of mmap usage can be seen by looking at a subset of the virtual memory areas for the X Window System server. Whenever the program reads or writes in the assigned address range, it is actually accessing the device. In the X server example, using mmap allows quick and easy access to the video card’s memory. For a performance-critical application like this, direct access makes a large difference.

...

Another typical example is a program controlling a PCI device. Most PCI peripherals map their control registers to a memory address, and a high-performance application might prefer to have direct access to the registers instead of repeatedly having to call ioctl to get its work done.

但是您是正确的,通常内核驱动程序在处理设备时不会向用户透露设备内存 space:

As you might suspect, not every device lends itself to the mmap abstraction; it makes no sense, for instance, for serial ports and other stream-oriented devices. Another limitation of mmap is that mapping is PAGE_SIZE grained.

最终,这完全取决于您希望用户如何使用您的设备 space:

  • 您希望从驱动程序向用户提供哪些接口space
  • 什么是性能要求

通常您会向用户隐藏设备内存,但有时需要让用户直接访问设备内存(当替代方案是性能不佳或界面丑陋时)。只有您,作为一名工程师,才能决定在每种特定情况下哪种方式最好。