SlideShare a Scribd company logo
1 of 40
Kernel Module Programming

BY:
MONIS JAVED
NALIN SHARMA
OBEDULLAH
WHAT IS KERNEL?
a program having control over everything that occurs in
system.
 central part of operating system.
 it acts as an interface between the user applications
and the hardware.

ROLE OF THE KERNEL
Kernel is responsible for:








Process Management
- allocating resources to a process.
- synchronization among processes.
Memory Management
- allocating and deallocating memory to programs.
Device Management
- controlling several devices attached to the system.
Storage Management
- disk management.
- disk scheduling.
TYPES OF KERNEL


Monolithic Kernel



Micro Kernel



Hybrid Kernel



Nano Kernel



Exo Kernel
MONOLITHIC KERNEL








Entire operating system works in kernel space.
Larger in size as they retain full privilege access
over various components like file system, IPC,
IO/device management etc.
Slower to load because of large size.
Recompilation is required to add more features
or remove bugs.
Example includes: Linux based OS, MSDOS etc.
MICRO KERNEL


Deals with only critical activities such as controlling
memory and CPU.



Everything else is handled under user mode.



Kernel don’t have to worry about lower level
functionality.



Example include: QNX ,Minix.
HYBRID KERNEL


have the ability to pick and choose what they want
to run in user mode and what they want to run in
supervisor mode.



Example : Windows NT,2000, XP, Vista,7,8, Mac
OS X.
INTRODUCING KERNEL MODULES
KERNEL MODULES


Linux Kernel has ability to extend its set of
features at run time.



Modules are pieces of code that can be
loaded and unloaded into the kernel upon
demand.



They extend the functionality of the kernel
without the need to reboot the system.
KERNEL MODULES CONTD…


A module run in kernel mode.



Without loadable kernel modules, an operating
system would have to include all possible
anticipated functionality already compiled directly
into the base kernel.



This require rebooting kernel every time when new
functionality is added.
KERNEL MODULES CONTD…


Kernel modules allow a Linux system to be set up
with a standard minimal kernel without any extra
device drivers built in.



For example, one type of module is the device
driver, which allows the kernel to access hardware
connected to the system.
MODULE PROGRAMMING
MODULE COMMANDS
• modinfo : display information about a kernel module
• lsmod : list loaded modules
• insmod : Install loadable kernel module
• rmmod : Unload loadable modules
• depmod : handle dependency descriptions for loadable
kernel modules

• modprobe : High level handling of loadable modules
HOW DO MODULES GET INTO THE KERNEL?


modules already loaded into the kernel can be
listed by running lsmod, which gets its
information by reading the file /proc/modules.



When the kernel needs a feature that is not
resident in the kernel, the kernel module
daemon kmod executes modprobe to load the
module in.



modprobe is passed a string in one of two forms:
· A module name like soft or ppp.
· A more generic identifier like
CONTD…...


If modprobe is handed a generic identifier, it first looks for
that string in the file /etc/modules.conf. If it finds an alias
line like:

alias char−major−10−30 soft
it knows that the generic identifier refers to the module
soft.o


Next, modprobe looks through the file
/lib/modules/version/modules.dep, to see if other modules
must be loaded before the requested module may be
loaded.



This file is created by depmod −a and contains module
dependencies.
CONTD….


Lastly, modprobe uses insmod to first load any prerequisite
modules into the kernel, and then the requested module. modprobe
directs insmod to /lib/modules/version/, the standard directory for
modules.



insmod is intended to be fairly dumb about the location of modules,
whereas modprobe is aware of the default location of modules.
CONTD…
Example:
We need to insert module msdos.o which requires fat.o
module to be already loaded. This can be done in two ways:

1. using insmod

insmod
/lib/modules/2.5.1/kernel/fs/fat/fat.o
insmod
/lib/modules/2.5.1/kernel/fs/msdos/msdos.
o
2. using modprobe
BEFORE WE BEGIN TO GET INTO THE CODE
MODVERSIONING


Everyone's system is different and everyone has
their own groove.



A module compiled for one kernel won't load if
you boot a different kernel.



