How to migrate an operating system to a Linux System

Source: Internet
Author: User
How to migrate an operating system to a Linux system-general Linux technology-Linux programming and kernel information. For more information, see the following section. Kang Hua: mainly engaged in Linux operating system kernel, virtual machine, Linux technical standards, computer security, software testing and other fields of research and development work, once working in MII-HP Software Laboratory, transient software company/MOTOROLA, he is now working at the Lenovo Research Institute.

Introduction to legacy Operating Systems
The Prime Minister wants to clarify that the system we want to migrate here is not an application system (such as a communication gateway), but a real-time operating system (named OLDOS for the time being ). It has its own complete system architecture-memory management, process management, exception handling, system calls, device drivers, and even shared libraries and shells. In general, although it is small, it has full data. As a bearer platform, it runs many services and applications.

Our old system was developed 20 years ago, and it was undoubtedly outstanding in that era, but as the underlying hardware grew richer and the requirements for upper-layer applications grew higher and higher. The management of hardware and processes by the old system is gradually stretched. Because the original design lacks a hierarchical structure for hardware support coding, the related and irrelevant codes in the architecture cannot be clearly divided, this makes the porting workload of the later board change very heavy. In addition, the process management and debugging functions of the system appear to be relatively weak now, and the process and debugging process cannot be effectively managed.

In addition to the technical reasons, the cost for maintaining OLDOS is much higher than that for maintaining Linux systems. The reason is simple ?? Linux is already widely used. Therefore, it is easier to train or search for Linux developers than to train OLDOS staff at a low cost. As mentioned above, choosing Linux as the operating system is the trend of the times.

Note: I have also chosen vxworks as an operating system. vxworks has a good reputation in the real-time control field. However, since it is a commercial product, every time we sell a card with vxworks, you must pay for Wind River (the vxworks manufacturer. This is not cost-effective compared to Linux, an open-source operating system.

This article will mainly analyze the porting requirements and porting basic strategies, and hope that the readers can understand the transformation of the system architecture, so as to enhance their understanding of the Linux system.

Porting requirements and Strategies
Our porting requirements are generally met: 1. The functions and interfaces of upper-layer applications and service programs remain unchanged; 2. The real-time performance of upper-layer applications and service program systems remains unchanged. That is to say, the previous applications and service programs can run on the new Linux operating system without modification.

From the above requirements, we can conclude that our system must have the same APIs for upper-layer applications. Therefore, we need to build an adaptation layer between the Linux operating system and the upper-layer applications and service programs (which is a bit of what the adapter means in the design mode ), our old operating system, OLDOS, finally evolved into this adaptation layer.

Previously, the OLDOS operating system operated the hardware directly, while the transplanted system architecture operated the hardware directly in the Linux operating system. The OLDOS operating system degraded into an adaptation layer (oldos api) and a group of OLDOS system service processes-the hardware operation code related to the original architecture is completely handed over to Linux for processing. In addition, the application and service programs still call the oldos api functions, because the original applications can run directly on the new architecture without modification.

However, you may also guess that they can also directly call the APIS provided by Linux, but such calls should be avoided as much as possible, so we say they are restricted to use.

Transplanted
Let's start the porting process from the bottom up at the system architecture level.

Driver
First, the driver section. The original architecture of the OLDOS operating system is similar to that of Linux. It also consists of user space and kernel space. The driver is in the kernel space and the hardware is controlled. Fortunately, we are a dedicated general-purpose system without a variety of peripherals, so the driver is closely limited to serial ports, Flash, and several dedicated PCI peripherals. After the architecture is migrated to the Linux operating system, all drivers must be transplanted, but the number of tasks is not large, because the Linux drivers such as serial port and Flash have been well supported, for several proprietary devices, you must manually port them to Linux. And will be accessed by applications as special device files in the future. There is nothing to say here.

File System
The OLDOS operating system does not contain a file system, but it supports retrieving executable objects (so it can be understood as the simplest File System ). Because the executable image is directly stored in the memory, you do not need to load the memory like Linux during execution. You only need to dynamically allocate space and then create a process memory ing, you can jump to the entry point for execution (this is a bit like vxworks ).

After we port the image to Linux, the object execution method has changed. In Linux, the executable images must be stored on disks (or simulated Block devices) in the format of ELF, such as ramdisk), which is loaded by the loader (ld) before you can create a ing for it and start execution. Therefore, we need to make some changes to the previously executable files (such as the main function needs to be added) and compile them into the ELF format for storage on the disk. It is dynamically loaded during execution and executed from the entry function (main by default) (called by exec system ).

