Linux address mappings 1, 2, 3 (☆☆☆)

Source: Internet
Author: User
Tags exception handling

Welcome to Mad Fuzi Sina Weibo: Http://weibo.com/cpjphone

I. Linear mapping and nonlinear mapping

1. Memory Management

① Physical Memory Management:

The minimum management unit for Linux memory is the page, usually a page of 4K. When initializing,Linux also establishes a page management structure for each physical memory (remember that Linux is not uboot, That is, the management of physical memory is definitely on a Linux system, and when you manipulate physical memory, you are actually manipulating page pages. Some devices are mapped outside the physical memory address, which creates the page structure when used.

② Process Memory Management:

Linux processes are managed through VMA , and each process has a task_struct structure for maintenance , mm_struct The struct manages all the memory of this process. The maintainer of mm_struct is a VMA linked list, where each VMA node corresponds to a contiguous process memory. The continuation here refers to continuous in the process space, not necessarily contiguous in the physical space. If you request a piece of memory using malloc , the kernel adds VMA nodes to the process.

2. Why the kernel is not coincident with process space

Process:0~3g

Kernel:3g~4g

The process is combined with the kernel to use 4G of address space, rather than using 4G space, the benefit is that the process into the kernel is not required to switch the page table, reducing the consumption of incoming and outgoing cores.

In the 2.6 kernel, the kernel space (3g~4g) of all processes is shared.

After Linux starts, the first process is the init process, and its page table is consistent with the kernel page table, and all other processes in the system are the sons or descendants of the init process. Process creation in Linux is implemented by Fork () , and the PGD and PTE of the child process are copies of the parent process that copies the page table of the kernel process to each process. In the course of each process, their page tables may change, such as the occurrence of fault pages. If the Process page table changes, as long as the Process's page table entry (0g~3g) is sufficient, if the kernel page table changes, you must notify all processes to change their own maintenance of a kernel page table (3g~4g). The simplest approach is that every time the kernel page table changes, it is obviously inefficient to traverse all processes to change the kernel page tables that they maintain. The Linux kernel implements the consistency of the kernel page table through the page fault mechanism. When the kernel page table changes, only the kernel page table of the init process is changed. When the process accesses the page, a fault page exception occurs, in which the kernel pages table of the current process is updated by the init process in exception handling.

3. Nonlinear region

Nonlinear and linear regions are concepts in the kernel address space.

For the existence of nonlinear zones, the following explanations can be made.

The Linux physical memory space is divided into the DMA memory Area (DMA zone), the low-end memory area (Normal zone) and the high-end memory area (Highmem zone ) Three parts. The DMA Zone is usually small and only dozens of M. The partitioning of low-end memory areas and high-side memory areas stems from the limitations of the Linux kernel space size.

The Linux kernel has only 1G of space, and the kernel typically maps the physical memory to its address space, which is one by one mappings, which can improve memory access speed.

When the memory exceeds 1G , the linear access mechanism is not enough, only 1G of memory can be mapped, the remaining memory cannot be used by the kernel. Of course I can't stand it.

To solve this problem,Linux divides the kernel into linear and nonlinear regions. The linear zone specifies a maximum of 896M, and the remainder is a nonlinear zone. Unlike linear regions, non-linear zones do not advance memory mapping, but are dynamically mapped when used. The linear zone maps the physical memory into low-end memory, and the remaining memory is called high-end memory.

Assuming that the physical memory is 2G, the lot's 896M is low-end memory and is used by a linear map to the kernel. Other 1128M memory is high-end memory and can be used by non-linear zones of the kernel. Due to the use of 128M non-linear zones to manage more than 1G of high-end memory, it is usually not mapped, only when used to make the kmap map, after use to be released as soon as possible with Kunmap .

Using 128M to manage 1G of memory isn't it a little bit small horse-cart feel? In fact, because the majority of high-end memory to process use.

For a kernel with physical memory of 1G , the system does not really allocate 896M to linear space,896M maximum limit. Below is a 1.5G physical Memory Linux system real distribution, only 721M allocated to the low-end memory area, if it is 1G linxu system, allocated less.

Memtotal 1547MB

Hightotal 825MB

Lowtotal 721MB

When requesting high-end memory, if the high-end memory is not enough,Linux will go to the low-end memory area to apply, and vice versa.

6} Physical address space outside of Linux administration (otherAddr)

Theoretically, the system can manage a physical address space of 4G. x86 architecture more io space. Each device in the computer system is to occupy a certain physical address space, such as PCI device, so will not give 4G to memory, which also means that the system does not support 4G memory.

For this part of physical memory outside the physical address space, this space does not know what to call, here is temporarily called Device Zone.

Because the Device Zone is not managed by Linux , it is not managed by a page structure, so the memory used in Linux is directly using its physical address. The use of physical memory is achieved by applying the free page to Linux . Similarly, this block certainly does not map to linear zones, but instead maps to nonlinear regions using ioremap , or maps directly to process space with mmap .

Two. Memory Request

If the requested memory is low-end memory, because the low-end memory is always mapped in the Kernel page table, only one page_address () function is required to complete the conversion, which is the linear mapping in the diagram.

If the application is high-end memory, it is not so simple.

First, if you can apply to enough memory in the high-end, you need to map the non-linear zone first, and after the operation is de-mapped, we can use Kmap () and Kunmap () to solve the problem. If high-end memory does not find enough memory, it allocates a block in the low-end memory area, which calls the Kmap () and kunmap () functions, but only calls page_address () to get the address internally when implemented. Without having to map to a non-linear zone.

