SlideShare una empresa de Scribd logo
1 de 29
UNIT-4
Device Drivers and Running
Modules
Device Drivers
Device Drivers
Device Drivers
Device Drivers
Device Drivers
The Role of the Device Driver
• The main purpose of device drivers is to provide abstraction by acting as a
translator between a hardware device and the applications or operating
systems that use it.
• Programmers can write higher-level application code independently of
whatever specific hardware the end-user is using
• Writing a device driver requires an in-depth understanding of how the
hardware and the software works for a given platform function.
• drivers require low-level access to hardware functions in order to operate,
• drivers typically operate in a highly privileged environment and can cause
system operational issues if something goes wrong.
• The task of writing drivers thus usually falls to software engineers or
computer engineers who work for hardware-development companies
The Role of the Device Driver
• The role of a device driver is providing mechanism, not policy
• Most programming problems can indeed be split into two parts:
• "what capabilities are to be provided" (the mechanism) and "how those capabilities can
be used" (the policy)
• For example, The floppy driver is policy free—its role is only to show the diskette as a
continuous array of data blocks
• When writing drivers, a programmer should pay particular attention to this fundamental
concept: write kernel code to access the hardware, but don't force particular policies on
the user, since different users have different needs.
• The driver should deal with making the hardware available, leaving all the issues about
how to use the hardware to the applications.
• The driver is also a software layer that lies between the applications and the actual
device
• Policy-free drivers have a number of typical characteristics.
• These include support for both synchronous and asynchronous operation, the ability to
be opened multiple times, the ability to exploit the full capabilities of the hardware, and
the lack of software layers to "simplify things" or provide policy-related operations.
Splitting the Kernel
• In a Unix system, several concurrent processes attend to different tasks.
• Each process asks for system resources, be it computing power, memory,
network connectivity, or some other resource.
• The kernel is the big chunk of executable code in charge of handling all such
requests.
• Although the distinction between the different kernel tasks isn't always
clearly marked, the kernel's role can be split into the following parts:
• Process management
• Memory management
• Filesystems
• Device control:
• With the exception of the processor, memory, and a very few other entities, any and all
device control operations are performed by code that is specific to the device being
addressed. That code is called a device driver
• Networking
Loadable Modules
• One of the good features of Linux is the ability to extend at runtime the set
of features offered by the kernel. This means that you can add functionality
to the kernel (and remove functionality as well) while the system is up and
running
• Each piece of code that can be added to the kernel at runtime is called a
module .
• The Linux kernel offers support for quite a few different types (or classes)
of modules, including, but not limited to, device drivers.
• Each module is made up of object code (not linked into a complete
executable) that can be dynamically linked to the running kernel by the
insmod program and can be unlinked by the rmmod program.
• insmod is a trivial program to insert a module into the kernel
• rmmod is a trivial program to remove a module from the kernel.
Classes of Devices and Modules
• The Linux way of looking at devices distinguishes between three
fundamental device types.
• Character devices
• Block devices
• Network devices
• Each module usually implements one of these types, and thus is classifiable
as a char module, a block module, or a network module.
• Character devices
• Writing/reading character by character
• Operate in blocking mode
• Synchronous with operations
• The text console (devconsole) and the serial ports (devttyS0 and friends) are
examples of char devices
• Char devices are accessed by means of filesystem nodes
• The only relevant difference between a char device and a regular file is that you can
always move back and forth in the regular file, whereas most char devices are just
data channels, which you can only access sequentially.
Classes of Devices and Modules
• Block devices
• Reading/writing block by block
• Operations takes long time to complete , asynchronous with operations
• Unlike character devices, block devices will always allow the programmer to
read or write a block of any size (including single characters/bytes) and any
alignment
• The downside is that because block devices are buffered, the programmer
does not know how long it will take before written data is passed from the
kernel's buffers to the actual device
Classes of Devices and Modules
• Network interfaces:
• Any network transaction is made through an interface, that is, a
device that is able to exchange data with other hosts
• A network interface is incharge of sending and receiving data packets,
driven by the network subsystem of the kernel, without knowing how
individual transactions map to the actual packets being transmitted.
• A network driver knows nothing about individual connections; it only
handles packets.
Security Issues
• Security is an increasingly important concern in modern times
• Any security check in the system is enforced by kernel code.
• If the kernel has security holes, then the system as a whole has holes.
• In the official kernel distribution, only an authorized user can load modules; the system call
init_module checks if the invoking process is authorized to load a module into the kernel.
• when running an official kernel, only the superuser,[1] or an intruder who has succeeded in
becoming privileged, can exploit the power of privileged code
• When possible, driver writers should avoid encoding security policy in their code.
• Security is a policy issue that is often best handled at higher levels within the kernel, under
the control of the system administrator
• Any input received from user processes should be treated with great suspicion; never trust it
unless you can verify it.
• Be careful when receiving 3rd party software,escpecially when the program is kernel related.
Building and Running Modules
• Setting up your Test system:
• Kernel modules are piece of code, that can be loaded and unloaded from kernel on demand.
• Kernel modules offers an easy way to extend the functionality of the base kernel without having
to rebuild or recompile the kernel again.
• Most of the drivers are implemented as a Linux kernel modules.
• When those drivers are not needed, we can unload only that specific driver, which will reduce
the kernel image size.
• The kernel modules will have a .ko extension.
• To insert module into kernel, use the insmod command. The syntax of this command is −
“insmod <module_name>.ko”
• To remove module from kernel, use the rmmod command. A module that is used by a program
cannot be removed. The syntax of the rmmod<.i> command is-
“rmmod <module_name>.ko”
• On a normal linux system, the kernel modules will reside inside
/lib/modules/<kernel_version>/kernel/ directory.
• For running kernel modules we have to obtain the "mainline" kernel directly from the kernel.org
mirror network, and install it on our system.
Building and Running Modules
• The Hello World Module:
• #include <linux/kernel.h>
• #include <linux/init.h>
• #include <linux/module.h>
• MODULE_DESCRIPTION("Kernel module 1 ");
• MODULE_AUTHOR("Harry");
• MODULE_LICENSE("GPL");
• static int example_init(void)
• { //according hardware fnctinalities code will be included//
• pr_debug("Welcome! Good to have you here!n");
• return 0;
• }
• static void example_exit(void)
• {
• pr_debug("Goodbye!n");
• }
• module_init(example_init);
• module_exit(example_exit);
Building and Running Modules
• Steps for running the program:
• Makefile:
• Makefile is a program building tool which runs on Unix, Linux, and their flavors.
• It aids in simplifying building program executables that may need various modules
• A makefile is a special file, containing shell commands, that you create and name
makefile
• Compile the module
• To insert the module −
“insmod example.ko”
• Remove the module
“rmmod example.ko”
• When a module is inserted into the kernel, the module_init macro
will be invoked, which will call the function hello_init.
• Similarly, when the module is removed with rmmod, module_exit
macro will be invoked, which will call the hello_exit.
Building and Running Modules
• KO modules loading (for instance the ones comprised of device’s
drivers) can be performed without the necessity to restart a
computer, with the insmod or kldload (in FreeBSD) tool, however,
sometimes initially other modules on which a given file is dependant
have to be loaded first.
• In order to check which modules are installed, user has to browse
through /proc/modules directory or use the lsmod command.
Kernel Modules Versus Applications
• Differences between kernel modules and applications
• Most small and medium-sized applications perform a single task from beginning to end,
every kernel module just registers itself in order to serve future requests, and its
initialization function terminates immediately.In otherwords,the task of module’s
initialization function is to prepare for later invocation of the module’s functions when
required.
• Both kernel modules and applications are event driven.event-driven programming is a
programming paradigm in which the flow of the program is determined by events such as
user actions (mouse clicks, key presses), sensor outputs, or messages from other
programs or threads.As a part of event driven type the applications and kernel modules
require some resources for perform activity,but the applications are lazy at releasing the
resources ,whereas the kernel modules clean up all resources which were used for the
completion of activity.
• Kernel modules have no library support,but the applications has huge library support.
Kernel Modules Versus Applications
• User Space and Kernel Space
• A module runs in kernel space, whereas applications run in user space
• Under Unix, the kernel executes in the highest level (also called supervisor mode
), where everything is allowed, whereas applications execute in the lowest level
(the so-called user mode ), where the processor regulates direct access to
hardware and unauthorized access to memory.
• Unix transfers execution from user space to kernel space whenever an application
issues a system call or is suspended by a hardware interrupt. Kernel code
executing a system call is working in the context of a process—it operates on
behalf of the calling process and is able to access data in the process's address
space.
• The role of a module is to extend kernel functionality; modularized code runs in
kernel space.
Kernel Modules Versus Applications
• Concurrency in the Kernel
• One way in which kernel programming differs greatly from
conventional application programming is the issue of concurrency
• In order to achieve the optimized system performance, kernel
provides the multitasking, where multiple threads can execute in
parallel and thereby utilizing the CPU in optimum way. Though useful,
but multitasking, if not implemented cautiously can lead to
concurrency issues, which can be very difficult to handle.
Concurrency issues are handled by semaphores and mutex.
Kernel Modules Versus Applications
• The Current Process:
• Although kernel modules don't execute sequentially as applications do, most actions
performed by the kernel are done on behalf of a specific process.
• Kernel code can refer to the current process by accessing the global item current,
defined in <asm/current.h>, which yields a pointer to struct task_struct,defined by
<linux/sched.h>.
• The current pointer refers to the process that is currently executing. During the
execution of a system call, such as open or read, the current process is the one that
invoked the call.
• Kernel code can use process specific information by using current, if it needs to do
so.
• A Few Other Details
• Often, as you look at the kernel API, you will encounter function names starting with
a double underscore ( ). Functions so marked are generally a low-level component of
the interface and should be used with caution.
• Kernel code cannot do floating point arithmetic
Compiling and Loading
• The Kernel Symbol Table:
• In linux,symbols are nothing but variables and functions that are needed to
implement modularized drivers.
• Each and every symbol has its address in the memory.
• How symbols are exported?
• Exporting kernel symbols is typically done with
• EXPORT_SYMBOL()
• EXPORT_SYMBOL_GPL()
• EXPORT_SYMBOL() which exports a given symbol to all loadable
modules.
• EXPORT_SYMBOL_GPL() functionality is same as EXPERT_SYMBOL,but
it marks the exported symbol as only in modules licensed through
either general public license
Compiling and Loading
• What is kernel symbol table?
• Kernel symbol table is a lookup table between the symbol names and their addresses in memory.
• When a module is loaded into kernel memory using insmod,any symbol exported by the module
becomes part of the kernel symbol table.
• New modules can use symbols exported by old module, and you can stack new modules on top of
other modules.This is known as Module stacking.
• Module stacking is useful in complex projects.
• Preliminaries
• Most kernel code ends up including a fairly large number of header files to get definitions of
functions, data types, and variables
• All module code has the following:
• #include <linux/module.h>
• #include <linux/init.h>
• module.h contains a great many definitions of symbols and functions needed by loadable
modules.
• init.h to specify your initialization and cleanup functions,
• Most modules also include moduleparam.h to enable the passing of parameters to the module at
load time
• Other descriptive definitions that can be contained within a module include
• MODULE_AUTHOR (stating who wrote the module),
• MODULE_DESCRIPTION (a human readable statement of what the module does),
• MODULE_VERSION (for a code revision number)
Initialization and Shutdown
• The module initialization function registers any facility offered by the module.
• By facility, we mean a new functionality, be it a whole driver or a new software
abstraction, that can be accessed by an application.
• The actual definition of the initialization function always looks like:
static int __init initialization_function(void)
{
/* Initialization code here */
} module_init(initialization_function);
• Initialization functions should be declared static, since they are not meant to be
visible outside the specific file
• it is a hint to the kernel that the given function is used only at initialization time.
• Without this definition, your initialization function is never called.
• Modules can register many different types of facilities, including different kinds of
devices, filesystems, cryptographic transforms, and more.
• For each facility,there is a specific kernel function that accomplishes this
registration.
Initialization and Shutdown
• The Cleanup Function
• Every nontrivial module also requires a cleanup function, which unregisters
interfaces and returns all resources to the system before the module is removed
• This function is defined as:
static void __exit cleanup_function(void)
{
/* Cleanup code here */
}
module_exit(cleanup_function);
• The cleanup function has no value to return, so it is declared void.
• The exit modifier marks the code as being for module unload only
• If your module is built directly into the kernel, or if your kernel is configured to
disallow the unloading of modules, functions marked exit are simply discarded.
• If your module does not define a cleanup function, the kernel does not allow it to
be unloaded.
Initialization and Shutdown
• Error Handling During Initialization:
• One thing you must always bear in mind when registering facilities with the kernel is that
the registration could fail.
• Even the simplest action often requires memory allocation, and the required memory
may not be available.
• So module code must always check return values, and be sure that the requested
operations have actually succeeded.
• If any errors occur when you register utilities, the first order of business is to decide
whether the module can continue initializing itself anyway.
• Often, the module can continue to operate after a registration failure, with degraded
functionality if necessary.
• Error recovery is sometimes best handled with the goto statement.
• Careful use of goto in error situations can eliminate a great deal of complicated, highly-
indented, "structured" logic.
Initialization and Shutdown
• Module-Loading Races
• The first is that you should always remember that some other part of the kernel
can make use of any facility you register immediately after that registration has
completed.
• It is entirely possible, in other words, that the kernel will make calls into your
module while your initialization function is still running.
• So your code must be prepared to be called as soon as it completes its first
registration.
• Do not register any facility until all of your internal initialization needed to
support that facility has been completed.
• You must also consider what happens if your initialization function decides to fail,
but some part of the kernel is already making use of a facility your module has
registered.
• If this situation is possible for your module, you should seriously consider not
failing the initialization at all.
• After all, the module has clearly succeeded in exporting something useful.
• If initialization must fail, it must carefully step around any possible operations
going on elsewhere in the kernel until those operations have completed.
Doing It in User Space
• The following are the reasons for executing the modules in user space
than the kernel space:
• The full C library can be linked in. The driver can perform many exotic tasks
without resorting to external programs
• The programmer can run a conventional debugger on the driver code without
having to go through contortions to debug a running kernel.
• If a userspace driver hangs, you can simply kill it
• User memory is swappable, unlike kernel memory
• A well-designed driver program can still, like kernel-space drivers, allow
concurrent access to a device

