Linux Kernel Driver (4): 2. kernel address space

Source: Internet
Author: User

I. kernel space

The kernel space is mapped by the kernel. It does not change with the process and is fixed.

Ii. High-end memory: The physical memory address exceeds MB, which we call high-end memory.

3. kernel space distribution chart



(1) The address range of the kernel space is located at the 3g-4g location of the virtual address, and the size is 1 GB.

(2) direc memory regiont: This is called the direct ing zone because there is a linear Conversion Relationship between the linear address and the physical address of the region: linear address = 3G + physical address; eg: physical address range 0x100000 -- 0x200000 ing to linear space is 3G + 0x100000-3G + 0x200000

(3) vmalloc region: the address of this region is allocated by the kernel function vmalloc. Note that vmalloc is used for allocation, only to obtain a memory address, physical memory is not actually allocated to it (vmalloc V indicates virtual); it is characterized by linear space continuity, but the corresponding physical space is not necessarily continuous.

(4) permanent memory ing area (pkmap region): for high-end memory larger than MB, you can use this area for access (note that the kmap area is only a window for accessing the high-end physical memory, therefore, it is only 4 MB in size; in other words, we can access all the memory addresses above MB in the physical memory through a 4 m window ). Access Method:

I. Use alloc_page (_ gfp_highmem) to allocate high-end memory pages (with alloc_page, the physical memory is allocated, and the physical memory address cannot be used directly. You must map the pages first, so you need to use the following function)

Ii. Use the kmap function to map the allocated high-end memory to this region

(5) fixed ing area: the ing between virtual addresses in the fixed ing area and physical memory space addresses is fixed. Each address item in the fixed ing area serves a specific purpose, such as acpi_base.

IV,

[Kernel space memory dynamic application]

There are three main functions: kmalloc (), _ get_free_pages, and vmalloc.

The memory applied for by kmalloc (), _ get_free_pages is located in the physical address ing area and is physically continuous. They have only one fixed offset from the real physical address, therefore, there is a simple conversion relationship. The memory applied for by vmalloc is located in the vmalloc virtual memory allocation area (these areas are measured in linear addresses). It provides a continuous memory area in the virtual memory space. In essence, this continuous virtual memory does not necessarily exist in the physical memory, but there is no simple conversion relationship between the virtual memory applied by vmalloc and the physical memory.

 

Because vmalloc applied for continuous memory areas in the virtual memory space is not necessarily continuous in the physical memory, we can imagine that in order to complete vmalloc, a new page table needs to be created. Therefore, it is inappropriate to call vmalloc to allocate a small amount of memory.

Generally, kmalloc is used to allocate less than KB of memory, while vmalloc is used to implement larger memory blocks.

 

[Relationship between virtual addresses and physical addresses]

For virtual memory in the physical memory ing area of the kernel (applied using kmalloc (), _ get_free_pages), use pai_to_phys () and phys_to_virt () to convert physical addresses and kernel virtual addresses. In fact, it only implements 3G address shifting.

 

The above method applies to the regular memory (kernel physical memory ing area). There is no such simple conversion relationship between the virtual address of high-end memory and the physical address. Because it involves the page table control mechanism for separating physical pages.

 

[Ioremap]

In arm, device registers or storage block space are part of the memory space, which is called Io memory.

 

Before accessing the I/O memory in the kernel, we only have the physical address of the I/O memory. In this way, we cannot directly access the memory through software. We need to use ioremap () first () the function maps the physical address of the device to the kernel virtual address space (3 GB ~ 4 GB ). Then, you can access these Io memory resources by using access commands Based on the kernel virtual address range obtained by the ing.

 

After ing the physical address of the I/O memory resources into the core virtual address, theoretically we can directly read and write the I/O memory resources like the read/write Ram. To ensure the cross-platform portability of the driver, we should use special functions in Linux to access the I/O memory resources, instead of the core virtual address pointer.

 

[MMAP]

MMAP is used to map a device, which means that an address in the user space is associated with the device memory. This allows the program to read or write data within the allocated address range, it is actually access to the device. This type of data transmission is direct and does not require kernel space as an intermediate station for data transfer.

 

The remap_page_range function constructs a new page table for ing a physical address to map the kernel space to the user space.

 

In the initialization phase of the kernel driver, the physical address is mapped to the kernel virtual space through ioremap (). In the MMAP System Call of the driver, the remap_page_range () map the Rom to the user virtual space. In this way, both the kernel space and the user space can access the mapped virtual address.

 

Ioremap:

Process space, kernel space, Io memory

The other two refer to the same block of physical memory, but one is a virtual address and the other is a physical address. The process space and kernel space correspond to different physical addresses. data transmission between them is the actual data copy.

 

MMAP:

Process space, Io memory

The virtual address obtained by MMAP in the process space corresponds to the same physical address as the IO memory. There is no additional data transfer in this process. read/write operations are directly performed on the physical address of the hardware.

 

Generally, ioremap () is enough for the transmission of small data volumes,

 

[General Io memory access method]

1. The first step is to call request_mem_region () to request resources, that is, to tell the kernel that the driver is using the physical memory, and other drivers are not allowed to access them. In the device driver module load or open () function.

2. Map the Register address to the virtual address of the kernel space through ioremap (), and then access the registers of these devices through the programming interface of the Linux device. In the device driver initialization, write (), read (), IOCTL () function.

3. After the access is complete, release the virtual address applied for by ioremap () and release the IO memory resources applied for by release_mem_region. In the device driver module uninstall or release () function.

















Related Article

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.