SlideShare a Scribd company logo
Linux Device Drivers
Module Programming
● Module Initialization
● Module Cleanup
● Compiling and Loading a module
● The Makefile
● Module Parameters
● Kernel Symbol Table
What are modules ?
● Simple pieces of C code.
● Each piece of code that can be added to the kernel at
runtime is called module.
● No main() function like normal C programs.
● Module Initialization and Cleanup
● Mechanisms of registering and De-registering with the
kernel.
Module Initialization
● A module always begin with the function you define
with module_init( ).
● module_init():
● This is the entry point for the module.
● It tells the kernel what functionality module provides.
● Once it's done, module remains in kernel waiting to be
called.
● Calls are executed by user space application.
Module Cleanup
● A module ends by calling module_exit() function.
● module_exit():
● Exit point for module.
● It will undo whatever initialization function performed.
● It unregisters the module from kernel.
● It returns all resources to the system.
Functions in Modules
● Standard C library functions cannot be used in
module programming.
● Functions/Variables shared between modules have
to be exported as kernel symbols into global space.
● To see the symbols exported into the kernel “cat
/proc/kallsyms“
● If symbol is not found, module will not be loaded to
kernel and return back with an error.
Important MACRO'S
● module_init (<name for your init function>)
● module_exit (<name for your exit function>)
● Whatever name is passed to the above macros
should be defined as the name of the init and exit
functions.
● MODULE_LICENSE(“GPL”)
● Without this kernel gives warning message of
“kernel tainted”.
License Terms
● Linux is licensed under version 2 of the GNU
General Public License (GPL) .
● The GPL allows anybody to redistribute, and even
sell a product covered by the GPL.
● The specific licenses recognized by the kernel are
● “GPL” (for any version of the GNU General Purpose
License)
● “GPL v2” (for GPL version two only)
The Hello World Module Initialization
#include <linux/init.h>
#include <linux/module.h>
static int __init hello_init(void)
{
printk(KERN_INFO“n Hello, Worldn”);
return 0;
}
module_init(hello_init);
The Hello World Module Cleanup
#include <linux/init.h>
#include <linux/module.h>
static void __exit hello_exit(void)
{
printk(KERN_INFO“n Goodye, Worldn”);
}
module_exit(hello_exit);
The Makefile
● Makefile is a file that describes the dependencies
among files in your program and provides
commands for updating each file.
● Once a suitable Makefile exists, each time you
change some source files, this simple shell
command:
– make
suffices to perform all necessary recompilations.
Makefile
obj-m := hello.o
KERN_SRC := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
make –C $(KERN_SRC) M=$(PWD) modules
modules_install:
make –C $(KERN_SRC) M=$(PWD) modules_install
Clean:
make –C $(KERN_SRC) M=$(PWD) clean
Compiling the Module
linux:~ $ make
linux:~ $ sudo insmod hello.ko
linux:~ $ dmesg
linux:~ $ lsmod
linux:~ $ sudo rmmod hello
linux:~ $ dmesg
Module Parameters
● Parameters may be passed to the modules during
module insertion
● The macro “module_param(name, type,perm)” is
used to initialize the parameter at runtime. It takes 3
arguments which are the parameter name, parameter
type and the permissions associated with the
parameter
● This macro may be defined anywhere in the module
The Kernel Symbol Table
● Global Kernel Space
● Modules can export services to other modules
through the use of kernel macros
“EXPORT_SYMBOL(name)” which takes the name
of the parameter or function as argument.
● This exported function may be used by other
modules in requirement of similar functionality as
done by the exported function.
Modprobe
● Modprobe is a cleverer version of insmod.
● It inserts dependent modules after examining
the contents of /lib/modules/X.Y.Z/modules.dep
● modules.dep is created by a utility callled
depmod.
● Depmod creates a Makefile-like depency file
based on symbols contained in the modules.
Insmod vs Modprobe
● modprobe works in similar way as that of insmod,
but it also loads any other modules that are required
by the module you want to load.
● If your module depends upon three more modules,
you have to do insmod three times plus once for
loading that module itself
● But with modprobe, one single time is enough. It
solves the dependency and loads the modules which
are required.
Precautions for Kernel Programming
● Do not use Infinite/never ending loops.
● We should avoid floating point numbers.
● Stack size is limited to 4KB.
● We don't have access to the libraries (user space).

More Related Content

What's hot (10)

PPTX
Linux Boot Process
darshhingu
 
PDF
Python Subprocess
samof76
 
PPTX
Linux booting Process
Gaurav Sharma
 
PPTX
Kubernetes
Lhouceine OUHAMZA
 
PPTX
An Insight into the Linux Booting Process
Hardeep Bhurji
 
PPTX
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Sneeker Yeh
 
PPTX
Bootloader and bootloading
Arpita Gupta
 
PPT
Linux installation and booting process
Siddharth Jain
 
PDF
Austin c-c++-meetup-feb2018-spectre
Kim Phillips
 
PPTX
Code4vn - Linux day - linux boot process
Cường Nguyễn
 
