Memory Paging for arm systems in Linux

Source: Internet
Author: User

Rk28PlatformLinuxSystemArmSystem memory paging awareness

Keywords

 

 

Arm, Memory management, paging, MMU, Virtual address

 

 

OverviewDescription

 

 

Memory paging of ARM chips in Linux

A Cognitive document,

Before reading this article, I think the reader understands MMU.

 

Contents

1. Overview... 5

2. Linux memory paging management... 5

3. Arm paging mode... 5

4. Data Structure related to memory paging... 5

5. Important system function calls... 5

6. Physical memory customization... 5

7. ing between virtual space and physical space... 5

8. Several Questions... 5

I. Overview

Memory Management is an extremely important part of the Linux system, involving virtual-physical address ing and addressing, memory paging, memory paging, memory swap, and memory allocation of the kernel and user processes, memory file system. This article only describes the memory paging and ing addressing of the Linux system ARM architecture.

II.LinuxPage Management supported

To support virtual memory management for multiple platform processors, the Linux (2.6.1) version later adopts the 4-level paging virtual address ing (3-level paging ing before this version) mode, it can meet the addressing requirements of 64-bit CPU. However, arm9-mmu only supports two levels of page table address conversion, and the two levels can meet the storage management needs of 32-bit CPU, so the arm system only uses two levels of pages in Level 4 Linux. The four-level paging mode of Linux is as follows:

Level 1:

Global page Directory table-corresponds to PGD in the Code (see pgtable. h) during system operation, the first address of the page table is stored in the C2 register of the arm coprocessor CP15. The C2 register is equivalent to the stack pointer SP and the program pointer PC, during task (process) switchover, the operating system without virtual memory only switches to SP and PC, and the operating system with virtual memory increases to switch to C2, that is, each process has its own independent virtual space, which also has an independent global page Directory table PGD.

2nd, 3, and 4

These three levels of behavior are basically the same, and are abbreviated

Pud -- parent directory of the page

-- Page center Directory

PTE -- page table (last level)

When the system accesses a 32-bit virtual address, if the cache is not hit and the address page does not exist in TLB, MMU reads the address of PGD in the global page Directory and retrieves the first several digits (32-macro pgdir_shift) of the virtual address for indexing in the table, the 32-bit integer indexed is the first address of the pud in the next-level page table, which is indexed to the last-level page table Pte. The index content in the PTE table (32-bit integer) the first digit is the physical page address corresponding to the virtual address, and the offset in the page is the last digit of the virtual address. In this way, a 32-bit physical address can be obtained.

The last few digits of the PTE index record the access attributes and permissions, read/write marks, and access marks of this page for MMU control.

In Linux, macro page_shift, pmd_shift, pud_shift, and pgdir_shift define the length of each field occupied by these four levels of pages. (../ARCH/ARM/include/ASM/pgtable. h ))

III.ARM9.SystemMMUSupported paging Mode

The arm storage system supports the following page sizes: 1 MB, 64 KB, 4 kb, and 1 kb. Two types of table sizes are supported: coarse-grained and fine-grained. In Linux, arm adopts the 4 K page mode of coarse-grained page tables. For example, the valid bit of the first-level index address is 11 bits, the valid bit of the second-level index is 9 bits, and the offset in the page is 12 bits:

The page size of 4 kb determines that the virtual address is 12 bit low for the offset address. It also determines the low 12 bits of the second-level page descriptor as the user flag. The 4 K page size also determines that the virtual address space can be mapped at most (4 GB/4 kb = 1024 × 1024) pages. The following Macros in the program are used to define the page size: (ARCH/ARM/include/ASM/page. h)

# Define page_shift 12

# Define page_size (1ul <page_shift)

From the above, we can see that the total length of the offset in the second-level page table plus pages is 12 + 9 = 21. Therefore, the number of bits in the intermediate page table defined in the program is: (ARCH/ARM/include/ASM/pgtable. h)

# Define pmd_shift 21

# Define pgdir_shift 21

The length of each table can be obtained by the number of digits of each index table:

# Define ptrs_per_pte 512 indicates that each last-level page table PTE contains 512 entries (9bit)

# Define ptrs_per_pmd 1 indicates that the top-level page table (PMD) is equivalent to the last-level page table (PTE ).

# Define ptrs_per_pgd 2048 indicates that the global page directory contains 2048 entries (11bit)

Therefore, in the arm system, the physical memory and virtual memory are paged by 4 kb. The page index table is divided into two levels. One global level page table is PGD, and the table contains 2048 entries, each entry corresponds to the physical first address of a second-level page table. There are a maximum of 2048 two-level page tables (PMD or PTE). Each table contains 512 entries, and each entry corresponds to the first physical address of a page.

In combination, the second-level paging of the arm system in the Linux system can be briefly described as follows:

When pud is started, this level is blocked, while PMD is equivalent to the same level of Pte. Therefore, the second-level paging of arm in Linux is:

Virtual Address --> PGD Conversion --> PTE Conversion --> physical address


Iv. Related Data Structure

1,Page descriptor, defined in the include/Linux/mm_types.h File

Struct page {,

}

Each (physical) Page has a data structure of this type and stores the status information of this page, for example, whether the page belongs to the kernel or user, whether it is idle, reference count, whether to cache and other information. This structure occupies 32 bytes of space. The kernel uses this structure to grasp the information of a page. The number of pages is the number of page descriptors, which are stored in the mem_map array by the kernel. 1/128 Of the total physical memory required

