Linux memory ing (MMAP)

Source: Internet
Author: User

I. Overview

Memory ing, in short, is to map a memory area of the user space to the kernel space. After the ing is successful, the user's modifications to this memory area can be directly reflected in the kernel space, modifications made to this region by the kernel space also directly reflect the user space. Therefore, if the kernel space <----> user space requires a large amount of data transmission and other operations, the efficiency is very high.

First, the driver allocates a piece of memory, and then the user process uses the library function MMAP () to tell the kernel How much memory to map to the kernel space, after calling a series of functions, the kernel calls the MMAP function in file_operation of the corresponding driver, and CALLS remap_pfn_range () in this function to establish a ing relationship. To put it bluntly, the driver uses the remap_pfn_range () function in MMAP () to map a portion of the kernel space to a portion of the user space.

User space MMAP () function:

Void * MMAP (void * Start, size_t length, int Prot, int flags, int FD, off_t offset)

Start: the starting address of a memory area to be mapped in a user process. It is usually null (specified by the kernel)

Length: size of the memory area to be mapped

Prot: The expected memory protection flag

Flags: specifies the type of the ing object.

FD: file descriptor (returned by the open function)

Offset: The offset of the memory area of the user space to be mapped in the memory area allocated in the kernel space. The size is an integer multiple of page_size.

 

II. Implementation

First, the driver allocates one page of memory, and then the user process maps a page of memory in the user space to the kernel space through MMAP. After the ing is completed, the driver writes 10 bytes of data to the memory. The user process displays the data.

Driver:

1 # include <Linux/miscdevice. h> 2 # include <Linux/delay. h> 3 # include <Linux/kernel. h> 4 # include <Linux/module. h> 5 # include <Linux/init. h> 6 # include <Linux/mm. h> 7 # include <Linux/Fs. h> 8 # include <Linux/types. h> 9 # include <Linux/delay. h> 10 # include <Linux/moduleparam. h> 11 # include <Linux/slab. h> 12 # include <Linux/errno. h> 13 # include <Linux/IOCTL. h> 14 # include <Linux/cdev. h> 15 # include <Linux/string. h> 16 # include <Linux/list. h> 17 # include <Linux/PCI. h> 18 # include <Linux/gpio. h> 19 20 21 # define device_name "mymap" 22 23 24 static unsigned char array [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 25 static unsigned char * buffer; 26 27 28 static int my_open (struct inode * inode, struct file * file) 29 {30 return 0; 31} 32 33 34 static int my_map (struct file * filp, struct vm_area_struct * VMA) 35 {36 unsigned long page; 37 unsigned char I; 38 unsigned long start = (unsigned long) VMA-> vm_start; 39 // unsigned long end = (unsigned long) VMA-> vm_end; 40 unsigned long size = (unsigned long) (VMA-> vm_end-VMA-> vm_start); 41 42 // obtain the physical address 43 page = pai_to_phys (buffer ); 44 // map a VMA virtual memory area of the user space to a continuous physical page starting with page 45 if (remap_pfn_range (VMA, start, page> page_shift, size, page_shared) // The third parameter is the page frame number. The physical address shifted to page_shift to get 46 Return-1; 47 48 // write 10 bytes of data to the memory 49 for (I = 0; I <10; I ++) 50 buffer [I] = array [I]; 51 52 return 0; 53} 54 55 56 static struct file_operations dev_fops = {57. owner = this_module, 58. open = my_open, 59. MMAP = my_map, 60}; 61 62 static struct miscdevice MISC = {63. minor = misc_dynamic_minor, 64. name = device_name, 65. foPs = & dev_fops, 66}; 67 68 69 static int _ init dev_init (void) 70 {71 int ret; 72 73 // register a Hybrid device 74 ret = misc_register (& MISC); 75 // memory allocation 76 buffer = (unsigned char *) kmalloc (page_size, gfp_kernel ); 77 // set this part of memory to keep 78 setpagereserved (effec_to_page (buffer); 79 80 return ret; 81} 82 83 84 static void _ exit dev_exit (void) 85 {86 // cancel device 87 misc_deregister (& MISC); 88 // clear Keep 89 clearpagereserved (pai_to_page (buffer); 90 // release memory 91 kfree (buffer ); 92} 93 94 95 module_init (dev_init); 96 module_exit (dev_exit); 97 module_license ("GPL"); 98 module_author ("lkn @ scut ");

Application:

1 # include <unistd. h> 2 # include <stdio. h> 3 # include <stdlib. h> 4 # include <string. h> 5 # include <fcntl. h> 6 # include <Linux/FB. h> 7 # include <sys/Mman. h> 8 # include <sys/IOCTL. h> 9 10 # define page_size 409611 12 13 int main (INT argc, char * argv []) 14 {15 int FD; 16 int I; 17 unsigned char * p_map; 18 19 // open the device 20 FD = open ("/dev/mymap", o_rdwr); 21 if (FD <0) 22 {23 printf ("Open fail \ n"); 24 exit (1); 25} 26 27 // memory ing 28 p_map = (unsigned char *) MMAP (0, page_size, prot_read | prot_write, map_shared, FD, 0); 29 If (p_map = map_failed) 30 {31 printf ("MMAP fail \ n"); 32 goto here; 33} 34 35 // print the first 10 bytes in the mapped memory 36 for (I = 0; I <10; I ++) 37 printf ("% d \ n", p_map [I]); 38 39 40 here: 41 munmap (p_map, page_size); 42 return 0; 43}

Load the driver first and then run the application. The user space is printed as follows:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.