130 likes | 305 Views
The ‘mmap()’ method. Adding the ‘mmap()’ capability to our ‘vram.c’ device-driver. Purpose of ‘mmap()’. The ‘mmap()’ system-call creates a new region in the task’s virtual memory map, and associates that region with a file (or with a device-file such as ‘/dev/vram’)
E N D
The ‘mmap()’ method Adding the ‘mmap()’ capability to our ‘vram.c’ device-driver
Purpose of ‘mmap()’ • The ‘mmap()’ system-call creates a new region in the task’s virtual memory map, and associates that region with a file (or with a device-file such as ‘/dev/vram’) • It returns a pointer to this memory area • Thus it allows the program to access the file as if it were an array in user-space • Avoids ‘read()’ and ‘write()’ system-calls
Mapping vram to userspace Kernel Text/Data Kernel space VRAM m map() STACK VRAM User space RUNTIME LIBRARIES HIGHMEM ZONE NORMAL TEXT/DATA physical address-space virtual address-space
‘mmap()’ syntax void *mmap( void *start, size_t length, int prot, int flags, int fd, off_t offset );
The ‘page-protection’ bits • PROT_READ • PROT_WRITE • PROT_EXEC • PROT_NONE
The ‘flags’ bits • MAP_SHARED • MAP_PRIVATE • MAP_FIXED • MAP_ANONYMOUS • MAP_GROWSDOWN • MAP_DENYWRITE • MAP_EXECUTABLE • MAP_LOCKED • MAP_NORESERVE • MAP_POPULATE • MAP_NONBLOCK
The region to be mapped Total extent of the file file region to be mapped offset length start (virtual address)
Kernel data-structures task_struct mm_struct vm_area_struct mm vma vm_start vm_end pgd vm_start vm_end vm_start vm_end pgd_t page directory vm_start vm_end vm_start vm_end vm_start vm_end vm_start vm_end
The ‘munmap()’ syntax int munmap( void *start, int length );
Demo-program: ‘vrammdraw’ • We added ‘my_mmap()’ to our ‘vram.c’ device-driver (calling it ‘vramm.c’) • We wrote an application (actually a new version of our ‘vramdraw.cpp’ program) that calls our driver’s ‘mmap()’ method • You can try it out – it’s on class website
In-class exercise #1 • Write a new version of our ‘grabber1.cpp’ and ‘grabber2.cpp’ demo-programs which will ‘grab’ a screen-image and write it out to a file, but will NOT make system-calls to ‘read()’ from the ‘/dev/vram’ device-file (it will instead use ‘mmap()’ to map the video display memory into user-space so it can be accessed just like an ordinary array)
In-class exercise #2 • Use the Linux ‘time’ command to compare the execution-speed of your ‘grabber3’ to observed speeds of ‘grabber1’, ’grabber2’