3

While I was going through the LDD3 book, in chapter 15, (memory mapping and dma), the introduction of mmap call says:

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

The confusion is regarding the address space. Why would device memory be mapped to user space since the kernel only takes care of the device. Why would one need to map it in user space. If the device memory is mapped in user space, why kernel manages it then? And what if the device could be used erroneously if it lies in user address space?

Please correct me if I am wrong, I am just new to it.

Thanks

2 Answers 2

3

The same chapter you are referring to, has the answer to your question.

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.

But you are correct that usually kernel drivers handle devices without revealing device memory to user 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.

In the end, it all depends on how you want your device to be used from user space:

  • which interfaces you want to provide from driver to user space
  • what are performance requirements

Usually you hide device memory from user, but sometimes it's needed to give user a direct access to device memory (when alternative is bad performance or ugly interface). Only you, as an engineer, can decide which way is the best, in each particular case.

Sign up to request clarification or add additional context in comments.

Comments

1

There are few usages I can think of:

  • user mode drivers - in this case kernel driver is only posing as a stub: for mapping memory to user space, passing interrupts, etc. (this is common for proprietary drivers).

  • some user space application is filling or reading DMA buffers directly, to avoid copying them between user space and kernel space.

Regards,

Mateusz.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.