1. Overview
The memory management function of the operating system is used to provide consistent address ing and Memory Page application and release operations to the operating system. In embedded real-time systems, memory management has different policies based on different systems. For some systems, there may be only a flat-style simple memory management mechanism.
2. Memory Management Mechanism:
In general, the memory management mechanism used by embedded systems mainly includes the following two types:
(1) Virtual Memory Management Mechanism:
Some embedded processors provide MMU and memory address ing and addressing functions in MMU, which makes memory management of the operating system more convenient. If MMU exists, the operating system will use it to convert the virtual address to the physical address. All applications only need to use the virtual address to address the data. This method of addressing the Primary and Secondary storage of the entire system using virtual addresses is called virtual memory in modern operating systems. MMU is a necessary condition for implementing virtual memory.
The virtual memory management method enables the system to run applications with a larger volume than the physical memory, and also implement the "On-Demand page adjustment" policy, which not only satisfies the program running speed, it also saves physical memory space.
In the L inux system, the implementation of the virtual memory mechanism provides us with a typical example: three or two levels of page management are used in different architectures, MMU is used to convert virtual addresses to physical addresses. The biggest advantage of memory based on virtual memory management is that different processes have their own separate process space, which effectively improves system reliability and security.
(2) Non-Virtual Memory Management Mechanism
When real-time requirements are high, many embedded systems do not need a virtual memory mechanism, because the virtual memory mechanism will lead to uncertain I/O blocking time, making the program running time unpredictable, this is a critical defect of real-time embedded systems. In addition, most embedded microprocessor without MMU is used for the cost of embedded processors. Therefore, most embedded systems adopt real-time memory management policies. Therefore, the access to the memory is direct. The access to the address does not need to go through MMU, but is directly sent to the online address for output. The addresses accessed in all programs are actual physical addresses, most embedded operating systems do not protect the memory space. Each process actually shares a runtime space. Before a process is executed, the system must allocate enough continuous address space for it and load all of it into the continuous space of the primary memory.
Therefore, developers of embedded systems have to participate in system memory management. Starting from compiling the kernel, developers must tell the system How much memory the Development Board has. When developing applications, they must consider the memory allocation and pay attention to the size of the runtime space required by applications. In addition, because the real-memory management policy is adopted, the user program is in the same address space as the kernel and other user programs. During program development, ensure that the address space of other programs is not infringed, so that the program will not damage the normal operation of the system, or cause exceptions in other programs; therefore, developers of embedded systems must be especially careful about some memory operations in the software.
UCOS is an example of non-Virtual Memory Management. In UCOS, all tasks share all the physical memory and there is no memory protection mechanism between tasks, which can improve the system's corresponding time, however, improper task memory operations may cause system crashes.
3. life cycle in the system:
For the entire embedded operation process, it exists in the "3" way:
1) In the bootstraping stage, the memory is allocated in the form of temporary memory. After the system is started, the memory will be recycled for future use by the system.
2) In the normal operation stage, the memory exists in two ways:
(1) The permanent memory allocated by the system for code and data will not be changed during system operation, some hardware peripherals such as I/O also map the corresponding address to a fixed memory space.
(2) dynamic memory allocation space: these memories are not allocated fixed, but are dynamically allocated according to system requirements. If the non-virtual memory management mechanism is used, in general, the dynamic memory allocation mechanism needs to be modified to improve performance.
4. Memory Management applications:
A. Linux memory management mechanism
In the Linux memory management mechanism, the X86 architecture is typical of using virtual memory management. On the i386cpu, segment ing is required first. Linux does not use segment management, it defines the size of the mid-section descriptor segment of gdt as 4 GB, that is, only a segment is divided, so that the segment ing does not work. In page-based ing, for Embedded i386 chips, it is actually a two-layer ing, skipping the middle Of the PMD layer. For programs, not all Virtual Memory maps to physical space, but dynamic ing. If the kernel finds that the virtual page is not mapped or the disk page is mapped when the program is running,
Corresponding page missing processing will be performed-allocate the Memory Page and create a ing, and then resume the program running.
During the process of running the program, the memory operations involved mainly include memory allocation, memory usage, memory recovery, Memory Page Swap-out, and Page Swap-in. Memory Allocation is performed in the Free Zone of the management area. The Buddy algorithm is used to obtain the required memory block in the free_area of the management area. If the memory is insufficient, kswapd daemon is started to free up some physical memory. In addition to being called, the kswapd process also starts regularly. Kswapd has two parts:
(1) Check the remaining physical memory. If there is a shortage, disconnect the ing of some interchangeable pages in the active_list Queue according to the LRU policy to make the page inactive, link to the inactive_clean_list queue or inactive_dirty_list queue to prepare for the exchange.
(2) execute every time, write the pages in inactive _ dirty_list to the switch device, and reclaim some pages in inactive_clean_list.
Shielding of virtual memory in Linux
Because of the unpredictable time in the virtual memory, the virtual memory mechanism must be blocked for systems with high real-time requirements. In uClinux, this technology is used to ensure the real-time performance of the system. The following describes how to block the virtual memory mechanism:
To meet the real-time requirements of some tasks in industrial control, the virtual memory management mechanism of the kernel must be shielded to enhance the real-time performance of Linux. When you want to change a mechanism of the kernel, you do not need to rewrite the code on a large scale. You can use the Conditional compilation method. The idea is to use # ifdef or # ifndef to shield existing statements and include self-written code in # else macro compilation statements. The mechanisms for implementing virtual memory include address ing, memory allocation and recovery, cache and refresh, page retrieval, swap, and Memory Sharing, the data structures and functions of these mechanisms will be shielded or modified, and relevant files will also be modified. The files to be modified are mainly
/Include/Linux,/mm,/Drivers/Char,/FS,/IPC/kernel,/init directory. The main changes are as follows: the main data structure related to virtual memory is vm_area_struct, which removes vm_area_struct from the mm_struct structure of the process. vm_area_struct abstracts the virtual memory processing method using vm_ops, block functions related to virtual memory operations. The memory ing is mainly implemented by do_mmap () and the Code of this function is rewritten. Cancel the exchange operation, block the structure and function declaration used for the exchange, and implement the Exchange Code. Cancel the kernel daemon kswapd.
B. UCOS memory management:
Like most embedded systems, the memory management of UCOS is flat memory. However, it is optimized based on the flat memory to reduce memory compaction during dynamic memory allocation, improves system performance.
The specific method of UCOS is to partition consecutive large blocks of memory. Each partition contains an integer with the same size and multiple partitions with different memory sizes in a system. In this way, the application allocates memory of the corresponding size from memory partitions of different sizes according to different requirements. For unused memory, it is released back to the original partition. This memory management algorithm solves the problem of memory compaction and improves the system performance. For specific UCOS implementation, see the source code. It is implemented by the following functions:
Osmemcreate (): Creates a memory partition.
Osmenget (): allocate a piece of memory to the application.
Osmenput (): reclaim memory that is no longer used by applications.
5. Conclusion:
Memory Management is an important aspect of embedded systems. The virtual memory management mechanism ensures process security and provides developers with a method to manage memory, this allows developers to focus more on other aspects. But it also brings about the defect of time uncertainty. Based on different system requirements, we can select the corresponding memory management policy. In most real-time systems, the non-virtual memory management mechanism is widely used, which ensures the real-time performance of the system, but increases the difficulty of Development and improper task memory operations, may cause system crash.
Because the request paging mechanism of virtual memory management has a great impact on the system's real-time performance, some developers have proposed a compromise, that is, the request paging mechanism of virtual memory management is not needed, only process protection, memory ing, and shared virtual memory are considered. This improves the real-time performance of the system and the security of the system. For details, see document 4.
References:
1. Design of Embedded System in Measurement and Control System Based on Linux. http://article.ednchina.com/Embeded/20070630090542.htm
2. the Linux kernel primer.
3. Research on the embedded l inux operating system. Liu wenfeng, Li Chengyuan, Li shanping
4. Research and Implementation of Virtual Memory Management Technology for Embedded Software. Qian Jing, Lu Dongxin, Xie Xin, and Xu lifeng
5. Understanding the Linux kernel.