Linux Kernel Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Linux kernel

programming

Vandana Salve,
LSI, Pune
Contents

 Introduction to Linux systems


 Kernel modules
 Writing kernel modules
 Devices in Linux
 Writing sample character driver
A Linux system is…

 Hardware
 Boot Loader
 Linux Kernel
 Linux kernel modules
 Root file systems
 User-mode programs
 Boot Loader
 X86 Example
 GRUB – Grand Unified boot loader
 Configuration file /boot/grub/grub.conf

 Kernel Binary image


 /boot/vmlinuz-VERSION
 Kernel Modules
 Are dynamically loaded as needed by the kernel
 Once loaded, becomes part of kernel and has full
access to all kernel functions
 /lib/modules/VERSION
 The search path for kernel modules
 Root file system
 The root “/” is a global hierarchical namespace
that contains several types of files
 Regular files
 Directories
 Symbolic links
 Character/Block special files
 Named Pipes (FIFOs)
Kernel modules
 Linux has the ability to extend at runtime the set of features
offered by the kernel
 Each piece of code that can be added at runtime is called a
“module”.
 Each module is made up of object code that can be dynamically
linked to the running kernel by “insmod” and can be unlinked by
“rmmod” program.
 Once loaded, becomes part of kernel and has full access to all
kernel functions.
Utilities

 Lsmod
 Display list of currently loaded modules
 /proc/modules
 Modinfo
 Display module information
 /sbin/modinfo
 Filename,description,author,license etc
 Insmod
 Insert a module
 Rmmod
 Remove a module
 Modprobe
 Loads modules plus any module dependencies
 Uses info provided in
/lib/modules/Version/modules.dep
 Updated by depmod command
Writing a simple kernel
module
/* hello-1.c - The simplest kernel module. */
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(DRIVER_AUTHOR);//author
MODULE_DESCRIPTION(“HELLO WORLD Module”);//what this module does
Makefile for a basic kernel
module (2.6kernel)

obj-m += hello-1.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

 Refer Documentation/kbuild/modules.txt : how to build modules


 Use modinfo, #modinfo hello-1.ko
 Use lsmod to list the module inserted
 Check /var/log/messages
 Passing command line arguments to the
module
 Example
Device drivers

 Option 1
 Build device drivers into the kernel
 Adv : driver available at boot time
 Dis-adv: need to load drivers that are rarely
used,increase kernel size
 Option 2
 Build device driver as a kernel module
 Adv : Load and unloaded as needed/not needed
 Dis-adv: potential attempts to load “bad” module into
kernel
….continued

 At the highest level of abstraction, all Linux


device drivers fit into 1 of 3 categories
 Character device
 Can be accessed as stream of bytes;transfer byte at a time to/from
user/kernel space
 Block device
 Block device is something that can host a file system such as a disk;
transfer block at a time to/from kernel filesystem
 Network device
….continued
 On Unix/Linux each piece of HW is represented by a file located in /dev/, named
device file which provides the means to communicate with the hardware
 Major and Minor numbers
 # ls –l /dev/hda*
brw-rw---- 1 root disk 3, 1 Jul 5 2000 /dev/hda1
brw-rw---- 1 root disk 3, 2 Jul 5 2000 /dev/hda2
brw-rw---- 1 root disk 3, 3 Jul 5 2000 /dev/hda3
 Major number
 Tells you which driver is used to access the hardware
 All device files with same major number are controlled by same driver
 Minor number
 Is used by driver to distinguish between the various hardware it controls
 Mknod command
 Used to create the device file
 See Documentation/devices.txt to see assigned major numbers
Character device drivers

 File_operations structure
 Every character driver needs to define functions
perform by the device
 ‘File_operations’ structure holds the address of
the modules functions that perform those
operations
 File_operations defined in Linux/fs.h
….continued

 Registering a device
 Adding a driver to your system means registering
it with the kernel
 “Register_chrdev” defined in Linux/fs.h
 int register_chrdev(unsigned int major, const char
*name, struct file_operations *fops);
 Major – major number
 Name – name of device, as it appears in /proc/devices
 Fops – file operation table
….continued
 Unregistering a device
 Whenever the module is unloaded, the major number
should be released
 int unregister_chrdev(unsigned int major, const char
*name);
 Major – major number
 Name – name of device
 Usage counter functions, defined in linux/module.h
 try_module_get(THIS_MODULE);// Increment the use
count
 module_put(THIS_MODULE);// Decrement the use count
Writing a simple character
driver
 Examples
Useful References for kernel
programming
 http://tldp.org/LDP/lkmpg/2.6/html/index.html
 Kernel module programming,
 http://lwn.net/Kernel/LDD3/
 Device drivers programming

You might also like