unless you enable CONFIG_MODVERSIONS in
the kernel.
STEPS



#cd /usr/src/linux-headers-3.8.0-34-generic/
#make menuconfig
HELLO WORLD PROGRAM


/* hello−1.c − The simplest kernel module.*/



#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h>
/* Needed for KERN_ALERT */
int init_module(void)
{
printk("Hello world n");
return 0;
//A non 0 return means init_module failed; module can't be








loaded.







}
void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.n");
}


Kernel modules must have at least two
functions:



a "start" (initialization) function called
init_module() which is called when the module is
insmoded into the kernel, and



an "end" (cleanup) function called
cleanup_module() which is called just before it is
rmmoded.


init_module() either registers a handler for
something with the kernel, or it replaces one of
the kernel functions with its own code (usually
code to do something and then call the original
function).



The cleanup_module() function is supposed to
undo whatever init_module() did, so the module
can be unloaded safely.
INTRODUCING PRINTK()









printk() is not meant to communicate information
to the user.
It is used to log information or give warnings.
each printk() statement comes with a priority.
There are 8 priorities and the kernel has macros
for them.
The header file linux/kernel.h describes what
each priority means.
PRINTK() CONTD…
1. KERN_EMERG "<0>" /* emergency message*/
2. KERN_ALERT

"<1>" /* action must be taken immediately*/

3 KERN_CRIT

"<2>" /* critical conditions*/

4. KERN_ERR

"<3>" /* error conditions*/

5. KERN_WARNING "<4>" /* warning conditions*/
6. KERN_NOTICE

7. KERN_INFO

"<5>" /* normal but significant condition*/

"<6>" /* informational message*/

8. KERN_DEBUG "<7>" /* debug-level messages*/
Default : DEFAULT_MESSAGE_LOGLEVEL
PRINTK() CONTD…


If the priority is less than int console_loglevel, the
message is printed on your current terminal.



We use a high priority, like KERN_ALERT, to make
sure the printk() messages get printed to your
console rather than just logged to your logfile.
STEPS FOR COMPILATION OF
KERNEL MODULE
1. write the kernel module code with filename
like filename.c
2. make a file containing code for compiling the
module and save it using name “Makefile”.
3. now compile the module code by issuing
command make.
To see what kind of information it is , type:modinfo filename.ko
CONTD…
4. now insert module by command
modprobe filename.ko
OR
insmod filename.ko
- you can see your inserted module in
cat /proc/modules
OR by command
lsmod
MAKE FILE


A makefile is a script for appropriate compilation of
different type of sources to the appropriate object
code.



Makefiles are used to define the procedure to
compile and link your program.



as number of files increases it's very difficult to
compile and link them one by one.
CONTD….


Also you will have to remember the dependencies
between these files.



Makefiles are used to automate these tasks so just
define your rules once and instead of compiling
and linking individual files you just need to execute
the makefile.
HOW TO MAKE A “MAKEFILE” FOR KERNEL
MODULE:
obj-m += filename.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
RENAME YOUR FUNCTION NAME:

you can rename the init and cleanup functions of
your modules.



This is done with the --module_init() and
module_exit()



These are macros defined in linux/init.h
EXAMPLE:














#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
static int _init myname(void)
{
printk(KERN_INFO “Hello world?”);
return 0;
}
static void _exit yourname(void)
{
printk(KERN_INFO “Goodbye”);
}
module_init(myname);
module_exit(yourname);
MODULES SPANNING MULTIPLE FILES:Modules Spanning Multiple Files: First we invent an object name for our
combined module,
 Second we tell make what object files are
part of that module.

begin.c
end.c
#include <linux/kernel.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/module.h>
int init_module(void)
void cleanup_module()
{
{
printk(KERN_INFO "Hello,
printk(KERN_INFO
worldn");
"Goodbyen");
return 0;
}
}
Makefile :obj-m += beginend.o
beginend-objs := begin.o end.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD)
modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
MODULE UNLOADING


Modules can be unloaded using rmmod command.



rmmod ensures the restriction that the modules are not
in use.



