• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Virtual Memory Management
2
3
4## Basic Concepts
5
6Virtual 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 user-mode processes. The virtual address space of each process space is independent, and the code and data do not affect each other.
7
8The 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**.
9
10**Table 1** Kernel-mode addresses
11
12| Zone| Description| Property|
13| -------- | -------- | -------- |
14| DMA zone | Addresses for direct memory access (DMA) of I/O devices.| Uncache |
15| Normal zone | Addresses for loading the kernel code segment, data segment, heap, and stack.| Cache |
16| high mem zone | Addresses for allocating contiguous virtual memory. The mapped physical memory blocks may not be contiguous.| Cache |
17
18**Table 2** User-mode virtual addresses
19
20| Zone| Description| Property|
21| -------- | -------- | -------- |
22| Code snippet| User-mode code segment address range.| Cache |
23| Heap| User-mode heap address range.| Cache |
24| Stack| User-mode stack address range.| Cache |
25| Shared databases| Address range for loading the user-mode shared library, including the address range mapped by mmap.| Cache |
26
27
28## Working Principles
29
30In virtual memory management, the virtual address space is contiguous, but the mapped physical memory is not necessarily contiguous, as depicted 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:
31
32- 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](kernel-small-basic-inner-reflect.md)"), accesses the physical memory based on the physical address information in the page table entry, and returns the content.
33
34- 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.
35
36  **Figure 1** Mapping between the virtual and physical memory addresses<br>
37
38  ![](figures/mapping-between-the-virtual-and-physical-memory-addresses.png "mapping-between-the-virtual-and-physical-memory-addresses")
39
40
41## Development Guidelines
42
43
44### Available APIs
45
46**Table 3** APIs of the virtual memory management module
47
48| API| Description|
49| -------- | -------- |
50|  LOS_CurrSpaceGet | Obtains the pointer to the current process space structure.|
51| LOS_SpaceGet | Obtains the pointer to the process space structure corresponding to the virtual address.|
52| LOS_GetKVmSpace | Obtains the pointer to the kernel process space structure.|
53| LOS_GetVmallocSpace | Obtains the pointer to the vmalloc space structure.|
54| LOS_GetVmSpaceList | Obtains the pointer to the process space linked list.|
55
56**Table 4** Operations related to the virtual address region
57
58| API| Description|
59| -------- | -------- |
60| LOS_RegionFind | Searches for and returns the virtual address region corresponding to the specified address in the process space.|
61| LOS_RegionRangeFind | Searches for and returns the virtual address region corresponding to the specified address range in the process space.|
62| LOS_IsRegionFileValid | Checks whether the virtual address region is mapped to a file.|
63| LOS_RegionAlloc | Requests a free virtual address region.|
64| LOS_RegionFree | Releases a specific region in the process space.|
65| LOS_RegionEndAddr | Obtains the end address of the specified address region.|
66| LOS_RegionSize | Obtains the size of a region.|
67| LOS_IsRegionTypeFile | Checks whether the address region is a file memory mapping.|
68| LOS_IsRegionPermUserReadOnly | Checks whether the address region is read-only in the user space.|
69| LOS_IsRegionFlagPrivateOnly | Checks whether the address region has private attributes.|
70| LOS_SetRegionTypeFile | Sets the file memory mapping attributes. |
71| LOS_IsRegionTypeDev | Checks whether the address region is device memory mapping.|
72| LOS_SetRegionTypeDev | Sets the device memory mapping attributes. |
73| LOS_IsRegionTypeAnon | Checks whether the address region is an anonymous mapping.|
74| LOS_SetRegionTypeAnon | Sets the anonymous mapping attributes. |
75
76**Table 5** APIs for address verification
77
78| API| Description|
79| -------- | -------- |
80| LOS_IsUserAddress | Checks whether the address is in the user space.|
81| LOS_IsUserAddressRange | Checks whether the address region is in the user space.|
82| LOS_IsKernelAddress | Checks whether the address is in the kernel space.|
83| LOS_IsKernelAddressRange | Checks whether the address region is in the kernel space.|
84| LOS_IsRangeInSpace | Checks whether the address region is in the process space.|
85
86**Table 6** APIs for vmalloc operations
87
88| API| Description|
89| -------- | -------- |
90| LOS_VMalloc | Requests memory using **vmalloc**.|
91| LOS_VFree | Releases memory using **vmalloc**.|
92| LOS_IsVmallocAddress | Checks whether the address is requested using **vmalloc**. |
93
94**Table 7** APIs for memory allocation
95
96| API| Description|
97| -------- | -------- |
98| LOS_KernelMalloc | Allocates memory from the heap memory pool if the requested memory is less than 16 KiB; allocates multiple contiguous physical pages if the requested memory is greater than 16 KiB. |
99| LOS_KernelMallocAlign | Allocates memory with alignment attributes. The allocation rule is the same as that of the **LOS_KernelMalloc** API.|
100| LOS_KernelFree | Releases the memory requested by **LOS_KernelMalloc** and **LOS_KernelMallocAlign**.|
101| LOS_KernelRealloc | Reallocates the memory requested by **LOS_KernelMalloc** and **LOS_KernelMallocAlign**.|
102
103**Table 8** Other APIs
104
105| API | Description |
106| -------- | -------- |
107| LOS_PaddrQuery | Obtains the physical address based on the virtual address. |
108| LOS_VmSpaceFree | Releases the process space, including the virtual memory region and page table. |
109| LOS_VmSpaceReserve | Reserves a memory space in the process space. |
110| LOS_VaddrToPaddrMmap | Maps the physical address region with the specified length to a virtual address region. You need to request the physical address region before the operation. |
111
112
113### How to Develop
114
115To use APIs related to virtual memory:
116
1171. Obtain the process space structure using the APIs for obtaining the process space, and access the structure information.
1182. Perform the following operations on the virtual address region:
119   - Call **LOS_RegionAlloc** to request a virtual address region.
120   - Call **LOS_RegionFind** and **LOS_RegionRangeFind** to check whether the corresponding address region exists.
121   - Call **LOS_RegionFree** to release a virtual address region.
122
1233. Call **vmalloc** APIs (see table 6) and memory allocation APIs (see table 7) to apply for memory in the kernel as required.
124
125> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
126>
127> The physical memory requested by using the memory allocation APIs must be contiguous. If the system cannot provide a large number of contiguous memory blocks, the request fails. Therefore, the memory allocation APIs are recommended for requesting small memory blocks.
128>
129> **vmalloc** APIs are recommended for requesting non-contiguous physical memory. 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** APIs. For example, you can use **vmalloc** to request memory for file reading in a file system, which demands a large cache.
130