Más contenido relacionado

La actualidad más candente

Kernel module programming
Kernel module programmingKernel module programming
Kernel module programmingVandana Salve
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesYourHelper1
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O YourHelper1
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OYourHelper1
 
Device Driver in WinCE 6.0 R2
Device Driver in WinCE 6.0 R2Device Driver in WinCE 6.0 R2
Device Driver in WinCE 6.0 R2rahul_p_shukla
 
Device Drivers in Linux
Device Drivers in LinuxDevice Drivers in Linux
Device Drivers in LinuxShreyas MM
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O YourHelper1
 
linux device driver
linux device driverlinux device driver
linux device driverRahul Batra
 
Linux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingLinux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingStephan Cadene
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module ProgrammingSaurabh Bangad
 
Understanding Modern Device Drivers
Understanding Modern Device DriversUnderstanding Modern Device Drivers
Understanding Modern Device Driversasimkadav
 
Introduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmwareIntroduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmwaredefinecareer
 
Board support package_on_linux
Board support package_on_linuxBoard support package_on_linux
Board support package_on_linuxVandana Salve
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module ProgrammingAmir Payberah
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory managementVandana Salve
 

La actualidad más candente (20)

Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
Linux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/OLinux System Programming - Advanced File I/O
Linux System Programming - Advanced File I/O
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Device Driver in WinCE 6.0 R2
Device Driver in WinCE 6.0 R2Device Driver in WinCE 6.0 R2
Device Driver in WinCE 6.0 R2
 