Unlinked from the kernel and unlisted from the
list of kernel modules



Dependency is released
KERNEL MODULES VS APPLICATION
PROGRAM








Applications perform a single task from
beginning to end.
Kernel module just registers itself in order to
serve future requests.
Application can use library function (like printf)
but a module can only use functions exported by
kernel (like printk).
A module runs in kernel space, whereas
applications run in user space.
REFERENCES
The Linux Kernel Module Programming Guide
by
Peter Jay Salzman
Michael Burian
Ori Pomerantz
THANK YOU

More Related Content

What's hot

Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driverVandana Salve
 
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)William Liang
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating Systemsubhsikha
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System ServerOpersys inc.
 
Learning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverLearning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverNanik Tolaram
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessNanik Tolaram
 
Run Qt on Linux embedded systems using Yocto
Run Qt on Linux embedded systems using YoctoRun Qt on Linux embedded systems using Yocto
Run Qt on Linux embedded systems using YoctoMarco Cavallini
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module ProgrammingSaurabh Bangad
 
Embedded Linux from Scratch to Yocto
Embedded Linux from Scratch to YoctoEmbedded Linux from Scratch to Yocto
Embedded Linux from Scratch to YoctoSherif Mousa
 
Linux installation and booting process
Linux installation and booting processLinux installation and booting process
Linux installation and booting processSiddharth Jain
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linuxsureskal
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesChris Simmonds
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsQUONTRASOLUTIONS
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBshimosawa
 
Yocto Project : Custom Embedded Linux Distribution
Yocto Project : Custom Embedded Linux DistributionYocto Project : Custom Embedded Linux Distribution
Yocto Project : Custom Embedded Linux Distributionemertxemarketing
 

What's hot (20)

Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
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)
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating System
 
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)
 
Understanding the Android System Server
Understanding the Android System ServerUnderstanding the Android System Server
Understanding the Android System Server
 
Learning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device DriverLearning AOSP - Android Linux Device Driver
Learning AOSP - Android Linux Device Driver
 
Learning AOSP - Android Booting Process
Learning AOSP - Android Booting ProcessLearning AOSP - Android Booting Process
Learning AOSP - Android Booting Process
 
Run Qt on Linux embedded systems using Yocto
Run Qt on Linux embedded systems using YoctoRun Qt on Linux embedded systems using Yocto
Run Qt on Linux embedded systems using Yocto
 
Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module Programming
 
Embedded Linux from Scratch to Yocto
Embedded Linux from Scratch to YoctoEmbedded Linux from Scratch to Yocto
Embedded Linux from Scratch to Yocto
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
Linux installation and booting process
Linux installation and booting processLinux installation and booting process
Linux installation and booting process
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Booting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot imagesBooting Android: bootloaders, fastboot and boot images
Booting Android: bootloaders, fastboot and boot images
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
Yocto Project : Custom Embedded Linux Distribution
Yocto Project : Custom Embedded Linux DistributionYocto Project : Custom Embedded Linux Distribution
Yocto Project : Custom Embedded Linux Distribution
 
Linux file system
Linux file systemLinux file system
Linux file system
 

Viewers also liked

Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel DevelopmentPriyank Kapadia
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modulesEddy Reyes
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Tushar B Kute
 
What is Kernel, basic idea of kernel
What is Kernel, basic idea of kernelWhat is Kernel, basic idea of kernel
What is Kernel, basic idea of kernelNeel Parikh
 
Kernel mode vs user mode in linux
Kernel mode vs user mode in linuxKernel mode vs user mode in linux
Kernel mode vs user mode in linuxSiddique Ibrahim
 
Unit 7
Unit 7Unit 7
Unit 7siddr
 
Workshop su Android Kernel Hacking
Workshop su Android Kernel HackingWorkshop su Android Kernel Hacking
Workshop su Android Kernel HackingDeveler S.r.l.
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentialselshiekh1980
 
Building a linux kernel
Building a linux kernelBuilding a linux kernel
Building a linux kernelRaghu nath
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxTushar B Kute
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...William Liang
 
