xenomai-4 ARM64 evl-kernel on Raspberry Pi 3,Zero-2,4 and 5

logo


see: xenomai-4 Overview

Raspberry PI Ports of Xenomai-4

The Xenomai-4 linux-evl-kernel repo is derived from mainline/LTS and SLTS kernel. There are some Xenomai-4 Ports for Raspberry PI zero,3 and 4, but they have many issues, by poor Raspberry Pi support in the mainline LTS kernel. Its impossible to run the kernel on Raspberry PI 5, by missing device support for the BCM2712 and RP1-chip in the mainline kernel.

The The Raspberry PI Foundations kernel repo on github is derived also from LTS mainline kernel. There are many customizations, fixes and optimization for different RPI-Models in the device tree of the soc with overlays, and also in other kernel components, which are never or late merged back into the main line LTS-kernel. So the Raspberry PI support of Xenomai-4 is very poor from the poor support by the mainline kernel.

Kernel build Prerequisites

# needed debian packages - apt install ...
build-essential
flex
pigz
tar
bc
debhelper
bison
perl
cmake
pkg-config
dpkg-dev
libelf-dev
libssl-dev
libncurses5-dev
libbpf-dev
libltdl-dev

The raspberry pi kernel can be compiled with a cross compiler, but the cross compiled debian rpi5,rpi4,rpi3 headers packages will contain x86_84 code and so it is not usable on the target, to build modules. The only quick fix is to create the header package on a arm64 real or qemu instance. The best solution is to compile the kernel on a Raspberry PI 5, with a Filesystem on a NVME-SSD. ( The build is faster than a qemu-build, hosted on a AMD-Threadripper Workstation with 12 Cores, by limitations in number of emulated arm-cores and slow software arm emulation).

Patching a Raspberry PI Kernel to Xenomai-4 linux-evl

So the idea is to merge the device-tree with overlays and the driver support from the Raspberry PI kernel repo with the dovetail/evl-microkernel modifications from the Xenomai-4 linux-evl repo.

repos

The Raspberry pi 5,rpi 4 or rpi3 xenomai-4 evl kernel is created by patching Raspberry PI foundations Raspberry Pi Kernel tag stable 20250127 ( based on linux-6.6.74 with a patch from diff to tag v6.6.69-evl1-rebase). This patch has a much lower size as the other possible alternative, building the diff of xenomai-4 linux-evl kernel to the raspberry pi kernel with all device tree changes. The RPI-kernel with tag stable 20250127 will work on rpi5/cm5, rpi4/cm4, rpi3, rpi zero 2/w2.

The rpi patch was created initial with:

and then apply this patch to rpi-kernel linux-stable_20250127 with:

I have made some modifications to bring the kernel to compile and run without major issues. Then the final patch is created with all included changes and the added linux-evl code with:

git add *
diff --cached > ../../patches/rpi3_4_5_linux-stable_20250127_to_evl.patch

Creating the Source Repository

Clone RPI kernel and patch the kernel to linux-evl

# makefile syntax 
# for bash,sh change macro syntax from $() to ${} 
BRANCH_RPI=stable_20250127

pull-linux:
    git clone --depth=1 https://github.com/raspberrypi/linux.git --branch $(BRANCH_RPI) linux-evl
    ( cd linux-evl; git apply --reject --whitespace=fix --stat --apply ../../patches/rpi3_4_5_linux-stable_$(BRANCH_RPI)_to_evl.patch)

Configure target

preconfigure kernel for rpi3/zero 2 rpi4/cm4, rpi5/cm5 ( raspberry pi kernel documentation )

# for rpi3, zero 2, zero 2w
cd linux-evl; make bcmrpi3_defconfig

# for rpi4/cm4
cd linux-evl; make bcm2711_defconfig

# for rpi5/cm5
cd linux-evl; make bcm2712_defconfig

prepare target

configure the kernel

cd linux-evl; make menuconfig

or copy a prepared config to linux-evl/.config

valid evl-configurations for rpi3,rpi4 and rpi5

download: /config/config-6.6.74-evl.rpi3

