Code Monkey home page Code Monkey logo

ada_kernel_module_framework's Introduction

Ada Linux Kernel Module Framework

Build Status

The goal of this project is to demonstrate that it is possible and is useful to use Ada programming language when developing Linux kernel modules.

This framework provides Ada bindings for Kernel functions as well as an Ada runtime modified to run in the kernel (statically linked into the kernel module).

Please note that this work is:

  1. Proof of concept - There are no plans to provide an exhaustive set of bindings as well as to make this robust against future Kernel changes.
  2. Work in progress - Every commit might break modules that were working with the previous version of the framework.

Directory structure

  • Makefile - Use this makefile to build and test everything.

  • rts - Ada runtime system that was modified to be able to run in the kernel. It's is based on Gnat's runtime.

  • rts/Makefile - A Makefile used to build the framework with a single make command.

  • rts/adainclude/linux-[package].adx - These files contain bindings to Linux kernel functions which are required by the RTS or the module being developed.

  • rts/adainclude/linux-wrappers.c - This file contains wrappers for C inline functions and pre-processor macros. This file will be compiled along with your kernel module.

  • Vagrantfile - Vagrant configuration file. See the section that discusses developing with virtual machine.

  • vm_scripts - Scripts used by Vagrant to setup the virtual machine and the tools.

  • testing-utils - Helper scripts for testing.

  • examples - This directory contains subdirectories with source code example kernel modules. Some of these are also being used as regression tests.

  • examples/template - A simple module that can be used as a starting point.

Each individual module directory is structured as following:

  • main.c - A C wrapper that is used to register init_module and cleanup_module. This currently can not be done in pure Ada as there are C macros involved.

  • src - The Ada source code of the kernel modules goes inside here.

  • Makefile - A makefile that builds the module. You should edit it to specify the location of the RTS.

Tools used

The following tools are used in the development of this module:

$ uname -rpio
4.9.0-4-amd64 unknown unknown GNU/Linux

$ gnat --version
GNAT GPL 2017 (20170515-63)
Copyright (C) 1996-2017, Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gprbuild --version
GPRBUILD GPL 2017 (20170515) (x86_64-pc-linux-gnu)
Copyright (C) 2004-2017, AdaCore
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ bats -v
Bats 0.4.0

It might be possible to use other tool versions, but it was not tested.

Developing using a virtual machine

It is good practice to develop and test kernel modules using a virtual machine rather than your everyday system.

The author of this module uses Vagrant as a VM manger.

An automatic process can setup a debian/stretch64 machine and install all the necessary tools:

  1. Clone this repository using git.
  2. Install Vagrant.
  3. Install VirtualBox (or your favorite supported VM provider). The author of this module uses VirtualBox.
  4. If using VirtualBox, install the vagrant-vbguest plugin (vagrant plugin install vagrant-vbguest).
  5. Run vagrant up where you cloned the repo (where Vagrantfile is) to create a shiny new VM with everything you need in it.
  6. Run vagrant ssh to log into the machine.

The source code of this directory will be mounted on /home/vagrant/project. Proceed normally from there.

Reference for the tools used by the author of this framework:

$ vagrant --version
Vagrant 2.0.0

$ virtualbox -h
Oracle VM VirtualBox Manager 5.1.34_Ubuntu

Using the main makefile

  • make - Build the RTS and all the example modules.
  • make clean - Clean the RTS and the example modules.
  • make test - Run all the tests.

Building a single module

First you need to make the RTS (run this from the root directory):

$ make -C rts

After that, change dir to the desired module's directory and make it:

$ cd examples/template
$ make

Running manually

A file called hello.ko will be generated.

Insert the module into the kernel:

$ sudo insmod hello.ko

Remove the module from the kernel:

$ sudo rmmod hello.ko

See message log:

$ dmesg | tail

GPLv3 License

See COPYING3 file for details.

ada_kernel_module_framework's People

Contributors

alkhimey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ada_kernel_module_framework's Issues

Port ACATS tests

The Ada Conformity Assessment Test Suite (ACATS) is the test suite used for Ada processor conformity testing.

Download the tests and try to port as many as possible into the testing framework.
Some additional code needs to be written for Iput/Output handling.
Functions like TEST needs to be implemented.

Incorporate testing framework

Add a framework for running tests.
Making changes to this framework might break existing features (in the RTS or in the bindings to the Kernel). Additionally, new Kernel releases might break this framework as well.
A testing framework is required that automatically can run regression tests and aggregate the results.

A test should be able to build a module, insert it and then test it's behavior using client and reading kernel logs.

Leading candidate: https://github.com/sstephenson/bats

DOD:
A single command that can be run from the root of the project that will run tests on all the modules inside examples subdirectories.
An example test file for the examples/template module.

Implement exception handling

Currently raising an exception will crash the Kernel.

Need to fix exception raising/catching mechanism.
Need to gracefully remove the module when exception reaches top level of stack.

Exception when initing the template module

This exception occurs randomly when inserting the examples/template kernel module:

