SlideShare a Scribd company logo
Kernel Module
 Programming
Kernel Modules
What exactly is a kernel module ?
• 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.
• For example, one type of module is the device driver, which
  allows the kernel to access hardware connected to the
  system.
• Without modules, we would have to build monolithic
  kernels
• Larger kernels,disadvantage of requiring us to rebuild and
  reboot the kernel every time for new functionality.
OS Basics
•   Linux operates in two modes
•   Kernel mode (kernel space) and
•   User mode (user space)
•   So kernel architectures depending upon this
•   Monolithic
•   Micro-kernel
Monolithic kernels
•   Kernel implemented as an only one process
•   Large program where all the functional
    components of the kernel have access to all
    of its internal data structures and routines
                Micro-kernels
•   kernel perform only the essential
    operations
•   Everything else should be performed in
    user space
•   Memory management, file systems and
    IPC communications are not inside the
    kernel
And the linux kernel is...?
•   Monolithic, but it is also modular
•   Kernel can dynamically load parts of the kernel
    code(Modules)
•   Possible to extend the kernel capabilities without
    modifying the rest of the code
•   Possible to insert the module while the kernel is
    running
•   Keeps the kernel size to a minimum and makes
    the kernel very flexible
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 seen
  by running lsmod

  kernel daemon(kerneld daemon or kmod) execs
  modprobe to load the module in
• modprobe is passed a string in one of two forms:
  -A module name like softdog or ppp.
  -A more generic identifier like char-major-10-30.
How Do Modules Get Into The Kernel?
• If modprobe is handed a generic identifier, it first looks for
  that string in the file
   /etc/modprobe.conf--> /lib/modules/version/modules.alias
• If it finds an alias line like:
  alias char-major-10-30 softdog
• Then dependencies are seen
• For example, msdos.ko requires the fat.ko module to be already
  loaded into the kernel.
• modprobe uses insmod to first load any prerequisite modules into
  the kernel, and then the requested module
How Do Modules Get Into The Kernel?
• Linux distributions provide modprobe, insmod and depmod
  as a package called modutils or mod-utils.

• Until 2.4 kernel versions, module file & object file had
  same extension i.e. .o

• From 2.4 & above to differentiate between object file &
  module file, module file has extension .ko
Module Unloading
•   Modules can be unloaded using rmmod
    command
•   rmmod ensures the restriction that the modules
    are not in use
