In the following series of blog posts I will document my attempts to write Linux modules using the Ada language.
I am not a professional and my knowledge of the Linux kernel and gnat is not comprehensive. Please pardon me for any inaccuracies I make.
Introduction and Motivation
Linux module is a binary that can be dynamically loaded and linked into the Kernel. One major use case is device drivers. Traditionally Linux modules are written in C and built using kbuild, an elaborate build system that is also used to build the kernel itself.
Code written for a module runs in the kernel with the privileges of the kernel, meaning that a programming error can cause anomalies such a system reboot, memory corruption of applications, data corruption of the hard disk and more.
Ada is a language that was designed specifically for embedded safety critical applications which makes it more suitable for writing device drivers.
Preparations
First, I investigated the current process of writing and building modules. An example tutorial can be found here.
In an nutshell, it is required to create a file that looks like this:
/* * hello-1.c - The simplest kernel module. */ #include/* Needed by all modules */ #include/* Needed for KERN_INFO */ int init_module(void) { printk(KERN_INFO "Hello world 1.n"); /* * A non 0 return means init_module failed; module can't be loaded. */ return 0; } void cleanup_module(void) { printk(KERN_INFO "Goodbye world 1.n"); }
Additionally, a mkefile that looks like this:
obj-m += hello-1.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Calling make will produce a .ko file which is the actual loadable module. It can be loaded and removed using the insmodule
and rmodule
.
The difference between a .ko and .o file is the additional memory sections added by the linker.
The Strategy
It is very tempting to abolish the cumbersome kbuild system and use a gpr file for the whole compilation and linkage. Unfortunately the makefiles that kbuild is made of are too complex for me to understand, and I was able to imitate everything it does to produce the “.ko” file. Additionally, it is not safe to work around the kbuild system as the internal details might change it future versions of the kernel.
So instead, I envision the following strategy. All the “logic” of the module will reside in Ada functions that will be compiled into a static library. The main file will be written in C and will consist of wrappers (eg init_module
, cleanup_module
) that will call the Ada functions. We will tell kbuild about the existence of the Ada library, so it will link it into the module, and everyone will be ha