[ 6992.867495] Init module.
[ 6992.867687] Hello Ada.
[ 6992.867919]  42
[ 6992.867920] PURPLE_BLUE
[ 6992.867920] TRUE
[ 6992.867921]  4.0
[ 6992.867921] -42
[ 6992.867921]  42
[ 6992.867922]  4.24242E+02
[ 6992.867922] C Bindings are working.
[ 6992.867923] Registering character device number...
[ 6992.867925] Registered character device number 249
[ 6992.867926] Creating class...
[ 6992.868085] Created class, check /sys/class/classname
[ 6992.868086] Creating device...
[ 6992.868089] ------------[ cut here ]------------
[ 6992.868094] WARNING: CPU: 0 PID: 11265 at /build/linux-EAZfyE/linux-4.9.51/lib/kobject.c:210 kobject_add_internal+0x25a/0x320
[ 6992.868094] kobject: (ffff9e0a056fe300): attempted to be registered with empty name!
[ 6992.868095] Modules linked in: hello(O+) vboxsf(O) vboxguest(O) nls_utf8 isofs loop ppdev parport_pc parport crct10dif_pclmul evdev pcspkr serio_raw sg crc32_pclmul ghash_clmulni_intel battery video button ac ip_tables x_tables autofs4 ext4 crc16 jbd2 crc32c_generic fscrypto ecb mbcache sd_mod crc32c_intel floppy ahci libahci aesni_intel aes_x86_64 glue_helper lrw gf128mul ablk_helper cryptd e1000 libata scsi_mod psmouse i2c_piix4 [last unloaded: hello]
[ 6992.868114] CPU: 0 PID: 11265 Comm: insmod Tainted: G        W  O    4.9.0-4-amd64 #1 Debian 4.9.51-1
[ 6992.868115] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006
[ 6992.868116]  0000000000000000 ffffffffa9129974 ffffb64700ec78b0 0000000000000000
[ 6992.868118]  ffffffffa8e76eae ffff9e0a056fe300 ffffb64700ec7908 0000000000000000
[ 6992.868119]  00000000ffffffea 0000000000000000 000000000f90000d ffffffffa8e76f2f
[ 6992.868121] Call Trace:
[ 6992.868123]  [<ffffffffa9129974>] ? dump_stack+0x5c/0x78
[ 6992.868125]  [<ffffffffa8e76eae>] ? __warn+0xbe/0xe0
[ 6992.868126]  [<ffffffffa8e76f2f>] ? warn_slowpath_fmt+0x5f/0x80
[ 6992.868127]  [<ffffffffa912c7da>] ? kobject_add_internal+0x25a/0x320
[ 6992.868128]  [<ffffffffa912c915>] ? kobject_add+0x75/0xd0
[ 6992.868129]  [<ffffffffa8eb8934>] ? __wake_up+0x34/0x50
[ 6992.868131]  [<ffffffffa92727e7>] ? get_device_parent.isra.20+0x177/0x1c0
[ 6992.868132]  [<ffffffffa9273464>] ? device_add+0xd4/0x620
[ 6992.868134]  [<ffffffffa8f9e6b3>] ? kstrdup+0x43/0x60
[ 6992.868135]  [<ffffffffa9273b99>] ? device_create_groups_vargs+0xd9/0xf0
[ 6992.868136]  [<ffffffffa9273c21>] ? device_create+0x51/0x70
[ 6992.868139]  [<ffffffffc0691ed6>] ? linux__device__device_create+0x5c/0x78 [hello]
[ 6992.868141]  [<ffffffffc06918b1>] ? ada_foo+0x581/0x660 [hello]
[ 6992.868143]  [<ffffffffc06915c8>] ? ada_foo+0x298/0x660 [hello]
[ 6992.868144]  [<ffffffffc06914cd>] ? ada_foo+0x19d/0x660 [hello]
[ 6992.868146]  [<ffffffffc0691434>] ? ada_foo+0x104/0x660 [hello]
[ 6992.868148]  [<ffffffffc0694001>] ? interfaces__c__to_ada__9+0x237/0x27e [hello]
[ 6992.868149]  [<ffffffffa8ffffff>] ? do_dentry_open+0xef/0x300
[ 6992.868151]  [<ffffffffc0691010>] ? init_thread+0x10/0x10 [hello]
[ 6992.868152]  [<ffffffffc0691037>] ? init_module+0x27/0x40 [hello]
[ 6992.868154]  [<ffffffffa8e0218b>] ? do_one_initcall+0x4b/0x180
[ 6992.868155]  [<ffffffffa8fc499d>] ? __vunmap+0x6d/0xc0
[ 6992.868156]  [<ffffffffa8f7b25c>] ? do_init_module+0x5b/0x1ed
[ 6992.868157]  [<ffffffffa8effca2>] ? load_module+0x2602/0x2a50
[ 6992.868158]  [<ffffffffa8efc400>] ? __symbol_put+0x60/0x60
[ 6992.868160]  [<ffffffffa8f00336>] ? SYSC_finit_module+0xc6/0xf0
[ 6992.868162]  [<ffffffffa94085bb>] ? system_call_fast_compare_end+0xc/0x9b
[ 6992.868162] ---[ end trace e1d96bcd1e7e4b5c ]---
[ 6992.868404] Created device, check /dev
[ 6992.868404] After Ada
[ 6992.934611] Will destroy device
[ 6992.934891] Will destroy class
[ 6992.935085] Will unregister device number 249
[ 6992.935088] Goodbye Ada.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.