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

PDF
Linux kernel driver tutorial vorlesung
PPTX
Linux Kernel Module - For NLKB
PDF
Linux Kernel Startup Code In Embedded Linux
PDF
Linux boot process
PDF
Linux booting procedure
PPT
Problem oriented software engineering01
PPT
Linux Booting Steps
PPTX
Linux booting process
Linux kernel driver tutorial vorlesung
Linux Kernel Module - For NLKB
Linux Kernel Startup Code In Embedded Linux
Linux boot process
Linux booting procedure
Problem oriented software engineering01
Linux Booting Steps
Linux booting process

What's hot (10)

PPTX
Linux Boot Process
PDF
Python Subprocess
PPTX
Linux booting Process
PPTX
Kubernetes
PPTX
An Insight into the Linux Booting Process
PPTX
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
PPTX
Bootloader and bootloading
PPT
Linux installation and booting process
PDF
Austin c-c++-meetup-feb2018-spectre
PPTX
Code4vn - Linux day - linux boot process
Linux Boot Process
Python Subprocess
Linux booting Process
Kubernetes
An Insight into the Linux Booting Process
Dead Lock Analysis of spin_lock() in Linux Kernel (english)
Bootloader and bootloading
Linux installation and booting process
Austin c-c++-meetup-feb2018-spectre
Code4vn - Linux day - linux boot process
Ad

Viewers also liked (20)

PPT
Linuxdd[1]
PPTX
Device drivers Introduction
PDF
Gnubs-pres-foss-cdac-sem
PPTX
Device Drivers in Linux
PDF
Breaking into Open Source and Linux: A USB 3.0 Success Story
PDF
brief intro to Linux device drivers
PDF
Raspberry Pi - Lecture 6 Working on Raspberry Pi
PPTX
Linux Device Driver’s
PDF
Linux Kernel Introduction
PPTX
Containers in the Cloud
PPTX
Performance comparison between Linux Containers and Virtual Machines
ODP
Introduction to Raspberry Pi and GPIO
PPT
Device tree support on arm linux
PPTX
Containers and Cloud: From LXC to Docker to Kubernetes
PPSX
Introduction to embedded linux device driver and firmware
PDF
Userspace drivers-2016
PPT
LINUX Device Drivers
PDF
Advanced Git
PPT
linux device driver
PDF
Introduction to char device driver
Linuxdd[1]
Device drivers Introduction
Gnubs-pres-foss-cdac-sem
Device Drivers in Linux
Breaking into Open Source and Linux: A USB 3.0 Success Story
brief intro to Linux device drivers
Raspberry Pi - Lecture 6 Working on Raspberry Pi
Linux Device Driver’s
Linux Kernel Introduction
Containers in the Cloud
Performance comparison between Linux Containers and Virtual Machines
Introduction to Raspberry Pi and GPIO
Device tree support on arm linux
Containers and Cloud: From LXC to Docker to Kubernetes
Introduction to embedded linux device driver and firmware
Userspace drivers-2016
LINUX Device Drivers
Advanced Git
linux device driver
Introduction to char device driver
Ad

Similar to Linux kernel code (20)

PPT
Kernel module programming
PPTX
Linux Kernel Programming
PDF
Kernel Module Programming
PDF
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
PPT
Linux Kernel Development
PPT
Sysfs filesystem to provide a hierarchi..
PDF
Studienarb linux kernel-dev
PDF
Linux Device Driver v3 [Chapter 2]
PPT
DUSK - Develop at Userland Install into Kernel
PPT
lesson03.ppt
PDF
Advanced Node.JS Meetup
PDF
Introduction To Linux Kernel Modules
PPTX
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
PDF
Autotools
PDF
PDF
Exploiting Llinux Environment
PPTX
JavaScript Module Loaders
PDF
Part 02 Linux Kernel Module Programming
PDF
Linux kernel modules
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
Kernel module programming
Linux Kernel Programming
Kernel Module Programming
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Linux Kernel Development
Sysfs filesystem to provide a hierarchi..
Studienarb linux kernel-dev
Linux Device Driver v3 [Chapter 2]
DUSK - Develop at Userland Install into Kernel
lesson03.ppt
Advanced Node.JS Meetup
Introduction To Linux Kernel Modules
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
Autotools
Exploiting Llinux Environment
JavaScript Module Loaders
Part 02 Linux Kernel Module Programming
Linux kernel modules
Linux Device Driver for Writing a real world driver for embedded Linux

Recently uploaded (20)

PDF
Well-logging-methods_new................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
OOP with Java - Java Introduction (Basics)
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Digital Logic Computer Design lecture notes
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
PPT on Performance Review to get promotions
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Well-logging-methods_new................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
OOP with Java - Java Introduction (Basics)
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Internet of Things (IOT) - A guide to understanding
Digital Logic Computer Design lecture notes
Structs to JSON How Go Powers REST APIs.pdf
PPT on Performance Review to get promotions
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lesson 3_Tessellation.pptx finite Mathematics
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
additive manufacturing of ss316l using mig welding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx

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).