Linux Boot Process
darshhingu
 
Python Subprocess
samof76
 
Linux booting Process
Gaurav Sharma
 
Kubernetes
Lhouceine OUHAMZA
 
An Insight into the Linux Booting Process
Hardeep Bhurji
 
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Sneeker Yeh
 
Bootloader and bootloading
Arpita Gupta
 
Linux installation and booting process
Siddharth Jain
 
Austin c-c++-meetup-feb2018-spectre
Kim Phillips
 
Code4vn - Linux day - linux boot process
Cường Nguyễn
 

Viewers also liked (20)

PPT
Linuxdd[1]
mcganesh
 
PPTX
Device drivers Introduction
vijay selva
 
PDF
Gnubs-pres-foss-cdac-sem
Sagun Baijal
 
PPTX
Device Drivers in Linux
Shreyas MM
 
PDF
Breaking into Open Source and Linux: A USB 3.0 Success Story
Sage Sharp
 
PDF
brief intro to Linux device drivers
Alexandre Moreno
 
PDF
Raspberry Pi - Lecture 6 Working on Raspberry Pi
Mohamed Abdallah
 
PPTX
Linux Device Driver’s
Rashmi Warghade
 
PDF
Linux Kernel Introduction
Sage Sharp
 
PPTX
Containers in the Cloud
James Darrell Pratt
 
PPTX
Performance comparison between Linux Containers and Virtual Machines
Soheila Dehghanzadeh
 
ODP
Introduction to Raspberry Pi and GPIO
Kris Findlay
 
PPT
Device tree support on arm linux
Chih-Min Chao
 
PPTX
Containers and Cloud: From LXC to Docker to Kubernetes
Shreyas MM
 
PPSX
Introduction to embedded linux device driver and firmware
definecareer
 
PDF
Userspace drivers-2016
Chris Simmonds
 
PPT
LINUX Device Drivers
Partha Bhattacharya
 
PDF
Advanced Git
segv
 
PPT
linux device driver
Rahul Batra
 
PDF
Introduction to char device driver
Vandana Salve
 
Linuxdd[1]
mcganesh
 
Device drivers Introduction
vijay selva
 
Gnubs-pres-foss-cdac-sem
Sagun Baijal
 
Device Drivers in Linux
Shreyas MM
 
Breaking into Open Source and Linux: A USB 3.0 Success Story
Sage Sharp
 
brief intro to Linux device drivers
Alexandre Moreno
 
Raspberry Pi - Lecture 6 Working on Raspberry Pi
Mohamed Abdallah
 
Linux Device Driver’s
Rashmi Warghade
 
Linux Kernel Introduction
Sage Sharp
 
Containers in the Cloud
James Darrell Pratt
 
Performance comparison between Linux Containers and Virtual Machines
Soheila Dehghanzadeh
 
Introduction to Raspberry Pi and GPIO
Kris Findlay
 
Device tree support on arm linux
Chih-Min Chao
 
Containers and Cloud: From LXC to Docker to Kubernetes
Shreyas MM
 
Introduction to embedded linux device driver and firmware
definecareer
 
Userspace drivers-2016
Chris Simmonds
 
LINUX Device Drivers
Partha Bhattacharya
 
Advanced Git
segv
 
linux device driver
Rahul Batra
 
Introduction to char device driver
Vandana Salve
 
Ad

Similar to Linux kernel code (20)

PPT
Kernel module programming
Vandana Salve
 
PDF
Kernel Module Programming
Saurabh Bangad
 
PDF
Linux Module Programming
Amir Payberah
 
PDF
Linux Device Driver v3 [Chapter 2]
Anupam Datta
 
PPTX
Linux Kernel Programming
Nalin Sharma
 
PPT
Linux Kernel Development
Priyank Kapadia
 
PDF
Linux kernel modules
Dheryta Jaisinghani
 
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
PPTX
Introduction Linux Device Drivers
NEEVEE Technologies
 
PDF
Introduction To Linux Kernel Modules
dibyajyotig
 
PDF
Course 102: Lecture 25: Devices and Device Drivers
Ahmed El-Arabawy
 
PPTX
Device Drivers and Running Modules
YourHelper1
 
PPT
Linux kernel modules
Hao-Ran Liu
 
PDF
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
PPTX
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
abhinandpk2405
 
PDF
Linux device driver
chatsiri
 
PPT
lesson03.ppt
IraqReshi
 
PDF
Linux kernel modules
Eddy Reyes
 
PDF
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Tushar B Kute
 
PDF
Studienarb linux kernel-dev
murali_purushothaman
 
Kernel module programming
Vandana Salve
 
Kernel Module Programming
Saurabh Bangad
 
Linux Module Programming
Amir Payberah
 
Linux Device Driver v3 [Chapter 2]
Anupam Datta
 
Linux Kernel Programming
Nalin Sharma
 
Linux Kernel Development
Priyank Kapadia
 
Linux kernel modules
Dheryta Jaisinghani
 
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
Introduction Linux Device Drivers
NEEVEE Technologies
 