download: /config/config-6.6.74-evl.rpi4

download: /config/config-6.6.74-evl.rpi5

and run

cd linux-evl; make oldconfig; make prepare

Building xenomai-4 evl-kernel, device tree,modules and debian linux-header package

The standard debian linux-image package is not usable, because the boot mount point is modified from \boot to \boot\firmware on RPI debian-12. So we will create a directory with the device tree and the kernel image and a tar-archive with the modules with the following Makefile:

# Makefile to build a rpi ARM64 evl-kernel
# enable for target
# export target=rpi3
# or export target=rpi4
# or export target=rpi5

ifeq ($(TARGET),rpi5)
KERNEL=kernel8-rpi5-bb-custom
FLAGS+=CFLAGS="-mtune=cortex-a76 -march=armv8-a+crc+simd -mno-outline-atomics"
endif
ifeq ($(TARGET),rpi4)
KERNEL=kernel8-rpi4-bb-custom
FLAGS+=CFLAGS="-mtune=cortex-a72 -march=armv8-a+crc+simd -mno-outline-atomics"
endif
ifeq ($(TARGET),rpi3)
KERNEL=kernel8-rpi3-bb-custom
FLAGS+=CFLAGS="-mtune=cortex-a53 -march=armv8-a+crc+simd -mno-outline-atomics"
endif

DTB_SRC_DIR=$(KSRC)/arch/arm64/boot
DTB_SRC1=dts/broadcom
BOOT_TEMPLATE=./boot-template
KSRC=./linux-evl
KERNELSRC_IMAGE=$(KSRC)/arch/arm64/boot/Image.gz
jobs= $(shell nproc)
DEB_REV = 0.0.1
RAWKERNELVERSION=$(shell make -s -C $(KSRC) kernelversion)
RAWKERNELRELEASE=$(shell make -s -C $(KSRC) kernelrelease)
MODGZ=$(abspath ./modules-$(RAWKERNELRELEASE).tar.gz)

.PHONY: all build mk-boot mk-modules

all:    build mk-modules mk-boot

# build linux-headers,linux-image deb packages
build: 
    (make $(FLAGS) -C $(KSRC) -j $(jobs) bindeb-pkg KDEB_PKGVERSION=$(DEB_REV) )

