SlideShare une entreprise Scribd logo
1  sur  35
(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
DEVICE
DRIVER
WINDOWS
(DEVICE
DRIVERS )
LINUX
(MODULES)
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
MEMORY
MANAGEMENT
FILE SYSTEMSDEVICE
CONTROL
NETWORKING
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
A SPLIT VIEW OF
KERNAL
CLASSES OF DEVICES THAT USE MODULES
CHARACTER DEVICES
BLOCK DEVICES
NETWORK DEVICES
OTHERS
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
DAMAGES
DELIBARATE
INCIDENTAL
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
BUILDING MODULES
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE(“Dual BSD/GPL”);
static int hello_init(void) {
printk(KERN_ALERT “Hello,
worldn”);
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
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,
worldn”);
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
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,
worldn”);
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
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,
worldn”);
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
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,
worldn”);
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
module_init(hello_init);
module_exit(hello_exit);
Micros to indicate which module
initialization and exit functions
to call
1) You need running kernel source code
2) Next go to your kernel module source
code directory and simply create the
Makefile file as follows
3) Compile module using make command
(module build can be done by any user)
4) Once module compiled successfully,
load it and run using insmod or
modprobe command.
Source code
Makefile
Compile
Load
+
run
$ tar -zxvf kernel* -C /usr/src
$ vi Makefile
$ make(complie)
# insmod HELLO.ko
EXAMPLE: HELLO.C MODULE
1. hello.c C source code
2. Create new Makefile
3. Save and close the file
4. Compile hello.c module($ make)
5. Become a root user (use su or sudo)
and load the module
6. Verify that module loaded:
7. See message in /var/log/message file:
8. 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??
 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);
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,
worldn”);
return 0;
}
static void hello_exit(void) {
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
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, worldn”);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT
“Goodbye, cruel worldn”);
}
module_init(hello_init);
module_exit(hello_exit);
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
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
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.
Linux Device Driver’s

Contenu connexe

Tendances

U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
Macpaul Lin
 
Linux Porting to a Custom Board
Linux Porting to a Custom BoardLinux Porting to a Custom Board
Linux Porting to a Custom Board
Patrick Bellasi
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Opersys inc.
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
Houcheng Lin
 

Tendances (20)

Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
 
Linux Porting to a Custom Board
Linux Porting to a Custom BoardLinux Porting to a Custom Board
Linux Porting to a Custom Board
 
Linux Kernel Tour
Linux Kernel TourLinux Kernel Tour
Linux Kernel Tour
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
 
Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)
 
Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
 
Linux programming - Getting self started
Linux programming - Getting self started Linux programming - Getting self started
Linux programming - Getting self started
 
Embedded linux
Embedded linuxEmbedded linux
Embedded linux
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013
 
Uboot startup sequence
Uboot startup sequenceUboot startup sequence
Uboot startup sequence
 
An introduction to the linux kernel and device drivers (NTU CSIE 2016.03)
An introduction to the linux kernel and device drivers (NTU CSIE 2016.03)An introduction to the linux kernel and device drivers (NTU CSIE 2016.03)
An introduction to the linux kernel and device drivers (NTU CSIE 2016.03)
 
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime RipardKernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
 

En vedette

Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
mcganesh
 

En vedette (20)

Device Drivers
Device DriversDevice Drivers
Device Drivers
 
Device Drivers
Device DriversDevice Drivers
Device Drivers
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Android platform
Android platform Android platform
Android platform
 
Usb
UsbUsb
Usb
 
Computer Interfaces
Computer Interfaces Computer Interfaces
Computer Interfaces
 
Usb
UsbUsb
Usb
 
Pakaagee
PakaageePakaagee
Pakaagee
 
Why Drivers Stay with Fleets
Why Drivers Stay with FleetsWhy Drivers Stay with Fleets
Why Drivers Stay with Fleets
 
The USB Generation
The USB GenerationThe USB Generation
The USB Generation
 
Usb
UsbUsb
Usb
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
Device drivers Introduction
Device drivers IntroductionDevice drivers Introduction
Device drivers Introduction
 
Linux kernel code
Linux kernel codeLinux kernel code
Linux kernel code
 
Gnubs-pres-foss-cdac-sem
Gnubs-pres-foss-cdac-semGnubs-pres-foss-cdac-sem
Gnubs-pres-foss-cdac-sem
 
Device Drivers in Linux
Device Drivers in LinuxDevice Drivers in Linux
Device Drivers in Linux
 
