SlideShare a Scribd company logo
Linux Kernel
Modules
Eddy Reyes eddy@tasqr.io
Eddy Reyes
Founder of Tasqr (www.tasqr.io)
In my past life:
● IBM- AIX kernel developer
● Lifesize- Linux device driver developer
● Various other non-kernel software
development
Linux Kernel Modules
In this presentation, I will show what is a Linux
Kernel Module
● Kernel module-
○ Code that executes as part of the Linux kernel
○ Extends the capabilities and sometimes might
modify the behavior of the kernel
Let’s Back Up For a Second...
What exactly is the Linux Kernel?
Your Computer == Pile of Hardware
Your average computer (or phone) has a quite
large variety of hardware.
● CPU, Disk, NIC, GPU, Speakers, Display,
RAM, DVD Drive, Game Controller,
Webcam, Microphone, GSM Radio, WiFi,
Keyboard, Mouse, Touch Screen, etc.
What Is the Kernel?
Very simple answer: it's a program that makes
your hardware look and feel like an OS to other
programs
Hardware (CPU, Keyboard, Disk, Monitor)
Linux Kernel
GNOME bash cat Chome grep
Who Uses the Kernel?
The kernel provides a way for other programs
to use the hardware under a common model
called an “Operating System”
● The kernel is not designed for direct human
consumption (no UI)
● The kernel’s user is other programs
● Linux conforms to the POSIX specification.
Hardware Protection
Most modern processors protect access to
hardware
● User Mode… running program:
○ Has no access to hardware
○ Has its own dedicated address space
● Privileged Mode… running program:
○ Can mess with all hardware
○ Has access to single shared global address space
When Does Kernel Code Execute?
The Kernel has 2 primary entry points:
● On behalf of a process
○ When that process calls a system call
● In response to a hardware interrupt
○ You just pressed a key
○ Your disk just finished storing a block of data
○ A network packet just arrived
○ The timer just ticked
Kernel And Hardware
Kernel is the middle-man for hardware access.
Disk Kernel
System Call
cat ~/file
interrupt
System Call Layer
The kernel provides system calls for user
programs to perform actions that require
Privileged Mode:
● Example: cat ~/my-file
○ fd = open(“/home/eddy/myfile”)
○ contents = read(fd)
○ close(fd)
○ print(contents)
Linux vs. Linux Ecosystem
“Linux” originally referred only to the kernel.
● Linus only works on the kernel
● He doesn’t care a whole lot about what
happens outside of that
○ Except when something irritates him :-)
All the programs you use live outside of the
kernel (GNU/Linux, all distros)
Back to Kernel Modules
Almost all problems are solved in user space
programs.
● Why do we need kernel modules?
○ I have some hardware I want to make work in Linux
○ I want to create a program that resides in the kernel
and runs in privileged mode
■ In practice, almost never happens
To Write a Kernel Module...
● Kernel Modules are written in the C
programming language.
● You must have a Linux kernel source tree to
build your module.
● You must be running the same kernel you
built your module with to run it.
Anatomy of a Kernel Module
A kernel module file has several typical
components:
● MODULE_AUTHOR(“your name”)
● MODULE_LICENSE(“GPL”)
○ The license must be an open source license (GPL,
BSD, etc.) or you will “taint” your kernel.
■ Tainted kernel loses many abilities to run other
other open source modules and capabilities.
Anatomy of a Kernel Module
● int init_module(void)
○ Called when the kernel loads your module.
○ Initialize all your stuff here.
○ Return 0 if all went well, negative if something blew
up.
● void cleanup_module(void)
○ Called when the kernel unloads your module.
○ Free all your resources here.
Hello World Kernel Module Example
#include <linux/kernel.h>
#include <linux/module.h>
MODULE_AUTHOR(“Eddy Reyes”);
MODULE_LICENSE(“GPL”);
int init_module(void)
{
printk(KERN_ALERT “Hello worldn”);
return 0;
}
void cleanup_module(void)
{
printk(KERN_ALERT “Goodbye, cruel worldn”);
}
Kernel Programming Environment
● Kernel has no libc, none of the standard headers and
libraries you are used to.
○ i.e. No system calls, no standard buffered output
(stdio.h)
● No memory protection!
○ You can stomp on any other part of memory, no one
will stop you.
Kernel Programming Environment
● One big single namespace
○ You have access to all “exported” symbols from your
kernel module.
○ Exported functions are called “kernel services”
● Always multi-threaded
○ All modules must be thread-safe, like it or not
○ What about single-processor systems?
■ No concurrency, but Linux is a preemptable
Building Your Kernel Module
● Accompany your module with a 1-line GNU Makefile:
○ obj-m += hello.o
○ Assumes file name is “hello.c”
● Run the magic make command:
○ make -C <kernel-src> M=`pwd` modules
○ Produces: hello.ko
Assumes current directory is the module source.
Running Your Kernel Module
● To manually load your module:
○ insmod hello.ko
○ Where’s our hello world message?
■ dmesg
● To unload your module:
○ rmmod hello.ko
Kernel Module Dependencies
insmod/rmmod can be cumbersome...
● You must manually enforce inter-module dependencies.
Modprobe automatically manages dependent modules
● Copy hello.ko into /lib/modules/<version>
● Run depmod
● modprobe hello / modprobe -r hello
Dependent modules are automatically loaded/unloaded.
More Interesting Example...
Let’s make a character device
● /dev/hello-char
● When you read from it, it says “hello”
● Doesn’t let you write into it
Character devices are tracked by the kernel via
major and minor numbers
● Major == type of device, Minor == nth device
Hello Char Device
Linux Kernel uses an “ops” struct pattern that
allows you to fill in behaviors via callbacks.
struct file_operations fops = {
.open = open_dev,
.close = close_dev,
.read = read_dev,
.write = write_dev
}
Registering Your Device
Kernel Service- register_chrdev()
● Accepts:
○ Major number (0 tells the kernel to pick one)
○ Device type name (appears in /proc/devices)
○ Pointer to fops struct
● Call this in the init_module() routine
Reading / Writing From Your Device
● cat /dev/hello-char
○ Calls your read_dev() function
● echo hi >/dev/hello-char
○ Calls your write_dev() function
read_dev must return 0 when the device has no more data.
Using Your Device
Demo...
Linux Device Model
If you write a real device driver, don’t follow this
example. Further reading:
● Linux Device Model
○ Much richer API for modules that implement device
drivers
○ Integrates with udev
○ Not available on tainted kernels!
■ Must be open source to use this.
Congratulations!
We’ve built a couple kernel modules together!
Any lingering questions?