Device Drivers in Linux
Device Drivers in LinuxDevice Drivers in Linux
Device Drivers in Linux
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Linux Kernel and Driver Development Training
Linux Kernel and Driver Development TrainingLinux Kernel and Driver Development Training
Linux Kernel and Driver Development Training
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module Programming
 
Understanding Modern Device Drivers
Understanding Modern Device DriversUnderstanding Modern Device Drivers
Understanding Modern Device Drivers
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Introduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmwareIntroduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmware
 
Board support package_on_linux
Board support package_on_linuxBoard support package_on_linux
Board support package_on_linux
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 

Similar a Device Drivers and Running Modules

Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Ahmed El-Arabawy
 
_Kernel and Kernel Architectures.ppt
_Kernel and Kernel Architectures.ppt_Kernel and Kernel Architectures.ppt
_Kernel and Kernel Architectures.pptHardeepKaurCSEAssist
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteTushar B Kute
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,Linux Device Driver,LDD,
Linux Device Driver,LDD,Rahul Batra
 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgSam Bowne
 
F9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
F9: A Secure and Efficient Microkernel Built for Deeply Embedded SystemsF9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
F9: A Secure and Efficient Microkernel Built for Deeply Embedded SystemsNational Cheng Kung University
 
Basics of micro controllers for biginners
Basics of  micro controllers for biginnersBasics of  micro controllers for biginners
Basics of micro controllers for biginnersGerwin Makanyanga
 