Introduction To Linux Kernel Modules
dibyajyotig
 
Course 102: Lecture 25: Devices and Device Drivers
Ahmed El-Arabawy
 
Device Drivers and Running Modules
YourHelper1
 
Linux kernel modules
Hao-Ran Liu
 
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
abhinandpk2405
 
Linux device driver
chatsiri
 
lesson03.ppt
IraqReshi
 
Linux kernel modules
Eddy Reyes
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Tushar B Kute
 
Studienarb linux kernel-dev
murali_purushothaman
 
Ad

Recently uploaded (20)

DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
Thermal runway and thermal stability.pptx
godow93766
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
EC3551-Transmission lines Demo class .pptx
Mahalakshmiprasannag
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 

Linux kernel code

  • 2. Module Programming ● Module Initialization ● Module Cleanup ● Compiling and Loading a module ● The Makefile ● Module Parameters ● Kernel Symbol Table
  • 3. What are modules ? ● Simple pieces of C code. ● Each piece of code that can be added to the kernel at runtime is called module. ● No main() function like normal C programs. ● Module Initialization and Cleanup ● Mechanisms of registering and De-registering with the kernel.
  • 4. Module Initialization ● A module always begin with the function you define with module_init( ). ● module_init(): ● This is the entry point for the module. ● It tells the kernel what functionality module provides. ● Once it's done, module remains in kernel waiting to be called. ● Calls are executed by user space application.
  • 5. Module Cleanup ● A module ends by calling module_exit() function. ● module_exit(): ● Exit point for module. ● It will undo whatever initialization function performed. ● It unregisters the module from kernel. ● It returns all resources to the system.
  • 6. Functions in Modules ● Standard C library functions cannot be used in module programming. ● Functions/Variables shared between modules have to be exported as kernel symbols into global space. ● To see the symbols exported into the kernel “cat /proc/kallsyms“ ● If symbol is not found, module will not be loaded to kernel and return back with an error.
  • 7. Important MACRO'S ● module_init (<name for your init function>) ● module_exit (<name for your exit function>) ● Whatever name is passed to the above macros should be defined as the name of the init and exit functions. ● MODULE_LICENSE(“GPL”) ● Without this kernel gives warning message of “kernel tainted”.
  • 8. License Terms ● Linux is licensed under version 2 of the GNU General Public License (GPL) . ● The GPL allows anybody to redistribute, and even sell a product covered by the GPL. ● The specific licenses recognized by the kernel are ● “GPL” (for any version of the GNU General Purpose License) ● “GPL v2” (for GPL version two only)
  • 9. The Hello World Module Initialization #include <linux/init.h> #include <linux/module.h> static int __init hello_init(void) { printk(KERN_INFO“n Hello, Worldn”); return 0; } module_init(hello_init);
  • 10. The Hello World Module Cleanup #include <linux/init.h> #include <linux/module.h> static void __exit hello_exit(void) { printk(KERN_INFO“n Goodye, Worldn”); } module_exit(hello_exit);
  • 11. The Makefile ● Makefile is a file that describes the dependencies among files in your program and provides commands for updating each file. ● Once a suitable Makefile exists, each time you change some source files, this simple shell command: – make suffices to perform all necessary recompilations.
  • 12. Makefile obj-m := hello.o KERN_SRC := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) modules: make –C $(KERN_SRC) M=$(PWD) modules modules_install: make –C $(KERN_SRC) M=$(PWD) modules_install Clean: make –C $(KERN_SRC) M=$(PWD) clean
  • 13. Compiling the Module linux:~ $ make linux:~ $ sudo insmod hello.ko linux:~ $ dmesg linux:~ $ lsmod linux:~ $ sudo rmmod hello linux:~ $ dmesg
  • 14. Module Parameters ● Parameters may be passed to the modules during module insertion ● The macro “module_param(name, type,perm)” is used to initialize the parameter at runtime. It takes 3 arguments which are the parameter name, parameter type and the permissions associated with the parameter ● This macro may be defined anywhere in the module
  • 15. The Kernel Symbol Table ● Global Kernel Space ● Modules can export services to other modules through the use of kernel macros “EXPORT_SYMBOL(name)” which takes the name of the parameter or function as argument. ● This exported function may be used by other modules in requirement of similar functionality as done by the exported function.
  • 16. Modprobe ● Modprobe is a cleverer version of insmod. ● It inserts dependent modules after examining the contents of /lib/modules/X.Y.Z/modules.dep ● modules.dep is created by a utility callled depmod. ● Depmod creates a Makefile-like depency file based on symbols contained in the modules.
  • 17. Insmod vs Modprobe ● modprobe works in similar way as that of insmod, but it also loads any other modules that are required by the module you want to load. ● If your module depends upon three more modules, you have to do insmod three times plus once for loading that module itself ● But with modprobe, one single time is enough. It solves the dependency and loads the modules which are required.
  • 18. Precautions for Kernel Programming ● Do not use Infinite/never ending loops. ● We should avoid floating point numbers. ● Stack size is limited to 4KB. ● We don't have access to the libraries (user space).