Linux driver
Linux driver developpement
Section titled “Linux driver developpement”- 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
Alternative to kernel developpement
Section titled “Alternative to kernel developpement”- 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 developpement
Section titled “Driver developpement”- Driver can be
- Compiled during kernel compilation
- Added dynamically to the kernel
Basic example
Section titled “Basic example”#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 chargedstatic 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 disabledstatic 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 functionmodule_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 makefileobj-m := first.oelse# first passKDIR ?= /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=$$PWDendifKernel modules tools
Section titled “Kernel modules tools”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.lsmodlists 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.
Debug linux
Section titled “Debug linux”pr_*functions (ring buffer of dmesg)dev_*functions (with a pointer to device)- Debugfs (
CONFIG_DEBUG_FSand mounting/sys/kernel/debug kgdbwith serialJTAG/SWDon the processor- QEMU (and
gdb)
Allocation
Section titled “Allocation”- Allocation functions sometimes needs flags
GFP_KERNEL: allocation can blockGFP_ATOMIC: allocation cannot block (for interruption control)- Allocation can fails (returning NULL)
- Memory leak exists -
devm_function can limit them
- Allocation
kmalloc,kzalloc,kcallocusing flagskfree,kzfree,kzfree_sensitive- for device:
devm_k*alloc(unalloc on device deletion) ordevm_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:asprintfequivalent (needkfree)devm_kasprintf
Commons errors
Section titled “Commons errors”- 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
- https://github.com/ghaerr/elks - Embeddable Linux Kernel Subset