Linux Process Management Workshop
Linux Process Management WorkshopLinux Process Management Workshop
Linux Process Management WorkshopVIT University
 
Linux fundamentals
Linux fundamentalsLinux fundamentals
Linux fundamentalsRaghu nath
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in LinuxTushar B Kute
 
Linux fundamentals Training
Linux fundamentals TrainingLinux fundamentals Training
Linux fundamentals TrainingLove Steven
 

Viewers also liked (20)

Linux Kernel Overview
Linux Kernel OverviewLinux Kernel Overview
Linux Kernel Overview
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
 
What is Kernel, basic idea of kernel
What is Kernel, basic idea of kernelWhat is Kernel, basic idea of kernel
What is Kernel, basic idea of kernel
 
Kernel mode vs user mode in linux
Kernel mode vs user mode in linuxKernel mode vs user mode in linux
Kernel mode vs user mode in linux
 
Kernel (OS)
Kernel (OS)Kernel (OS)
Kernel (OS)
 
Unit 7
Unit 7Unit 7
Unit 7
 
Workshop su Android Kernel Hacking
Workshop su Android Kernel HackingWorkshop su Android Kernel Hacking
Workshop su Android Kernel Hacking
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentials
 
Building a linux kernel
Building a linux kernelBuilding a linux kernel
Building a linux kernel
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in Linux
 
An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...An Introduction to the Android Framework -- a core architecture view from app...
An Introduction to the Android Framework -- a core architecture view from app...
 
Android Custom Kernel/ROM design
Android Custom Kernel/ROM designAndroid Custom Kernel/ROM design
Android Custom Kernel/ROM design
 
Linux Process Management Workshop
Linux Process Management WorkshopLinux Process Management Workshop
Linux Process Management Workshop
 
Linux fundamentals
Linux fundamentalsLinux fundamentals
Linux fundamentals
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Linux fundamentals Training
Linux fundamentals TrainingLinux fundamentals Training
Linux fundamentals Training
 
Linux
Linux Linux
Linux
 

Similar to Linux Kernel Programming

Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel DevelopmentMohammed Farrag
 
Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungLinux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungdns -
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modulesdibyajyotig
 
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
 
Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]Anupam Datta
 
Linux kernel code
Linux kernel codeLinux kernel code
Linux kernel codeGanesh Naik
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module ProgrammingAmir Payberah
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]mcganesh
 
Load module kernel
Load module kernelLoad module kernel
Load module kernelAbu Azzam
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
Linux device driver
Linux device driverLinux device driver
Linux device driverchatsiri
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,Linux Device Driver,LDD,
Linux Device Driver,LDD,Rahul Batra
 
linux device driver
linux device driverlinux device driver
linux device driverRahul Batra
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modulesHao-Ran Liu
 
Node Session - 2
Node Session - 2Node Session - 2
Node Session - 2Bhavin Shah
 
DUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelDUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelAlexey Smirnov
 

Similar to Linux Kernel Programming (20)

Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
 
Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungLinux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesung
 
Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
 
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 Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]Linux Device Driver v3 [Chapter 2]
Linux Device Driver v3 [Chapter 2]
 
Linux kernel code
Linux kernel codeLinux kernel code
Linux kernel code
 
Linux Module Programming
Linux Module ProgrammingLinux Module Programming
Linux Module Programming
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
Load module kernel
Load module kernelLoad module kernel
Load module kernel
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Walking around linux kernel
Walking around linux kernelWalking around linux kernel
Walking around linux kernel
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,Linux Device Driver,LDD,
Linux Device Driver,LDD,
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Node Session - 2
Node Session - 2Node Session - 2
Node Session - 2
 
DUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelDUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into Kernel
 
Device drivers tsp
Device drivers tspDevice drivers tsp
Device drivers tsp
 

Recently uploaded

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 

Recently uploaded (20)

What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 