More Related Content

What's hot

Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
Abhishek Sagar
 
Linux-Internals-and-Networking
Linux-Internals-and-NetworkingLinux-Internals-and-Networking
Linux-Internals-and-Networking
Emertxe Information Technologies Pvt Ltd
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
Dheryta Jaisinghani
 
Linux process management
Linux process managementLinux process management
Linux process managementRaghu nath
 
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
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
SysPlay eLearning Academy for You
 
File systems for Embedded Linux
File systems for Embedded LinuxFile systems for Embedded Linux
File systems for Embedded Linux
Emertxe Information Technologies Pvt Ltd
 
Linux file system
Linux file systemLinux file system
Linux file system
Midaga Mengistu
 
Linux device drivers
Linux device drivers Linux device drivers
Linux kernel
Linux kernelLinux kernel
Linux kernel
Goutam Sahoo
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
Emertxe Information Technologies Pvt Ltd
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
Sadia Bashir
 
Bootloaders
BootloadersBootloaders
Bootloaders
Anil Kumar Pugalia
 
Linux Boot Process
Linux Boot ProcessLinux Boot Process
Linux Boot Process
darshhingu
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
Bhavesh Padharia
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
Tushar B Kute
 

What's hot (20)

Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
 
Linux-Internals-and-Networking
Linux-Internals-and-NetworkingLinux-Internals-and-Networking
Linux-Internals-and-Networking
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Linux process management
Linux process managementLinux process management
Linux process management
 
Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)Part 01 Linux Kernel Compilation (Ubuntu)
Part 01 Linux Kernel Compilation (Ubuntu)
 
Embedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernelEmbedded Linux Kernel - Build your custom kernel
Embedded Linux Kernel - Build your custom kernel
 
BeagleBone Black Booting Process
BeagleBone Black Booting ProcessBeagleBone Black Booting Process
BeagleBone Black Booting Process
 
File systems for Embedded Linux
File systems for Embedded LinuxFile systems for Embedded Linux
File systems for Embedded Linux
 
Linux file system
Linux file systemLinux file system
Linux file system
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
U-Boot - An universal bootloader
U-Boot - An universal bootloader U-Boot - An universal bootloader
U-Boot - An universal bootloader
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
Lesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File SystemLesson 2 Understanding Linux File System
Lesson 2 Understanding Linux File System
 
Bootloaders
BootloadersBootloaders
Bootloaders
 
Linux Boot Process
Linux Boot ProcessLinux Boot Process
Linux Boot Process
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 

Viewers also liked

Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel ProgrammingNalin Sharma
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
Priyank Kapadia
 
Kernel Configuration and Compilation
Kernel Configuration and CompilationKernel Configuration and Compilation
Kernel Configuration and Compilation
Bud Siddhisena
 
Gumby: Package Dependency Visualization for Linux
Gumby: Package Dependency Visualization for LinuxGumby: Package Dependency Visualization for Linux
Gumby: Package Dependency Visualization for Linux
Andre Guerreiro
 
Psi android telephony_case_study_v10
Psi android telephony_case_study_v10Psi android telephony_case_study_v10
Psi android telephony_case_study_v10Primesoftinc
 
Labmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM System
Labmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM SystemLabmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM System
Labmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM System
Syuan Wang
 
Introduction to Information Visualization (Part 1)
Introduction to Information Visualization (Part 1)Introduction to Information Visualization (Part 1)
Introduction to Information Visualization (Part 1)
Andrew Vande Moere
 
Encrypted Voice Communications
Encrypted Voice CommunicationsEncrypted Voice Communications
Encrypted Voice Communications
sbwahid
 
Voice encryption for gsm using arduino
Voice encryption for gsm using arduinoVoice encryption for gsm using arduino
Voice encryption for gsm using arduinoiruldaworld
 
RT Procedure new KTM
RT Procedure new KTMRT Procedure new KTM
RT Procedure new KTMRaj Pradhan
 
Android telephony stack
Android telephony stackAndroid telephony stack
Android telephony stackDavid Marques
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMSJussi Pohjolainen
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
Morteza Nourelahi Alamdari
 
5432 cellular network
5432 cellular network5432 cellular network
5432 cellular network
Raafat younis
 
Voice securityprotocol review
Voice securityprotocol reviewVoice securityprotocol review
Voice securityprotocol review
Fabio Pietrosanti
 
RIL and Android Telephony
RIL and Android TelephonyRIL and Android Telephony
RIL and Android Telephony
Leaf Johnson
 

Viewers also liked (20)

Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Kernel modules
Kernel modulesKernel modules
Kernel modules
 
Kernel Configuration and Compilation
Kernel Configuration and CompilationKernel Configuration and Compilation
Kernel Configuration and Compilation
 
Gumby: Package Dependency Visualization for Linux
Gumby: Package Dependency Visualization for LinuxGumby: Package Dependency Visualization for Linux
Gumby: Package Dependency Visualization for Linux
 
Psi android telephony_case_study_v10
Psi android telephony_case_study_v10Psi android telephony_case_study_v10
Psi android telephony_case_study_v10
 
Labmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM System
Labmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM SystemLabmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM System
Labmeeting - 20150211 - Novel End-to-End Voice Encryption Method in GSM System
 
Introduction to Information Visualization (Part 1)
Introduction to Information Visualization (Part 1)Introduction to Information Visualization (Part 1)
Introduction to Information Visualization (Part 1)
 
