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

What's hot

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
 
Kernel module in linux os.
Kernel module in linux os.Kernel module in linux os.
Kernel module in linux os.
MUKESH BADIGINENI
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
Goutam Sahoo
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Adrian Huang
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKB
shimosawa
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
shimosawa
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
Adrian Huang
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
Anil Kumar Pugalia
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
Satpal Parmar
 
Android Boot Time Optimization
Android Boot Time OptimizationAndroid Boot Time Optimization
Android Boot Time Optimization
Kan-Ru Chen
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
Anil Kumar Pugalia
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
Vandana Salve
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
shimosawa
 
Static partitioning virtualization on RISC-V
Static partitioning virtualization on RISC-VStatic partitioning virtualization on RISC-V
Static partitioning virtualization on RISC-V
RISC-V International
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
Vandana Salve
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
Adrian Huang
 
Introduction to armv8 aarch64
Introduction to armv8 aarch64Introduction to armv8 aarch64
Introduction to armv8 aarch64
Yi-Hsiu Hsu
 
How to use KASAN to debug memory corruption in OpenStack environment- (2)
How to use KASAN to debug memory corruption in OpenStack environment- (2)How to use KASAN to debug memory corruption in OpenStack environment- (2)
How to use KASAN to debug memory corruption in OpenStack environment- (2)
Gavin Guo
 

What's hot (20)

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
 
Kernel module in linux os.
Kernel module in linux os.Kernel module in linux os.
Kernel module in linux os.
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 
Linux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKBLinux Kernel Booting Process (2) - For NLKB
Linux Kernel Booting Process (2) - For NLKB
 
Linux Initialization Process (1)
Linux Initialization Process (1)Linux Initialization Process (1)
Linux Initialization Process (1)
 
Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...Decompressed vmlinux: linux kernel initialization from page table configurati...
Decompressed vmlinux: linux kernel initialization from page table configurati...
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Android Boot Time Optimization
Android Boot Time OptimizationAndroid Boot Time Optimization
Android Boot Time Optimization
 
Embedded Linux on ARM
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARM
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
Static partitioning virtualization on RISC-V
Static partitioning virtualization on RISC-VStatic partitioning virtualization on RISC-V
Static partitioning virtualization on RISC-V
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
qemu + gdb + sample_code: Run sample code in QEMU OS and observe Linux Kernel...
 
Introduction to armv8 aarch64
Introduction to armv8 aarch64Introduction to armv8 aarch64
Introduction to armv8 aarch64
 
How to use KASAN to debug memory corruption in OpenStack environment- (2)
How to use KASAN to debug memory corruption in OpenStack environment- (2)How to use KASAN to debug memory corruption in OpenStack environment- (2)
How to use KASAN to debug memory corruption in OpenStack environment- (2)
 

Similar to Linux kernel modules

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
RajKumar Rampelli
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
Ishan A B Ambanwela
 
Linux Device Driver’s
Linux Device Driver’sLinux Device Driver’s
Linux Device Driver’s
Rashmi Warghade
 
Introduction to containers
Introduction to containersIntroduction to containers
Introduction to containers
Nitish Jadia
 
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)
Peter Martin
 
Docker 原理與實作
Docker 原理與實作Docker 原理與實作
Docker 原理與實作
kao kuo-tung
 
Linux Kernel Debugging
Linux Kernel DebuggingLinux Kernel Debugging
Linux Kernel Debugging
GlobalLogic Ukraine
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
Priyank Kapadia
 
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
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device driver
Linux device driverLinux device driver
Linux device driver
chatsiri
 
Lec 10-linux-review
Lec 10-linux-reviewLec 10-linux-review
Lec 10-linux-review
abinaya m
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
Alexandre Moreno
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
mukul bhardwaj
 
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
Phil Estes
 
Evolution of Linux Containerization
Evolution of Linux Containerization Evolution of Linux Containerization
Evolution of Linux Containerization
WSO2
 
Evoluation of Linux Container Virtualization
Evoluation of Linux Container VirtualizationEvoluation of Linux Container Virtualization
Evoluation of Linux Container Virtualization
Imesh Gunaratne
 
Android for Embedded Linux Developers
Android for Embedded Linux DevelopersAndroid for Embedded Linux Developers
Android for Embedded Linux Developers
Opersys inc.
 
Tuning systemd for embedded
Tuning systemd for embeddedTuning systemd for embedded
Tuning systemd for embedded
Alison Chaiken
 
Device drivers tsp
Device drivers tspDevice drivers tsp
Device drivers tsp
Pradeep Kumar TS
 

Similar to 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 drivers
Linux device drivers Linux device drivers
Linux device drivers
 
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
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
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
 

Recently uploaded

คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.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