Mmap system call (function)
void *mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
Memory mapping function Mmap, is responsible for the file content mapping to the process of virtual memory space, through the memory of the read and modify, to implement the file read and modify, and do not need to call read, write and other operations.
Addr: Specifies the starting address of the map, usually set to NULL, as specified by the system.
Len: Length of file mapped to memory
Prot: How the map area is protected can be:
Prot_exec: Map area can be executed
Prot_read: Map area can be read
Prot_write: Map area can be written
Flags: The attributes of the map area, which can be:
Map_shared:
Data written to the map area is copied back to the file, and other processes that map the file are allowed to share
Map_private:
Writes to the map area produce a copy of the map area (Copy-on-write), and changes made to this area are not written back to the original file.
FD: File descriptor returned by open, representing the file to be mapped
Offset: At the beginning of the file, it must be an integer multiple of the paging size, usually 0, which represents the mapping from the beginning of the file
de-mapping
int Munmap (void *start, size_t length)
Function:
Cancels the mapped memory that the parameter start points to, and the length of the parameter indicates the size of the memory to be canceled
return value:
The cancellation succeeds 0, otherwise returns-1, the cause of the error is stored in errno.
Attention:
Mmap does not affect the length of the original file, if the length of the write exceeds the length of the original file, then only the length of the original file can be written to the data content
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
int main (void)
-{
| int FD;
| Char *start=null;
| Char buf[100];
|
| FD = open ("T.txt", O_RDWR | O_creat);
| Start = Mmap (NULL, prot_read| Prot_write, map_shared, FD, 0);
|
| strcpy (buf, start);
| printf ("Buf is%s\r\n", buf);
|
| strcpy (Start, "Sky is Hello");
| Munmap (start, 100);
| Close (FD);
|
| return 0;
|}
Virtual Memory Area
A virtual memory area is a homogeneous interval in the virtual address space of a process, which is a contiguous address with the same characteristics
Range. The memory image of a process consists of the following parts: program code, data, BSS and stack area, and memory
The mapped region.
The memory area of a process can be viewed by/proc/pid/maps
Example: Cat/proc/6/maps
The fields for each row are:
Start_end Perm Offset Major:minor inode
Start: The region start virtual address
End: The region ends the virtual address
Perm: Read, Write, and execute permissions, indicating what the process is allowed to do for this zone. The last character of this field is either p for private, or s for shared
Offset: The starting address of the mapped part in the file
Major, Minor: primary and secondary equipment number
Inode: Index Node
Linux kernel uses structure vm_area_struct
(<linux/mm_types.h>) to describe the virtual memory area, where several key members are:
unsigned long Vm_start virtual memory region start address
unsigned long vm_end virtual memory area End Address
unsigned long vm_flags the region's markup. such as: Vm_io and vm_reserved.
Vm_io the VMA is marked as a memory-mapped IO area, Vm_io prevents the system from including the zone in the process's storage dump (core dump), and the VM_RESERVED flag memory area cannot be swapped out.
Mmap Device operation
Mapping a device refers to associating a segment of the user's space with the device's memory. When the program reads and writes the address of this user space, it is actually accessing the device.
The memory snap-in is associated with page-based management, specifically how to relate to Linux memory management.
What functions does the Mmap device method need to complete?
The Mmap method is a member of the file_oprations struct that is called when the MMAP system call is issued. Until then, the kernel has done a lot of work. The Mmap device method needs to do is to establish a virtual address to the physical address of the page table.
Int (*mmap) (struct file*, struct vm_area_struct *)
Mmap How to complete the creation of the page table?
There are two methods:
1. Use Remap_pfn_range to set up all page tables at once;
2. Use Nopage VMA method to set up one page table at a time
The work of constructing a page table can be done by the Remap_pfn_range function, with the following prototype:
int Remap_pfn_range (struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t prot)
VMA: Virtual memory Area pointer
VIRT_ADDR: The starting value of the virtual address
PFN: The physical page frame number where the physical address is to be mapped, the physical address can be >>page_shift, the macro definition is 12, dividing by 4KB
Size: The sizes of the areas to map
Protection properties of PROT:VMA
Example:
int memdev_mmap (struct file *filp, struct vm_area_struct *vma)
{
Setting Protection properties
Vma->vm_flags |= Vm_io;
Vma->vm_flags |= vm_reserved;
if (Remap_pfm_range (VMA, Vma->vma-start, Virt_to_phys (dev->data) >>page_shift,
Size, Vma->vm_page_prot))
Return–eagain;
return 0;
}
Dev-data is a virtual address, you need to use Virt_to_phys to convert to physical address, if it is a physical address, then do not have to convert.
This enables the operation of MMAP devices.
Welcome to Exchange If there is reprint please indicate the source
Sina Blog: http://blog.sina.com.cn/u/2049150530
Blog Park: http://www.cnblogs.com/sky-heaven/
Know: Http://www.zhihu.com/people/zhang-bing-hua
Linux kernel-driven--mmap device method "original"