Virtual Memory Management
Basic Concepts
Virtual memory management is a technology used by computer systems to manage memory. Each process has a continuous virtual address space. The size of the virtual address space is determined by the number of CPU bits. The maximum addressing space for a 32-bit hardware platform ranges from 0 GiB to 4 GiB. The 4 GiB space is divided into two parts: 3 GiB higher-address space for the LiteOS-A kernel and 1 GiB lower-address space for processes. The virtual address space of each process space is independent, and the code and data do not affect each other.
The system divides the virtual memory into memory blocks called virtual pages. The size of a virtual page is generally 4 KiB or 64 KiB. The virtual page of the LiteOS-A kernel is 4 KiB by default. You can configure memory management units (MMUs) as required. The minimum unit of the virtual memory management is a page. A virtual address region in the LiteOS-A kernel can contain one virtual page or multiple virtual pages with contiguous addresses. Similarly, the physical memory is also divided by page, and each memory block is called page frame. The virtual address space is divided as follows: 3 GiB (0x40000000 to 0xFFFFFFFF) for the kernel space and 1 GiB (0x01000000 to 0x3F000000) for the user space. The following tables describe the virtual address plan. You can view or configure virtual addresses in los_vm_zone.h.
Table 1 Kernel-space addresses
Addresses for loading the kernel code segment, data segment, heap, and stack. |
||
Addresses for allocating contiguous virtual memory. The mapped physical memory blocks may not be contiguous. |
Table 2 User-space virtual addresses
Address range for loading the user-space shared library, including the address range mapped by mmap. |
Working Principles
In virtual memory management, the virtual address space is contiguous, but the mapped physical memory is not necessarily contiguous, as shown in the following figure. When an executable program is loaded and runs, the CPU accesses the code or data in the virtual address space in the following two cases:
- If the page (for example, V0) containing the virtual address accessed by the CPU is mapped to a physical page (for example, P0), the CPU locates the page table entry corresponding to the process (for details, see Virtual-to-Physical Mapping"), accesses the physical memory based on the physical address information in the page table entry, and returns the content.
- If the page (for example, V2) containing the virtual address accessed by the CPU is not mapped to a physical page, the system triggers a page missing fault, requests a physical page, copies the corresponding information to the physical page, and updates the start address of the physical page to the page table entry. Then, the CPU can access specific code or data by executing the instruction for accessing the virtual memory again.
Figure 1 Mapping between the virtual and physical memory addresses
Development Guidelines
Available APIs
Table 3 Virtual memory management module APIs
How to Develop
To use APIs related to virtual memory:
-
Obtain the process space structure using the APIs for obtaining the process space, and access the structure information.
-
Perform the following operations on the virtual address region:
-
Call LOS_RegionAlloc to request a virtual address region.
-
Call LOS_RegionFind and LOS_RegionRangeFind to check whether the corresponding address region exists.
-
Call LOS_RegionFree to release a virtual address region.
-
-
Call vmalloc and memory requesting APIs to apply for memory in the kernel as required.
NOTE: The physical memory requested by using the memory requesting APIs must be contiguous. If the system cannot provide a large number of contiguous memory blocks, the request fails. Therefore, the memory requesting APIs are recommended for requesting small memory blocks. Non-contiguous physical memory can be obtained by using vmalloc. However, the memory is allocated in the unit of pages (4096 bytes/page in the current system). If you want memory that is an integer multiple of a page, you can use vmalloc. For example, you can use vmalloc to request memory for file reading in a file system, which demands a large cache.