•   Automatically removed from the system by
    `kerneld' when they are no longer used
•   cleanup_module function of the concerned
    module is called to freeup the kernel resources it
    has allocated
•   Unlinked from the kernel and unlisted from the
    list of kernel modules
•   Dependency is released
Module Functions
• 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
• Typically, init_module( ) either registers a handler for
  something with the kernel.
• An "end" (cleanup) function called cleanup_module ( )
  which is called just before it is rmmoded.
• The cleanup_module( ) function is supposed to undo
  whatever init_module( ) did, so the module can be unloaded
  safely.
Module Functions
•   As of Linux 2.4, you can rename the init
    and cleanup functions

•   This is done with the module_init() and
    module_exit() macros

•   These macros are defined in linux/init.h.
Using printk( )
• printk seems to be the similar function like printf
• Since we are dealing with kernel programming , kernel can’t
  access user library functions
• So only functions used by modules are the one defined by
  kernel.
• printk( ) happens to be a logging mechanism for the kernel,
  and is used to log information or give warnings.
• There are 8 priorities and the kernel has macros for them,
  and you can view them (and their meanings) in
  linux/kernel.h.
Using printk( )
• If you don't specify a priority level, the default priority,
  DEFAULT_MESSAGE_LOGLEVEL, will be used

• We usually use a high priority, like KERN_ALERT

• When you write real modules, you'll want to use priorities
  that are meaningful for the situation at hand.
Differences between
Modules
And
Programmes
How programmes begin and end

    Program begins with a main() function, executes a bunch
    of instructions and terminates upon completion of those
    instructions.
•   Module begins with either the init_module or the function
    you specify with module_init call.
•   This is the entry function for modules; it tells the kernel
    what functionality the module provides and sets up the
    kernel to run the module's functions when they're needed.
•   Once it does this, entry function returns and the module
    does nothing until the kernel wants to do something with
    the code that the module provides.
How modules begin and end

• All modules end by calling either cleanup_module
  or the function you specify with the module_exit
  call.
• This is the exit function for modules; it undoes
  whatever entry function did.
• It unregisters the functionality that the entry
  function registered.
• Every module must have an entry function and an
  exit function.
Functions available to Programmes

• Programmers use functions they don't define all the
  time.

• A prime example of this is printf().

• You use these library functions which are provided
  by the standard C library.
Functions available to Modules
• Kernel modules are different here, too.The only
  functions you can use are the ones provided by the
  kernel.
• That's because modules are object files whose
  symbols get resolved upon insmod'ing.
• The definition for the symbols comes from the
  kernel itself.
• Symbols have been exported by your kernel,the
  information is available from
     –    /proc/ksyms (old versions)-->
     –   /proc/kallsyms
User Space vs Kernel Space
• Library functions are higher level, run completely in
  user space and provide a more convenient interface for
  the programmer
• System calls run in kernel mode on the user's behalf and
  are provided by the kernel itself.
• Library function calls one or more system calls, and
  these system calls execute in supervisor mode since they
  are part of the kernel itself.
• Once the system call completes its task, it returns and
  execution gets transferred back to user mode.
Namespace
• When writing kernel code, even the smallest module will be
  linked against the entire kernel, so this is definitely an issue.
• The best way to deal with this is to declare all your variables
  as static and to use a well-defined prefix for your symbols.
• By convention, all kernel prefixes are lowercase.
• If you don't want to declare everything as static, another
  option is to declare a symbol table and register it with a
  kernel.
• The file /proc/ksyms-->/proc/kallsyms holds all the symbols
  that the kernel knows about and which are therefore
  accessible to your modules since they share the kernel's
  codespace.
Codespace
• The kernel has its own space of memory as well.
• Since a module is code which can be dynamically
  inserted and removed in the kernel, it shares the
  kernel's codespace rather than having its own.
• Therefore, if your module segfaults, the kernel
  segfaults.
• In general it is true for any operating system which
  uses a monolithic kernel.
Application to Module Programming

 An introduction to device drivers
• One class of modules is the device driver, which provides
  functionality for hardware like a TV card or a serial port.
• On unix, each piece of hardware is represented by a file
  located in /dev named a device file which provides the
  means to communicate with the hardware.
• The device driver provides the communication on behalf of
  a user program.
References
The Linux Kernel Module Programming Guide by
  Peter Jay Salzman
http://en.wikipedia.org/wiki/Linux_Kernel_Module
http://ftp.kernel.org/pub/linux/utils/kernel/module-
  init-tools/
http://en.wikipedia.org/wiki/Modprobe
http://ftp.kernel.org/pub/linux/utils/kernel/modutils/
http://www.linuxhq.com/guides/LKMPG/mpg.html
Linux Loadable Kernel Module HOWTO by Bryan
  Henderson
Any Questions...?
Thank You ...

Saurabh S. Bangad
Ad

Recommended

Embedded Android : System Development - Part III
Embedded Android : System Development - Part III
Emertxe Information Technologies Pvt Ltd
 
Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)
Emertxe Information Technologies Pvt Ltd
 
Linux device drivers
Linux device drivers
Emertxe Information Technologies Pvt Ltd
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
linux device driver
linux device driver
Rahul Batra
 
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
Nanik Tolaram
 
Embedded Android : System Development - Part I
Embedded Android : System Development - Part I
Emertxe Information Technologies Pvt Ltd
 
Linux kernel modules
Linux kernel modules
Dheryta Jaisinghani
 
U Boot or Universal Bootloader
U Boot or Universal Bootloader
Satpal Parmar
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
shimosawa
 
Linux Audio Drivers. ALSA
Linux Audio Drivers. ALSA
GlobalLogic Ukraine
 
Browsing Linux Kernel Source
Browsing Linux Kernel Source
Motaz Saad
 
Q4.11: Porting Android to new Platforms
Q4.11: Porting Android to new Platforms
Linaro
 
Linux Internals - Part II
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
Kernel module programming
Kernel module programming
Vandana Salve
 
Static Partitioning with Xen, LinuxRT, and Zephyr: A Concrete End-to-end Exam...
Static Partitioning with Xen, LinuxRT, and Zephyr: A Concrete End-to-end Exam...
Stefano Stabellini
 
Embedded Android : System Development - Part III (Audio / Video HAL)
Embedded Android : System Development - Part III (Audio / Video HAL)
Emertxe Information Technologies Pvt Ltd
 
Embedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr
 
Android IPC Mechanism
Android IPC Mechanism
National Cheng Kung University
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
dibyajyotig
 
Linux for embedded_systems
Linux for embedded_systems
Vandana Salve
 
Explore Android Internals
Explore Android Internals
National Cheng Kung University
 
U-Boot presentation 2013
U-Boot presentation 2013
Wave Digitech
 
Kernel module in linux os.
Kernel module in linux os.
MUKESH BADIGINENI
 
Introduction to yocto
Introduction to yocto
Alex Gonzalez
 
U boot porting guide for SoC
U boot porting guide for SoC
Macpaul Lin
 
Embedded Android : System Development - Part IV
Embedded Android : System Development - Part IV
Emertxe Information Technologies Pvt Ltd
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
shimosawa
 

More Related Content

What's hot (20)

U Boot or Universal Bootloader
U Boot or Universal Bootloader
Satpal Parmar
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
shimosawa
 
Linux Audio Drivers. ALSA
Linux Audio Drivers. ALSA
GlobalLogic Ukraine
 
Browsing Linux Kernel Source
Browsing Linux Kernel Source
Motaz Saad
 
Q4.11: Porting Android to new Platforms
Q4.11: Porting Android to new Platforms
Linaro
 
Linux Internals - Part II
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
Kernel module programming
Kernel module programming
Vandana Salve
 
Static Partitioning with Xen, LinuxRT, and Zephyr: A Concrete End-to-end Exam...
Static Partitioning with Xen, LinuxRT, and Zephyr: A Concrete End-to-end Exam...
Stefano Stabellini
 
Embedded Android : System Development - Part III (Audio / Video HAL)
Embedded Android : System Development - Part III (Audio / Video HAL)
Emertxe Information Technologies Pvt Ltd
 
Embedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr
 
Android IPC Mechanism
Android IPC Mechanism
National Cheng Kung University
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
dibyajyotig
 
Linux for embedded_systems
Linux for embedded_systems
Vandana Salve
 
Explore Android Internals
Explore Android Internals
National Cheng Kung University
 
U-Boot presentation 2013
U-Boot presentation 2013
Wave Digitech
 
Kernel module in linux os.
Kernel module in linux os.
MUKESH BADIGINENI
 
Introduction to yocto
Introduction to yocto
Alex Gonzalez
 
U boot porting guide for SoC
U boot porting guide for SoC
Macpaul Lin
 
Embedded Android : System Development - Part IV
Embedded Android : System Development - Part IV
Emertxe Information Technologies Pvt Ltd
 
U Boot or Universal Bootloader
U Boot or Universal Bootloader
Satpal Parmar
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
shimosawa
 
Browsing Linux Kernel Source
Browsing Linux Kernel Source
Motaz Saad
 
Q4.11: Porting Android to new Platforms
Q4.11: Porting Android to new Platforms
Linaro
 
Kernel module programming
Kernel module programming
Vandana Salve
 
Static Partitioning with Xen, LinuxRT, and Zephyr: A Concrete End-to-end Exam...
Static Partitioning with Xen, LinuxRT, and Zephyr: A Concrete End-to-end Exam...
Stefano Stabellini
 
Embedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
dibyajyotig
 
Linux for embedded_systems
Linux for embedded_systems
Vandana Salve
 
U-Boot presentation 2013
U-Boot presentation 2013
Wave Digitech
 
Introduction to yocto
Introduction to yocto
Alex Gonzalez
 
U boot porting guide for SoC
U boot porting guide for SoC
Macpaul Lin
 

Viewers also liked (20)

Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
shimosawa
 
Linux Kernel Programming
Linux Kernel Programming
Nalin Sharma
 
Linux Kernel Development
Linux Kernel Development
Priyank Kapadia
 
Linux Module Programming
Linux Module Programming
Amir Payberah
 
Linux kernel module programming regular and summer training in waayoo.com
Linux kernel module programming regular and summer training in waayoo.com
Praveen Pandey
 
Linux_kernelmodule
Linux_kernelmodule
sudhir1223
 
Linux kernel module programming guide
Linux kernel module programming guide
Dũng Nguyễn
 
How does SCRUM change Software Management Process?
How does SCRUM change Software Management Process?
Saurabh Bangad
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
Tushar B Kute
 
Module Programming with Project Jigsaw
Module Programming with Project Jigsaw
Yuichi Sakuraba
 
ITT Project Information Technology Basic
ITT Project Information Technology Basic
Mayank Garg
 
Building a linux kernel
Building a linux kernel
Raghu nath
 
Red hat linux essentials
Red hat linux essentials
elshiekh1980
 
Remote procedure call on client server computing
Remote procedure call on client server computing
Satya P. Joshi
 
Linux Process Management Workshop
Linux Process Management Workshop
VIT University
 
Csc1401 lecture05 - cache memory
Csc1401 lecture05 - cache memory
IIUM
 
Linux fundamentals
Linux fundamentals
Raghu nath
 
Linux fundamentals Training
Linux fundamentals Training
Love Steven
 
Red hat linux essentials
Red hat linux essentials
Haitham Raik
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Linux Kernel Module - For NLKB
Linux Kernel Module - For NLKB
shimosawa
 
Linux Kernel Programming
Linux Kernel Programming
Nalin Sharma
 
Linux Kernel Development
Linux Kernel Development
Priyank Kapadia
 
Linux Module Programming
Linux Module Programming
Amir Payberah
 
Linux kernel module programming regular and summer training in waayoo.com
Linux kernel module programming regular and summer training in waayoo.com
Praveen Pandey
 
Linux_kernelmodule
Linux_kernelmodule
sudhir1223
 
Linux kernel module programming guide
Linux kernel module programming guide
Dũng Nguyễn
 
How does SCRUM change Software Management Process?
How does SCRUM change Software Management Process?
Saurabh Bangad
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
Tushar B Kute
 
Module Programming with Project Jigsaw
Module Programming with Project Jigsaw
Yuichi Sakuraba
 
ITT Project Information Technology Basic
ITT Project Information Technology Basic
Mayank Garg
 
Building a linux kernel
Building a linux kernel
Raghu nath
 
Red hat linux essentials
Red hat linux essentials
elshiekh1980
 
Remote procedure call on client server computing
Remote procedure call on client server computing
Satya P. Joshi
 
Linux Process Management Workshop
Linux Process Management Workshop
VIT University
 
Csc1401 lecture05 - cache memory
Csc1401 lecture05 - cache memory
IIUM
 
Linux fundamentals
Linux fundamentals
Raghu nath
 
Linux fundamentals Training
Linux fundamentals Training
Love Steven
 
Red hat linux essentials
Red hat linux essentials
Haitham Raik
 
Ad

Similar to Kernel Module Programming (20)

Device Drivers and Running Modules
Device Drivers and Running Modules
YourHelper1
 
Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers
Ahmed El-Arabawy
 
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 Kute
Tushar B Kute
 
_Kernel and Kernel Architectures.ppt
_Kernel and Kernel Architectures.ppt
HardeepKaurCSEAssist
 
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
abhinandpk2405
 
Linux device drivers
Linux device drivers
Abhishek Sagar
 
managing kernal module from egineering sunject operating system
managing kernal module from egineering sunject operating system
mohammadshahnawaz77
 
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
KesavanT10
 
Device Drivers
Device Drivers
Kushal Modi
 
Embedded system - embedded system programming
Embedded system - embedded system programming
Vibrant Technologies & Computers
 
Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesung
dns -
 
Linux kernel code
Linux kernel code
Ganesh Naik
 
2. microkernel new
2. microkernel new
AbDul ThaYyal
 
Studienarb linux kernel-dev
Studienarb linux kernel-dev
murali_purushothaman
 
lesson03.ppt
lesson03.ppt
IraqReshi
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,
Rahul Batra
 
Linux kernel modules
Linux kernel modules
Hao-Ran Liu
 
Embedded Operating System-Kernel Features.pptx
Embedded Operating System-Kernel Features.pptx
ssuseradc877
 
Linux Device Driver for Writing a real world driver for embedded Linux
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
Regarding About Operating System Structure
Regarding About Operating System Structure
sankarkvdc
 
Device Drivers and Running Modules
Device Drivers and Running Modules
YourHelper1
 
Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers
Ahmed El-Arabawy
 
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 Kute
Tushar B Kute
 
_Kernel and Kernel Architectures.ppt
_Kernel and Kernel Architectures.ppt
HardeepKaurCSEAssist
 
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
LINUX M1 P4 notes.pptxgyfdes e4e4e54v 4
abhinandpk2405
 
managing kernal module from egineering sunject operating system
managing kernal module from egineering sunject operating system
mohammadshahnawaz77
 
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
UNIT -5 EMBEDDED DRIVERS AND APPLICATION PORTING.pptx
KesavanT10
 
Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesung
dns -
 
Linux kernel code
Linux kernel code
Ganesh Naik
 
lesson03.ppt
lesson03.ppt
IraqReshi
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,
Rahul Batra
 
Linux kernel modules
Linux kernel modules
Hao-Ran Liu
 
Embedded Operating System-Kernel Features.pptx
Embedded Operating System-Kernel Features.pptx
ssuseradc877
 
Linux Device Driver for Writing a real world driver for embedded Linux
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
Regarding About Operating System Structure
Regarding About Operating System Structure
sankarkvdc
 
Ad

Recently uploaded (20)

How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Introduction to problem solving Techniques
Introduction to problem solving Techniques
merlinjohnsy
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Paper 109 | Archetypal Journeys in ‘Interstellar’: Exploring Universal Themes...
Rajdeep Bavaliya
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Introduction to problem solving Techniques
Introduction to problem solving Techniques
merlinjohnsy
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 

Kernel Module Programming

  • 2. Kernel Modules What exactly is a kernel module ? • 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. • For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system. • Without modules, we would have to build monolithic kernels • Larger kernels,disadvantage of requiring us to rebuild and reboot the kernel every time for new functionality.
  • 3. OS Basics • Linux operates in two modes • Kernel mode (kernel space) and • User mode (user space) • So kernel architectures depending upon this • Monolithic • Micro-kernel
  • 4. Monolithic kernels • Kernel implemented as an only one process • Large program where all the functional components of the kernel have access to all of its internal data structures and routines Micro-kernels • kernel perform only the essential operations • Everything else should be performed in user space • Memory management, file systems and IPC communications are not inside the kernel
  • 5. And the linux kernel is...? • Monolithic, but it is also modular • Kernel can dynamically load parts of the kernel code(Modules) • Possible to extend the kernel capabilities without modifying the rest of the code • Possible to insert the module while the kernel is running • Keeps the kernel size to a minimum and makes the kernel very flexible
  • 6. 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
  • 7. How Do Modules Get Into The Kernel?  Modules already loaded into the kernel can be seen by running lsmod  kernel daemon(kerneld daemon or kmod) execs modprobe to load the module in • modprobe is passed a string in one of two forms: -A module name like softdog or ppp. -A more generic identifier like char-major-10-30.
  • 8. How Do Modules Get Into The Kernel? • If modprobe is handed a generic identifier, it first looks for that string in the file /etc/modprobe.conf--> /lib/modules/version/modules.alias • If it finds an alias line like: alias char-major-10-30 softdog • Then dependencies are seen • For example, msdos.ko requires the fat.ko module to be already loaded into the kernel. • modprobe uses insmod to first load any prerequisite modules into the kernel, and then the requested module
  • 9. How Do Modules Get Into The Kernel? • Linux distributions provide modprobe, insmod and depmod as a package called modutils or mod-utils. • Until 2.4 kernel versions, module file & object file had same extension i.e. .o • From 2.4 & above to differentiate between object file & module file, module file has extension .ko
  • 10. Module Unloading • Modules can be unloaded using rmmod command • rmmod ensures the restriction that the modules are not in use • Automatically removed from the system by `kerneld' when they are no longer used • cleanup_module function of the concerned module is called to freeup the kernel resources it has allocated • Unlinked from the kernel and unlisted from the list of kernel modules • Dependency is released
  • 11. Module Functions • 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 • Typically, init_module( ) either registers a handler for something with the kernel. • An "end" (cleanup) function called cleanup_module ( ) which is called just before it is rmmoded. • The cleanup_module( ) function is supposed to undo whatever init_module( ) did, so the module can be unloaded safely.
  • 12. Module Functions • As of Linux 2.4, you can rename the init and cleanup functions • This is done with the module_init() and module_exit() macros • These macros are defined in linux/init.h.
  • 13. Using printk( ) • printk seems to be the similar function like printf • Since we are dealing with kernel programming , kernel can’t access user library functions • So only functions used by modules are the one defined by kernel. • printk( ) happens to be a logging mechanism for the kernel, and is used to log information or give warnings. • There are 8 priorities and the kernel has macros for them, and you can view them (and their meanings) in linux/kernel.h.
  • 14. Using printk( ) • If you don't specify a priority level, the default priority, DEFAULT_MESSAGE_LOGLEVEL, will be used • We usually use a high priority, like KERN_ALERT • When you write real modules, you'll want to use priorities that are meaningful for the situation at hand.
  • 16. How programmes begin and end  Program begins with a main() function, executes a bunch of instructions and terminates upon completion of those instructions. • Module begins with either the init_module or the function you specify with module_init call. • This is the entry function for modules; it tells the kernel what functionality the module provides and sets up the kernel to run the module's functions when they're needed. • Once it does this, entry function returns and the module does nothing until the kernel wants to do something with the code that the module provides.
  • 17. How modules begin and end • All modules end by calling either cleanup_module or the function you specify with the module_exit call. • This is the exit function for modules; it undoes whatever entry function did. • It unregisters the functionality that the entry function registered. • Every module must have an entry function and an exit function.
  • 18. Functions available to Programmes • Programmers use functions they don't define all the time. • A prime example of this is printf(). • You use these library functions which are provided by the standard C library.
  • 19. Functions available to Modules • Kernel modules are different here, too.The only functions you can use are the ones provided by the kernel. • That's because modules are object files whose symbols get resolved upon insmod'ing. • The definition for the symbols comes from the kernel itself. • Symbols have been exported by your kernel,the information is available from – /proc/ksyms (old versions)--> – /proc/kallsyms
  • 20. User Space vs Kernel Space • Library functions are higher level, run completely in user space and provide a more convenient interface for the programmer • System calls run in kernel mode on the user's behalf and are provided by the kernel itself. • Library function calls one or more system calls, and these system calls execute in supervisor mode since they are part of the kernel itself. • Once the system call completes its task, it returns and execution gets transferred back to user mode.
  • 21. Namespace • When writing kernel code, even the smallest module will be linked against the entire kernel, so this is definitely an issue. • The best way to deal with this is to declare all your variables as static and to use a well-defined prefix for your symbols. • By convention, all kernel prefixes are lowercase. • If you don't want to declare everything as static, another option is to declare a symbol table and register it with a kernel. • The file /proc/ksyms-->/proc/kallsyms holds all the symbols that the kernel knows about and which are therefore accessible to your modules since they share the kernel's codespace.
  • 22. Codespace • The kernel has its own space of memory as well. • Since a module is code which can be dynamically inserted and removed in the kernel, it shares the kernel's codespace rather than having its own. • Therefore, if your module segfaults, the kernel segfaults. • In general it is true for any operating system which uses a monolithic kernel.
  • 23. Application to Module Programming An introduction to device drivers • One class of modules is the device driver, which provides functionality for hardware like a TV card or a serial port. • On unix, each piece of hardware is represented by a file located in /dev named a device file which provides the means to communicate with the hardware. • The device driver provides the communication on behalf of a user program.
  • 24. References The Linux Kernel Module Programming Guide by Peter Jay Salzman http://en.wikipedia.org/wiki/Linux_Kernel_Module http://ftp.kernel.org/pub/linux/utils/kernel/module- init-tools/ http://en.wikipedia.org/wiki/Modprobe http://ftp.kernel.org/pub/linux/utils/kernel/modutils/ http://www.linuxhq.com/guides/LKMPG/mpg.html Linux Loadable Kernel Module HOWTO by Bryan Henderson