2,Page table items, page intermediate directory items, the data structure of the parent directory items on the page and the global directory items on the page are all 32bit integers:

Typedef struct {unsigned long PTE;} pte_t; -- type of the page table item

Typedef struct {unsigned long PMD;} pmd_t; -- type of directory items in the middle of the page

Typedef struct {unsigned long PGD [2];} pgd_t; -- type of global directory items on the page

3,Mm_struct (include/Linux/sched. h). This structure describes the storage and distribution of processes. The continuous linearity controlled by a process is called a VMA (vm_area_struct). The linear space of a process is connected by multiple VMA linked lists.

5. Memory-related important calls

Pgd_allocAllocate a new global page Directory table, which is usually used in the process initialization phase.

BRK-- Adjust the Dynamic Data Segment Size of a process.

_ Get_free_pagesObtain consecutive pages

_ Alloc_pagesObtain consecutive pages

KmallocAllocate space from slab cache and its physical memory is continuous

VmallocThe virtual memory in the virtual memory area is continuous, but the physical memory is not necessarily continuous.

ShmgetAllocate a shared storage area for inter-process Sharing

MMAP,MunmapMap a file to memory

Vi. Definition of physical memory

In Linux, you only need to provide the first address and size of the physical memory to define the physical memory for use by the kernel. The macros are located in/ARCH/ARM/include/ASM/memory. h.

Define the first address of the physical memory:

# DefinePhys_offset(Config_dram_base)

Define the last address of the physical memory:

# DefineEnd_mem(Config_dram_base + config_dram_size)

Define the start address of the physical page

# DefinePage_offset(Phys_offset)

7. ing from virtual space to physical space

The low 3 GB part of the virtual space ranges from 0-0xbfffffff's virtual linear address. Both the user and kernel states can be addressable, which is also the independent space of each process.

The high 1g part of the virtual space ranges from 0xc0000000 to 0xffffffff. Only kernel-State processes can access the virtual address. This restriction is determined by the permission flag of the page Directory and page table descriptor, automatically Controlled by MMU.

From the distribution of virtual space and to the physical address space ing:

The kernel space of 1 GB is divided into several parts:

1. Linear address ing zone(Kernel Logical Address): The maximum 896m space starting from 0xc0. When the physical memory is smaller than 896m, it is equal to the physical memory size. In this area, the virtual addresses and physical addresses are linearly mapped. There is only one constant difference between the virtual addresses and physical addresses. All physical memory is also managed in the kernel. The physical memory applied in this region through kmalloc is continuous.

2. Virtual Address ing area (vmalloc area): the virtual addresses in this area are continuous, but the corresponding physical addresses may be discontinuous (paging ), the physical address of the block applied in this region through vmalloc may be discontinuous.

3. High-end ing zone: when the physical memory is larger than MB, the excess physical memory cannot be directly addressable by the kernel.

Low 3G user space is mainly divided into static code segments, read-only data segments, writable data segments, shared libraries and dynamic stacks and stacks. A dynamic stack can be adjusted by its own growth. A dynamic stack can be adjusted by calling BRK. When the process heap is insufficient, the process allocates more pages to the kernel for adjustment.

In summary, ing from a virtual space to a physical space may occur in many cases. The same physical page may be mapped to multiple virtual pages, for example: the kernel logical address is the linear address ing area above. Its ing is linear and continuous, and the kernel virtual address is the vmalloc area above. Its ing to physical space is not consecutive. The physical addresses mapped to various data in the user process space are not consecutive and overlap with the physical address areas used by the kernel. Describes the general situation:

VIII. Several other issues related to memory paging:

1Who controls the physical space?

Physical space is centrally controlled by the kernel and allocated to user processes as needed.

2How to isolate processes?

Each process has its own PGD (Global page Directory table). Therefore, each process has its own space ing. The kernel ensures that each process uses different physical space for ing to isolate it.

3How can we ensure that processes cannot access kernel data?

Controlled by the permission flag (System/user) of the page descriptor, combined with the S and R bits in the corresponding register C1 on the ARM9. the virtual address is greater than 0xc0000000, the page description indicates that it can only be accessed in ARM system mode.

4Does the ing from virtual memory to physical memory ensure system security and stability?

The pointers used by the master processor are all virtual addresses. Therefore, the data that the master processor can access can only be data within the ing, this ensures that User Tasks running on the main processor do not access data of their own. (Based on the assumption that the kernel trusts itself)

Except for the AMBA bus-host device other than the main processor, for example, the DMA controller fails to connect to the bus through MMU, so the physical address is used. If a user process assigns an invalid physical address (data for the master processor) to the DMA controller, data in any area of the physical memory may be destroyed !!!

5Why does a user program generally not work with Linux?System compilation together?

Because user programs generally have new processes, and the process can only be created in the form of a copy (fork or clone), because the need to create a new process space, the process entry function can only start one binary process file through exec, instead, it cannot be a static compilation target function (because static compilation cannot place the main function of the process in another independent 3G process space .) Therefore, user programs and libraries are compiled independently from Linux. We will further discuss this issue in other process-related documents.

6Can an application be executed when it exceeds the physical memory?

Yes. The paging and page feed mechanism makes it unnecessary for applications to load all the pages to the memory, but only loads some of the currently used pages to the memory, so that applications larger than the physical space can run. It's just about execution efficiency.

Other questions about storage management will not be discussed in this article for the time being. We hope you will not give any suggestions on errors and deficiencies.

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.