Exception Handling
The exception handling mechanism before OLDOS is no longer used after transplantation, because the Linux system has already handled the exception. But the OLDOS system originally supported software error management ?? The most important feature of error management is to track and save the system status when an error occurs for debugging and correction.

There is also an error management mechanism in the Linux system. For example, we often see Kernel errors such as oops (generated by the kernel die () function, call chain, etc. After OLDOS is transplanted to Linux, it becomes the oldos api of the user space (in fact, it is a dynamic library, which we will talk about later) and still needs to support the original error management mechanism, that is to say, the upper-layer application should still be able to save it on-site when an error occurs. (This is similar to oops in the Linux kernel, but it is implemented at the user layer ). In order to achieve this goal, we must be able to obtain the on-site information of the system in case of system exceptions.

The problem now is how to obtain the register information in the user space (previously obtained in the kernel space) and when a fatal error (unrecoverable) occurs, can terminate the current application task (because the system continues to run or causes errors ). The first requirement is that we use the Linux system (in fact, almost all posix systems do this) and send special signals (such as SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGQUIT, SIGINT, SIGTERM) is used as a bridge to obtain system field information. The specific method is to extract the system register information from the sigcontext parameter of the signal processing function (this parameter is generally ignored in the use of the signal processing function ). The second requirement requires us to establish a high-priority daemon process to receive the above signal. The reason why we use a high-priority process to receive it is because we have to rush to the error application process to run the pre-processing signal-print the error scene, terminate the error application process. The specific implementation is not expanded. Click it to the end.

System Call and database migration
The OLDOS system provided many system calls for upper-layer applications. However, after the whole system is migrated to the Linux system, the system resources are no longer directly operated by the OLDOS system call, however, in order to ensure interface access is inconvenient, we still need to retain the original system call interface. Our simplest practice is to convert OLDOS system calls into library function calls.

To make all applications accessible and save storage space as much as possible, we collectively convert OLDOS system calls into dynamic library functions. These library functions actually call the Linux system call or the system library function at the underlying layer.

However, there is a problem here: the data segment of the shared library is private. That is to say, if a process changes the global variable in the shared library, other processes will not be able to see the change. This is because of the write-on-copy behavior of the Data Segment of the shared library. However, the system calls in our OLDOS share a lot of data structures (these data structures were previously shared in the kernel, such as the process control block TCB ), these data structures must be accessible by various processes after migration.

Therefore, we use shared memory to store these shared variables that require global access. What is the specific approach ?? At the beginning of the system, an auxiliary process was used to create a shared memory area to store global shared variables and then mounted to our application space. In this way, all processes can access the same variable copy. Of course, to synchronize access, the shared variables need to be locked.

The library porting method is similar to the OLDOS system call, and is finally encapsulated into a Linux shared library (*. so ). There are many details here. I will not introduce them here.

Performance Requirements
The performance requirement of our system is the real-time performance of the system. The original OLDOS operating system showed a very strong "restricted environment" to meet real-time requirements. The so-called restricted environment refers to the restrictions on the operating environment of the system ?? For example, the stack size used by each process is fixed, and the process memory allocation is performed at the time of creation. Dynamic Allocation is not allowed. We know that the Linux system is designed based on a general system and can run any program. Therefore, the process running mode is not suitable for real-time performance:

Process images are dynamically loaded as needed-loading of the running process will inevitably affect the certainty of task execution. The process stack allocation size is not fixed and is dynamically allocated as needed. The process can dynamically allocate memory.

To this end, we have taken the following measures to correct the above problems. Process image loading at a time ?? To use mlockall to call the load and lock the memory ). Fixed process stack size ?? You can use rlimit to limit the size of a process stack. When a process is created, code that occupies the stack is run to expand the stack space (because the memory is locked, so the stack space will not be released during the process running ). Is the process heap space allocated once completed ?? A fixed memory pool is allocated at the time of Process Creation for heap allocation of all processes. That is, each task has its own fixed heap space and has been allocated. In addition to the above improvements, we also need to patch the Linux kernel in real time (mainly kernel preemption patches ). Therefore, the system's real-time performance will be ensured.

Summary
Linux systems are mainly designed for server applications. Therefore, some inherent limitations exist for desktop applications and embedded applications, such as poor real-time performance and high resource occupation, the interface is not beautiful enough. However, as more and more developers join and apply more and more applications, the development of Linux systems is now moving towards the desktop and embedded fields, especially the embedded field. Many performance patches are constantly added to the kernel, and the real-time and stability of the system is steadily improved. I believe that Linux will certainly have a place in the field of embedded communication soon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.