+ All Categories
Home > Software > Linux kernel modules

Linux kernel modules

Date post: 16-Jul-2015
Category:
Upload: eddy-reyes
View: 196 times
Download: 4 times
Share this document with a friend
Popular Tags:
29
Linux Kernel Modules Eddy Reyes [email protected]
Transcript

Linux Kernel ModulesEddy Reyes [email protected]

Eddy Reyes

Founder of Tasqr (www.tasqr.io)In my past life:● IBM- AIX kernel developer● Lifesize- Linux device driver developer● Various other non-kernel software

development

Linux Kernel Modules

In this presentation, I will show what is a Linux Kernel Module● Kernel module-

○ Code that executes as part of the Linux kernel○ Extends the capabilities and sometimes might

modify the behavior of the kernel

Let’s Back Up For a Second...

What exactly is the Linux Kernel?

Your Computer == Pile of Hardware

Your average computer (or phone) has a quite large variety of hardware.● CPU, Disk, NIC, GPU, Speakers, Display,

RAM, DVD Drive, Game Controller, Webcam, Microphone, GSM Radio, WiFi, Keyboard, Mouse, Touch Screen, etc.

What Is the Kernel?

Very simple answer: it's a program that makes your hardware look and feel like an OS to other programs

Hardware (CPU, Keyboard, Disk, Monitor)

Linux Kernel

GNOME bash cat Chome grep

Who Uses the Kernel?

The kernel provides a way for other programs to use the hardware under a common model called an “Operating System”● The kernel is not designed for direct human

consumption (no UI)● The kernel’s user is other programs● Linux conforms to the POSIX specification.

Hardware Protection

Most modern processors protect access to hardware● User Mode… running program:

○ Has no access to hardware○ Has its own dedicated address space

● Privileged Mode… running program:○ Can mess with all hardware○ Has access to single shared global address space

When Does Kernel Code Execute?

The Kernel has 2 primary entry points:● On behalf of a process

○ When that process calls a system call

● In response to a hardware interrupt○ You just pressed a key○ Your disk just finished storing a block of data○ A network packet just arrived○ The timer just ticked

Kernel And Hardware

Kernel is the middle-man for hardware access.

Disk Kernel

System Call

cat ~/file

interrupt

System Call Layer

The kernel provides system calls for user programs to perform actions that require Privileged Mode:● Example: cat ~/my-file

○ fd = open(“/home/eddy/myfile”)○ contents = read(fd)○ close(fd)○ print(contents)

Linux vs. Linux Ecosystem

“Linux” originally referred only to the kernel.● Linus only works on the kernel● He doesn’t care a whole lot about what

happens outside of that○ Except when something irritates him :-)

All the programs you use live outside of the kernel (GNU/Linux, all distros)

Back to Kernel Modules

Almost all problems are solved in user space programs.● Why do we need kernel modules?

○ I have some hardware I want to make work in Linux○ I want to create a program that resides in the kernel

and runs in privileged mode■ In practice, almost never happens

To Write a Kernel Module...

● Kernel Modules are written in the C programming language.

● You must have a Linux kernel source tree to build your module.

● You must be running the same kernel you built your module with to run it.

Anatomy of a Kernel Module

A kernel module file has several typical components:● MODULE_AUTHOR(“your name”)● MODULE_LICENSE(“GPL”)

○ The license must be an open source license (GPL, BSD, etc.) or you will “taint” your kernel.■ Tainted kernel loses many abilities to run other

other open source modules and capabilities.

Anatomy of a Kernel Module● int init_module(void)

○ Called when the kernel loads your module.○ Initialize all your stuff here.○ Return 0 if all went well, negative if something blew

up.

● void cleanup_module(void)○ Called when the kernel unloads your module.○ Free all your resources here.

Hello World Kernel Module Example#include <linux/kernel.h>#include <linux/module.h>

MODULE_AUTHOR(“Eddy Reyes”);

MODULE_LICENSE(“GPL”);

int init_module(void)

{

printk(KERN_ALERT “Hello world\n”);

return 0;

}

void cleanup_module(void)

{

printk(KERN_ALERT “Goodbye, cruel world\n”);

}

Kernel Programming Environment● Kernel has no libc, none of the standard headers and

libraries you are used to.○ i.e. No system calls, no standard buffered output

(stdio.h)

● No memory protection!○ You can stomp on any other part of memory, no one

will stop you.

Kernel Programming Environment● One big single namespace

○ You have access to all “exported” symbols from your kernel module.

○ Exported functions are called “kernel services”

● Always multi-threaded○ All modules must be thread-safe, like it or not○ What about single-processor systems?

■ No concurrency, but Linux is a preemptable kernel! Still must be thread-safe.

Building Your Kernel Module● Accompany your module with a 1-line GNU Makefile:

○ obj-m += hello.o○ Assumes file name is “hello.c”

● Run the magic make command:○ make -C <kernel-src> M=`pwd` modules○ Produces: hello.ko

Assumes current directory is the module source.

Running Your Kernel Module

● To manually load your module:○ insmod hello.ko○ Where’s our hello world message?

■ dmesg

● To unload your module:○ rmmod hello.ko

Kernel Module Dependenciesinsmod/rmmod can be cumbersome...● You must manually enforce inter-module dependencies.

Modprobe automatically manages dependent modules● Copy hello.ko into /lib/modules/<version>● Run depmod● modprobe hello / modprobe -r hello

Dependent modules are automatically loaded/unloaded.

More Interesting Example...

Let’s make a character device● /dev/hello-char● When you read from it, it says “hello”● Doesn’t let you write into itCharacter devices are tracked by the kernel via major and minor numbers● Major == type of device, Minor == nth device

Hello Char Device

Linux Kernel uses an “ops” struct pattern that allows you to fill in behaviors via callbacks.

struct file_operations fops = {

.open = open_dev,

.close = close_dev,

.read = read_dev,

.write = write_dev

}

Registering Your Device

Kernel Service- register_chrdev()● Accepts:

○ Major number (0 tells the kernel to pick one)○ Device type name (appears in /proc/devices)○ Pointer to fops struct

● Call this in the init_module() routine

Reading / Writing From Your Device

● cat /dev/hello-char○ Calls your read_dev() function

● echo hi >/dev/hello-char○ Calls your write_dev() function

read_dev must return 0 when the device has no more data.

Using Your Device

Demo...

Linux Device Model

If you write a real device driver, don’t follow this example. Further reading:● Linux Device Model

○ Much richer API for modules that implement device drivers

○ Integrates with udev○ Not available on tainted kernels!

■ Must be open source to use this.

Congratulations!

We’ve built a couple kernel modules together!

Any lingering questions?


Recommended