DEV Community

loading...

How to Port Operating System to Different Chip Architecture?

Abby
This account is set to share everything new about OpenSource RT-Thread IoTOS.
・4 min read

There are consolidate steps to transplant the OS onto other hardware platforms, which includes various chips of the same architecture, as well as of different architectures. This article will walk you through how to port OS to many various chips and we learn from open source RT-Thread OS as a case study.

Here are the milestones developers should meet during the porting process:

1.The C-language execution environment is prepared
2.RT-Thread can output characters via a serial port
3.The context switch code works fine
4.Hardware timers are supported by RT-Thread
5.The interrupt routine could handle received data via the serial port

The Execution Model

For most advanced architectures, the operating system and user applications run at different privilege levels, in order to prevent malfunction code affects the integration and safety of OS. For example, In the architecture of ARMv7-A, OS usually runs in the System mode; while in ARMv8-A, OS could run in at the EL2 or EL3 privilege level. Usually, the chip executes the boot-up code at the highest privilege level when powered up. After that, the OS switches the privilege level to its target mode.

Execute C Code

The key action of this step is to set the BSS section to zero and set up the stack pointers.

In the C-language implementations, the uninitialized global variables and static variables are usually stored in the BSS section, which doesn't occupy any space in the storage device. When the program is loaded, the corresponding space is allocated in memory and initialized to zero. When OS boots up, it has to do this work by itself.

On the other hand, the OS has to initialize the stack space and set up the stack pointer as well. Since C-language programs save and restore local variables on the stack when entering and exiting a function, the stack pointer has to be set before invoking any C functions. RT-Thread must do this step for each newly created thread.

At least one serial drive

RT-Thread outputs information and logs via the serial port, which also helps debug the code during the transplantation process. At this step, receiving data via serial ports is not required yet.
If everything works fine, you should see the RT-Thread logo via the serial port.

Context switching logic

The context of a task is its whole execution environment, which contains the generic registers, the program counter, the location of the stack frame, and so on. When a new thread is created, RT-Thread has to allocate and set up its context manually, so that the scheduler could switch to the new thread as it does to others.

There are three things we need to pay attention to. First, When RT-Thread starts up, interrupts are disabled by default. They are enabled when the task scheduler is enabled for the first time, the process of which is implemented in assembly language during the context switch period. Second, the next scheduling starts when a thread exits, by which the resources owned will be reclaimed by the idle thread. Third, the order of pushing data into the stack must be consistent with that of poping data out of the stack.

The sign to improve this function is well working is that you can enter the main function and the msh console normally. However, input control cannot be achieved because serial input interrupts are not implemented, and if serial interrupts have been implemented, msh inputs can be made.

The timer can be normally used

RT-Thread requires a timer to generate interrupts periodically, which is used to count the ticks that elapse since the system startup. The tick number is used not only to provide software interrupt functions but also to instruct the kernel when to start a task scheduling.

Please be careful about setting the value of the time slice, which is usually 10 ms or 1 ms. If a small time slice is chosen on a slow CPU, most of the time will be spent on task switching so that nothing could be done in effect.

Serial port works correctly

In this step, users could interact with RT-Thread msh via the serial port. We input commands and type enter, then msh executes the command and displays results. This process is usually not difficult to implement, however, developers should not forget to clear the interrupt flag on some platforms after the serial port interrupt is handled.

Once the serial port works correctly, the fundamental transplantation process is done.

Conclusion

To port different chip architectures, you should very clear about the architecture of the chip, and very familiar with the underlying code at the most critical point of the Operating System. However, chip manual combines with the actual work experience, you’ll need to learn the chip privilege mode, understand its register and its compilation method.

Now you can give it a try with OpenSource RT-Thread OS. Any questions, feel free to ask for support in RT-Thread Club.

RT-Thread Contact Info:

Website | Github | Twitter | Facebook | Youtube

Discussion (0)