Breaking into Open Source and Linux: A USB 3.0 Success Story
Breaking into Open Source and Linux: A USB 3.0 Success StoryBreaking into Open Source and Linux: A USB 3.0 Success Story
Breaking into Open Source and Linux: A USB 3.0 Success Story
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Linux Device Driver Training
Linux Device Driver TrainingLinux Device Driver Training
Linux Device Driver Training
 
USB Universal Serial Bus
USB Universal Serial BusUSB Universal Serial Bus
USB Universal Serial Bus
 

Similaire à Linux Device Driver’s

Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
Nalin Sharma
 

Similaire à Linux Device Driver’s (20)

Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
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
 
Linux
LinuxLinux
Linux
 
Walking around linux kernel
Walking around linux kernelWalking around linux kernel
Walking around linux kernel
 
Device drivers tsp
Device drivers tspDevice drivers tsp
Device drivers tsp
 
Linux
LinuxLinux
Linux
 
Windows Architecture Explained by Stacksol
Windows Architecture Explained by StacksolWindows Architecture Explained by Stacksol
Windows Architecture Explained by Stacksol
 
Operating system
Operating systemOperating system
Operating system
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
 
3CS LSP UNIT 1-1.pdf
3CS LSP UNIT 1-1.pdf3CS LSP UNIT 1-1.pdf
3CS LSP UNIT 1-1.pdf
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
 
Linux security
Linux securityLinux security
Linux security
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Linux Operating System
Linux Operating SystemLinux Operating System
Linux Operating System
 
Docker dDessi november 2015
Docker dDessi november 2015Docker dDessi november 2015
Docker dDessi november 2015
 
Nguyen lyhedieuhanh 14-15_hedieuhanhlinux
Nguyen lyhedieuhanh 14-15_hedieuhanhlinuxNguyen lyhedieuhanh 14-15_hedieuhanhlinux
Nguyen lyhedieuhanh 14-15_hedieuhanhlinux
 
Fight with linux reverse
Fight with linux reverseFight with linux reverse
Fight with linux reverse
 
Linux Virus
Linux VirusLinux Virus
Linux Virus
 

Dernier

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Linux Device Driver’s

  • 2. 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??
  • 3.  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??
  • 4. 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 DEVICE DRIVER WINDOWS (DEVICE DRIVERS ) LINUX (MODULES)
  • 5. Kernel model with device driver hierarchy.
  • 6. 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
  • 7. THAT’S ALL FOR TODAY…. ANY QUESTION??
  • 8. SPLITIING THE ROLES OF KERNAL PROCESS MANAGEMENT MEMORY MANAGEMENT FILE SYSTEMSDEVICE CONTROL NETWORKING
  • 9. 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
  • 10. 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
  • 11. A SPLIT VIEW OF KERNAL
  • 12. CLASSES OF DEVICES THAT USE MODULES CHARACTER DEVICES BLOCK DEVICES NETWORK DEVICES OTHERS 1 2 3 4
  • 13. 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
  • 14. 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
  • 15. SECURITY ISSUES Kernel modules present possibilities for both System does rudimentary checks at module load time It relies on limiting privilege to load modules DAMAGES DELIBARATE INCIDENTAL Hack, Virus, Log Files, Encryption, Logic Bomb Something Happens By Chance, w/o Intention
  • 16. 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
  • 17. 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
  • 18.
  • 19. BUILDING MODULES #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit); The HELLO WORLD program
  • 20. #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit); This module bears a free license The ordering matters sometimes
  • 21. #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit); No main function is used
  • 22. #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit); Invoked when the module is loaded Invoked when the module is removed
  • 23. #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit); Micros to indicate which module initialization and exit functions to call
  • 24. 1) You need running kernel source code 2) Next go to your kernel module source code directory and simply create the Makefile file as follows 3) Compile module using make command (module build can be done by any user) 4) Once module compiled successfully, load it and run using insmod or modprobe command. Source code Makefile Compile Load + run
  • 25. $ tar -zxvf kernel* -C /usr/src $ vi Makefile $ make(complie) # insmod HELLO.ko EXAMPLE: HELLO.C MODULE 1. hello.c C source code 2. Create new Makefile 3. Save and close the file 4. Compile hello.c module($ make) 5. Become a root user (use su or sudo) and load the module 6. Verify that module loaded: 7. See message in /var/log/message file: 8. Unload the module(# rmmod hello)
  • 26. 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
  • 27. LINKING MODULE TO A KERNEL
  • 29.  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);
  • 30. 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, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit);
  • 31. #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, worldn”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); } module_init(hello_init); module_exit(hello_exit); 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
  • 32. 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 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
  • 33.
  • 34. 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.