fix-mapped Addresses

Source: Internet
Author: User

First, preface

One day, Wowo classmate suddenly came to a sentence: if you want to start_kernel midpoint led,ioremap at what point in time to call it? I think he was trying to debug the initialization code after Start_kernel (for example, the code of the DtB parsing section) by a dot led. That day, two of us spent 20 minutes discussing related issues, and I felt very interesting, so I decided to write a document such as fix mapped address.

In the assembler code, because the MMU is not opened, how to access the peripheral is very simple, directly using physical address, however, after entering start kernel (open the MMU), want to access hardware is so inconvenient, at least need to obtain the virtual address through Ioremap to be accessible. However, in fact, in the initial phase of the kernel startup, the memory management subsystem is not ready,ioremap yet (it can be used normally after mm_init).

In fact, this requirement is related to the early Ioremap module, in addition to some other requirements, the kernel incorporates these requirements and presents the concept of fix mapped address. This article describes all aspects of fix mapped address, BTW, the code for this article comes from 4.4.6 Kernel, architecture-related code still chooses ARM64.

Second, what is Fixmap?

Fix in fix map refers to fixed meaning, so what is fixed? In fact, the virtual address is fixed, that is, some virtual address in the compilation (Compile-time) is fixed down, and these virtual address corresponding to the physical address is not fixed, is determined during the kernel startup process. To facilitate understanding of FIXMAP, we provide a specific example: DTB image processing.

In fact, DtB's handling did not use Fixmap at first, but was placed near the kernel image. The more specific requirement is that it must be placed within 512M of the kernel image start address, 8-byte aligned, and DTB image cannot cross the boundary of the 2M section size. The reason for this requirement, mainly to borrow Kernel image page table "Dongfeng", anyway, the creation of kernel image required PGD/PUD/PMD page table has been statically assigned, the requirements for DTB image can not need to create a new page table, Just add a entry to the original page table, but this design has the following problem:

(1) The location of the DTB image is limited, not very flexible (who does not want to be free?) Bootload copy DTB img When of course want to have no care AH).

(2) The position of the DTB image is checked at head. s, need to assemble the implementation (who want to write a compilation Ah, with C to achieve more easy to understand Ah, maintainability, portability how good AH).

Now, since the kernel has the support of early fixmap, the above restrictions can be relaxed. The process for the entire DTB image is as follows:

(1) bootloader copy DTB image to a location in memory. The specific location, of course, or to meet the 8-byte alignment, DTB image cannot cross the 2M section size boundary requirements, after all we also want a section mapping on the DTB image.

(2) Bootload passes the physical address of the DTB image through the register x0, DTB the virtual address of the image is determined when compiling the kernel image.

(3) The assembly initialization phase does not do any processing of DTB image

(4) In the initialization code after start kernel (specifically in the setup_arch--->setup_machine_fdt), create the relevant translation tables for DTB image, and then have free access to DTB Image.

Thirdly, why is there a concept of fixmap?

The dynamic allocation of virtual addresses and the establishment of address mappings is a complex process, after the kernel is fully booted, memory management can provide a variety of rich APIs to allow the kernel to complete the virtual address assignment and the creation of address mapping function, but during the boot process of the kernel, Some modules need to use virtual memory and mapping to the specified physical address, and there is no way for the modules to wait for the full memory management module to initialize before addressing the map. Therefore, the Linux kernel fixed assigned some fixmap virtual addresses, these addresses have a fixed purpose, the module that uses this address at the time of initialization, say these fixed assigned address mapping to the specified physical address up.

The most intuitive requirements come from debugging the initialization code, and think about the situation we face when we come to Start_kernel:

(1) We cannot access all of the memory, only access kernel image nearby.

(2) We cannot access any hardware, all IO memory is not mapping

Want to output error messages from the serial console? Sorry, the initialization of the console driver is still a long time away. Want to order a LED light to see how the kernel works? The Sorry,ioremp function requires Kmalloc to allocate memory, but the partner system is not initialized yet. What to do? One of the simplest methods is to simplify the allocation and management of virtual memory (the method used in Ioremp to manage virtual memory addresses is too heavy), and the simplest way is to fix virtual address.

Iv. where is the specific location of Fixmap?

The address area of the Fixmap is located between Fixaddr_start and Fixaddr_top, which can be consulted:

, the block in the red box is the exact location of the Fixmap address.

Five, Fixmap specific application in which scenarios?

The address area of the Fixmap has been further subdivided, as follows:

Enum Fixed_addresses {
Fix_hole,
Fix_fdt_end,
FIX_FDT = Fix_fdt_end + fix_fdt_size/page_size-1,

Fix_earlycon_mem_base,
FIX_TEXT_POKE0,
__end_of_permanent_fixed_addresses,

Fix_btmap_end = __end_of_permanent_fixed_addresses,
Fix_btmap_begin = Fix_btmap_end + total_fix_btmaps-1,
__end_of_fixed_addresses
};

By definition, the Fixmap address area is divided into two parts, part of which is called permanent fixed address, which is used for a specific kernel module, and the usage relationship is permanent. Another is called temporary fixed address, each kernel module can be used, after the use of the release, the module and virtual address is a dynamic relationship.

The main modules of the permanent fixed address include:

(1) DTB parsing module.

(2) Early console module. Standard Serial Console driver initialization in the entire kernel initialization process is very much behind the thing, if you can in the kernel start-up phase of a console, can output a variety of debug information is more buy wonderful things ah, early The console will be able to satisfy your desire to use early param to initialize the functionality of the module, so it can be used very early to help engineers understand the boot process of the kernel.

(3) Dynamic patching module. The body segment is generally mapped to read only, and the module can use fix mapped address to map the body segment of RW, from the dynamic modification of the program body segment, to complete the dynamic patching function.

Temporary fixed address is mainly used for early ioremap modules. Linux kernel opens fix_btmaps_slots slots in the virtual address space of the FIX map area (size of each slot is nr_fix_btmaps), and the modules in the kernel are capable of early_ioremap, early _iounmap interface to request or release the use of a virtual address for a slot.

fix-mapped Addresses

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.