Chapter one_oS.ppt
Chapter one_oS.pptChapter one_oS.ppt
Chapter one_oS.pptmiki304759
 
Regarding About Operating System Structure
Regarding About Operating System StructureRegarding About Operating System Structure
Regarding About Operating System Structuresankarkvdc
 

Similar a Device Drivers and Running Modules (20)

Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers
 
_Kernel and Kernel Architectures.ppt
_Kernel and Kernel Architectures.ppt_Kernel and Kernel Architectures.ppt
_Kernel and Kernel Architectures.ppt
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Embedded system - embedded system programming
Embedded system - embedded system programmingEmbedded system - embedded system programming
Embedded system - embedded system programming
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
System structure
System structureSystem structure
System structure
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,Linux Device Driver,LDD,
Linux Device Driver,LDD,
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
CNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbgCNIT 126: 10: Kernel Debugging with WinDbg
CNIT 126: 10: Kernel Debugging with WinDbg
 
Operating System 2.pptx
Operating System 2.pptxOperating System 2.pptx
Operating System 2.pptx
 
Microkernel
MicrokernelMicrokernel
Microkernel
 
Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 
OS_MD_1.pdf
OS_MD_1.pdfOS_MD_1.pdf
OS_MD_1.pdf
 
F9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
F9: A Secure and Efficient Microkernel Built for Deeply Embedded SystemsF9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
F9: A Secure and Efficient Microkernel Built for Deeply Embedded Systems
 
