UCLinux system analysis Huaheng Company
Introduction Linux is a very popular operating system that is compatible with UNIX systems and open source. It was originally designed to desktop system, and now is now widely used in the server field. The greater impact is that it is gradually applied to embedded devices. Uclinux is produced under this atmosphere. In this English word in Uclinux, U represents micro, little meaning, C represents Control, controlling, so uclinux is Micro-Control-Linux, literal understanding is "Linux system designed for micro-control fields". UCLinux miniaturized practice Standard Linux Pixably Method 1. Re-compile the kernel Linux kernel adopt modular design, that is, many function blocks can be independently coupled or removed, the developer uses the kernel module as Optional options can be specified when compiling system cores. Therefore, a more common approach is to recompile the Linux kernel, carefully select the function support module required for embedded devices at compile time, while deleting unwanted features. By reconfiguring the kernel, the kernel required for the system can be significantly reduced, thereby reducing resource usage. 2. Making the ROOT file system Image Linux system must load root file system when starting, so the tailoring system includes clipping of root file system. Under x86, Linux can load start with Loadlin files in DOS, and the miniaturization method of Uclinux is 1. Uclinux's kernel load mode UClinux has two optional mode: can run directly on Flash, or load it into memory. This approach can reduce memory needs. Flash Operation: Write the kernel's executive image to flash, and execute it from a address of Flash when the system starts. This method is actually a method of using many embedded systems. Nuclear load mode: Store the compressed file of the kernel in Flash, read the compressed file when the system starts to decompress in the memory, then start execution, this method is relatively complex, but the run speed may be faster (RAM's access rate Higher than Flash). At the same time, this is also the start-up mode of the standard Linux system. 2. Uclinux's root (root) file system UCLinux system uses ROMFS file system, which requires less space relative to the general EXT2 file system. Space savings come from two aspects, first-core support ROMFS file system requires less code than supporting EXT2 file systems, secondary ROMFS file system is relatively simple, and a fewer storage space is required in the establishment of file system hyper block (SuperBlock). The ROMFS file system does not support dynamic erasing storage, and the system that requires dynamic saving data using the virtual RAM disk method (the RAM disk will adopt an EXT2 file system). 3. Another practice of UClinux's application library UClinux is to rewrite the application library, which is streamlined with LIBC relative to increasingly large and increasingly full GLIBC libraries. Uclinux uses a static connection to the user program, which makes the application becomes large, but the problem based on memory management, has to do this (this will be described below), this The practice is also closer to usual embedded systems. UCLinux Development Environment GNU Development Kit GNU Development Kit As a general Linux open suit, including a range of development and debugging tools. The main components: GCC: Compiler, can make the form of cross-compilation, that is, developed two-creditable binaries on the translation target on the host.
Binutils: Some auxiliary tools, including Objdump, AS (Compiler), LD (Connector), and more. GDB: Debugger, you can use a variety of cross-debugging methods, GDB-BDM (Background Debug Tools), GDBServer (using Ethernet debugging). UCLinux print terminal is usually, UCLinux's default terminal is serial port, and the kernel is printed to the serial terminal (printing using the Printk function) at startup, and can also interact with the system through the serial terminal. Uclinux launches Telnetd (remote login service) at startup, and the operator can log in to the system remotely to control the operation of the system. As for whether remote login is allowed to have a user to determine whether the remote login service is started by burning the ROMFS file system. The cross-compilation debugging tool supports a new processor, must have some compile, assembly tools, using these tools to form binaries that can run this processor. The compilation tool used by the kernel is different from the application. Before explanation, you need some explanation to the GCC connection: .ld (link description) file: The LD file is a file that indicates the memory image format when the connection is connected. CRT0.s: The startup file required when the application is compiled, mainly to initialize the application stack. PIC: POSITION Independence Code, a binary format file independent with location, must include a RELOC segment in the block, so that the code is loaded. When the kernel is compiled, use the UCSIMM.LD file to form an executable file image, and the resulting code segment can be used either indirect addressing mode (ie, adding the RELOC segment), or absolute addressing mode. This can give the compiler more optimized space. Because the kernel may use absolute addressing, the memory address space loaded by the kernel must be identical to a given memory space given in the LD file. The application's connection is different from the kernel connection. The application is loaded by the kernel (the executable file loader will discuss later), because the memory space given by the application's LD file is different from the memory location actually loaded, so that one is required during the application loading The process of resetting, that is, correcting the RELOC segment so that the program is indirectly addressed. (This issue is different on the method of advanced processors such as I386, which will be further analyzed later). From the above discussion, at least two sets of compilation tools are required. This article will give the workflow of the entire system and the spatial distribution of the system in Flash and RAM after discussion of the memory management of UCLinux. The executable file format is first written for some nouns: Coff (Common Object File Format): A universal object file format ELF (Excutive Linked file): a universal file format used in the Linux system, supports dynamic connection Flat : The ELF format has a large file header, the FLAT file makes the file header and some segment information simplified the UCLinux system using the FLAT executable file format, and the GCC's compiler cannot form this file format, but can form a COFF or ELF format. The executable, these two files require the Coff2flt or ELF2FLT tool to format to form a FLAT file.
When the user performs an app, the execution file loader of the kernel will further process the FLAT file, mainly to fix the Reloc segment (see FS / BINFMT_FLAT.C for the executable file loader). The following discusses the RELOC segment. The root cause of the RELOC segment is that the program is different from the program running space assumed by the connector when the program is connected to the memory space loaded with the actual program. If there is such an instruction: jsr app_start; this instruction is executed directly, jumps to the App_Start address, the connection program will calculate the actual address of the app_start (if the actual address is 0x10000), this actual address is The LD file is calculated (because the connector assumes that the program will be loaded into memory space indicated by the LD file). However, due to the relationship between memory allocation, the operating system cannot guarantee that the program will be loaded according to the LD file when loading. At this time, if the program is still jumped to the absolute address 0x10000, it is usually not correct. A solution is to add a storage space for the actual address of App_Start, and set the storage space if the variable addr is used. Then, the above program will be changed to: MOVL ADDR, A0; JSR (A0); added variable addr will occupy a 4-byte space in the data segment, and the connector stores the absolute address of the app_start to the variable. When the executable file is loaded, the executable file loader calculates the actual location in memory according to the memory space to be loaded, and writes the ADDR variable. The system does not need to know the exact storage location of this variable (it is impossible to know), and the system can be read as long as the entire Reloc section is handled (the system can be read). Processing is simple, only need to unify a value stored in the RELOC segment (if the loaded space is longer than expected, it is actually minus an offset). The bias is calculated from the actual physical address start value to the address start value specified by the LD file. This Reloc manner is caused by UCLinux memory allocation issues, which will be described in UCLinux memory management analysis. Aiming at real-time solution UCLinux does not pay attention to real-time issues, it is not proposed for the real-timeness of Linux. There is another Linux - RT-Linux focuses on real-time issues. The RT-Linux executive manager operates the normal Linux kernel as a task and manages the real-time process. The non-real-time process is handed over to normal Linux kernel processing. This method has been applied to many operating systems for enhancing the real-time performance of the operating system, including some commercial version UNIX systems, Windows NT, etc. One of this method is to achieve simple, and real-time performance is easy to test. Advantage 2 is that due to the non-real-time process runs on standard Linux systems, there is a lot of compatibility with other Linux commercial versions. Advantages The third application can support hard real-time clock applications. Uclinux can use RT-Linux's Patch to enhance UCLinux real-time, so that UCLinux can be applied to industrial control, process control, etc. Some real-time requirements for real-time requirements. Uclinux's memory management should say that UCLinux is the biggest difference between standard Linux lies in memory management, and because of UCLinux memory management, some standard Linux does not have problems. This article will compare the UCLinux memory management in comparison with the standard Linux's memory management section. The standard Linux uses virtual memory technology standard Linux uses virtual memory technology, which is used to provide much memory space than physical memory actually used in computer systems. The user will feel that the program can use very large memory space, so that the programmer does not need to consider the actual capacity of physical memory in the computer when the program is written.
In order to support the management of virtual storage managers, the Linux system uses paging (pangu) to load processes. The so-called paging is both a segment of the actual memory into the same size, such as 1024 bytes per segment, such as a 1024-byte size segment is referred to as a page (PAGE). Virtual memory is supported by memory management mechanisms and a large-capacity fast hard disk memory. Its implementation is based on local principles. When a program is running, it is not necessary to load memory, but only those parts or segments of the currently running into memory operation, the rest is temporarily If the page left if it is on the hard disk is run, if it is going to access, the program continues to run, if the page does not exist (segment), the operating system will generate a page error (this error causes the operation. The system is loaded into the memory in the system. If necessary, the operating system can also exchange unwanted memory pages (segments) to disk. With this way to manage the memory, you can use the memory you need to use to decrease into zero, depending on the requirements of the demand, and the core procedure completes the addressing of each page by page number belonging to each page. The work of the section. Standard Linux is designed for processors with memory management units. On this processor, the virtual address is sent to the memory management unit (MMU), map the virtual address to the physical address. Supports protection between different tasks by imparting different virtual-physical address conversion mappings different from each task. The address conversion function is defined in each task, and the virtual address space in one task is mapped to a part of the physical memory, while the virtual address space of another task is mapped to the additional area in the physical memory. The storage management unit (MMU) of the computer typically has a set of registers to identify the conversion table of the currently running process. When the CPU is discarded to another process (once context switch), the kernel loads these registers by pointing to the new process address. The MMU register is privileged and can only be accessed in the kernel. This ensures that a process can only access the address in your own user space without accessing and modifying the space for other processes. When the executable is loaded, the loader loads the program into a space of the virtual memory according to the default LD file, because this reason is actually the same virtual address space, but because the conversion function is different, The memory area where the location is also different. For multi-process management When the processor is handover and execute a new task, an important part is to switch the task conversion table for the new task. We can see that the memory management of the Linux system has implemented the following features: Running a program that is much larger than memory. Ideally should be able to run any size of the program ◇ You can run only partial programs, shorten the time of the program started to enable multiple programs to increase the Utilization of the CPU at the same time ◇ can run the replacement program. That is, the program can be anywhere in memory, and can be moved during execution. ◇ Write the machine-independent code. The program does not have to agree on the configuration of the machine in advance. ◇ 减 Reduce programmers allocate and manage the burden of memory resources. ◇ You can share - for example, if two processes run the same program, they should share the same copy of the program code. ◇ Provide memory protection, the process cannot access or modify the page, kernel protects the data and code of a single process in an unauthorized manner to prevent other processes from modifying them. Otherwise, the user program may accidentally (or maliciously) destroy the kernel or other user program. The virtual memory system is not a price. Memory management requires address conversion tables and other data structures, and the memory left is reduced. Address Transformation adds the execution time of each instruction, and instructions with additional memory operation will be more serious. When the process accesses the page that is not in the memory, the system has failed.
The system processes the failure and loads the page into memory, which requires a very time-consuming disk I / O operation. In short, the memory management activity occupies a considerable part of the CPU time (about 10% in a relatively busy system). Uclinux For NOMMU special processing For Uclinux, it is designed for processors without MMU, ie Uclinux cannot use virtual memory management techniques for the processor (it should be said that this processor with MMU is quite popular in embedded devices. Bias. Uclinux still uses the memory paging management, and the system pags the actual memory when starting. The program paging is loaded when the application is loaded. However, because there is no MMU management, it is actually UCLinux uses the real memory management policy. This affects many aspects of system work. The UCLinux system is straightforward for memory, and it does not require MMU to access the address, but directly to the address line output), all programs are actually physical and physical addresses. The operating system is not protected against memory space (this is actually the characteristics of many embedded systems), and each process actually shares a running space (there is no separate address conversion table). Before execution, the system must assign enough continuous address space for the process, and then fully load the continuous space of the main memory. Corresponding to it is that the standard Linux system does not have to ensure that the actual physical storage space is continuous when allocating internal survival, and as long as it guarantees that the virtual address space is continuous. Another aspect program load address is usually different (pointed out in the LD file), so the Relocation process is required. In addition, the disk switching space is also unused. If the system is executed, it will not be improved by disk exchange. Uclinux has given a higher demand for the management of memory. If this is used, UCLinux's memory management is a reverse, returned to UNIX early or the DOS system era. Developers have to participate in the system's memory management. Starting from the compiled kernel, the developer must tell the system how much memory has this development board (if you deceive the system, it will be punished after running the program later), so that the system will paginate in the initial phase of the startup, And tag the used and unused memory. These paging memory will be used when the application is running. Since the application is loaded, a continuous address space must be assigned, and the one-time block (continuous address) of the different hardware platforms (continuous address) is different (currently for the EZ328 processor UCLinux is 128K, and the system for the ColdFire processor Memory does not limit this limit), so developers must consider the allocation of memory when developing applications and follows applications to run space. In addition, due to the use of real memory management policies, user programs, the user program is guaranteed to ensure the address space of other programs when developing, so that the program does not destroy the normal operation of the system or cause other programs. Run an exception. From the perspective of memory access, the development of developers is increasing (the developer can access any address space when programming), but at the same time, the system is also declined. In addition, the system will have a large change in the management of multi-process, which will be explained in the multi-process management of UClinux. Although UCLinux's memory management is much different compared to standard Linux systems, it should be said that this is an embedded device. In embedded devices, due to the effects of sensitivity factors such as cost, the generally bias is used without MMU, which determines that the system does not have enough hardware support to implement virtual storage management technology.
From the functions implemented from embedded devices, embedded devices are typically run in a particular environment, as long as specific functions are implemented, their functionality is relatively simple, and the requirements for memory management can be considered by developers. Standard Linux system process, thread process: Process is an operating program and provides an entity that enables the environment, which includes an address space and at least one control point that performs a single instruction sequence on this address space. The process address space includes a collection of memory cells that can be accessed or referenced, and the process control point is controlled and tracked process instruction sequences through a hardware register of Program Counter, PC. Fork: Since the process is an enabled environment, you must first create a "run" program before executing the program. The Linux system provides system call to copy the content of the current process, to generate a new process, calling the process called a parent process; and the new process generated is called a child process. The sub-process will bear all the features of the father process, but it has its own data segment, that is, although the child process changes the variables of the locale, it does not affect the variable value of the parent process. The parent process and sub-process share a block, but each has its own stack, data segment, user space, and process control block. In other words, the program code executed by the two processes is the same, but each program counter has its own private data. When the kernel receives the Fork request, it will check three things first: first check the memory is enough; second is whether the process table is still available; the last is to see if the user has established too many child processes. If the above three conditions are satisfied, the operating system will give a pro process a process identification code, and set the CPU time, then set the segment shared with the parent process, and copy the inode of the parent process to the child process. The final child process will return to the value of 0 to indicate that it is a child process, as for the parent process, which may wait for the execution of the sub-process, or do one with the sub-process. EXEC system call: This system call provides a process to perform another process, and the EXEC system call is to cover the old process memory content, so the stack of the original program, the data segment and blocks will be modified, only the user area stay the same. VFORK system call: Because the kernel will copy a parent process to a child process when using Fork, this is quite wasting time, because most of the case is the program to call EXEC immediately after calling fork, so just copy The process area is immediately covered by the new data. So the Linux system provides a system call vFork, and vfork assumes that the system will execute Exec when calling to complete VFORK, so Vfork does not copy the parent process, just initializing the private data structure and preparing enough page table. This actually has the same block after the VFORK call is completed (before the child process calls EXEC or EXIT), so the child process can change the data and stack information of the parent process, so the VFORK system call is completed, the parent process enters Sleep, until the child process executes EXEC. When the child process executes EXEC, since Exec is to use the executed data, the code covers the storage area of the sub-process, which will generate a write protection error (DO_WP_PAGE) (this time the child process writes in the store area of the parent process) This error causes the kernel to reassign the storage space as a child process. When the child progresses correctly, the parent process will wake up so that the parent process will continue. Uclinux multi-process processes UCLinux do not have the MMU management memory, and data protection needs to be implemented when implementing multiple processes (Fork Call Generator Process).