Categories: Technology

Harnessing ARM Architecture: Running Zephyr RTOS on Raspberry Pi 4 with Xen

Running Zephyr RTOS on Raspberry Pi 4: The Raspberry Pi 4 is an ARM-based device with a wide range of use cases. This is why the Raspberry Pi 4 is often used as a mini embedded system in industrial environments.

It is a lightweight, single-board computer, and ARM devices typically use RISC architecture. Due to the reduced instruction set, the Raspberry Pi 4 can handle operating systems efficiently while generating less heat compared to traditional x86 systems. As one of the most powerful single-board computers available, the Raspberry Pi 4 is very portable and can also serve as a low-cost mini server.

The most suitable hypervisor transforms the Raspberry Pi 4 into a multitasking powerhouse. In traditional x86 architecture, it’s easy to install the Xen hypervisor and run multiple operating systems simultaneously. However, on ARM devices, this task is a bit more challenging due to architectural limitations and limited community support.

It is possible to install the Xen hypervisor on the Raspberry Pi 4. Xen is a bare-metal hypervisor, and many ARM-based devices use U-Boot as a bootloader. Xen also supports U-Boot, allowing operating systems to be installed in legacy mode on ARM devices using bare-metal bootloaders like U-Boot.

However, using legacy mode for installations is increasingly seen as a dead end, as UEFI BIOS offers more advantages than traditional boot methods. The Raspberry Pi 4 does support UEFI, but installing the Xen hypervisor in UEFI mode can be tricky. First, you would boot via UEFI, then install Xen using U-Boot from UEFI, which is a time-consuming process. Therefore, it is more efficient to install Xen directly using U-Boot on the Raspberry Pi 4.

How Hypervisor Works?

Running Zephyr RTOS on Raspberry Pi

XEN Hypervisor:

The Xen hypervisor is an open-source, bare-metal hypervisor that serves as a tool to manage other installed operating systems. It handles all memory and system operations, acting as the foundation for all installed OSes. Through the Xen hypervisor, we can allocate RAM limits for each operating system.

The Xen hypervisor alone does not run; it comes with a management operating system. Users can choose one OS from a variety of operating systems available in the digital environment.

For embedded systems, a minimal and reduced-size operating system is often preferred, as its operations are limited to specific tasks. Xen creates several interfaces to interact with its operating systems, such as the network interface xenbr0.

Initial Preparation:

We need a Raspberry Pi 4 with a memory card and USB drive. If the Raspberry Pi 4 is upgraded to UEFI firmware, it must be downgraded to legacy BIOS because the Xen hypervisor is installed through the U-Boot bootloader. If it runs in UEFI mode, it will not detect the U-Boot Xen hypervisor.

A system with Ubuntu 22.04 or WSL Ubuntu 22.04 is required, and a desktop version of Ubuntu 22.04 is preferred due to its higher clock speed since compilation can take a significant amount of time. Some packages are essential for compiling a Xen build on Ubuntu:

  • bzip2
  • chrpath
  • cpp
  • diffstat
  • g++
  • gcc
  • lz4
  • make
  • zstd

Procedure:

The Xen hypervisor can be installed on the Raspberry Pi 4 through several methods, each with its own advantages and disadvantages. In this context, we will focus on an embedded approach, which requires an operating system with minimal functionalities. This is why we are choosing the Yocto Project for installing the Xen hypervisor on the Raspberry Pi 4.

Image Generation

The Yocto build is composed of several layers:

  • Meta-OpenEmbedded: Contains essential software and tools for embedded systems.
  • Meta-RaspberryPi: Includes support files for the Raspberry Pi board.
  • OpenEmbedded-Core: Serves as the core of the build image.
  • Poky: A Linux distribution primarily developed for embedded systems.
  • Meta-Virtualization: Provides support for virtualization.

Using git clone, we can clone these repositories to a local directory. We need the Xen and Xen-tools available in the Meta-Virtualization layer. After cloning the repositories, set appropriate permissions for the directory at the poky path using:

bash
sudo chown <user>:<password> path/to/poky Next, set up your environment:
bash
source oe-init-build-env

 

Then edit bblayers.conf and local.conf files to meet your requirements.

bblayers.conf

