Linux address space and MMAP ing

Source: Internet
Author: User
Article Title: Linux address space and MMAP ing. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Linux uses the virtual memory technology. All processes in the system share the memory in a virtual way. For each process, it seems that they can access all the physical memory of the entire system. More importantly, even a single process can have a much larger address space than the system's physical memory.

The process address space consists of linear address zones in each process. Each process has a 32-bit or 64-bit flat space. The specific size of the space depends on the architecture. "Flat" indicates that the address space range is an independent continuous interval. Generally, each process has a unique flat space, and the IP addresses of each process are irrelevant to each other. Two different processes can store different data in the same address memory of their respective address spaces. However, you can also select the shared address space between processes. We call such processes as threads.

In the address space, we are more concerned with the virtual memory address range that the process has access to, such as 08048000 ~ 0804c000. These accessible valid address ranges are used as memory areas. Through the kernel, the process can dynamically add or reduce memory areas to its address space.

The process can only access the memory address within the valid range. Each memory area also has specific access attributes that the corresponding process must follow, such as read-only, write-only, and executable attributes. If a process has accessed an address out of the valid range or accessed the valid address in an incorrect way, the kernel terminates the process and returns a "segment error" message.

   ? The memory area can contain various memory objects as follows:

   ? Memory ing of executable file code, which becomes a text section ).

   ? The memory ing of the initialized global variables of the executable file becomes the data section ).

   ? Contains the memory ing of a zero-page (bss segment) that does not initialize global variables. Zero page means that all data on the page is 0.

   ? It is used for the Zero-page memory ing of the process user space stack.

   ? The code segments, data segments, and bss of each shared library, such as the C library or dynamic link program, are also loaded into the address space of the process.

   ? Any memory ing file.

   ? Any shared memory segments.

   ? Any anonymous memory ing, such as memory allocated by malloc.

Any valid address of the process address space can only be located in a unique region, and these memory areas cannot overlap. As you can see, in the execution process, each memory segment corresponds to an independent memory region: Stack, object code, global variables, mapped files, and so on.

The kernel uses the memory descriptor to indicate the address space of the process. The memory descriptor is represented by the mm_struct struct and is defined in the file The structure contains all information related to the process address space.

VMA

The memory area is described by the vm_area_struct struct, which is defined in the file In the kernel, the memory area is often referred to as the virtual memory area or VMA.

The VMA mark is a bit mark defined in the vm_area_struct structure (vm_flags subdomain in this structure ). Unlike the access permission for a physical page, the VMA mark reflects the code of conduct that the kernel needs to comply with to process the page cable, rather than the hardware requirements. VM_IO indicates that the memory area contains a ing to the device I/O space. This flag is usually set only when the device driver executes the mmap () function for I/O space ing, it also indicates that the memory area cannot be contained in the core dump of any process. VM_RESERVED indicates that the memory area cannot be swapped out. It is also set when the device driver maps.

The vm_ops field in the vm_area_struct struct points to the operation function table related to the specified memory area, and the kernel uses the method in the table to operate VMA.

Mmap () and do_mmap (): Create an address range

The kernel uses the do_mmap () function to create a new linear address range. However, it is not very accurate to create a new VMA for the function, because if the created address range is adjacent to an existing address range and they have the same access permissions, then the two intervals are merged into one. If it cannot be merged, you must create a new VMA. In either case, the do_mmap () function adds an address range to the address space of the process-whether to expand an existing memory area or create a new area.

Do_mmap () function declaration in the file The prototype is as follows:

Unsigned long do_mmap (struct file * file, unsigned long addr,

Unsigned long len, unsigned long prot,

Unsigned long flag, unsigned long offset)

In the user space, you can call the mmap () function to obtain the do_mmap () function of the kernel function. The mmap () System Call prototype is as follows:

Void * mmap2 (void * start, size_t length,

Int prot, int flags,

Int fd, off_t pgoff)

The do_munmap () function deletes a specified address range from a specific process address space. Statement:

Int do_munmap (struct mm_struct * mm, unsigned long start, size_t len)

The system calls munmap () to provide a method for the user space program to delete the specified address range from its own address space, which is opposite to the function of the system call mmap:

Int munmap (void * start, size_t length)

Mmap device operations

For drivers, memory ing can provide users with the ability to directly access the device memory. Ing a device means that an address in the user space is associated with the device memory. At any time, as long as the program reads or writes data within the allocated address range, it is actually accessing the device.

Not all devices can perform mmap abstraction. For example, serial devices and other stream-oriented devices cannot implement this abstraction. Another limitation of mmap is that all mappings are in PAGE_SIZE units. The kernel can only process virtual addresses at the page table level. Therefore, the mapped area must be an integer multiple of PAGE_SIZE and must be located in the physical memory starting from the integer multiple of PAGE_SIZE. If the area is not an integer multiple of the page size, the kernel will generate a slightly larger area to accommodate it.

The mmap method is a member of the file_operations structure and is called when the mmap system call is executed. Before calling an actual method, the kernel will do a lot of work, and the prototype of this method is very different from that of the system call.

The file operation statement is as follows:

Int (* mmap) (struct file * filp, struct vm_area_struct * vma );

The vma parameter contains the virtual address range used to access the device. Most of the work has been completed by the kernel. To implement mmap, the driver only needs to construct a suitable page table for this address range. If necessary, replace vma-> vm_ops with a new operation set.

There are two ways to create a page table: Use the remap_page_range function to create all the page tables at a time, or use the nopage VMA method to create a page table at a time.

[1] [2] [3] [4] [5] Next page

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.