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

Zone

Description

Property

DMA zone

Addresses for direct memory access (DMA) of I/O devices.

Uncache

Normal zone

Addresses for loading the kernel code segment, data segment, heap, and stack.

Cache

high mem zone

Addresses for allocating contiguous virtual memory. The mapped physical memory blocks may not be contiguous.

Cache

Table 2 User-space virtual addresses

Zone

Description

Property

Code segment

User-space code segment address range

Cache

Heap

User-space heap address range

Cache

Stack

User-space stack address range

Cache

Shared library

Address range for loading the user-space shared library, including the address range mapped by mmap.

Cache

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

Category

API

Description

Obtaining process memory space

LOS_CurrSpaceGet

Obtains the pointer to the current process space structure.

LOS_SpaceGet

Obtains the pointer to the process space structure corresponding to the virtual address.

LOS_GetKVmSpace

Obtains the pointer to the kernel process space structure.

LOS_GetVmallocSpace

Obtains the pointer to the vmalloc space structure.

LOS_GetVmSpaceList

Obtains the pointer to the process space linked list.

Operations related to the virtual address region

LOS_RegionFind

Checks whether a virtual address region exists in the process space based on the start address.

LOS_RegionRangeFind

Checks whether a virtual address region exists in the process space based on the address region.

LOS_IsRegionFileValid

Checks whether the virtual address region is mapped to a file.

LOS_RegionAlloc

Requests a free virtual address region.

LOS_RegionFree

Releases a specific region in the process space.

LOS_RegionEndAddr

Obtains the end address of the specified address region.

LOS_RegionSize

Obtains the size of a region.

LOS_IsRegionTypeFile

Checks whether it is a file memory mapping.

LOS_IsRegionPermUserReadOnly

Checks whether the address region is read-only in the user space.

LOS_IsRegionFlagPrivateOnly

Checks whether the address region has private attributes.

LOS_SetRegionTypeFile

Sets the file memory mapping attribute.

LOS_IsRegionTypeDev

Checks whether it is device memory mapping.

LOS_SetRegionTypeDev

Sets the device memory mapping attribute.

LOS_IsRegionTypeAnon

Checks whether it is an anonymous mapping.

LOS_SetRegionTypeAnon

Sets the anonymous mapping attribute.

Verifying address

LOS_IsUserAddress

Checks whether the address is in the user space.

LOS_IsUserAddressRange

Checks whether the address region is in the user space.

LOS_IsKernelAddress

Checks whether the address is in the kernel space.

LOS_IsKernelAddressRange

Checks whether the address region is in the kernel space.

LOS_IsRangeInSpace

Checks whether the address region is in the process space.

vmalloc operations

LOS_VMalloc

Requests memory using vmalloc.

LOS_VFree

Releases memory using vmalloc.

LOS_IsVmallocAddress

Checks whether the address is requested by using vmalloc.

Requesting memory

LOS_KernelMalloc

Requests memory less than 16 KiB from the heap memory pool; otherwise, requests multiple contiguous physical memory pages.

LOS_KernelMallocAlign

Requests memory with alignment attributes according to the following rules: obtain memory less than 16 KiB from the heap memory pool; otherwise, request multiple contiguous physical memory pages.

LOS_KernelFree

Releases kernel heap memory.

LOS_KernelRealloc

Reallocates the kernel memory space.

Others

LOS_PaddrQuery

Obtains the physical IP address based on the virtual address.

LOS_VmSpaceFree

Releases the process space, including the virtual memory region and page table.

LOS_VmSpaceReserve

Reserves a memory space in the process space.

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.

LOS_UserSpaceVmAlloc

Requests an address region in the user process space based on information such as the address, size, and permission.

How to Develop

To use APIs related to virtual memory:

  1. Obtain the process space structure using the APIs for obtaining the process space, and access the structure information.

  2. 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.

  3. 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.