home | list info | list archive | date index | thread index

[OCLUG-Tech] map kernel memory to user space without /dev/mem or knowing phys addresses?

  • Subject: [OCLUG-Tech] map kernel memory to user space without /dev/mem or knowing phys addresses?
  • From: "Robert P. J. Day" <rpjday [ at ] crashcourse [ dot ] ca>
  • Date: Wed, 13 Jan 2016 12:12:21 -0500
  quite possibly a silly question since i'm really supposed to know this
stuff but what's the canonical way to map a number of memory areas from
a single device into a process's user space if one doesn't know the
underlying physical addresses?

  first, i don't want to use /dev/mem since that requires knowing the
underlying physical addresses, and i consider /dev/mem being lazy,
anyway.

  and, second, let's assume the physical memory of interest is always
mapped, so i don't need to worry about page faults.

  so, consider a physical device that has, say, 4 different areas of
physical memory -- possibly to be mapped with different lengths and
different permissions. i'm going to assume the driver knows what the
physical addresses are so it knows how to complete the mmap() mappings
on request. so what should one do?

  as i see it, with just the one driver, i could define four special
device files, say:

* /dev/drv_area1
* /dev/drv_area2
* /dev/drv_area3
* /dev/drv_area4

and from within my user-space code, whenever i want to map one of the
areas corresponding to one of those device files, i'd open the
appropriate special device file and call mmap()with the normal parameters
and all with an offset of zero.

  within the driver, the mmap_fop() routine (since it is passed
the struct file* pointer) can trivially figure which device file is
being mmap'ed, and complete the VMA appropriately with the correct
physical address (which the user space code would never need to
know).

  am i making sense here? i'm assuming that, even if the user space
code doesn't know the physical address of each area it wants to map,
it needs to know the length as that will be part of the VMA already
set when the driver mmap() routine is called to complete the mapping,
yes?

  finally, if this is the way to go, is there a nice example in the
linux kernel source tree of exactly this sort of thing? thanks.

rday



message navigation