Basics of micro controllers for biginners
Basics of  micro controllers for biginnersBasics of  micro controllers for biginners
Basics of micro controllers for biginners
 
Operating system
Operating systemOperating system
Operating system
 
Chapter one_oS.ppt
Chapter one_oS.pptChapter one_oS.ppt
Chapter one_oS.ppt
 
Regarding About Operating System Structure
Regarding About Operating System StructureRegarding About Operating System Structure
Regarding About Operating System Structure
 
Distributive operating system
Distributive operating systemDistributive operating system
Distributive operating system
 
Architecture of Linux
 Architecture of Linux Architecture of Linux
Architecture of Linux
 

Último

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Último (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Device Drivers and Running Modules

  • 1. UNIT-4 Device Drivers and Running Modules
  • 7. The Role of the Device Driver • The main purpose of device drivers is to provide abstraction by acting as a translator between a hardware device and the applications or operating systems that use it. • Programmers can write higher-level application code independently of whatever specific hardware the end-user is using • Writing a device driver requires an in-depth understanding of how the hardware and the software works for a given platform function. • drivers require low-level access to hardware functions in order to operate, • drivers typically operate in a highly privileged environment and can cause system operational issues if something goes wrong. • The task of writing drivers thus usually falls to software engineers or computer engineers who work for hardware-development companies
  • 8. The Role of the Device Driver • The role of a device driver is providing mechanism, not policy • Most programming problems can indeed be split into two parts: • "what capabilities are to be provided" (the mechanism) and "how those capabilities can be used" (the policy) • For example, The floppy driver is policy free—its role is only to show the diskette as a continuous array of data blocks • When writing drivers, a programmer should pay particular attention to this fundamental concept: write kernel code to access the hardware, but don't force particular policies on the user, since different users have different needs. • The driver should deal with making the hardware available, leaving all the issues about how to use the hardware to the applications. • The driver is also a software layer that lies between the applications and the actual device • Policy-free drivers have a number of typical characteristics. • These include support for both synchronous and asynchronous operation, the ability to be opened multiple times, the ability to exploit the full capabilities of the hardware, and the lack of software layers to "simplify things" or provide policy-related operations.
  • 9. Splitting the Kernel • In a Unix system, several concurrent processes attend to different tasks. • Each process asks for system resources, be it computing power, memory, network connectivity, or some other resource. • The kernel is the big chunk of executable code in charge of handling all such requests. • Although the distinction between the different kernel tasks isn't always clearly marked, the kernel's role can be split into the following parts: • Process management • Memory management • Filesystems • Device control: • With the exception of the processor, memory, and a very few other entities, any and all device control operations are performed by code that is specific to the device being addressed. That code is called a device driver • Networking
  • 10. Loadable Modules • One of the good features of Linux is the ability to extend at runtime the set of features offered by the kernel. This means that you can add functionality to the kernel (and remove functionality as well) while the system is up and running • Each piece of code that can be added to the kernel at runtime is called a module . • The Linux kernel offers support for quite a few different types (or classes) of modules, including, but not limited to, device drivers. • Each module is made up of object code (not linked into a complete executable) that can be dynamically linked to the running kernel by the insmod program and can be unlinked by the rmmod program. • insmod is a trivial program to insert a module into the kernel • rmmod is a trivial program to remove a module from the kernel.
  • 11. Classes of Devices and Modules • The Linux way of looking at devices distinguishes between three fundamental device types. • Character devices • Block devices • Network devices • Each module usually implements one of these types, and thus is classifiable as a char module, a block module, or a network module. • Character devices • Writing/reading character by character • Operate in blocking mode • Synchronous with operations • The text console (devconsole) and the serial ports (devttyS0 and friends) are examples of char devices • Char devices are accessed by means of filesystem nodes • The only relevant difference between a char device and a regular file is that you can always move back and forth in the regular file, whereas most char devices are just data channels, which you can only access sequentially.
  • 12. Classes of Devices and Modules • Block devices • Reading/writing block by block • Operations takes long time to complete , asynchronous with operations • Unlike character devices, block devices will always allow the programmer to read or write a block of any size (including single characters/bytes) and any alignment • The downside is that because block devices are buffered, the programmer does not know how long it will take before written data is passed from the kernel's buffers to the actual device
  • 13. Classes of Devices and Modules • Network interfaces: • Any network transaction is made through an interface, that is, a device that is able to exchange data with other hosts • A network interface is incharge of sending and receiving data packets, driven by the network subsystem of the kernel, without knowing how individual transactions map to the actual packets being transmitted. • A network driver knows nothing about individual connections; it only handles packets.
  • 14. Security Issues • Security is an increasingly important concern in modern times • Any security check in the system is enforced by kernel code. • If the kernel has security holes, then the system as a whole has holes. • In the official kernel distribution, only an authorized user can load modules; the system call init_module checks if the invoking process is authorized to load a module into the kernel. • when running an official kernel, only the superuser,[1] or an intruder who has succeeded in becoming privileged, can exploit the power of privileged code • When possible, driver writers should avoid encoding security policy in their code. • Security is a policy issue that is often best handled at higher levels within the kernel, under the control of the system administrator • Any input received from user processes should be treated with great suspicion; never trust it unless you can verify it. • Be careful when receiving 3rd party software,escpecially when the program is kernel related.
  • 15. Building and Running Modules • Setting up your Test system: • Kernel modules are piece of code, that can be loaded and unloaded from kernel on demand. • Kernel modules offers an easy way to extend the functionality of the base kernel without having to rebuild or recompile the kernel again. • Most of the drivers are implemented as a Linux kernel modules. • When those drivers are not needed, we can unload only that specific driver, which will reduce the kernel image size. • The kernel modules will have a .ko extension. • To insert module into kernel, use the insmod command. The syntax of this command is − “insmod <module_name>.ko” • To remove module from kernel, use the rmmod command. A module that is used by a program cannot be removed. The syntax of the rmmod<.i> command is- “rmmod <module_name>.ko” • On a normal linux system, the kernel modules will reside inside /lib/modules/<kernel_version>/kernel/ directory. • For running kernel modules we have to obtain the "mainline" kernel directly from the kernel.org mirror network, and install it on our system.
  • 16. Building and Running Modules • The Hello World Module: • #include <linux/kernel.h> • #include <linux/init.h> • #include <linux/module.h> • MODULE_DESCRIPTION("Kernel module 1 "); • MODULE_AUTHOR("Harry"); • MODULE_LICENSE("GPL"); • static int example_init(void) • { //according hardware fnctinalities code will be included// • pr_debug("Welcome! Good to have you here!n"); • return 0; • } • static void example_exit(void) • { • pr_debug("Goodbye!n"); • } • module_init(example_init); • module_exit(example_exit);
  • 17. Building and Running Modules • Steps for running the program: • Makefile: • Makefile is a program building tool which runs on Unix, Linux, and their flavors. • It aids in simplifying building program executables that may need various modules • A makefile is a special file, containing shell commands, that you create and name makefile • Compile the module • To insert the module − “insmod example.ko” • Remove the module “rmmod example.ko” • When a module is inserted into the kernel, the module_init macro will be invoked, which will call the function hello_init. • Similarly, when the module is removed with rmmod, module_exit macro will be invoked, which will call the hello_exit.
  • 18. Building and Running Modules • KO modules loading (for instance the ones comprised of device’s drivers) can be performed without the necessity to restart a computer, with the insmod or kldload (in FreeBSD) tool, however, sometimes initially other modules on which a given file is dependant have to be loaded first. • In order to check which modules are installed, user has to browse through /proc/modules directory or use the lsmod command.
  • 19. Kernel Modules Versus Applications • Differences between kernel modules and applications • Most small and medium-sized applications perform a single task from beginning to end, every kernel module just registers itself in order to serve future requests, and its initialization function terminates immediately.In otherwords,the task of module’s initialization function is to prepare for later invocation of the module’s functions when required. • Both kernel modules and applications are event driven.event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs or threads.As a part of event driven type the applications and kernel modules require some resources for perform activity,but the applications are lazy at releasing the resources ,whereas the kernel modules clean up all resources which were used for the completion of activity. • Kernel modules have no library support,but the applications has huge library support.
  • 20. Kernel Modules Versus Applications • User Space and Kernel Space • A module runs in kernel space, whereas applications run in user space • Under Unix, the kernel executes in the highest level (also called supervisor mode ), where everything is allowed, whereas applications execute in the lowest level (the so-called user mode ), where the processor regulates direct access to hardware and unauthorized access to memory. • Unix transfers execution from user space to kernel space whenever an application issues a system call or is suspended by a hardware interrupt. Kernel code executing a system call is working in the context of a process—it operates on behalf of the calling process and is able to access data in the process's address space. • The role of a module is to extend kernel functionality; modularized code runs in kernel space.
  • 21. Kernel Modules Versus Applications • Concurrency in the Kernel • One way in which kernel programming differs greatly from conventional application programming is the issue of concurrency • In order to achieve the optimized system performance, kernel provides the multitasking, where multiple threads can execute in parallel and thereby utilizing the CPU in optimum way. Though useful, but multitasking, if not implemented cautiously can lead to concurrency issues, which can be very difficult to handle. Concurrency issues are handled by semaphores and mutex.
  • 22. Kernel Modules Versus Applications • The Current Process: • Although kernel modules don't execute sequentially as applications do, most actions performed by the kernel are done on behalf of a specific process. • Kernel code can refer to the current process by accessing the global item current, defined in <asm/current.h>, which yields a pointer to struct task_struct,defined by <linux/sched.h>. • The current pointer refers to the process that is currently executing. During the execution of a system call, such as open or read, the current process is the one that invoked the call. • Kernel code can use process specific information by using current, if it needs to do so. • A Few Other Details • Often, as you look at the kernel API, you will encounter function names starting with a double underscore ( ). Functions so marked are generally a low-level component of the interface and should be used with caution. • Kernel code cannot do floating point arithmetic
  • 23. Compiling and Loading • The Kernel Symbol Table: • In linux,symbols are nothing but variables and functions that are needed to implement modularized drivers. • Each and every symbol has its address in the memory. • How symbols are exported? • Exporting kernel symbols is typically done with • EXPORT_SYMBOL() • EXPORT_SYMBOL_GPL() • EXPORT_SYMBOL() which exports a given symbol to all loadable modules. • EXPORT_SYMBOL_GPL() functionality is same as EXPERT_SYMBOL,but it marks the exported symbol as only in modules licensed through either general public license
  • 24. Compiling and Loading • What is kernel symbol table? • Kernel symbol table is a lookup table between the symbol names and their addresses in memory. • When a module is loaded into kernel memory using insmod,any symbol exported by the module becomes part of the kernel symbol table. • New modules can use symbols exported by old module, and you can stack new modules on top of other modules.This is known as Module stacking. • Module stacking is useful in complex projects. • Preliminaries • Most kernel code ends up including a fairly large number of header files to get definitions of functions, data types, and variables • All module code has the following: • #include <linux/module.h> • #include <linux/init.h> • module.h contains a great many definitions of symbols and functions needed by loadable modules. • init.h to specify your initialization and cleanup functions, • Most modules also include moduleparam.h to enable the passing of parameters to the module at load time • Other descriptive definitions that can be contained within a module include • MODULE_AUTHOR (stating who wrote the module), • MODULE_DESCRIPTION (a human readable statement of what the module does), • MODULE_VERSION (for a code revision number)
  • 25. Initialization and Shutdown • The module initialization function registers any facility offered by the module. • By facility, we mean a new functionality, be it a whole driver or a new software abstraction, that can be accessed by an application. • The actual definition of the initialization function always looks like: static int __init initialization_function(void) { /* Initialization code here */ } module_init(initialization_function); • Initialization functions should be declared static, since they are not meant to be visible outside the specific file • it is a hint to the kernel that the given function is used only at initialization time. • Without this definition, your initialization function is never called. • Modules can register many different types of facilities, including different kinds of devices, filesystems, cryptographic transforms, and more. • For each facility,there is a specific kernel function that accomplishes this registration.
  • 26. Initialization and Shutdown • The Cleanup Function • Every nontrivial module also requires a cleanup function, which unregisters interfaces and returns all resources to the system before the module is removed • This function is defined as: static void __exit cleanup_function(void) { /* Cleanup code here */ } module_exit(cleanup_function); • The cleanup function has no value to return, so it is declared void. • The exit modifier marks the code as being for module unload only • If your module is built directly into the kernel, or if your kernel is configured to disallow the unloading of modules, functions marked exit are simply discarded. • If your module does not define a cleanup function, the kernel does not allow it to be unloaded.
  • 27. Initialization and Shutdown • Error Handling During Initialization: • One thing you must always bear in mind when registering facilities with the kernel is that the registration could fail. • Even the simplest action often requires memory allocation, and the required memory may not be available. • So module code must always check return values, and be sure that the requested operations have actually succeeded. • If any errors occur when you register utilities, the first order of business is to decide whether the module can continue initializing itself anyway. • Often, the module can continue to operate after a registration failure, with degraded functionality if necessary. • Error recovery is sometimes best handled with the goto statement. • Careful use of goto in error situations can eliminate a great deal of complicated, highly- indented, "structured" logic.
  • 28. Initialization and Shutdown • Module-Loading Races • The first is that you should always remember that some other part of the kernel can make use of any facility you register immediately after that registration has completed. • It is entirely possible, in other words, that the kernel will make calls into your module while your initialization function is still running. • So your code must be prepared to be called as soon as it completes its first registration. • Do not register any facility until all of your internal initialization needed to support that facility has been completed. • You must also consider what happens if your initialization function decides to fail, but some part of the kernel is already making use of a facility your module has registered. • If this situation is possible for your module, you should seriously consider not failing the initialization at all. • After all, the module has clearly succeeded in exporting something useful. • If initialization must fail, it must carefully step around any possible operations going on elsewhere in the kernel until those operations have completed.
  • 29. Doing It in User Space • The following are the reasons for executing the modules in user space than the kernel space: • The full C library can be linked in. The driver can perform many exotic tasks without resorting to external programs • The programmer can run a conventional debugger on the driver code without having to go through contortions to debug a running kernel. • If a userspace driver hangs, you can simply kill it • User memory is swappable, unlike kernel memory • A well-designed driver program can still, like kernel-space drivers, allow concurrent access to a device