Mapping of Linux-driven virtual and physical addresses

Source: Internet
Author: User

In general, the Linux system, the process of 4GB memory space is divided into two parts------ user space and kernel space, the size of 0~3g, 3~4g.

Typically, a user process can access only the virtual address of the user space and cannot access the kernel space.

The user space for each process is completely independent and irrelevant, and the user process has separate page tables. Kernel space is mapped by the kernel, and it does not follow the process and is fixed. The kernel space address has its own page table, and the kernel's virtual space is independent of other programs.

in the kernel space between 3~4g, from low address to high address: physical memory Map Area- isolation-vmalloc virtual memory allocation-isolation zone- high- End memory Map area- dedicated page map area- reserved.

"Kernel Space memory Dynamic Request"

It consists of three functions: Kmalloc (), __get_free_pages, Vmalloc.

Kmalloc (), the memory applied by __get_free_pages is located in the Physical address mapping area, and is physically contiguous, with only a fixed offset from the real physical address, so there is a simpler conversion relationship. The memory that Vmalloc applies for is located in the Vmalloc virtual memory allocation area (which is measured as a linear address), which gives a contiguous memory area in the virtual memory space, in essence, there is no continuous physical memory in this contiguous virtual, and Vmalloc There is also no simple conversion relationship between the requested virtual memory and the physical memory.

Because Vmalloc applications in the virtual memory space contiguous memory area in the physical memory is not necessarily contiguous, you can imagine that in order to complete the Vmalloc, the new page table needs to be built, therefore, the knowledge call Vmalloc to allocate a small amount of memory is inappropriate.

In general, Kmalloc is used to allocate less than 128K of memory, while larger chunks of memory need to be implemented with Vmalloc .

"Virtual address and Physical address relationship"

For virtual memory in the internal nuclear Physics memory map area (with Kmalloc (), __get_free_pages request), use Virt_to_phys () and Phys_to_virt () To convert between physical addresses and kernel virtual addresses. In fact, it's just a 3G address shift.

The above method is suitable for conventional memory (internal nuclear Physics memory mapping area), the virtual address of high-end memory and physical address there is no such a simple conversion relationship. Because it involves separating the page Table control mechanism of the physical page.

"Ioremap"

In arm , this part of the device's register or storage block is part of the memory space, which we call IO memory.

Before accessing IO memory in the kernel, we only have the physical address of IO memory, which is not directly accessible through the software, and we need to first map the physical address of the device to the kernel virtual address space (3GB~4GB) using the Ioremap () function. These IO memory resources can then be accessed through access instructions based on the kernel virtual address range obtained by the mapping .

After the physical address of the I/O memory resource is mapped to a core virtual address, we can theoretically read and write the I/O memory resources as read and write Ram . To ensure cross-platform portability of drivers, we should use specific functions in Linux to access I/O memory resources rather than through pointers to the core virtual addresses.

"Mmap"

mapping a device with mmap means that a segment of the user's space is associated with the device's memory, which allows the program to read or write within the allocated address range, which is actually access to the device. This data transfer is straightforward and does not require the use of kernel space as the intermediate station for data transfer.

the function of the Remap_page_range function is to construct a new page table for mapping a physical address, which implements the mapping between kernel space and user space.

During the initialization phase of the kernel driver, thephysical address is mapped to the kernel virtual space by Ioremap (), and The block ROM is mapped to the user virtual space using Remap_page_range () in the driver's mmap system call. This allows the kernel space and user space to access the mapped virtual address.

Ioremap:

Process space ? Kernel space ? IO Memory

The latter two refers to the same piece of physical memory area, just one for the virtual address, and one for the physical address. Process space and kernel space correspond to different physical addresses, and data transfer between them is a copy of the actual data.

Mmap:

Process space ? IO Memory

Where the virtual address obtained by the process space mmap corresponds to the same physical address as IO memory. This process does not have additional data transfers, and reads and writes are directed to the physical address of the hardware.

In general, the transmission of small amounts of data with Ioremap () is sufficient,

" general access method for IO memory"

1. First, call the Request_mem_region () request resource, which tells the kernel that this driver is using this physical memory and that other drivers must not access them. In the device driver module load or the open () function.

2. The register address is then mapped to the virtual address of the kernel space via Ioremap () , after which the registers of these devices can be accessed through the Linux Device Access programming interface. In device-driven initialization, write (), read (), the IOCTL () function.

3. After the access is completed, the virtual address of the Ioremap () request should be released and the IO memory resources of the release_mem_region () request should be released . In the device driver module unload or release () function.

Mapping of Linux-driven virtual and physical addresses

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.