# generate modules tar archive
mk-modules: build
    mkdir -p ./dist
    rm -rf ./dist/lib/modules/*
    (export INSTALL_MOD_PATH=$(abspath ./dist); export KERNEL=$(KERNEL); make $(FLAGS) -C $(KSRC) -j $(jobs) modules_install )
    (cd ./dist/lib/modules; tar cf - . | pigz  > $(MODGZ) )

# extract kernel and device tree to template directory
mk-boot: build
    mkdir -p $(BOOT_TEMPLATE)
    mkdir -p $(BOOT_TEMPLATE)/overlays
    cp -f $(KERNELSRC_IMAGE) $(BOOT_TEMPLATE)/$(KERNEL).img
    cp -f $(DTB_SRC_DIR)/$(DTB_SRC1)/*.dtb $(BOOT_TEMPLATE)/
    cp -f $(DTB_SRC_DIR)/dts/overlays/*.dtb*   $(BOOT_TEMPLATE)/overlays/
    cp -f $(DTB_SRC_DIR)/dts/overlays/README   $(BOOT_TEMPLATE)/overlays/

Raspberry PI Boot

The Raspberry PI bootloader is able to load the device tree and the kernel image from a subdirectory. So we can copy the new kernel image and the device tree to a custom directory under /boot/firmware. We have only to copy a valid cmdline.txt to the same directory:

boot-dir

the cmdline.txt can be copied from the boot directory root:

# example cmdline.txt - setup right PART-UUD for root-fs
console=serial0,115200 console=tty1 root=PARTUUID=85ba498d-06 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

setup custom boot directory in config.txt:

... other settings

[pi02]
# zero 2,zero w2
# use custom directory 
os_prefix=xenomai-4/
arm_64bit=1
kernel=kernel8-rpi3-bb-custom.img

[pi3]
# rpi 3
# use custom directory
os_prefix=xenomai-4/
arm_64bit=1
kernel=kernel8-rpi3-bb-custom.img

[pi4]
# rpi4,cm4
# use custom directory
os_prefix=xenomai-4/
arm_64bit=1
kernel=kernel8-rpi4-bb-custom.img

[pi5]
... other settings
#
# use custom directory
os_check=0
os_prefix=bookworm64.way.xenomai-4/
kernel=kernel8-rpi5-bb-custom.img
# for 64 bit kernel
arm_64bit=1
... other settings

Before booting the new kernel,we have to expand the modules from ./modules-$(RAWKERNELRELEASE).tar.gz to /lib/modules.

# expand for raspberry Pi 5 kernel
cd /lib/modules; tar xf /projects/xenomai-4-build/rpi5/modules-6.6.74-v8-16k-evl-xenomai-4.rpi5.tar.gz

building libevl

Before build of libevl, we have to install the linux-headers package and meson and ninja. The debian-12 meson package is outdated, so we install a newer version with pip.

# get rid of pip externaly managed error message
mv  /lib/python3.11/EXTERNALLY-MANAGED /lib/python3.11/_EXTERNALLY-MANAGED
python3 -p pip install meson ninja

refer to xenomai-4 building libevl

Running evl test on a Raspberry PI 5

root@raspi-5-000:/# uname -a
Linux raspi-5-000 6.6.74-v8-16k-evl-xenomai-4 #1 SMP PREEMPT EVL Mon Feb  3 11:00:13 CET 2025 aarch64 GNU/Linux
root@raspi-5-000:/# evl test
basic-xbuf: OK
clock-timer-periodic: OK
clone-fork-exec: OK
detach-self: OK
duplicate-element: OK
element-visibility: OK
fault: OK
fpu-preload: OK
fpu-stress: OK
heap-torture: OK
mapfd: OK
monitor-deadlock: OK
monitor-deboost-stress: OK
monitor-event: OK
monitor-event-targeted: OK
monitor-event-untrack: OK
monitor-flags: OK
monitor-flags-broadcast: OK
monitor-flags-inband: OK
monitor-pi: OK
monitor-pi-deadlock: OK
monitor-pi-deboost: OK
monitor-pi-stress: OK
monitor-pp-dynamic: OK
monitor-pp-lazy: OK
monitor-pp-lower: OK
monitor-pp-nested: OK
monitor-pp-pi: OK
monitor-pp-raise: OK
monitor-pp-tryenter: OK
monitor-pp-weak: OK
monitor-recursive: OK
monitor-steal: OK
monitor-trylock: OK
monitor-wait-multiple: OK
monitor-wait-requeue: OK
observable-hm: OK
observable-inband: OK
observable-onchange: OK
observable-oob: OK
observable-race: OK
observable-thread: OK
observable-unicast: OK
poll-close: OK
poll-flags: OK
poll-many: OK
poll-multiple: OK
poll-nested: OK
poll-observable-inband: OK
poll-observable-oob: OK
poll-sem: OK
poll-xbuf: OK
proxy-echo: OK
proxy-eventfd: OK
proxy-pipe: OK
proxy-poll: OK
ring-spray: OK
rwlock-read: OK
rwlock-write: OK
sched-quota-accuracy: 340.0%
sched-tp-accuracy: OK
sched-tp-overrun: OK
sem-close-unblock: OK
sem-flush: OK
sem-timedwait: OK
sem-wait: OK
simple-clone: OK
stax-lock: OK
stax-warn: OK
thread-mode-bits: OK
root@raspi-5-000:/#

Downloads

Issues

clock-timer-periodic: BROKEN

On high load (kernel build in background) evl test will report this:

root@raspi-5-000:/# evl test
basic-xbuf: OK
../libevl/tests/clock-timer-periodic.c:43: FAILED: ticks == 1 (=false)
** clock-timer-periodic: BROKEN

I have checked all clock specific evl/dovetail modifications in the clock-system and the irq pipeline ( arm arch timer.c, arm global timer.c, bcm2835 timer.c,irq_pipeline.h ). All are included in the kernel build.