linux device drivers: Role of Device Drivers, Splitting The Kernel, Classes of
Devices and Modules, Security Issues, Version Numbering, Building and Running Modules
Kernel Modules Vs. Applications, Compiling and Loading, Kernel Symbol Table,
Preliminaries, Interaction and Shutdown, Module Parameters...
linux device drivers: Role of Device Drivers, Splitting The Kernel, Classes of
Devices and Modules, Security Issues, Version Numbering, Building and Running Modules
Kernel Modules Vs. Applications, Compiling and Loading, Kernel Symbol Table,
Preliminaries, Interaction and Shutdown, Module Parameters, Doing It in User Space.
Size: 2.1 MB
Language: en
Added: Nov 22, 2015
Slides: 35 pages
Slide Content
(IN LINUX-KERNAL) DEVICE DRIVER’S
Linux is, in simplest terms, an operating system Linux is very similar to other operating systems, such as windows, android and OS x(mac). Free and open-source software Android uses the Linux kernel under the hood. Because Linux is open-source, Google's android developers could modify the Linux kernel to fit their needs. Linux gives the android developers a pre-built, already maintained operating system kernel to start with so they don’t have to write their own kernel. WHAT IS LINUX??
A kernel is the lowest level of easily replaceable software that interfaces with the hardware in your computer. It is responsible for interfacing all of your applications that are running in “user mode” down to the physical hardware, and allowing processes, known as servers, to get information from each other using inter-process communication (IPC). WHAT DOES KERNAL DOES??
HERE COMES A DEVICE DRIVER A device driver is a program that controls a particular type of device that is attached to your computer. Black boxes to hide details of hardware devices Use standardized calls Independent of the specific driver Main role is to Map standard calls to device-specific operations Can be developed separately from the rest of the kernel Plugged in at runtime when needed
Kernel model with device driver hierarchy.
Implements the mechanisms to access the hardware. E.g., show a disk as an array of data blocks Does not force particular policies on the user. Support for synchronous/asynchronous operation Be opened multiple times Exploit the full capabilities of the hardware Easier user model Easier to write and maintain To assist users with policies, release device drivers with user programs THE ROLE OF THE DEVICE DRIVER
THAT’S ALL FOR TODAY…. ANY QUESTION??
SPLITIING THE ROLES OF KERNAL
PROCESS MANAGEMENT Creates , destroys processes Supports communication among processes Signals , pipes, etc. Schedules how processes share the CPU MEMORY MANAGEMENT Managing memory by Virtual addressing
FILE SYSTEMS DEVICE CONTROL NETWORKING Everything in UNIX can be treated as a file Linux supports multiple file systems Every system operation maps to a physical device Few exceptions: CPU, memory, etc. Handles packets Handles routing and network address resolution issues
CLASSES OF DEVICES THAT USE MODULES 1 2 3 4
CHARACTER DEVICES Abstraction: a stream of bytes Examples Text console ( / dev /console ) Serial ports ( / dev /ttyS0 ) Usually supports open , close , read , write instructions Accessed sequentially (in most cases) Might not support file seeks Exception: frame grabbers Can access acquired image using mmap or lseek BLOCK DEVICES Abstraction: array of storage blocks However, applications can access a block device in bytes Block and char devices differ only at the kernel level A block device can host a file system
NETWORK DEVICES OTHERS Abstraction: data packets Send and receive packets Do not know about individual connections Have unique names (e.g., eth0 ) Not in the file system Support protocols and streams related to packet transmission (i.e., no read and write ) Examples that do not fit to previous categories: USB SCSI FireWire I2O MTD
SECURITY ISSUES Kernel modules present possibilities for both System does rudimentary checks at module load time It relies on limiting privilege to load modules Hack, Virus, Log Files, Encryption, Logic Bomb Something Happens By Chance, w/o Intention
SECURITY ISSUES Driver writer must be on guard for security problems. Do not define security policies, Provide mechanisms to enforce policies. Be aware of operations that affect global resources. Beware of bugs. Treat input/parameters with utmost suspicion. Uninitialized memory , Kernel memory should be zeroed before being made available to a user. Otherwise, information leakage could result. Passwords protected. Avoid running kernels compiled by an untrusted friend
VERSION NUMBER’S Every software package used in Linux has a release number . You need a particular version of one package to run a particular version of another package. Prepackaged distribution contains matching versions of various packages. Linux kernel version numbers: <major>.<minor>.< release> For example: 2.6.31
ANY QUESTIONS?
BUILDING MODULES #include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit ); The HELLO WORLD program
#include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit ); This module bears a free license The ordering matters sometimes
#include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit ); No main function is used
#include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit ); Invoked when the module is loaded Invoked when the module is removed
#include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit ); Micros to indicate which module initialization and exit functions to call
STEP TO COMPLIE AND RUN HELLO WORLD MODULES You need running kernel source code Next go to your kernel module source code directory and simply create the Makefile file as follows Compile module using make command (module build can be done by any user ) Once module compiled successfully, load it and run using insmod or modprobe command. Source code Makefile C ompile Load + run
$ tar - zxvf kernel* -C / usr / src $ vi Makefile $ make( complie ) # insmod HELLO.ko EXAMPLE: HELLO.C MODULE hello.c C source code Create new Makefile Save and close the file Compile hello.c module( $ make ) Become a root user (use su or sudo ) and load the module Verify that module loaded: See message in /var/log/message file: Unload the module( # rmmod hello )
KERNEL MODULES VS. APPLICATIONS Applications: Can access various functions in user-level libraries (e.g., printf in C library ) Kernel modules: No user-level libraries printk is defined within the kernel Exported to modules Should include only header files defined within the kernel source tree
LINKING MODULE TO A KERNEL
END OF CLASS.. ANY QUESTION??
KERNAL SYMBOL TABLE The table contains the addresses of global kernel items functions and variables that are needed to implement modularized drivers. When a module is loaded, any symbol exported by the module becomes part of the kernel symbol table. In the usual case, a module implements its own functionality without the need to export any symbols at all . Example alias eth0 e1000 Whenever eth0 is referenced, the kernel module e1000 is loaded IN MODULE HEADER FILES USE THE FOLLOWING MACROS EXPORT_SYMBOL(NAME); EXPORT_SYMBOL_GPL(NAME);
PRILIMINARIES Just about all module code includes the following header files < linux / module.h > Symbols and functions needed by modules < linux / init.h > Allows you to specify initialization and cleanup functions #include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit );
#include < linux / init.h > #include < linux / module.h > MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init (void) { printk (KERN_ALERT “Hello, world\n”); return 0; } static void hello_exit (void) { printk (KERN_ALERT “Goodbye, cruel world\n”); } module_init ( hello_init ); module_exit ( hello_exit ); INTIALLIZATION AND SHUTDOWN Initialization function: Registers any facility, or functionality offered by the module. Syntax: module_init ( initialization_function ); Shut down: Unregisters various functionalities and returns all resources
A facility is available once a register call is completed Kernel can make calls to registered functions before the initialization function completes Obtain and initialize all critical resources before calling the register function MODULE PARAMETERS Include moduleparam.h , stat.h Need to use the following macros module_param (name, type, permission) module_param_array (name, type, num , permission) “ hello world” module to say hello to someone a number of times %/ sbin / insmod ./ hello.ko someone=“Mom” times=2 Output: Hello Mom Hello Mom
DOING IT IN USER SPACE
DOING IT IN USER SPACE ADVANTAGES The full C library can be linked in. The programmer can run a conventional debugger on the driver code without having to go through contortions to debug a running kernel. If a user-space driver hangs, you can simply kill it. User memory is swappable, unlike kernel memory. A well-designed driver program can still allow concurrent access to a device. DISADVANTAGES • Interrupts are not available in user space. • Direct access to memory is possible if only a privileged user can do that. • Access to I/O ports is available only after calling • Response time is slower. • The most important devices can’t be handled in user space, including, but not limited to, network interfaces and block devices.