Specifications
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
422
|
Chapter 15: Memory Mapping and DMA
struct page *(*nopage)(struct vm_area_struct *vma, unsigned long address, int
*type);
When a process tries to access a page that belongs to a valid VMA, but that is
currently not in memory, the nopage method is called (if it is defined) for the
related area. The method returns the
struct page pointer for the physical page
after, perhaps, having read it in from secondary storage. If the nopage method
isn’t defined for the area, an empty page is allocated by the kernel.
int (*populate)(struct vm_area_struct *vm, unsigned long address, unsigned
long len, pgprot_t prot, unsigned long pgoff, int nonblock);
This method allows the kernel to “prefault” pages into memory before they are
accessed by user space. There is generally no need for drivers to implement the
populate method.
The Process Memory Map
The final piece of the memory management puzzle is the process memory map struc-
ture, which holds all of the other data structures together. Each process in the sys-
tem (with the exception of a few kernel-space helper threads) has a
struct mm_struct
(defined in <linux/sched.h>) that contains the process’s list of virtual memory areas,
page tables, and various other bits of memory management housekeeping informa-
tion, along with a semaphore (
mmap_sem) and a spinlock (page_table_lock). The
pointer to this structure is found in the task structure; in the rare cases where a driver
needs to access it, the usual way is to use
current->mm. Note that the memory man-
agement structure can be shared between processes; the Linux implementation of
threads works in this way, for example.
That concludes our overview of Linux memory management data structures. With
that out of the way, we can now proceed to the implementation of the mmap system
call.
The mmap Device Operation
Memory mapping is one of the most interesting features of modern Unix systems. As
far as drivers are concerned, memory mapping can be implemented to provide user
programs with direct access to device memory.
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:
cat /proc/731/maps
000a0000-000c0000 rwxs 000a0000 03:01 282652 /dev/mem
000f0000-00100000 r-xs 000f0000 03:01 282652 /dev/mem
00400000-005c0000 r-xp 00000000 03:01 1366927 /usr/X11R6/bin/Xorg
006bf000-006f7000 rw-p 001bf000 03:01 1366927 /usr/X11R6/bin/Xorg
2a95828000-2a958a8000 rw-s fcc00000 03:01 282652 /dev/mem
2a958a8000-2a9d8a8000 rw-s e8000000 03:01 282652 /dev/mem
...
,ch15.13676 Page 422 Friday, January 21, 2005 11:04 AM