Preparation assistance for Linux Device Driver. This doesn't fully provide the in and out of LDD3 book. Rather it advices outline for studying Linux Device Driver. It will be updated in the course of time if required.
3. Outline : Kernel Module
➔ A hello_world program for kernel (source code available in folder).
➔ insmod and rmmod for load unload kernel.
➔ Segmentation fault kills process.
➔ Modules runs in kernel space and application in user space.
➔ Each space has their own memory mapping and address space.
➔ Processor protects from unauthorized access through protection levels.
➔ Kernel is executed under highest level ie. supervisor mode.
➔ For a system call UNIX transfers execution from user to kernel space
4. Outline : Kernel Module
➔ Written in a way to maintain concurrent action can happen.
➔ Kernel codes must be reentrant - Running in multiple context in same time.
➔ Avoid race condition in data structure which might have shared data.
Current process -
➔ current is not truly global variable. For supporting SMP developer developed
this to find current process in relevant processor.
➔ The pointer refers to struct task_struct which refers process currently
executing.
5. Outline : Kernel Module
➔ Smaller stack size, as small as 4096 byte.
➔ DO NOT allocate large auto variable, instead use dynamic memory allocation.
➔ Functions with __ as start of name must be handled with care as these are
low level interface.
➔ No floating point arithmetic as it needs to save floating point state in
processor at each entry and exit. So extra overhead.
➔ modprobe searches for other symbols from any other modules and loads,
whereas insmod may fail with unresolved symbols.
➔ lsmod reads /proc/modules to list loaded modules.
➔ sysfs /sys/module shows same (both virtual filesystem).
6. Outline : Kernel Module
➔ Module build steps has version verification by linking vermagic.o with
module. If things doesn’t match, module won’t be loaded.
➔ /var/log/messages shows problem logs.
➔ To build kernel for multiple version, use macros.ie.LINUX_VERSION_CODE,
KERNEL_VERSION(major,minor,release).
➔ Deal incompatibilities with specific header file.
➔ Kernel symbol table contains global symbol and functions.
➔ Driver module exports symbol to table for other modules to benefit (Module
Stacking).
7. Outline : Kernel Module
➔ Initialization functions are declared as static as it should not be visible
outside specific file.
➔ Module can register many facilities and each facility a specific kernel function
accomplish the registration.
➔ Arguments passed to registration functions is pointer that points to data
structure containing details of facility.
➔ Cleanup function is declared as void as it has nothing to return.
8. Module : Error Handling
➔ For an error a module must perform an undo of all the things that it did before
failure. Linux doesn’t save these activities hence a module must implement
this.
➔ goto is used for error handling. For complex situations, run cleanup function
from within initialization function.
➔ Must check of status of each item before unregistering and clean-up function
can not have __exit when called from non-exit function.
➔ Module load race - Module must complete all supporting initialization before
enabling all facilities.
9. Module Parameter
➔ Parameter can be taken in load time by insmod or from /etc/modprobe.conf
by modprobe
➔ Parameters are declared with the module_param macro, defined in
moduleparam.h.
➔ Macros are placed outside function and found near the head of source file.
➔ All module params should be given a default value.
➔ User-space driver can be implemented although it has pros and cons.