SlideShare a Scribd company logo
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

More Related Content

PPTX
Linux device drivers
PDF
Kernel Module Programming
PDF
U-Boot - An universal bootloader
PDF
Linux kernel modules
PDF
Bootloaders
PDF
Hands-on ethernet driver
PPTX
Linux Device Tree
Linux device drivers
Kernel Module Programming
U-Boot - An universal bootloader
Linux kernel modules
Bootloaders
Hands-on ethernet driver
Linux Device Tree

What's hot (20)

PDF
Network Drivers
PPTX
The TCP/IP Stack in the Linux Kernel
PDF
Embedded Linux Kernel - Build your custom kernel
PPTX
Linux I2C
PDF
Understanding a kernel oops and a kernel panic
PDF
Linux kernel debugging
PPTX
Memory model
PDF
Introduction To Linux Kernel Modules
PPTX
U-Boot presentation 2013
PDF
Arm device tree and linux device drivers
PDF
Linux Internals - Part III
PDF
Introduction to Modern U-Boot
PDF
Uboot startup sequence
PDF
A practical guide to buildroot
PDF
Linux-Internals-and-Networking
PPT
Memory management in linux
PDF
Part 02 Linux Kernel Module Programming
PDF
Learn C Programming Language by Using GDB
PDF
Linux Systems: Getting started with setting up an Embedded platform
Network Drivers
The TCP/IP Stack in the Linux Kernel
Embedded Linux Kernel - Build your custom kernel
Linux I2C
Understanding a kernel oops and a kernel panic
Linux kernel debugging
Memory model
Introduction To Linux Kernel Modules
U-Boot presentation 2013
Arm device tree and linux device drivers
Linux Internals - Part III
Introduction to Modern U-Boot
Uboot startup sequence
A practical guide to buildroot
Linux-Internals-and-Networking
Memory management in linux
Part 02 Linux Kernel Module Programming
Learn C Programming Language by Using GDB
Linux Systems: Getting started with setting up an Embedded platform
Ad

Similar to Linux kernel modules (20)

PPTX
Writing Character driver (loadable module) in linux
PDF
An Introduction To Linux
PPTX
Linux Device Driver’s
PDF
Operating-Systems-Network-System-Lecture 2.pdf
PPTX
Introduction to containers
ODP
Basis Linux (aan de hand van LPIC-1)
PDF
Docker 原理與實作
PPT
Linux Kernel Debugging
PPT
Linux Kernel Development
PDF
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UK
PDF
Linux device driver
PPTX
Lec 10-linux-review
PDF
brief intro to Linux device drivers
PPT
Basic Linux Internals
PDF
How Secure Is Your Container? ContainerCon Berlin 2016
PDF
Evoluation of Linux Container Virtualization
PDF
Evolution of Linux Containerization
PDF
Android for Embedded Linux Developers
PDF
Tuning systemd for embedded
PPT
Device drivers tsp
Writing Character driver (loadable module) in linux
An Introduction To Linux
Linux Device Driver’s
Operating-Systems-Network-System-Lecture 2.pdf
Introduction to containers
Basis Linux (aan de hand van LPIC-1)
Docker 原理與實作
Linux Kernel Debugging
Linux Kernel Development
Zephyr RTOS in One Hour | HARDWARIO @ IoT North UK
Linux device driver
Lec 10-linux-review
brief intro to Linux device drivers
Basic Linux Internals
How Secure Is Your Container? ContainerCon Berlin 2016
Evoluation of Linux Container Virtualization
Evolution of Linux Containerization
Android for Embedded Linux Developers
Tuning systemd for embedded
Device drivers tsp
Ad

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Classroom Observation Tools for Teachers
PDF
Business Ethics Teaching Materials for college
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Insiders guide to clinical Medicine.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
Microbial diseases, their pathogenesis and prophylaxis
01-Introduction-to-Information-Management.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial disease of the cardiovascular and lymphatic systems
Classroom Observation Tools for Teachers
Business Ethics Teaching Materials for college
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharma ospi slides which help in ospi learning
Insiders guide to clinical Medicine.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH

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