Encrypted Voice Communications
Encrypted Voice CommunicationsEncrypted Voice Communications
Encrypted Voice Communications
 
Android presentation
Android presentationAndroid presentation
Android presentation
 
Voice encryption for gsm using arduino
Voice encryption for gsm using arduinoVoice encryption for gsm using arduino
Voice encryption for gsm using arduino
 
Ch2
Ch2Ch2
Ch2
 
RT Procedure new KTM
RT Procedure new KTMRT Procedure new KTM
RT Procedure new KTM
 
Cellular network
Cellular networkCellular network
Cellular network
 
Android telephony stack
Android telephony stackAndroid telephony stack
Android telephony stack
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMS
 
Basic Linux kernel
Basic Linux kernelBasic Linux kernel
Basic Linux kernel
 
5432 cellular network
5432 cellular network5432 cellular network
5432 cellular network
 
Voice securityprotocol review
Voice securityprotocol reviewVoice securityprotocol review
Voice securityprotocol review
 
RIL and Android Telephony
RIL and Android TelephonyRIL and Android Telephony
RIL and Android Telephony
 

Similar to Linux kernel modules

Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
Rashmi Warghade
 
01 linux-quick-start
01 linux-quick-start01 linux-quick-start
01 linux-quick-startNguyen Vinh
 
Embedded platform choices
Embedded platform choicesEmbedded platform choices
Embedded platform choices
Tavish Naruka
 
Leveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVLeveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVOpersys inc.
 
Linux
LinuxLinux
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
Opersys inc.
 
Grub and dracut ii
Grub and dracut iiGrub and dracut ii
Grub and dracut ii
plarsen67
 
embedded-linux-120203.pdf
embedded-linux-120203.pdfembedded-linux-120203.pdf
embedded-linux-120203.pdf
twtester
 
Part 1 of 'Introduction to Linux for bioinformatics': Introduction
Part 1 of 'Introduction to Linux for bioinformatics': IntroductionPart 1 of 'Introduction to Linux for bioinformatics': Introduction
Part 1 of 'Introduction to Linux for bioinformatics': Introduction
Joachim Jacob
 
Android Variants, Hacks, Tricks and Resources presented at AnDevConII
Android Variants, Hacks, Tricks and Resources presented at AnDevConIIAndroid Variants, Hacks, Tricks and Resources presented at AnDevConII
Android Variants, Hacks, Tricks and Resources presented at AnDevConII
Opersys inc.
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
Ishan A B Ambanwela
 
Systemd: the modern Linux init system you will learn to love
Systemd: the modern Linux init system you will learn to loveSystemd: the modern Linux init system you will learn to love
Systemd: the modern Linux init system you will learn to love
Alison Chaiken
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
chatsiri
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
dotCloud
 
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Docker, Inc.
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
Bimal Jain
 
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UK
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UKZephyr RTOS in One Hour | HARDWARIO @ IoT North UK
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UK
HARDWARIO
 
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewireIntroduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
dotCloud
 

Similar to Linux kernel modules (20)

Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
 
01 linux-quick-start
01 linux-quick-start01 linux-quick-start
01 linux-quick-start
 
Embedded platform choices
Embedded platform choicesEmbedded platform choices
Embedded platform choices
 
Leveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IVLeveraging Android's Linux Heritage at AnDevCon IV
Leveraging Android's Linux Heritage at AnDevCon IV
 
Linux
LinuxLinux
Linux
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
 
Device drivers tsp
Device drivers tspDevice drivers tsp
Device drivers tsp
 
Grub and dracut ii
Grub and dracut iiGrub and dracut ii
Grub and dracut ii
 
embedded-linux-120203.pdf
embedded-linux-120203.pdfembedded-linux-120203.pdf
embedded-linux-120203.pdf
 
Part 1 of 'Introduction to Linux for bioinformatics': Introduction
Part 1 of 'Introduction to Linux for bioinformatics': IntroductionPart 1 of 'Introduction to Linux for bioinformatics': Introduction
Part 1 of 'Introduction to Linux for bioinformatics': Introduction
 
