Skip to content

Linux driver

  • Developpement at the kernel level
    • crutial security level
    • API can change between kernel versions
    • low level
      • no standard C library
      • no floating point
      • portability (endianness)
      • restricted heap
  • Interface with the hardware
    • model discovery
    • interuptions
    • communication with the hardware abstraction in memory
  • Interface with the kernel
    • module separation and abstraction
    • allocations
    • concurrency (access)
    • debug tools
  • Interface with the application
    • create a file abstraction
    • framework developpement (or library)
    • handling ordering and waits between differents applications
  • Using a generic driver (USB Human Interface Device)
  • Using a direct access from user space
    • using a library (libusb)
    • using a file abstraction (/dev/, for example /dev/i2c-X)
  • Using userpace I/O Driver
    • a tiny kernel driver
    • most of the code in user space
  • Driver can be
    • Compiled during kernel compilation
    • Added dynamically to the kernel
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
// headers contains macros and functions declarations
// No standard libc stdio
// __init means that we can discord the code once charged
static int __init first_init(void)
{
pr_info("Triggered when module is loaded !\n");
// initialize the module
return 0; // < 0 when error
}
// __exit means that this code should not be loaded when static compilation OR the kernel module loading is disabled
static void __exit first_exit(void)
{
pr_info("Triggered when module is unloaded \n");
// unitiliaza the module (clear the allocations)
}
module_init(first_init); // this macro bind the init to the correct function
module_exit(first_exit); // this macro bind the exit to the correct function
MODULE_LICENSE("GPL"); // is license is different than GPL the module is will have a restricted access to functions
// can be "license+GPL"
MODULE_DESCRIPTION("Module desc");
MODULE_AUTHOR("Name and email");
ifneq ($(KERNELRELEASE) ,)
# second pass
# kbuild part of makefile
obj-m := first.o
else
# first pass
KDIR ?= /lib/modules/`uname -r`/build
default:
# call make in the KDIR directory and M contains the path
# this make will trigger again but with the kernel build system
$(MAKE) -C $(KDIR) M=$$PWD
endif
  • modinfo <module_name|module.ko> provides information about a module (author, description, license, kernel version it was compiled for, list of accepted parameters, etc.).
  • insmod <module.ko> loads a module.
  • modprobe <module_name> loads a module and any required dependencies.
  • lsmod lists the modules currently loaded.
  • rmmod <module_name> unloads a module. This operation is only possible if the module is not currently in use.
  • modprobe -r <module_name> also unloads a module and all of its dependencies that are not being used.
  • pr_* functions (ring buffer of dmesg)
  • dev_* functions (with a pointer to device)
  • Debugfs (CONFIG_DEBUG_FS and mounting /sys/kernel/debug
  • kgdb with serial
  • JTAG/SWD on the processor
  • QEMU (and gdb)
  • Allocation functions sometimes needs flags
    • GFP_KERNEL: allocation can block
    • GFP_ATOMIC: allocation cannot block (for interruption control)
    • Allocation can fails (returning NULL)
    • Memory leak exists - devm_ function can limit them
  • Allocation
    • kmalloc, kzalloc, kcalloc using flags
    • kfree, kzfree, kzfree_sensitive
    • for device: devm_k*alloc (unalloc on device deletion) or devm_kfree
  • Page allocation (big content or continuous memory)
    • get_zeroed_page (get blank page), __get_free_page (get page - not blank), __get_free_pages (get second order pages)
    • free_page, free_pages: must free the same number of pages
    • for device: devm_get_free_pages, devm_free_pages
  • Print function
    • kasprintf: asprintf equivalent (need kfree)
    • devm_kasprintf
  • In embed developpement
    • No updates possible
    • Find a way to make an update
    • Find a way to make any update failure recoverable
    • low level - error during bootloader update
    • high level - error during library/dependency update
  • Low randomness at startup of the kernel
  • No real-time (we don’t know the current time at startup)
    • use an external dependency (GNSS, NTP, or hardware)
    • Incorrect real time
  • Incorrect License usage and code redistribution
  • Poor security