bash
BBLAYERS ?= " \
/home/ubuntu/yoctolinux/poky/meta \
/home/ubuntu/yoctolinux/poky/meta-poky \
/home/ubuntu/yoctolinux/poky/meta-yocto-bsp \
/home/ubuntu/yoctolinux/meta-raspberrypi \
/home/ubuntu/yoctolinux/meta-virtualization \
/home/ubuntu/yoctolinux/meta-openembedded/meta-python \
/home/ubuntu/yoctolinux/meta-openembedded/meta-oe \
/home/ubuntu/yoctolinux/meta-openembedded/meta-filesystems \
/home/ubuntu/yoctolinux/meta-openembedded/meta-networking \
"

local.conf

bash
MACHINE="raspberrypi4-64"
DISTRO_FEATURES:append = " virtualization xen"
QEMU_TARGETS = "i386 x86_64 aarch64 arm"

Finally, build the Linux distribution using:

bash
bitbake xen-image-minimal

Running Zephyr RTOS on Raspberry Pi: This process can take up to 8 hours; hence a desktop system with a latest-generation processor is recommended to reduce build time. Upon completion, you will receive an .rpi-sdimg file located in build/tmp/deploy/images/raspberrypi-64/. You can write this file to an SD card using Balena Etcher or Linux commands. During this process, Poky Linux distribution kernel is created with support from Xen hypervisor and additional tools included in our layers. 

Running Zephyr RTOS as Domain-U in XEN Hypervisor

Running Zephyr RTOS on Raspberry Pi 4 requires compiling the Zephyr kernel for ARM architecture. Download the Zephyr SDK from their official site. After extracting it, run the setup shell script which will prompt you to install additional dependency tools such as CMake and Python 3’s pip:

bash
echo $ZEPHYR_SDK_INSTALL_DIR
export ZEPHYR_SDK_INSTALL_DIR=~/<zephyr-sdk-directory>
echo 'export ZEPHYR_SDK_INSTALL_DIR=~/zephyr-sdk-0.16.8' >> ~/.bashrc
source ~/.bashrc
mkdir -p ~/zephyrproject
cd ~/zephyrproject
west init

Next, update your environment:

bash
west update
source zephyr/zephyr-env.sh
west build -b xenvm zephyr/samples/synchronization

The built zephyr.bin file will be located in <project_directory>/build/zephyr/.... When building for Raspberry Pi 4, remember that it supports only GIC v2; GIC v3 kernel builds won’t work.

Creating conf file for Zephyr

Create a file called zephyr.conf with this content:

text
kernel="zephyr.bin"
name="zephyr"
vcpus=1
memory=16
gic_version="v2"
on_crash="preserve"

Copy both zephyr.bin and zephyr.conf files to the root directory of the Xen hypervisor. Then create an instance of Zephyr RTOS VM:

bash
xl create -c zephyr.conf

Conclusion

Running Zephyr RTOS on Raspberry Pi as Domain-U in a Xen hypervisor offers benefits for various applications but has limitations due to being an unprivileged domain. Running Zephyr as Dom-0 would eliminate these limitations and provide full functionality. Additionally, note that Raspberry Pi 4 lacks hardware compatibility typically expected by RTOS due to not having an internal real-time clock. This format will ensure clarity and proper structure when published on WordPress while maintaining readability for your audience. PM Square Soft Pioneers in Custom Software development, Critical Communications and IoT integrations

Kannan Balakrishnan

Recent Posts

Exploring Model-View Architecture in Qt using C++

The Model-View architecture in Qt is a design pattern that separates the data (model) from…

1 month ago

ASP.NET Project Structure – A Comprehensive guide to setup project in C#

ASP.NET Project Structure Using a structured approach in any project is important because it helps…

2 months ago

AI vs Generative AI: Which Technology is Right for Your Mobile App?

In the ever-evolving world of technology, distinguishing between Artificial Intelligence AI vs Generative AI Gen AI is essential…

3 months ago

Efficient Git Repository Management: Mirroring Code to Client Repositories

Mirroring Git Repository: In the world of software development, managing code repositories efficiently is crucial,…

7 months ago

The Future of Healthcare Software: Some Types to Watch

As the healthcare IT market is projected to reach $907.18 billion by 2031, growing at…

8 months ago

AutoHive Parking & Valet Management System: Revolutionizing the Parking Paradigm

The labyrinth of urban life presents a perpetual challenge for parking operators and facility managers…

9 months ago