Android Variants, Hacks, Tricks and Resources presented at AnDevConII
Android Variants, Hacks, Tricks and Resources presented at AnDevConIIAndroid Variants, Hacks, Tricks and Resources presented at AnDevConII
Android Variants, Hacks, Tricks and Resources presented at AnDevConII
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Systemd: the modern Linux init system you will learn to love
Systemd: the modern Linux init system you will learn to loveSystemd: the modern Linux init system you will learn to love
Systemd: the modern Linux init system you will learn to love
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013Lightweight Virtualization with Linux Containers and Docker | YaC 2013
Lightweight Virtualization with Linux Containers and Docker | YaC 2013
 
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013Lightweight Virtualization with Linux Containers and Docker I YaC 2013
Lightweight Virtualization with Linux Containers and Docker I YaC 2013
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Unix fundamentals
Unix fundamentalsUnix fundamentals
Unix fundamentals
 
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UK
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UKZephyr RTOS in One Hour | HARDWARIO @ IoT North UK
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UK
 
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @GuidewireIntroduction to Docker at SF Peninsula Software Development Meetup @Guidewire
Introduction to Docker at SF Peninsula Software Development Meetup @Guidewire
 

Recently uploaded

Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
Sharepoint Designs
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 

Recently uploaded (20)

Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024Explore Modern SharePoint Templates for 2024
Explore Modern SharePoint Templates for 2024
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 