Linux Kernel Programming

  • 1. Kernel Module Programming BY: MONIS JAVED NALIN SHARMA OBEDULLAH
  • 2. WHAT IS KERNEL? a program having control over everything that occurs in system.  central part of operating system.  it acts as an interface between the user applications and the hardware. 
  • 3. ROLE OF THE KERNEL Kernel is responsible for:     Process Management - allocating resources to a process. - synchronization among processes. Memory Management - allocating and deallocating memory to programs. Device Management - controlling several devices attached to the system. Storage Management - disk management. - disk scheduling.
  • 4. TYPES OF KERNEL  Monolithic Kernel  Micro Kernel  Hybrid Kernel  Nano Kernel  Exo Kernel
  • 5. MONOLITHIC KERNEL      Entire operating system works in kernel space. Larger in size as they retain full privilege access over various components like file system, IPC, IO/device management etc. Slower to load because of large size. Recompilation is required to add more features or remove bugs. Example includes: Linux based OS, MSDOS etc.
  • 6. MICRO KERNEL  Deals with only critical activities such as controlling memory and CPU.  Everything else is handled under user mode.  Kernel don’t have to worry about lower level functionality.  Example include: QNX ,Minix.
  • 7. HYBRID KERNEL  have the ability to pick and choose what they want to run in user mode and what they want to run in supervisor mode.  Example : Windows NT,2000, XP, Vista,7,8, Mac OS X.
  • 9. KERNEL MODULES  Linux Kernel has ability to extend its set of features at run time.  Modules are pieces of code that can be loaded and unloaded into the kernel upon demand.  They extend the functionality of the kernel without the need to reboot the system.
  • 10. KERNEL MODULES CONTD…  A module run in kernel mode.  Without loadable kernel modules, an operating system would have to include all possible anticipated functionality already compiled directly into the base kernel.  This require rebooting kernel every time when new functionality is added.
  • 11. KERNEL MODULES CONTD…  Kernel modules allow a Linux system to be set up with a standard minimal kernel without any extra device drivers built in.  For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system.
  • 13. MODULE COMMANDS • modinfo : display information about a kernel module • lsmod : list loaded modules • insmod : Install loadable kernel module • rmmod : Unload loadable modules • depmod : handle dependency descriptions for loadable kernel modules • modprobe : High level handling of loadable modules
  • 14. HOW DO MODULES GET INTO THE KERNEL?  modules already loaded into the kernel can be listed by running lsmod, which gets its information by reading the file /proc/modules.  When the kernel needs a feature that is not resident in the kernel, the kernel module daemon kmod executes modprobe to load the module in.  modprobe is passed a string in one of two forms: · A module name like soft or ppp. · A more generic identifier like
  • 15. CONTD…...  If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modules.conf. If it finds an alias line like: alias char−major−10−30 soft it knows that the generic identifier refers to the module soft.o  Next, modprobe looks through the file /lib/modules/version/modules.dep, to see if other modules must be loaded before the requested module may be loaded.  This file is created by depmod −a and contains module dependencies.
  • 16. CONTD….  Lastly, modprobe uses insmod to first load any prerequisite modules into the kernel, and then the requested module. modprobe directs insmod to /lib/modules/version/, the standard directory for modules.  insmod is intended to be fairly dumb about the location of modules, whereas modprobe is aware of the default location of modules.
  • 17. CONTD… Example: We need to insert module msdos.o which requires fat.o module to be already loaded. This can be done in two ways: 1. using insmod insmod /lib/modules/2.5.1/kernel/fs/fat/fat.o insmod /lib/modules/2.5.1/kernel/fs/msdos/msdos. o 2. using modprobe
  • 18. BEFORE WE BEGIN TO GET INTO THE CODE
  • 19. MODVERSIONING  Everyone's system is different and everyone has their own groove.  A module compiled for one kernel won't load if you boot a different kernel.  unless you enable CONFIG_MODVERSIONS in the kernel.
  • 21.
  • 22. HELLO WORLD PROGRAM  /* hello−1.c − The simplest kernel module.*/  #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ int init_module(void) { printk("Hello world n"); return 0; //A non 0 return means init_module failed; module can't be      loaded.      } void cleanup_module(void) { printk(KERN_ALERT "Goodbye world 1.n"); }
  • 23.  Kernel modules must have at least two functions:  a "start" (initialization) function called init_module() which is called when the module is insmoded into the kernel, and  an "end" (cleanup) function called cleanup_module() which is called just before it is rmmoded.
  • 24.  init_module() either registers a handler for something with the kernel, or it replaces one of the kernel functions with its own code (usually code to do something and then call the original function).  The cleanup_module() function is supposed to undo whatever init_module() did, so the module can be unloaded safely.
  • 25. INTRODUCING PRINTK()      printk() is not meant to communicate information to the user. It is used to log information or give warnings. each printk() statement comes with a priority. There are 8 priorities and the kernel has macros for them. The header file linux/kernel.h describes what each priority means.
  • 26. PRINTK() CONTD… 1. KERN_EMERG "<0>" /* emergency message*/ 2. KERN_ALERT "<1>" /* action must be taken immediately*/ 3 KERN_CRIT "<2>" /* critical conditions*/ 4. KERN_ERR "<3>" /* error conditions*/ 5. KERN_WARNING "<4>" /* warning conditions*/ 6. KERN_NOTICE 7. KERN_INFO "<5>" /* normal but significant condition*/ "<6>" /* informational message*/ 8. KERN_DEBUG "<7>" /* debug-level messages*/ Default : DEFAULT_MESSAGE_LOGLEVEL
  • 27. PRINTK() CONTD…  If the priority is less than int console_loglevel, the message is printed on your current terminal.  We use a high priority, like KERN_ALERT, to make sure the printk() messages get printed to your console rather than just logged to your logfile.
  • 28. STEPS FOR COMPILATION OF KERNEL MODULE 1. write the kernel module code with filename like filename.c 2. make a file containing code for compiling the module and save it using name “Makefile”. 3. now compile the module code by issuing command make. To see what kind of information it is , type:modinfo filename.ko
  • 29. CONTD… 4. now insert module by command modprobe filename.ko OR insmod filename.ko - you can see your inserted module in cat /proc/modules OR by command lsmod
  • 30. MAKE FILE  A makefile is a script for appropriate compilation of different type of sources to the appropriate object code.  Makefiles are used to define the procedure to compile and link your program.  as number of files increases it's very difficult to compile and link them one by one.
  • 31. CONTD….  Also you will have to remember the dependencies between these files.  Makefiles are used to automate these tasks so just define your rules once and instead of compiling and linking individual files you just need to execute the makefile.
  • 32. HOW TO MAKE A “MAKEFILE” FOR KERNEL MODULE: obj-m += filename.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  • 33. RENAME YOUR FUNCTION NAME: you can rename the init and cleanup functions of your modules.  This is done with the --module_init() and module_exit()  These are macros defined in linux/init.h
  • 34. EXAMPLE:              #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> static int _init myname(void) { printk(KERN_INFO “Hello world?”); return 0; } static void _exit yourname(void) { printk(KERN_INFO “Goodbye”); } module_init(myname); module_exit(yourname);
  • 35. MODULES SPANNING MULTIPLE FILES:Modules Spanning Multiple Files: First we invent an object name for our combined module,  Second we tell make what object files are part of that module. 
  • 36. begin.c end.c #include <linux/kernel.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/module.h> int init_module(void) void cleanup_module() { { printk(KERN_INFO "Hello, printk(KERN_INFO worldn"); "Goodbyen"); return 0; } } Makefile :obj-m += beginend.o beginend-objs := begin.o end.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  • 37. MODULE UNLOADING  Modules can be unloaded using rmmod command.  rmmod ensures the restriction that the modules are not in use.  Unlinked from the kernel and unlisted from the list of kernel modules  Dependency is released
  • 38. KERNEL MODULES VS APPLICATION PROGRAM     Applications perform a single task from beginning to end. Kernel module just registers itself in order to serve future requests. Application can use library function (like printf) but a module can only use functions exported by kernel (like printk). A module runs in kernel space, whereas applications run in user space.
  • 39. REFERENCES The Linux Kernel Module Programming Guide by Peter Jay Salzman Michael Burian Ori Pomerantz