1. Process Space Memory allocation

Malloc/free: The most commonly used memory allocation function

Valloc/free: Allocated memory is aligned by page

2. Kernel Space Memory allocation

__get_free_pages/free_pages: Allocate low-end memory for the number of pages, cannot be allocated at high end

alloc_pages/__free_pages: Allocates izhiding pages of memory, which can be high-end memory

Kmalloc/kfree: Allocated memory is physically contiguous and can only be assigned at the low end

Vmalloc/vfree: The allocated memory is contiguous in the kernel space and is not physically contiguous. Vmalloc because it does not need to be continuous in the house, it will cause TLB jitter, so the performance is very poor, generally only when the large amount of memory must be requested to use, such as the dynamic Insert module.

Three. Address Translation

The transformation relationship between process space, kernel space and physical address is revealed.

In Linux , the physical address is represented by the page structure, and physical memory has generated page structure Management at initialization time, and the other address space needs to generate page Again for management (Ioremap). Physical addresses can be mapped to kernel space or process space, or physical addresses (page) can be removed from kernel space or process user space.

In all conversions, only mmap can be used in the process, and the others are kernel functions. Even with Mmap, the internals are implemented by using Remap_pfn_range in the kernel. All address space transformations are implemented in the kernel.

1. Process space and kernel space

Copy_from_user

Copy_to_user

These two functions are not pure address translation, they complete the process space and address space data copy, the use is very simple.

Staticssize_t Led_read (structFile *f, __userChar*p, size_t size, loff_t *off) {if(Copy_to_user (p, dev.mem, size)) {PRINTK (kern_alert "Led:led_read err.\n"); return-1;} PRINTK (kern_alert "Led:led_read Ok:off= %d.\n ", off);return 0;}Staticssize_t Led_write (structFile *f, __userChar*p, size_t size, loff_t *off) {if(Copy_from_user (Dev.mem, p, size)) {PRINTK (kern_alert "Led:led_ write err.\n"); return-1;} PRINTK (kern_alert "Led:led_ write Ok:off= %d.\n ", off);return 0;}

2. Process space and Physical address

Get_user_pages

This function completes the process address to the physical address conversion, note that the process address is not necessarily also to its, but the physical address is given in the form of a page ,4k alignment, when used to remember to add offset. The following is a routine,. The current is useful for representing the process that called the kernel function, which is a task_struct struct, where mm is the memory management structure of the process, inmm vmalist Manager process VMA linked list.

Staticssize_t Led_read (structFile *f, __userChar*p, size_t size, loff_t *off) {structPage *pg=NULL;Char*addr;intLoop, Ret;down_read (¤t->mm->Mmap_sem); RET= Get_user_pages (Current, current->mm, p,1,0,1, &PG, NULL); Up_read (¤t->mm->mmap_sem); addr= (Char*) Kmap (PG);if((page_size-(unsignedLong) P &0xfff) <8{PRINTK (kern_alert "led:led_read size too small); return 0;} memcpy (Addr+ ((unsignedLong) P &0xfff), Dev.mem,8); Kunmap (PG);p RINTK (kern_alert "Led:led_read Ok:off= %d.\n ", off);return 0;}

Remap_pfn_range

Mmap

This function completes the physical address to process space mapping, the address space of each process is represented by VMA , so the physical address is mapped into the VMA. The physical address mapped here is generally not memory, only the Device space outside of Linux management needs to be mapped in a physical address, while the general memory is used only with Kmalloc or malloc can apply directly.

mmap is a system call that is called when the user space needs to map the physical address directly, and its interior is generally implemented through Remap_pfn_range .

Call In Process:

caddr_t addr = mmap (NULL, Page_size, Prot_write, map_shared, F, paddr & Page_mask);

Implemented in the kernel:

Static int led_mmap (structstruct vm_area_struct *VMA) {         if (remap_pfn_ Range (VMA, vma->start,            + vma->vm_pgoff) >> page_shift,            VMA->vm_end–vma- >vm_start, vma->Vm_page_prot)) {           printk (kern_alert "Led:led_mmap error.\n");     return –eagain;} return 0 ;}

After the user calls Mmap , before entering Led_mmap , the process address space is already requested for the process space and will be stored in a new VMA struct if mmap first entry is NULL, the start address of this address space has Linux Auto-assigned, and the length is the second parameter. The led_mmap VMA is the previously assigned VMA.

Vm_pgoff is the paddrpassed in before mmap . epld_base_addr is the base address of the device if it is 0, then vm_pgoff is the actual physical location.

3. Kernel Space and Physical address

Kmap

Kmap implements the mapping of physical memory to the kernel address space, the physical memory address can be a low-end memory area, or a high-end memory area, if it is low-end, the same role as page_address .

Ioremap

Ioremap The physical address to the kernel space mapping, the so-called physical address is generally referred to as non-physical memory address space, that is not under the Linux Management of the physical address space. It can map this segment to a non-linear space in the kernel space.

Page_address

Simple address translation, only applies to the linear region, the implementation of the page to the kernel address space conversion.

Phy_to_virt

Virt_to_page

Virt_to_phy

Several simple address conversion inverse functions are used only for linear zones.

Original: http://www.cnblogs.com/Ph-one/p/4809714.html

Record: http://blog.chinaunix.net/uid-20528014-id-314322.html

----------------

Linux address mappings 1, 2, 3 (☆☆☆)

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.