SlideShare une entreprise Scribd logo
1  sur  52
Télécharger pour lire hors ligne
Linux Kernel Modules
Let’s solve the puzzle
Dheryta Jaisinghani
Workshop on Computer Systems, Ashoka University
Dec 9, 2018
(Note: Most of the codes in the slides are from references in the end)
Basics of Operating
Systems
From user space to kernel space
Basic Operating System Structure
Hardware
Kernel
User Services
User Applications
& Processes
System Call
Interface
● What is an operating system?
○ An operating system is system software
that manages computer hardware and
software resources and provides
common services for computer
programs. [Wikipedia]
● What is Kernel?
○ Core of the operating system
○ Loads at startup and takes care of
everything else -
resources/memory/scheduling and many
more
○ Types: Monolithic, Microkernel, Modular,
Nano, Exo 3
What is a Kernel Module?
● Piece of code - Runtime Load/Unload
● Examples - Device Drivers - printer
driver, WLAN driver, vbox driver and
many more
● Actual kernel image is small - Modules
make it big
○ Monolithic kernels would have
been huge
User-level
Programs
System Call Interface
Kernel Services
Device Modules and Drivers
Physical Devices
User space
Kernel space
4
Module vs Program
● Program
○ main() - sequentially executes instructions and terminates
● Kernel Modules
○ init_module() or module_init() - Entry function
■ Initial setup to tell kernel about this module
■ Kernel executes it when needed
○ cleanup_module() or module_exit - Exit function
■ Unregister the module
5
Example 1
(user space to kernel
space)
User Space (Library Functions) → Kernel Space (System
Calls)
#include <stdio.h>
int main(void)
{
printf("hello");
return 0;
}
SystemCalls
7
Example 2
My First Kernel
Module
Example 2.1 from Ref [1]
Prepare the system
● Update the system
○ sudo apt-get update
● Search appropriate headers
○ apt-cache search linux-headers-$(uname -r)
● Download and install correct Linux headers
○ sudo apt-get install linux-headers-$(uname -r)
● Check if installed correctly
○ cd /usr/src/linux-headers-$(uname -r)
○ ls - should show the header files
● Follow steps under Prepare the System for Building LKMs, presented here
(http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/)
9
Hello World Kernel Module
#include <linux/module.h>
#include <linux/kernel.h>
DRIVER_AUTHOR "Dheryta Jaisinghani"
DRIVER_DESC "A Hello World kernel module."
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
int init_module(void)
{
printk(KERN_INFO "Hello Worldn");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye Worldn");
}
10
Header files to make it a kernel module
and log levels
Module Information
Load the module
0 - Success, Else - Failure
Unload the module
Example 3
Different Log Levels
printk
● Log at kernel
● 8 priority levels (See: include/linux/kern_levels.h)
○ KERN_EMERG 0 system is unusable
○ KERN_ALERT 1 action must be taken immediately
○ KERN_CRIT 2 critical conditions
○ KERN_ERR 3 error conditions
○ KERN_WARNING 4 warning conditions
○ KERN_NOTICE 5 normal but significant condition
○ KERN_INFO 6 informational
○ KERN_DEBUG 7 debug-level messages
12
Module Makefile
obj−m += hello.o
all:
make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules
clean:
make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean
13
Multiple modules can be built with single makefile. Refer to
Section 2.2 in The Linux Kernel Module Programming Guide
Example 4
Extracting info about
modules
All about modules
● lsmod - Show all loaded modules
○ lsmod
● insmod - Insert a Module (excludes dependencies)
○ sudo insmod <module_name>
● modprobe - Insert a Module (includes dependencies)
○ sudo modprobe <module_name>
● modinfo - Show information about a module
○ modinfo <module_name.ko>
● depmod - Build module dependency database
○ /lib/modules/$(uname -r)/modules.dep
● rmmod - Remove a module
○ rmmod <module_name.ko>
● Show the log
○ dmesg or cat /var/log/syslog
15
Example 5
Moving away from
default init and exit
Example 2.3 from Ref [1]
Not Using Default init and cleanup
#include <linux/module.h>
#include <linux/kernel.h>
DRIVER_AUTHOR "Dheryta Jaisinghani"
DRIVER_DESC "A Hello World kernel module."
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
static int __init myInit(void)
{
printk(KERN_INFO "Hello Worldn");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
static void __exit myExit(void)
{
printk(KERN_INFO "Goodbye Worldn");
}
module_init(myInit);
module_exit(myExit);
17
● __init and __exit are Macros
● Kernel can free memory
when initialization or module
unloading is done
Example 6
Variables in Kernel
Modules
Example 2.5 from Ref [1]
Declaring init variables in kernel
modules#include <linux/module.h>
#include <linux/kernel.h>
DRIVER_AUTHOR "Dheryta Jaisinghani"
DRIVER_DESC "A Hello World kernel module."
MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
static int myData __initdata = 3;
static int __init myInit(void)
{
printk(KERN_INFO "Hello World, My Data %dn", myData);
return 0;}
static void __exit myExit(void)
{
printk(KERN_INFO "Goodbye Worldn");
}
module_init(myInit);
module_exit(myExit); 19
● Static init variable
● __initdata is a macro
● Notice the change from standard
c program
Example 7
Arguments in Kernel
Modules
Example 2.7 from Ref [1]
Passing command line arguments
21
Conventional argc and argv[] does not work with kernel modules
● Register Parameters
○ //Name, Type, Permission bits
○ module_param(myDataInt, int, 0000);
○ MODULE_PARM_DESC(myDataInt, "An
Integer");
○ module_param(myDataStr, charp, 0000);
○ MODULE_PARM_DESC(myDataStr, "A String");
○ //Name, Type, Pointer to variable to array length,
Permission bits
○ module_param_array(myDataArr, int,
&myDataArrCount, 0000);
○ MODULE_PARM_DESC(myDataArr, "An Array");
● Declare static variables
○ static int myDataInt = 1;
○ static char *myDataStr = "WoCS";
○ static int myDataArr[4] = {0,0,0,0};
○ static int myDataArrCount = 0;
Passing command line arguments
$ sudo insmod helloworld-command_line_args.ko myDataStr="Dheryta"
myDataArrCount=3 myDataInt=10 myDataArr=1,2,3
[23492.223323] Hello World
******
[23492.223327] myDataInt is an integer: 10
[23492.223329] myDataStr is a string: Dheryta
[23492.223331] myDataArr[0] = 1
[23492.223332] myDataArr[1] = 2
[23492.223334] myDataArr[2] = 3
[23492.223336] got 3 arguments for myDataArrCount.
22
Device Drivers
Device Drivers vs Device Files
● Everything is a file or a directory
● Every device is represented by a file in /dev/
● Device Driver: Kernel Module that controls a device
● Device File:
○ Interface for the Device Driver to read from or
write to a physical device
○ Also known as Device Nodes
○ Created with mknod system call [ex. mknod
<c/b> <major> <minor>]
Device File
(/dev/xxx)
Device Driver
Physical Device
User space
Kernel space
24
Example 8
Device Files
Types of Device Files
● Character Files
○ Stream of data one character at a time
○ No restriction on number of bytes
● Block Files
○ Random access to block of data
○ Can buffer and schedule the requests
$ ls -a /dev/
$crw-rw---- 1 root dialout 4, 64 Nov 30 09:51 ttyS0
$brw-rw---- 1 root dialout 4, 64 Nov 30 09:52 sdd
26
Major and Minor Device Number
root@iiitd-HP-Compaq-8200-Elite-MT-PC:/home/iiitd# ls -l /dev/sda*
brw-rw---- 1 root disk 8, 0 Dec 3 11:48 /dev/sda
brw-rw---- 1 root disk 8, 1 Dec 3 11:48 /dev/sda1
brw-rw---- 1 root disk 8, 2 Dec 3 11:48 /dev/sda2
brw-rw---- 1 root disk 8, 3 Dec 3 11:48 /dev/sda3
● Major Number
○ The driver for the hardware
○ Unique for each driver
● Minor Number
○ The number of unique hardware a driver manages
27
Example 9
Some Fun with Device
Files
Writing to/Read from device file
$cat /usr/share/sounds/ubuntu/notifications/Amsterdam.ogg > /dev/audio
$cat /dev/urandom | padsp tee /dev/audio > /dev/null
$echo “Hello World” > /dev/tty1
$cat /dev/psaux
29
Example 10
System Runtime
Information
Getting Runtime Information of System
● proc file system
○ Reports runtime information about various resources
○ Memory usage/hardware/modules loaded and many more
● Try
○ ls /proc/ (Numbered directories correspond to processes!)
○ cat /proc/devices
○ cat /proc/modules
○ cat /proc/cpuinfo
○ cat /proc/meminfo
● More Details: http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
31
Example 11
My First Character
Device Driver
Example 4.1 from Ref [1]
Developing a Character Device Driver
Device Driver → Kernel Module Device File → /dev/<filename>
Register the device -
● register_chrdev
● mknod
● struct file_operations
Do the operations -
read/write/open/close
Unregister the device -
● unregister_chrdev
Try Example - chardev.c
33
● Try_module_get
● module_put
Example 12
Dancing Lights with
Keyboard
Example 10.2 from Ref [1]
Interacting with External Devices
● printk does not always help
● Device specific commands should be known
● Interfacing with Keyboard
○ Periodic Lighting Keyboard - Example 10.2 from Ref[1]
○ Dancing Lights Keyboard - Modify Example 10.2 (see help on next slide)
35
Dancing Lights with Keyboard
● Define 3 Macros
○ #define ONE_LEDS_ON 0x04
○ #define TWO_LEDS_ON 0x06
○ #define THREE_LEDS_ON 0x07
36
● Modify my_timer_func
static void my_timer_func(unsigned long ptr)
{
int *pstatus = (int *)ptr;
if (*pstatus == RESTORE_LEDS)
*pstatus = ONE_LEDS_ON;
else if (*pstatus == ONE_LEDS_ON)
*pstatus = TWO_LEDS_ON;
else if (*pstatus == TWO_LEDS_ON)
*pstatus = THREE_LEDS_ON;
else
*pstatus = RESTORE_LEDS;
(my_driver->ops->ioctl) (vc_cons[fg_console].d->port.tty, KDSETLED,
*pstatus);
(my_driver->ops->ioctl) (vc_cons[fg_console].d->port.tty, KIOCSOUND,
CLOCK_TICK_RATE/DEFAULT_FREQ);
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
Interrupt Handlers
37
Processor Hardware
Instructions
How? → Interrupts
Received an interrupt
Pause current work.
Save the state (How?)
Address the interrupt
with Interrupt Handler
Resume the work
request_irq() → Create entry in /proc/interrupts
request_irq() → Create entry in /proc/interrupts
Example 13
Interrupt Handling
Refer Example 7.1 for chardev.c and Ref [8] for interrupts
Raise and Capture Interrupt on a
Character Device
● chardev.c
● Raise the interrupt whenever file is read [asm instruction]
● Capture the interrupt
● Tell user that the interrupt is captured
39
Example 14
Practice with IOCTL
(Do it on your own)
Ref [7]
Interacting with Physical Devices
Kernel Modules
41
/proc/ file system
/dev/ file system
Device read/write
IOCTLs
System Calls
Interrupt Handlers
Use Existing or Write Your Own Custom Calls
Shell Scripts
What and Why of Shell Scripts
● Shell is an interface to allow the use of operating system services
● All commands are executed in a shell through a terminal
● Bash shell is the most common shell
● Shell scripts allow to
○ Automate the execution of repetitive shell commands
○ Routine procedures such as backups
○ System monitoring
43
Shell Script Structure
● Every shell script starts with a shell name like → # !/bin/bash
● As per convention, a shell script file extension is .sh
● A shell script should be made executable with chmod command
● A shell script can have constructs such as for, while, if-elseif-else, switch
● A shell script can read/write from files
● A shell script can call another program may it be a python or C or any other
● In summary - shell scripts are very powerful
44
Few Interesting
Examples
Control CD Drive
#!/bin/bash
while :
do
eject
eject -t
done
Ref:https://www.quora.com/What-is-the-most-interesting-shell-script-you-have-ever-written
46
GRE Prep
1. sudo apt-get install cowsay
2. Prepare a dictionary
a. Apple == red fruit
b. LadyFinger == green vegetable
c. Clock == A device to show time
d. English == It is a language
3. In .bashrc → shuf -n 1 MyDictionary.txt | cowsay
Ref:https://www.quora.com/What-is-the-most-interesting-shell-script-you-have-ever-
written 47
System Health
● Use standard commands to summarize all vitals
48
Ref: https://www.tecmint.com/linux-server-health-monitoring-script/
Conclusion
● Kernel Modules: An introduction
● Hello world modules: Creating, Compiling, Inserting, Initialization and Passing
variables
● Proc, Device file system
● Device drivers, files
● Interaction with devices
● Interrupt handling
● Shell Scripts
49
Linux WiFi Subsytem
● Tutorial: Opening Nuts and Bolts of Linux WiFi Subsytem
● Slides:
https://www.slideshare.net/DherytaJaisinghani/tutorial-wifi-driver-code-opening
-nuts-and-bolts-of-linux-wifi-subsystem
● Video: https://www.youtube.com/watch?v=pa1oEyc7Dm0
50
Contact
● Web: www.dheryta.co.in
● Email: dherytaj@iiitd.ac.in
51
References
1. https://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf
2. http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/
3. http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/
4. https://elinux.org/Debugging_by_printing
5. ftp://ftp.lpp.polytechnique.fr/jeandet/keep/sync/LINUX/interrupt/t3.pdf
6. http://lwn.net/Kernel/LDD3/
7. https://embetronicx.com/tutorials/linux/device-drivers/ioctl-tutorial-in-linux/
8. https://embetronicx.com/tutorials/linux/device-drivers/linux-device-driver-tutori
al-part-13-interrupt-example-program-in-linux-kernel/
9. http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
10. https://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/dev.html
11. https://01.org/linuxgraphics/gfx-docs/drm/driver-api/index.html
52

Contenu connexe

Tendances

Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
Rashila Rr
 
Linux booting procedure
Linux booting procedureLinux booting procedure
Linux booting procedure
Dhaval Kaneria
 

Tendances (20)

U-Boot Porting on New Hardware
U-Boot Porting on New HardwareU-Boot Porting on New Hardware
U-Boot Porting on New Hardware
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Continguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux KernelContinguous Memory Allocator in the Linux Kernel
Continguous Memory Allocator in the Linux Kernel
 
Linux booting Process
Linux booting ProcessLinux booting Process
Linux booting Process
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Introduction Linux Device Drivers
Introduction Linux Device DriversIntroduction Linux Device Drivers
Introduction Linux Device Drivers
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
Linux Networking Explained
Linux Networking ExplainedLinux Networking Explained
Linux Networking Explained
 
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime RipardKernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
ROM BIOS & POST
ROM BIOS & POSTROM BIOS & POST
ROM BIOS & POST
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Debugging linux kernel tools and techniques
Debugging linux kernel tools and  techniquesDebugging linux kernel tools and  techniques
Debugging linux kernel tools and techniques
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
Linux booting procedure
Linux booting procedureLinux booting procedure
Linux booting procedure
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 

Similaire à Linux kernel modules

Evolution of Linux Containerization
Evolution of Linux Containerization Evolution of Linux Containerization
Evolution of Linux Containerization
WSO2
 
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
camp_drupal_ua
 

Similaire à Linux kernel modules (20)

Writing Character driver (loadable module) in linux
Writing Character driver (loadable module) in linuxWriting Character driver (loadable module) in linux
Writing Character driver (loadable module) in linux
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
 
Introduction to containers
Introduction to containersIntroduction to containers
Introduction to containers
 
Basis Linux (aan de hand van LPIC-1)
Basis Linux (aan de hand van LPIC-1)Basis Linux (aan de hand van LPIC-1)
Basis Linux (aan de hand van LPIC-1)
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作
 
Linux Kernel Debugging
Linux Kernel DebuggingLinux Kernel Debugging
Linux Kernel Debugging
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
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
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
Lec 10-linux-review
Lec 10-linux-reviewLec 10-linux-review
Lec 10-linux-review
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
How Secure Is Your Container? ContainerCon Berlin 2016
How Secure Is Your Container? ContainerCon Berlin 2016How Secure Is Your Container? ContainerCon Berlin 2016
How Secure Is Your Container? ContainerCon Berlin 2016
 
Evolution of Linux Containerization
Evolution of Linux Containerization Evolution of Linux Containerization
Evolution of Linux Containerization
 
Evoluation of Linux Container Virtualization
Evoluation of Linux Container VirtualizationEvoluation of Linux Container Virtualization
Evoluation of Linux Container Virtualization
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
 
Tuning systemd for embedded
Tuning systemd for embeddedTuning systemd for embedded
Tuning systemd for embedded
 
Device drivers tsp
Device drivers tspDevice drivers tsp
Device drivers tsp
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Linux kernel modules

  • 1. Linux Kernel Modules Let’s solve the puzzle Dheryta Jaisinghani Workshop on Computer Systems, Ashoka University Dec 9, 2018 (Note: Most of the codes in the slides are from references in the end)
  • 2. Basics of Operating Systems From user space to kernel space
  • 3. Basic Operating System Structure Hardware Kernel User Services User Applications & Processes System Call Interface ● What is an operating system? ○ An operating system is system software that manages computer hardware and software resources and provides common services for computer programs. [Wikipedia] ● What is Kernel? ○ Core of the operating system ○ Loads at startup and takes care of everything else - resources/memory/scheduling and many more ○ Types: Monolithic, Microkernel, Modular, Nano, Exo 3
  • 4. What is a Kernel Module? ● Piece of code - Runtime Load/Unload ● Examples - Device Drivers - printer driver, WLAN driver, vbox driver and many more ● Actual kernel image is small - Modules make it big ○ Monolithic kernels would have been huge User-level Programs System Call Interface Kernel Services Device Modules and Drivers Physical Devices User space Kernel space 4
  • 5. Module vs Program ● Program ○ main() - sequentially executes instructions and terminates ● Kernel Modules ○ init_module() or module_init() - Entry function ■ Initial setup to tell kernel about this module ■ Kernel executes it when needed ○ cleanup_module() or module_exit - Exit function ■ Unregister the module 5
  • 6. Example 1 (user space to kernel space)
  • 7. User Space (Library Functions) → Kernel Space (System Calls) #include <stdio.h> int main(void) { printf("hello"); return 0; } SystemCalls 7
  • 8. Example 2 My First Kernel Module Example 2.1 from Ref [1]
  • 9. Prepare the system ● Update the system ○ sudo apt-get update ● Search appropriate headers ○ apt-cache search linux-headers-$(uname -r) ● Download and install correct Linux headers ○ sudo apt-get install linux-headers-$(uname -r) ● Check if installed correctly ○ cd /usr/src/linux-headers-$(uname -r) ○ ls - should show the header files ● Follow steps under Prepare the System for Building LKMs, presented here (http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/) 9
  • 10. Hello World Kernel Module #include <linux/module.h> #include <linux/kernel.h> DRIVER_AUTHOR "Dheryta Jaisinghani" DRIVER_DESC "A Hello World kernel module." MODULE_LICENSE("GPL"); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); int init_module(void) { printk(KERN_INFO "Hello Worldn"); return 0; } void cleanup_module(void) { printk(KERN_INFO "Goodbye Worldn"); } 10 Header files to make it a kernel module and log levels Module Information Load the module 0 - Success, Else - Failure Unload the module
  • 12. printk ● Log at kernel ● 8 priority levels (See: include/linux/kern_levels.h) ○ KERN_EMERG 0 system is unusable ○ KERN_ALERT 1 action must be taken immediately ○ KERN_CRIT 2 critical conditions ○ KERN_ERR 3 error conditions ○ KERN_WARNING 4 warning conditions ○ KERN_NOTICE 5 normal but significant condition ○ KERN_INFO 6 informational ○ KERN_DEBUG 7 debug-level messages 12
  • 13. Module Makefile obj−m += hello.o all: make −C /lib/modules/$(shell uname −r)/build M=$(PWD) modules clean: make −C /lib/modules/$(shell uname −r)/build M=$(PWD) clean 13 Multiple modules can be built with single makefile. Refer to Section 2.2 in The Linux Kernel Module Programming Guide
  • 14. Example 4 Extracting info about modules
  • 15. All about modules ● lsmod - Show all loaded modules ○ lsmod ● insmod - Insert a Module (excludes dependencies) ○ sudo insmod <module_name> ● modprobe - Insert a Module (includes dependencies) ○ sudo modprobe <module_name> ● modinfo - Show information about a module ○ modinfo <module_name.ko> ● depmod - Build module dependency database ○ /lib/modules/$(uname -r)/modules.dep ● rmmod - Remove a module ○ rmmod <module_name.ko> ● Show the log ○ dmesg or cat /var/log/syslog 15
  • 16. Example 5 Moving away from default init and exit Example 2.3 from Ref [1]
  • 17. Not Using Default init and cleanup #include <linux/module.h> #include <linux/kernel.h> DRIVER_AUTHOR "Dheryta Jaisinghani" DRIVER_DESC "A Hello World kernel module." MODULE_LICENSE("GPL"); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); static int __init myInit(void) { printk(KERN_INFO "Hello Worldn"); /* * A non 0 return means init_module failed; module can't be loaded. */ return 0; } static void __exit myExit(void) { printk(KERN_INFO "Goodbye Worldn"); } module_init(myInit); module_exit(myExit); 17 ● __init and __exit are Macros ● Kernel can free memory when initialization or module unloading is done
  • 18. Example 6 Variables in Kernel Modules Example 2.5 from Ref [1]
  • 19. Declaring init variables in kernel modules#include <linux/module.h> #include <linux/kernel.h> DRIVER_AUTHOR "Dheryta Jaisinghani" DRIVER_DESC "A Hello World kernel module." MODULE_LICENSE("GPL"); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); static int myData __initdata = 3; static int __init myInit(void) { printk(KERN_INFO "Hello World, My Data %dn", myData); return 0;} static void __exit myExit(void) { printk(KERN_INFO "Goodbye Worldn"); } module_init(myInit); module_exit(myExit); 19 ● Static init variable ● __initdata is a macro ● Notice the change from standard c program
  • 20. Example 7 Arguments in Kernel Modules Example 2.7 from Ref [1]
  • 21. Passing command line arguments 21 Conventional argc and argv[] does not work with kernel modules ● Register Parameters ○ //Name, Type, Permission bits ○ module_param(myDataInt, int, 0000); ○ MODULE_PARM_DESC(myDataInt, "An Integer"); ○ module_param(myDataStr, charp, 0000); ○ MODULE_PARM_DESC(myDataStr, "A String"); ○ //Name, Type, Pointer to variable to array length, Permission bits ○ module_param_array(myDataArr, int, &myDataArrCount, 0000); ○ MODULE_PARM_DESC(myDataArr, "An Array"); ● Declare static variables ○ static int myDataInt = 1; ○ static char *myDataStr = "WoCS"; ○ static int myDataArr[4] = {0,0,0,0}; ○ static int myDataArrCount = 0;
  • 22. Passing command line arguments $ sudo insmod helloworld-command_line_args.ko myDataStr="Dheryta" myDataArrCount=3 myDataInt=10 myDataArr=1,2,3 [23492.223323] Hello World ****** [23492.223327] myDataInt is an integer: 10 [23492.223329] myDataStr is a string: Dheryta [23492.223331] myDataArr[0] = 1 [23492.223332] myDataArr[1] = 2 [23492.223334] myDataArr[2] = 3 [23492.223336] got 3 arguments for myDataArrCount. 22
  • 24. Device Drivers vs Device Files ● Everything is a file or a directory ● Every device is represented by a file in /dev/ ● Device Driver: Kernel Module that controls a device ● Device File: ○ Interface for the Device Driver to read from or write to a physical device ○ Also known as Device Nodes ○ Created with mknod system call [ex. mknod <c/b> <major> <minor>] Device File (/dev/xxx) Device Driver Physical Device User space Kernel space 24
  • 26. Types of Device Files ● Character Files ○ Stream of data one character at a time ○ No restriction on number of bytes ● Block Files ○ Random access to block of data ○ Can buffer and schedule the requests $ ls -a /dev/ $crw-rw---- 1 root dialout 4, 64 Nov 30 09:51 ttyS0 $brw-rw---- 1 root dialout 4, 64 Nov 30 09:52 sdd 26
  • 27. Major and Minor Device Number root@iiitd-HP-Compaq-8200-Elite-MT-PC:/home/iiitd# ls -l /dev/sda* brw-rw---- 1 root disk 8, 0 Dec 3 11:48 /dev/sda brw-rw---- 1 root disk 8, 1 Dec 3 11:48 /dev/sda1 brw-rw---- 1 root disk 8, 2 Dec 3 11:48 /dev/sda2 brw-rw---- 1 root disk 8, 3 Dec 3 11:48 /dev/sda3 ● Major Number ○ The driver for the hardware ○ Unique for each driver ● Minor Number ○ The number of unique hardware a driver manages 27
  • 28. Example 9 Some Fun with Device Files
  • 29. Writing to/Read from device file $cat /usr/share/sounds/ubuntu/notifications/Amsterdam.ogg > /dev/audio $cat /dev/urandom | padsp tee /dev/audio > /dev/null $echo “Hello World” > /dev/tty1 $cat /dev/psaux 29
  • 31. Getting Runtime Information of System ● proc file system ○ Reports runtime information about various resources ○ Memory usage/hardware/modules loaded and many more ● Try ○ ls /proc/ (Numbered directories correspond to processes!) ○ cat /proc/devices ○ cat /proc/modules ○ cat /proc/cpuinfo ○ cat /proc/meminfo ● More Details: http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html 31
  • 32. Example 11 My First Character Device Driver Example 4.1 from Ref [1]
  • 33. Developing a Character Device Driver Device Driver → Kernel Module Device File → /dev/<filename> Register the device - ● register_chrdev ● mknod ● struct file_operations Do the operations - read/write/open/close Unregister the device - ● unregister_chrdev Try Example - chardev.c 33 ● Try_module_get ● module_put
  • 34. Example 12 Dancing Lights with Keyboard Example 10.2 from Ref [1]
  • 35. Interacting with External Devices ● printk does not always help ● Device specific commands should be known ● Interfacing with Keyboard ○ Periodic Lighting Keyboard - Example 10.2 from Ref[1] ○ Dancing Lights Keyboard - Modify Example 10.2 (see help on next slide) 35
  • 36. Dancing Lights with Keyboard ● Define 3 Macros ○ #define ONE_LEDS_ON 0x04 ○ #define TWO_LEDS_ON 0x06 ○ #define THREE_LEDS_ON 0x07 36 ● Modify my_timer_func static void my_timer_func(unsigned long ptr) { int *pstatus = (int *)ptr; if (*pstatus == RESTORE_LEDS) *pstatus = ONE_LEDS_ON; else if (*pstatus == ONE_LEDS_ON) *pstatus = TWO_LEDS_ON; else if (*pstatus == TWO_LEDS_ON) *pstatus = THREE_LEDS_ON; else *pstatus = RESTORE_LEDS; (my_driver->ops->ioctl) (vc_cons[fg_console].d->port.tty, KDSETLED, *pstatus); (my_driver->ops->ioctl) (vc_cons[fg_console].d->port.tty, KIOCSOUND, CLOCK_TICK_RATE/DEFAULT_FREQ); my_timer.expires = jiffies + BLINK_DELAY; add_timer(&my_timer); }
  • 37. Interrupt Handlers 37 Processor Hardware Instructions How? → Interrupts Received an interrupt Pause current work. Save the state (How?) Address the interrupt with Interrupt Handler Resume the work request_irq() → Create entry in /proc/interrupts request_irq() → Create entry in /proc/interrupts
  • 38. Example 13 Interrupt Handling Refer Example 7.1 for chardev.c and Ref [8] for interrupts
  • 39. Raise and Capture Interrupt on a Character Device ● chardev.c ● Raise the interrupt whenever file is read [asm instruction] ● Capture the interrupt ● Tell user that the interrupt is captured 39
  • 40. Example 14 Practice with IOCTL (Do it on your own) Ref [7]
  • 41. Interacting with Physical Devices Kernel Modules 41 /proc/ file system /dev/ file system Device read/write IOCTLs System Calls Interrupt Handlers Use Existing or Write Your Own Custom Calls
  • 43. What and Why of Shell Scripts ● Shell is an interface to allow the use of operating system services ● All commands are executed in a shell through a terminal ● Bash shell is the most common shell ● Shell scripts allow to ○ Automate the execution of repetitive shell commands ○ Routine procedures such as backups ○ System monitoring 43
  • 44. Shell Script Structure ● Every shell script starts with a shell name like → # !/bin/bash ● As per convention, a shell script file extension is .sh ● A shell script should be made executable with chmod command ● A shell script can have constructs such as for, while, if-elseif-else, switch ● A shell script can read/write from files ● A shell script can call another program may it be a python or C or any other ● In summary - shell scripts are very powerful 44
  • 46. Control CD Drive #!/bin/bash while : do eject eject -t done Ref:https://www.quora.com/What-is-the-most-interesting-shell-script-you-have-ever-written 46
  • 47. GRE Prep 1. sudo apt-get install cowsay 2. Prepare a dictionary a. Apple == red fruit b. LadyFinger == green vegetable c. Clock == A device to show time d. English == It is a language 3. In .bashrc → shuf -n 1 MyDictionary.txt | cowsay Ref:https://www.quora.com/What-is-the-most-interesting-shell-script-you-have-ever- written 47
  • 48. System Health ● Use standard commands to summarize all vitals 48 Ref: https://www.tecmint.com/linux-server-health-monitoring-script/
  • 49. Conclusion ● Kernel Modules: An introduction ● Hello world modules: Creating, Compiling, Inserting, Initialization and Passing variables ● Proc, Device file system ● Device drivers, files ● Interaction with devices ● Interrupt handling ● Shell Scripts 49
  • 50. Linux WiFi Subsytem ● Tutorial: Opening Nuts and Bolts of Linux WiFi Subsytem ● Slides: https://www.slideshare.net/DherytaJaisinghani/tutorial-wifi-driver-code-opening -nuts-and-bolts-of-linux-wifi-subsystem ● Video: https://www.youtube.com/watch?v=pa1oEyc7Dm0 50
  • 51. Contact ● Web: www.dheryta.co.in ● Email: dherytaj@iiitd.ac.in 51
  • 52. References 1. https://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf 2. http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction/ 3. http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ 4. https://elinux.org/Debugging_by_printing 5. ftp://ftp.lpp.polytechnique.fr/jeandet/keep/sync/LINUX/interrupt/t3.pdf 6. http://lwn.net/Kernel/LDD3/ 7. https://embetronicx.com/tutorials/linux/device-drivers/ioctl-tutorial-in-linux/ 8. https://embetronicx.com/tutorials/linux/device-drivers/linux-device-driver-tutori al-part-13-interrupt-example-program-in-linux-kernel/ 9. http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html 10. https://www.tldp.org/LDP/Linux-Filesystem-Hierarchy/html/dev.html 11. https://01.org/linuxgraphics/gfx-docs/drm/driver-api/index.html 52