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