Linux kernel modules

  • 2. Eddy Reyes Founder of Tasqr (www.tasqr.io) In my past life: ● IBM- AIX kernel developer ● Lifesize- Linux device driver developer ● Various other non-kernel software development
  • 3. Linux Kernel Modules In this presentation, I will show what is a Linux Kernel Module ● Kernel module- ○ Code that executes as part of the Linux kernel ○ Extends the capabilities and sometimes might modify the behavior of the kernel
  • 4. Let’s Back Up For a Second... What exactly is the Linux Kernel?
  • 5. Your Computer == Pile of Hardware Your average computer (or phone) has a quite large variety of hardware. ● CPU, Disk, NIC, GPU, Speakers, Display, RAM, DVD Drive, Game Controller, Webcam, Microphone, GSM Radio, WiFi, Keyboard, Mouse, Touch Screen, etc.
  • 6. What Is the Kernel? Very simple answer: it's a program that makes your hardware look and feel like an OS to other programs Hardware (CPU, Keyboard, Disk, Monitor) Linux Kernel GNOME bash cat Chome grep
  • 7. Who Uses the Kernel? The kernel provides a way for other programs to use the hardware under a common model called an “Operating System” ● The kernel is not designed for direct human consumption (no UI) ● The kernel’s user is other programs ● Linux conforms to the POSIX specification.
  • 8. Hardware Protection Most modern processors protect access to hardware ● User Mode… running program: ○ Has no access to hardware ○ Has its own dedicated address space ● Privileged Mode… running program: ○ Can mess with all hardware ○ Has access to single shared global address space
  • 9. When Does Kernel Code Execute? The Kernel has 2 primary entry points: ● On behalf of a process ○ When that process calls a system call ● In response to a hardware interrupt ○ You just pressed a key ○ Your disk just finished storing a block of data ○ A network packet just arrived ○ The timer just ticked
  • 10. Kernel And Hardware Kernel is the middle-man for hardware access. Disk Kernel System Call cat ~/file interrupt
  • 11. System Call Layer The kernel provides system calls for user programs to perform actions that require Privileged Mode: ● Example: cat ~/my-file ○ fd = open(“/home/eddy/myfile”) ○ contents = read(fd) ○ close(fd) ○ print(contents)
  • 12. Linux vs. Linux Ecosystem “Linux” originally referred only to the kernel. ● Linus only works on the kernel ● He doesn’t care a whole lot about what happens outside of that ○ Except when something irritates him :-) All the programs you use live outside of the kernel (GNU/Linux, all distros)
  • 13. Back to Kernel Modules Almost all problems are solved in user space programs. ● Why do we need kernel modules? ○ I have some hardware I want to make work in Linux ○ I want to create a program that resides in the kernel and runs in privileged mode ■ In practice, almost never happens
  • 14. To Write a Kernel Module... ● Kernel Modules are written in the C programming language. ● You must have a Linux kernel source tree to build your module. ● You must be running the same kernel you built your module with to run it.
  • 15. Anatomy of a Kernel Module A kernel module file has several typical components: ● MODULE_AUTHOR(“your name”) ● MODULE_LICENSE(“GPL”) ○ The license must be an open source license (GPL, BSD, etc.) or you will “taint” your kernel. ■ Tainted kernel loses many abilities to run other other open source modules and capabilities.
  • 16. Anatomy of a Kernel Module ● int init_module(void) ○ Called when the kernel loads your module. ○ Initialize all your stuff here. ○ Return 0 if all went well, negative if something blew up. ● void cleanup_module(void) ○ Called when the kernel unloads your module. ○ Free all your resources here.
  • 17. Hello World Kernel Module Example #include <linux/kernel.h> #include <linux/module.h> MODULE_AUTHOR(“Eddy Reyes”); MODULE_LICENSE(“GPL”); int init_module(void) { printk(KERN_ALERT “Hello worldn”); return 0; } void cleanup_module(void) { printk(KERN_ALERT “Goodbye, cruel worldn”); }
  • 18. Kernel Programming Environment ● Kernel has no libc, none of the standard headers and libraries you are used to. ○ i.e. No system calls, no standard buffered output (stdio.h) ● No memory protection! ○ You can stomp on any other part of memory, no one will stop you.
  • 19. Kernel Programming Environment ● One big single namespace ○ You have access to all “exported” symbols from your kernel module. ○ Exported functions are called “kernel services” ● Always multi-threaded ○ All modules must be thread-safe, like it or not ○ What about single-processor systems? ■ No concurrency, but Linux is a preemptable
  • 20. Building Your Kernel Module ● Accompany your module with a 1-line GNU Makefile: ○ obj-m += hello.o ○ Assumes file name is “hello.c” ● Run the magic make command: ○ make -C <kernel-src> M=`pwd` modules ○ Produces: hello.ko Assumes current directory is the module source.
  • 21. Running Your Kernel Module ● To manually load your module: ○ insmod hello.ko ○ Where’s our hello world message? ■ dmesg ● To unload your module: ○ rmmod hello.ko
  • 22. Kernel Module Dependencies insmod/rmmod can be cumbersome... ● You must manually enforce inter-module dependencies. Modprobe automatically manages dependent modules ● Copy hello.ko into /lib/modules/<version> ● Run depmod ● modprobe hello / modprobe -r hello Dependent modules are automatically loaded/unloaded.
  • 23. More Interesting Example... Let’s make a character device ● /dev/hello-char ● When you read from it, it says “hello” ● Doesn’t let you write into it Character devices are tracked by the kernel via major and minor numbers ● Major == type of device, Minor == nth device
  • 24. Hello Char Device Linux Kernel uses an “ops” struct pattern that allows you to fill in behaviors via callbacks. struct file_operations fops = { .open = open_dev, .close = close_dev, .read = read_dev, .write = write_dev }
  • 25. Registering Your Device Kernel Service- register_chrdev() ● Accepts: ○ Major number (0 tells the kernel to pick one) ○ Device type name (appears in /proc/devices) ○ Pointer to fops struct ● Call this in the init_module() routine
  • 26. Reading / Writing From Your Device ● cat /dev/hello-char ○ Calls your read_dev() function ● echo hi >/dev/hello-char ○ Calls your write_dev() function read_dev must return 0 when the device has no more data.
  • 28. Linux Device Model If you write a real device driver, don’t follow this example. Further reading: ● Linux Device Model ○ Much richer API for modules that implement device drivers ○ Integrates with udev ○ Not available on tainted kernels! ■ Must be open source to use this.
  • 29. Congratulations! We’ve built a couple kernel modules together! Any lingering questions?