SlideShare a Scribd company logo
1 of 66
Download to read offline
Linux Kernel Module Programming
Amir H. Payberah
amir@sics.se
Amirkabir University of Technology
(Tehran Polytechnic)
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 1 / 46
Introduction
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 2 / 46
What Is A Kernel Module?
Pieces of code that can be loaded and unloaded into the kernel upon
demand.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 3 / 46
What Is A Kernel Module?
Pieces of code that can be loaded and unloaded into the kernel upon
demand.
They extend the functionality of the kernel without the need to
reboot the system.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 3 / 46
What Is A Kernel Module?
Pieces of code that can be loaded and unloaded into the kernel upon
demand.
They extend the functionality of the kernel without the need to
reboot the system.
One type of module is the device driver.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 3 / 46
Already Loaded Modules
You can see already loaded modules into kernel: lsmod
It gets its information by reading the file /proc/modules.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 4 / 46
How Do Modules Get Into The Kernel? (1/2)
When the kernel needs a feature that is not resident in the kernel:
The kernel module daemon kmod execs modprobe to load the mod-
ule in.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 5 / 46
How Do Modules Get Into The Kernel? (1/2)
When the kernel needs a feature that is not resident in the kernel:
The kernel module daemon kmod execs modprobe to load the mod-
ule in.
modprobe looks file /lib/modules/<version>/modules.dep.
• Contains module dependencies
• Created by depmod -a
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 5 / 46
How Do Modules Get Into The Kernel? (2/2)
modprobe uses insmod to load any prerequisite modules and the
requested module into the kernel.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 6 / 46
How Do Modules Get Into The Kernel? (2/2)
modprobe uses insmod to load any prerequisite modules and the
requested module into the kernel.
insmod is dumb about the location of modules, whereas modprobe
is aware of the default location of modules.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 6 / 46
How Do Modules Get Into The Kernel? (2/2)
modprobe uses insmod to load any prerequisite modules and the
requested module into the kernel.
insmod is dumb about the location of modules, whereas modprobe
is aware of the default location of modules.
modprobe also knows how to figure out the dependencies and load
the modules in the right order.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 6 / 46
insmod vs. modprobe
insmod requires you to pass it the full pathname and to insert the
modules in the right order.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 7 / 46
insmod vs. modprobe
insmod requires you to pass it the full pathname and to insert the
modules in the right order.
modprobe just takes the name, without any extension, and figures
out all it needs to know.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 7 / 46
insmod vs. modprobe
insmod requires you to pass it the full pathname and to insert the
modules in the right order.
modprobe just takes the name, without any extension, and figures
out all it needs to know.
For example:
insmod /lib/modules/2.6.11/kernel/fs/fat/fat.ko
insmod /lib/modules/2.6.11/kernel/fs/msdos/msdos.ko
modprobe msdos
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 7 / 46
Hello World
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 8 / 46
Hello World
#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!n");
// A non 0 return means init_module failed; module can’t be loaded.
return 0;
}
void cleanup_module(void) {
printk(KERN_INFO "Goodbye world!.n");
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 9 / 46
Hello World Structure (1/2)
Two main functions of kernel modules: initialization and cleanup.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 10 / 46
Hello World Structure (1/2)
Two main functions of kernel modules: initialization and cleanup.
init module(): it is called when the module is insmoded into the
kernel.
• It registers a handler for something with the kernel.
• or it replaces one of the kernel functions with its own code.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 10 / 46
Hello World Structure (1/2)
Two main functions of kernel modules: initialization and cleanup.
init module(): it is called when the module is insmoded into the
kernel.
• It registers a handler for something with the kernel.
• or it replaces one of the kernel functions with its own code.
cleanup module(): it is called just before it is rmmoded.
• It undos whatever init module() did, so the module can be
unloaded safely
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 10 / 46
Hello World Structure (2/2)
Every kernel module needs linux/module.h.
It needs linux/kernel.h only for the macro expansion for the
printk() log level, e.g., KERN INFO.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 11 / 46
Introducing printk()
Logging mechanism for the kernel.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
Introducing printk()
Logging mechanism for the kernel.
Each printk() statement comes with a priority.
• Eight priority levels
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
Introducing printk()
Logging mechanism for the kernel.
Each printk() statement comes with a priority.
• Eight priority levels
Prints out the message in the kernel ring buffer.
• Use dmesg to print the kernel ring buffer.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
Introducing printk()
Logging mechanism for the kernel.
Each printk() statement comes with a priority.
• Eight priority levels
Prints out the message in the kernel ring buffer.
• Use dmesg to print the kernel ring buffer.
The message is appended to /var/log/messages, if syslogd is
running.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
Compiling Kernel Modules
A simple Makefile for compiling a module named hello.c.
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
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 13 / 46
Compiling Kernel Modules
A simple Makefile for compiling a module named hello.c.
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
To compile and insert the module in the kernel:
# to compile the module
> make
# to insert the module in the kernel
> sudo insmod hello.ko
# to see the module message
> dmesg
# to remove the module from the kernel
> sudo insmod hello.ko
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 13 / 46
Hello World Version 2
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 14 / 46
Hello World V. 2
Renaming the init and cleanup functions with module init() and
and module exit() macros.
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_init(void) {
printk(KERN_INFO "Hello, world!n");
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, world!n");
}
module_init(hello_init);
module_exit(hello_exit);
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 15 / 46
Hello World Version 3
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 16 / 46
Passing Command Line Arguments to a Module
module_param(name, type, perm)
/*
* The first param is the parameter’s name
* The second param is it’s data type
* The final argument is the permissions bits
*/
module_param_array(name, type, num, perm);
/*
* The first param is the parameter’s
* The second param is the data type of the elements of the array
* The third argument is a pointer to the variable that will store the number
* of elements of the array initialized by the user at module loading time
* The fourth argument is the permission bits
*/
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 17 / 46
Hello World V. 3 (1/2)
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Peter Jay Salzman");
static int myint = 420;
static int myintArray[2] = { -1, -1 };
static int arr_argc = 0;
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "An integer");
module_param_array(myintArray, int, &arr_argc, 0000);
MODULE_PARM_DESC(myintArray, "An array of integers");
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 18 / 46
Hello World V. 3 (2/2)
static int __init hello_init(void) {
int i;
printk(KERN_INFO "Hello, world!n");
printk(KERN_INFO "myint is an integer: %dn", myint);
for (i = 0; i < (sizeof myintArray / sizeof (int)); i++)
printk(KERN_INFO "myintArray[%d] = %dn", i, myintArray[i]);
printk(KERN_INFO "got %d arguments for myintArray.n", arr_argc);
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_INFO "Goodbye, world!n");
}
module_init(hello_init);
module_exit(hello_exit);
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 19 / 46
Compiling Kernel Modules
# to insert the module in the kernel
> sudo insmod hello.ko myint=10 myintArray=10,20
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 20 / 46
Device Drivers
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 21 / 46
Device Drivers (1/2)
One class of module is the device driver.
Each piece of hardware is represented by a file located in /dev.
A device file provides the means to communicate with the hardware.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 22 / 46
Device Drivers (2/2)
For example, the es1370.o sound card device driver might connect
the /dev/sound device file to the Ensoniq IS1370 sound card.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 23 / 46
Device Drivers (2/2)
For example, the es1370.o sound card device driver might connect
the /dev/sound device file to the Ensoniq IS1370 sound card.
A userspace program like mp3blaster can use /dev/sound without
ever knowing what kind of sound card is installed.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 23 / 46
Major and Minor Numbers
The major device number identifies a device driver that should be
called.
The minor device number is passed to the device driver, and it is
entirely up to the driver how the minor number is being interpreted.
> ls -l /dev/sda*
brw-rw---- 1 root disk 8, 0 Dec 4 19:50 /dev/sda
brw-rw---- 1 root disk 8, 1 Dec 4 19:50 /dev/sda1
brw-rw---- 1 root disk 8, 2 Dec 4 19:50 /dev/sda2
brw-rw---- 1 root disk 8, 3 Dec 4 19:50 /dev/sda3
brw-rw---- 1 root disk 8, 4 Dec 4 19:50 /dev/sda4
brw-rw---- 1 root disk 8, 5 Dec 4 19:50 /dev/sda5
brw-rw---- 1 root disk 8, 6 Dec 4 19:50 /dev/sda6
brw-rw---- 1 root disk 8, 7 Dec 4 19:50 /dev/sda70
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 24 / 46
Creating A Device File
All of device files are created by the mknod command.
For example, to create a new char device named coffee with ma-
jor/minor number 12 and 2.
> mknod /dev/coffee c 12 2
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 25 / 46
Character Device Drivers
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 26 / 46
The file operations Structure (1/2)
It holds pointers to functions defined by the driver that perform
various operations on the device.
Defined in linux/fs.h.
struct file_operations {
struct module *owner;
int (*open)(struct inode *, struct file *);
ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long);
int (*mmap)(struct file *, struct vm_area_struct *);
int (*flush)(struct file *);
int (*release)(struct inode *, struct file *);
int (*lock)(struct file *, int, struct file_lock *);
unsigned int (*poll)(struct file *, struct poll_table_struct *);
...
};
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 27 / 46
The file operations Structure (2/2)
The common syntax to use file operations structure.
struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 28 / 46
The file Structure
Each device is represented in the kernel by a file structure, which
is defined in linux/fs.h.
The file is a kernel level structure and never appears in a user space
program.
• It is not the same thing as a FILE.
struct file {
mode_t f_mode;
loff_t f_pos;
unsigned int f_flags;
struct file_operations *f_op;
void *private_data;
...
};
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 29 / 46
Registering A Device
Adding a driver to the system.
Using the register chrdev function, defined by linux/fs.h.
int register_chrdev(unsigned int major, const char *name,
struct file_operations *fops);
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 30 / 46
Unregistering A Device
Keeps track of the number of processes using the module.
• 3rd field of /proc/modules
• If this number is not zero, rmmod will fail.
#include<linux/module.h>
// Increment the use count
try_module_get(THIS_MODULE)
// Decrement the use count
module_put(THIS_MODULE)
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 31 / 46
put user and get user
put user() and get user() are used to get and put single values
(such as an int, char, or long) from and to userspace.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 32 / 46
Character Driver Example (1/7)
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h> /* for put_user */
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 33 / 46
Character Driver Example (2/7)
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 34 / 46
Character Driver Example (3/7)
int init_module(void) {
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %dn", Major);
return Major;
}
printk(KERN_INFO "I was assigned major number %d. To talk ton", Major);
printk(KERN_INFO "’mknod /dev/%s c %d 0’.n", DEVICE_NAME, Major);
return SUCCESS;
}
void cleanup_module(void) {
unregister_chrdev(Major, DEVICE_NAME);
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 35 / 46
Character Driver Example (4/7)
/*
* Called when a process tries to open the device file, like
* "cat /dev/mycharfile"
*/
static int device_open(struct inode *inode, struct file *file) {
static int counter = 0;
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "I already told you %d times Hello world!n", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 36 / 46
Character Driver Example (5/7)
/*
* Called when a process closes the device file.
*/
static int device_release(struct inode *inode, struct file *file) {
Device_Open--; /* We’re now ready for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you’ll
* never get get rid of the module.
*/
module_put(THIS_MODULE);
return 0;
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 37 / 46
Character Driver Example (6/7)
/*
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset) {
int bytes_read = 0;
if (*msg_ptr == 0)
return 0;
/*
* The buffer is in the user data segment. We have to use put_user which
* copies data from the kernel data segment to the user data segment.
*/
while (length && *msg_ptr) {
put_user(*(msg_ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 38 / 46
Character Driver Example (7/7)
/*
* Called when a process writes to dev file: echo "hi" > /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t *off) {
printk(KERN_ALERT "Sorry, this operation isn’t supported.n");
return -EINVAL;
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 39 / 46
Talking To Device Files
ioctl (input-output contro) is a device-specific system call.
There are only a few system calls in Linux, which are not enough to
express all the unique functions devices may have.
So a driver can define an ioctl that allows a userspace application
to send it orders.
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 40 / 46
ioctl Example (1/3)
struct file_operations Fops = {
.read = device_read,
.write = device_write,
.ioctl = device_ioctl,
.open = device_open,
.release = device_release, /* a.k.a. close */
};
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 41 / 46
ioctl Example (2/3)
#define MAJOR_NUM 100
#define IOCTL_SET_MSG _IOR(MAJOR_NUM, 0, char *)
#define IOCTL_GET_MSG _IOR(MAJOR_NUM, 1, char *)
int device_ioctl(struct inode *inode, struct file *file,
unsigned int ioctl_num, /* number and param for ioctl */
unsigned long ioctl_param) {
char *temp;
switch (ioctl_num) {
case IOCTL_SET_MSG:
temp = (char *)ioctl_param;
get_user(ch, temp);
...
break;
case IOCTL_GET_MSG:
...
break;
}
return SUCCESS;
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 42 / 46
ioctl Example (3/3)
main() {
int fd;
int ret_val;
char *msg = "Message passed by ioctln";
char message[100];
fd = open("/dev/chardev", 0);
ret_val = ioctl(fd, IOCTL_GET_MSG, message);
printf("get_msg message:%sn", message);
ret_val = ioctl(fd, IOCTL_SET_MSG, msg);
close(file_desc);
}
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 43 / 46
Summary
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 44 / 46
Summary
Kernel modules
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Summary
Kernel modules
insmod and modprobe
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Summary
Kernel modules
insmod and modprobe
init module and cleanup module
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Summary
Kernel modules
insmod and modprobe
init module and cleanup module
printk
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Summary
Kernel modules
insmod and modprobe
init module and cleanup module
printk
module param and module param array
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Summary
Kernel modules
insmod and modprobe
init module and cleanup module
printk
module param and module param array
Device drivers: major and minor numbers
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Summary
Kernel modules
insmod and modprobe
init module and cleanup module
printk
module param and module param array
Device drivers: major and minor numbers
file operations
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
Questions?
Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 46 / 46

More Related Content

What's hot

Linux scheduling and input and output
Linux scheduling and input and outputLinux scheduling and input and output
Linux scheduling and input and outputSanidhya Chugh
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in LinuxAdrian Huang
 
Kdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysisKdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysisBuland Singh
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchlinuxlab_conf
 
Linux Porting to a Custom Board
Linux Porting to a Custom BoardLinux Porting to a Custom Board
Linux Porting to a Custom BoardPatrick Bellasi
 
The Linux Kernel Scheduler (For Beginners) - SFO17-421
The Linux Kernel Scheduler (For Beginners) - SFO17-421The Linux Kernel Scheduler (For Beginners) - SFO17-421
The Linux Kernel Scheduler (For Beginners) - SFO17-421Linaro
 
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
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)shimosawa
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File SystemAdrian Huang
 
Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelAdrian Huang
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory ManagementNi Zo-Ma
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Pankaj Suryawanshi
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 

What's hot (20)

Linux scheduling and input and output
Linux scheduling and input and outputLinux scheduling and input and output
Linux scheduling and input and output
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
 
Kdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysisKdump and the kernel crash dump analysis
Kdump and the kernel crash dump analysis
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
Jagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratchJagan Teki - U-boot from scratch
Jagan Teki - U-boot from scratch
 
Linux Porting to a Custom Board
Linux Porting to a Custom BoardLinux Porting to a Custom Board
Linux Porting to a Custom Board
 
Linux device drivers
Linux device driversLinux device drivers
Linux device drivers
 
The Linux Kernel Scheduler (For Beginners) - SFO17-421
The Linux Kernel Scheduler (For Beginners) - SFO17-421The Linux Kernel Scheduler (For Beginners) - SFO17-421
The Linux Kernel Scheduler (For Beginners) - SFO17-421
 
U boot-boot-flow
U boot-boot-flowU boot-boot-flow
U boot-boot-flow
 
Android 10
Android 10Android 10
Android 10
 
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...
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Linux Kernel - Virtual File System
Linux Kernel - Virtual File SystemLinux Kernel - Virtual File System
Linux Kernel - Virtual File System
 
Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)Linux Memory Management with CMA (Contiguous Memory Allocator)
Linux Memory Management with CMA (Contiguous Memory Allocator)
 
Linux boot process
Linux boot processLinux boot process
Linux boot process
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 

Viewers also liked

Viewers also liked (20)

Spark Stream and SEEP
Spark Stream and SEEPSpark Stream and SEEP
Spark Stream and SEEP
 
MapReduce
MapReduceMapReduce
MapReduce
 
MegaStore and Spanner
MegaStore and SpannerMegaStore and Spanner
MegaStore and Spanner
 
Process Management - Part2
Process Management - Part2Process Management - Part2
Process Management - Part2
 
Security
SecuritySecurity
Security
 
IO Systems
IO SystemsIO Systems
IO Systems
 
Protection
ProtectionProtection
Protection
 
Main Memory - Part2
Main Memory - Part2Main Memory - Part2
Main Memory - Part2
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2Introduction to Operating Systems - Part2
Introduction to Operating Systems - Part2
 
Storage
StorageStorage
Storage
 
CPU Scheduling - Part2
CPU Scheduling - Part2CPU Scheduling - Part2
CPU Scheduling - Part2
 
The Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics PlatformThe Stratosphere Big Data Analytics Platform
The Stratosphere Big Data Analytics Platform
 
Data Intensive Computing Frameworks
Data Intensive Computing FrameworksData Intensive Computing Frameworks
Data Intensive Computing Frameworks
 
The Spark Big Data Analytics Platform
The Spark Big Data Analytics PlatformThe Spark Big Data Analytics Platform
The Spark Big Data Analytics Platform
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
File System Interface
File System InterfaceFile System Interface
File System Interface
 
CPU Scheduling - Part1
CPU Scheduling - Part1CPU Scheduling - Part1
CPU Scheduling - Part1
 
Scala
ScalaScala
Scala
 
Spark
SparkSpark
Spark
 

Similar to Linux Module Programming

Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungLinux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungdns -
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel ProgrammingNalin Sharma
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel DevelopmentMohammed Farrag
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel DevelopmentPriyank Kapadia
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modulesdibyajyotig
 
DUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelDUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelAlexey Smirnov
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]mcganesh
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteTushar B Kute
 
Linux device driver
Linux device driverLinux device driver
Linux device driverchatsiri
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
Managing Perl Installations: A SysAdmin's View
Managing Perl Installations: A SysAdmin's ViewManaging Perl Installations: A SysAdmin's View
Managing Perl Installations: A SysAdmin's ViewBaden Hughes
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programmingVandana Salve
 
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...Toradex
 

Similar to Linux Module Programming (20)

Linux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesungLinux kernel driver tutorial vorlesung
Linux kernel driver tutorial vorlesung
 
Linux Kernel Programming
Linux Kernel ProgrammingLinux Kernel Programming
Linux Kernel Programming
 
Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 
Lecture 5 Kernel Development
Lecture 5 Kernel DevelopmentLecture 5 Kernel Development
Lecture 5 Kernel Development
 
Linux Kernel Development
Linux Kernel DevelopmentLinux Kernel Development
Linux Kernel Development
 
Introduction To Linux Kernel Modules
Introduction To Linux Kernel ModulesIntroduction To Linux Kernel Modules
Introduction To Linux Kernel Modules
 
DUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into KernelDUSK - Develop at Userland Install into Kernel
DUSK - Develop at Userland Install into Kernel
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
Android Custom Kernel/ROM design
Android Custom Kernel/ROM designAndroid Custom Kernel/ROM design
Android Custom Kernel/ROM design
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Linux device driver
Linux device driverLinux device driver
Linux device driver
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Portin g
Portin gPortin g
Portin g
 
Managing Perl Installations: A SysAdmin's View
Managing Perl Installations: A SysAdmin's ViewManaging Perl Installations: A SysAdmin's View
Managing Perl Installations: A SysAdmin's View
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Perl Modules
Perl ModulesPerl Modules
Perl Modules
 
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
 
Autotools
AutotoolsAutotools
Autotools
 
Driver_linux
Driver_linuxDriver_linux
Driver_linux
 

More from Amir Payberah

P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution NetworkAmir Payberah
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2Amir Payberah
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1Amir Payberah
 
Virtual Memory - Part2
Virtual Memory - Part2Virtual Memory - Part2
Virtual Memory - Part2Amir Payberah
 
Virtual Memory - Part1
Virtual Memory - Part1Virtual Memory - Part1
Virtual Memory - Part1Amir Payberah
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2Amir Payberah
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1Amir Payberah
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3Amir Payberah
 
Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1Amir Payberah
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Amir Payberah
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Amir Payberah
 
Graph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXGraph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXAmir Payberah
 

More from Amir Payberah (15)

P2P Content Distribution Network
P2P Content Distribution NetworkP2P Content Distribution Network
P2P Content Distribution Network
 
File System Implementation - Part2
File System Implementation - Part2File System Implementation - Part2
File System Implementation - Part2
 
File System Implementation - Part1
File System Implementation - Part1File System Implementation - Part1
File System Implementation - Part1
 
Virtual Memory - Part2
Virtual Memory - Part2Virtual Memory - Part2
Virtual Memory - Part2
 
Virtual Memory - Part1
Virtual Memory - Part1Virtual Memory - Part1
Virtual Memory - Part1
 
Main Memory - Part1
Main Memory - Part1Main Memory - Part1
Main Memory - Part1
 
Process Synchronization - Part2
Process Synchronization -  Part2Process Synchronization -  Part2
Process Synchronization - Part2
 
Process Synchronization - Part1
Process Synchronization -  Part1Process Synchronization -  Part1
Process Synchronization - Part1
 
Threads
ThreadsThreads
Threads
 
Process Management - Part3
Process Management - Part3Process Management - Part3
Process Management - Part3
 
Process Management - Part1
Process Management - Part1Process Management - Part1
Process Management - Part1
 
Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3Introduction to Operating Systems - Part3
Introduction to Operating Systems - Part3
 
Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1Introduction to Operating Systems - Part1
Introduction to Operating Systems - Part1
 
Mesos and YARN
Mesos and YARNMesos and YARN
Mesos and YARN
 
Graph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphXGraph processing - Powergraph and GraphX
Graph processing - Powergraph and GraphX
 

Recently uploaded

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 

Recently uploaded (20)

What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

Linux Module Programming

  • 1. Linux Kernel Module Programming Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 1 / 46
  • 2. Introduction Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 2 / 46
  • 3. What Is A Kernel Module? Pieces of code that can be loaded and unloaded into the kernel upon demand. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 3 / 46
  • 4. What Is A Kernel Module? Pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 3 / 46
  • 5. What Is A Kernel Module? Pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. One type of module is the device driver. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 3 / 46
  • 6. Already Loaded Modules You can see already loaded modules into kernel: lsmod It gets its information by reading the file /proc/modules. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 4 / 46
  • 7. How Do Modules Get Into The Kernel? (1/2) When the kernel needs a feature that is not resident in the kernel: The kernel module daemon kmod execs modprobe to load the mod- ule in. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 5 / 46
  • 8. How Do Modules Get Into The Kernel? (1/2) When the kernel needs a feature that is not resident in the kernel: The kernel module daemon kmod execs modprobe to load the mod- ule in. modprobe looks file /lib/modules/<version>/modules.dep. • Contains module dependencies • Created by depmod -a Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 5 / 46
  • 9. How Do Modules Get Into The Kernel? (2/2) modprobe uses insmod to load any prerequisite modules and the requested module into the kernel. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 6 / 46
  • 10. How Do Modules Get Into The Kernel? (2/2) modprobe uses insmod to load any prerequisite modules and the requested module into the kernel. insmod is dumb about the location of modules, whereas modprobe is aware of the default location of modules. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 6 / 46
  • 11. How Do Modules Get Into The Kernel? (2/2) modprobe uses insmod to load any prerequisite modules and the requested module into the kernel. insmod is dumb about the location of modules, whereas modprobe is aware of the default location of modules. modprobe also knows how to figure out the dependencies and load the modules in the right order. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 6 / 46
  • 12. insmod vs. modprobe insmod requires you to pass it the full pathname and to insert the modules in the right order. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 7 / 46
  • 13. insmod vs. modprobe insmod requires you to pass it the full pathname and to insert the modules in the right order. modprobe just takes the name, without any extension, and figures out all it needs to know. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 7 / 46
  • 14. insmod vs. modprobe insmod requires you to pass it the full pathname and to insert the modules in the right order. modprobe just takes the name, without any extension, and figures out all it needs to know. For example: insmod /lib/modules/2.6.11/kernel/fs/fat/fat.ko insmod /lib/modules/2.6.11/kernel/fs/msdos/msdos.ko modprobe msdos Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 7 / 46
  • 15. Hello World Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 8 / 46
  • 16. Hello World #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!n"); // A non 0 return means init_module failed; module can’t be loaded. return 0; } void cleanup_module(void) { printk(KERN_INFO "Goodbye world!.n"); } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 9 / 46
  • 17. Hello World Structure (1/2) Two main functions of kernel modules: initialization and cleanup. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 10 / 46
  • 18. Hello World Structure (1/2) Two main functions of kernel modules: initialization and cleanup. init module(): it is called when the module is insmoded into the kernel. • It registers a handler for something with the kernel. • or it replaces one of the kernel functions with its own code. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 10 / 46
  • 19. Hello World Structure (1/2) Two main functions of kernel modules: initialization and cleanup. init module(): it is called when the module is insmoded into the kernel. • It registers a handler for something with the kernel. • or it replaces one of the kernel functions with its own code. cleanup module(): it is called just before it is rmmoded. • It undos whatever init module() did, so the module can be unloaded safely Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 10 / 46
  • 20. Hello World Structure (2/2) Every kernel module needs linux/module.h. It needs linux/kernel.h only for the macro expansion for the printk() log level, e.g., KERN INFO. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 11 / 46
  • 21. Introducing printk() Logging mechanism for the kernel. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
  • 22. Introducing printk() Logging mechanism for the kernel. Each printk() statement comes with a priority. • Eight priority levels Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
  • 23. Introducing printk() Logging mechanism for the kernel. Each printk() statement comes with a priority. • Eight priority levels Prints out the message in the kernel ring buffer. • Use dmesg to print the kernel ring buffer. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
  • 24. Introducing printk() Logging mechanism for the kernel. Each printk() statement comes with a priority. • Eight priority levels Prints out the message in the kernel ring buffer. • Use dmesg to print the kernel ring buffer. The message is appended to /var/log/messages, if syslogd is running. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 12 / 46
  • 25. Compiling Kernel Modules A simple Makefile for compiling a module named hello.c. 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 Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 13 / 46
  • 26. Compiling Kernel Modules A simple Makefile for compiling a module named hello.c. 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 To compile and insert the module in the kernel: # to compile the module > make # to insert the module in the kernel > sudo insmod hello.ko # to see the module message > dmesg # to remove the module from the kernel > sudo insmod hello.ko Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 13 / 46
  • 27. Hello World Version 2 Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 14 / 46
  • 28. Hello World V. 2 Renaming the init and cleanup functions with module init() and and module exit() macros. #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO */ #include <linux/init.h> /* Needed for the macros */ static int __init hello_init(void) { printk(KERN_INFO "Hello, world!n"); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, world!n"); } module_init(hello_init); module_exit(hello_exit); Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 15 / 46
  • 29. Hello World Version 3 Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 16 / 46
  • 30. Passing Command Line Arguments to a Module module_param(name, type, perm) /* * The first param is the parameter’s name * The second param is it’s data type * The final argument is the permissions bits */ module_param_array(name, type, num, perm); /* * The first param is the parameter’s * The second param is the data type of the elements of the array * The third argument is a pointer to the variable that will store the number * of elements of the array initialized by the user at module loading time * The fourth argument is the permission bits */ Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 17 / 46
  • 31. Hello World V. 3 (1/2) #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/stat.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Peter Jay Salzman"); static int myint = 420; static int myintArray[2] = { -1, -1 }; static int arr_argc = 0; module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); MODULE_PARM_DESC(myint, "An integer"); module_param_array(myintArray, int, &arr_argc, 0000); MODULE_PARM_DESC(myintArray, "An array of integers"); Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 18 / 46
  • 32. Hello World V. 3 (2/2) static int __init hello_init(void) { int i; printk(KERN_INFO "Hello, world!n"); printk(KERN_INFO "myint is an integer: %dn", myint); for (i = 0; i < (sizeof myintArray / sizeof (int)); i++) printk(KERN_INFO "myintArray[%d] = %dn", i, myintArray[i]); printk(KERN_INFO "got %d arguments for myintArray.n", arr_argc); return 0; } static void __exit hello_exit(void) { printk(KERN_INFO "Goodbye, world!n"); } module_init(hello_init); module_exit(hello_exit); Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 19 / 46
  • 33. Compiling Kernel Modules # to insert the module in the kernel > sudo insmod hello.ko myint=10 myintArray=10,20 Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 20 / 46
  • 34. Device Drivers Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 21 / 46
  • 35. Device Drivers (1/2) One class of module is the device driver. Each piece of hardware is represented by a file located in /dev. A device file provides the means to communicate with the hardware. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 22 / 46
  • 36. Device Drivers (2/2) For example, the es1370.o sound card device driver might connect the /dev/sound device file to the Ensoniq IS1370 sound card. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 23 / 46
  • 37. Device Drivers (2/2) For example, the es1370.o sound card device driver might connect the /dev/sound device file to the Ensoniq IS1370 sound card. A userspace program like mp3blaster can use /dev/sound without ever knowing what kind of sound card is installed. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 23 / 46
  • 38. Major and Minor Numbers The major device number identifies a device driver that should be called. The minor device number is passed to the device driver, and it is entirely up to the driver how the minor number is being interpreted. > ls -l /dev/sda* brw-rw---- 1 root disk 8, 0 Dec 4 19:50 /dev/sda brw-rw---- 1 root disk 8, 1 Dec 4 19:50 /dev/sda1 brw-rw---- 1 root disk 8, 2 Dec 4 19:50 /dev/sda2 brw-rw---- 1 root disk 8, 3 Dec 4 19:50 /dev/sda3 brw-rw---- 1 root disk 8, 4 Dec 4 19:50 /dev/sda4 brw-rw---- 1 root disk 8, 5 Dec 4 19:50 /dev/sda5 brw-rw---- 1 root disk 8, 6 Dec 4 19:50 /dev/sda6 brw-rw---- 1 root disk 8, 7 Dec 4 19:50 /dev/sda70 Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 24 / 46
  • 39. Creating A Device File All of device files are created by the mknod command. For example, to create a new char device named coffee with ma- jor/minor number 12 and 2. > mknod /dev/coffee c 12 2 Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 25 / 46
  • 40. Character Device Drivers Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 26 / 46
  • 41. The file operations Structure (1/2) It holds pointers to functions defined by the driver that perform various operations on the device. Defined in linux/fs.h. struct file_operations { struct module *owner; int (*open)(struct inode *, struct file *); ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); int (*ioctl)(struct inode *, struct file *, unsigned int, unsigned long); int (*mmap)(struct file *, struct vm_area_struct *); int (*flush)(struct file *); int (*release)(struct inode *, struct file *); int (*lock)(struct file *, int, struct file_lock *); unsigned int (*poll)(struct file *, struct poll_table_struct *); ... }; Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 27 / 46
  • 42. The file operations Structure (2/2) The common syntax to use file operations structure. struct file_operations fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 28 / 46
  • 43. The file Structure Each device is represented in the kernel by a file structure, which is defined in linux/fs.h. The file is a kernel level structure and never appears in a user space program. • It is not the same thing as a FILE. struct file { mode_t f_mode; loff_t f_pos; unsigned int f_flags; struct file_operations *f_op; void *private_data; ... }; Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 29 / 46
  • 44. Registering A Device Adding a driver to the system. Using the register chrdev function, defined by linux/fs.h. int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 30 / 46
  • 45. Unregistering A Device Keeps track of the number of processes using the module. • 3rd field of /proc/modules • If this number is not zero, rmmod will fail. #include<linux/module.h> // Increment the use count try_module_get(THIS_MODULE) // Decrement the use count module_put(THIS_MODULE) Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 31 / 46
  • 46. put user and get user put user() and get user() are used to get and put single values (such as an int, char, or long) from and to userspace. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 32 / 46
  • 47. Character Driver Example (1/7) #include <linux/kernel.h> #include <linux/module.h> #include <linux/fs.h> #include <asm/uaccess.h> /* for put_user */ /* * Prototypes - this would normally go in a .h file */ int init_module(void); void cleanup_module(void); static int device_open(struct inode *, struct file *); static int device_release(struct inode *, struct file *); static ssize_t device_read(struct file *, char *, size_t, loff_t *); static ssize_t device_write(struct file *, const char *, size_t, loff_t *); #define SUCCESS 0 #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */ #define BUF_LEN 80 /* Max length of the message from the device */ Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 33 / 46
  • 48. Character Driver Example (2/7) /* * Global variables are declared as static, so are global within the file. */ static int Major; /* Major number assigned to our device driver */ static int Device_Open = 0; /* Is device open? * Used to prevent multiple access to device */ static char msg[BUF_LEN]; /* The msg the device will give when asked */ static char *msg_ptr; static struct file_operations fops = { .read = device_read, .write = device_write, .open = device_open, .release = device_release }; Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 34 / 46
  • 49. Character Driver Example (3/7) int init_module(void) { Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major < 0) { printk(KERN_ALERT "Registering char device failed with %dn", Major); return Major; } printk(KERN_INFO "I was assigned major number %d. To talk ton", Major); printk(KERN_INFO "’mknod /dev/%s c %d 0’.n", DEVICE_NAME, Major); return SUCCESS; } void cleanup_module(void) { unregister_chrdev(Major, DEVICE_NAME); } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 35 / 46
  • 50. Character Driver Example (4/7) /* * Called when a process tries to open the device file, like * "cat /dev/mycharfile" */ static int device_open(struct inode *inode, struct file *file) { static int counter = 0; if (Device_Open) return -EBUSY; Device_Open++; sprintf(msg, "I already told you %d times Hello world!n", counter++); msg_Ptr = msg; try_module_get(THIS_MODULE); return SUCCESS; } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 36 / 46
  • 51. Character Driver Example (5/7) /* * Called when a process closes the device file. */ static int device_release(struct inode *inode, struct file *file) { Device_Open--; /* We’re now ready for our next caller */ /* * Decrement the usage count, or else once you opened the file, you’ll * never get get rid of the module. */ module_put(THIS_MODULE); return 0; } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 37 / 46
  • 52. Character Driver Example (6/7) /* * Called when a process, which already opened the dev file, attempts to * read from it. */ static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ char *buffer, /* buffer to fill with data */ size_t length, /* length of the buffer */ loff_t * offset) { int bytes_read = 0; if (*msg_ptr == 0) return 0; /* * The buffer is in the user data segment. We have to use put_user which * copies data from the kernel data segment to the user data segment. */ while (length && *msg_ptr) { put_user(*(msg_ptr++), buffer++); length--; bytes_read++; } return bytes_read; } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 38 / 46
  • 53. Character Driver Example (7/7) /* * Called when a process writes to dev file: echo "hi" > /dev/hello */ static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t *off) { printk(KERN_ALERT "Sorry, this operation isn’t supported.n"); return -EINVAL; } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 39 / 46
  • 54. Talking To Device Files ioctl (input-output contro) is a device-specific system call. There are only a few system calls in Linux, which are not enough to express all the unique functions devices may have. So a driver can define an ioctl that allows a userspace application to send it orders. Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 40 / 46
  • 55. ioctl Example (1/3) struct file_operations Fops = { .read = device_read, .write = device_write, .ioctl = device_ioctl, .open = device_open, .release = device_release, /* a.k.a. close */ }; Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 41 / 46
  • 56. ioctl Example (2/3) #define MAJOR_NUM 100 #define IOCTL_SET_MSG _IOR(MAJOR_NUM, 0, char *) #define IOCTL_GET_MSG _IOR(MAJOR_NUM, 1, char *) int device_ioctl(struct inode *inode, struct file *file, unsigned int ioctl_num, /* number and param for ioctl */ unsigned long ioctl_param) { char *temp; switch (ioctl_num) { case IOCTL_SET_MSG: temp = (char *)ioctl_param; get_user(ch, temp); ... break; case IOCTL_GET_MSG: ... break; } return SUCCESS; } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 42 / 46
  • 57. ioctl Example (3/3) main() { int fd; int ret_val; char *msg = "Message passed by ioctln"; char message[100]; fd = open("/dev/chardev", 0); ret_val = ioctl(fd, IOCTL_GET_MSG, message); printf("get_msg message:%sn", message); ret_val = ioctl(fd, IOCTL_SET_MSG, msg); close(file_desc); } Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 43 / 46
  • 58. Summary Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 44 / 46
  • 59. Summary Kernel modules Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 60. Summary Kernel modules insmod and modprobe Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 61. Summary Kernel modules insmod and modprobe init module and cleanup module Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 62. Summary Kernel modules insmod and modprobe init module and cleanup module printk Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 63. Summary Kernel modules insmod and modprobe init module and cleanup module printk module param and module param array Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 64. Summary Kernel modules insmod and modprobe init module and cleanup module printk module param and module param array Device drivers: major and minor numbers Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 65. Summary Kernel modules insmod and modprobe init module and cleanup module printk module param and module param array Device drivers: major and minor numbers file operations Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 45 / 46
  • 66. Questions? Amir H. Payberah (Tehran Polytechnic) Linux Module Programming 1393/9/17 46 / 46