Loadable Kernel Modules

Similar documents
Linux Kernel Module Programming. Tushar B. Kute,

CPSC Tutorial 17

Abstraction via the OS. Device Drivers. Software Layers. Device Drivers. Types of Devices. Mechanism vs. Policy. Jonathan Misurda

Linux Kernel Modules & Device Drivers April 9, 2012

Software Layers. Device Drivers 4/15/2013. User

Itron Riva Kernel Module Building

/dev/hello_world: A Simple Introduction to Device Drivers under Linux

ECEN 449: Microprocessor System Design Department of Electrical and Computer Engineering Texas A&M University

Kernel Modules. Kartik Gopalan

Modules. Mail: Web: University of Nice - Sophia Antipolis

Advanced Operating Systems #13

Linux drivers - Exercise

Linux Device Driver. Amir Hossein Payberah. (Kmod & Advanced Modularization)

University of Texas at Arlington. CSE Spring 2018 Operating Systems Project 4a - The /Proc File Systems and mmap. Instructor: Jia Rao

Make Your Own Linux Module CS 444/544

Working with Kernel Modules Lab Wind River Systems, Inc

Kernel. Kernel = computer program that connects the user applications to the system hardware Handles:

7.3 Simplest module for embedded Linux drivers

Introduction p. 1 Why Linux? p. 2 Embedded Linux Today p. 3 Open Source and the GPL p. 3 Free Versus Freedom p. 4 Standards and Relevant Bodies p.

Operating Systems II BS degree in Computer Engineering Sapienza University of Rome Lecturer: Francesco Quaglia. Topics: 1.

Developing Real-Time Applications

Virtual File System (VFS) Implementation in Linux. Tushar B. Kute,

CS 423 Operating System Design: Introduction to Linux Kernel Programming (MP1 Q&A)

Kernel Module Programming

Install Cisco UCS VIC Drivers for Linux

Downloading and Installing Cisco UCS VIC Drivers

RTAI 3.8 ON Ubuntu(9.10)-Linux-kernel :

Distribution Kernel Security Hardening with ftrace

Instructions for setting up to compile and run OSGPS code under Linux

Linux modules 1. Support system calls and services 2. Programming facilities 3. Kernel probing

The Linux Kernel Module Programming Guide

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

Linux Kernel Development (LKD)

PMON Module An Example of Writing Kernel Module Code for Debian 2.6 on Genesi Pegasos II

University of Colorado at Colorado Springs CS4500/ Fall 2018 Operating Systems Project 1 - System Calls and Processes

Linux Device Driver. Analog/Digital Signal Interfacing

Loadable Kernel Module

DigSig novelties. Libre Software Meeting 2005 Security Topic July 2005

Step Motor. Step Motor Device Driver. Step Motor. Step Motor (2) Step Motor. Step Motor. source. open loop,

Device Drivers. CS449 Fall 2017

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Finish up OS topics Group plans

Operating System. Hanyang University. Hyunmin Yoon Operating System Hanyang University

The kernel constitutes the core part of the Linux operating system. Kernel duties:

QLogic QLA4010/QLA4010C/QLA4050/QLA4050C/ QLA4052C/QMC4052/QLE4060C/QLE4062C iscsi Driver for Linux Kernel 2.6.x.

2 Setting up the RDMA Framework for Development

Unix (Linux) Device Drivers

LKM Preresolver A Lightweight Prelinker for Linux Kernel Modules

SuSE Labs / Novell.

Installation under Linux

Linux Device Drivers

Linux Loadable Kernel Modules (LKM)

2 Setting up the RDMA Framework for Development

The device driver (DD) implements these user functions, which translate system calls into device-specific operations that act on real hardware

Cisco UCS Virtual Interface Card Drivers for Linux Installation Guide

Oracle Linux 7: System Administration Ed 2 Duration: 5 Days

Installation Guide for Linux

OCTVQE Zaptel Echo Canceller (PRELIMINARY)

ATTO UL4D & UL5D Troubleshooting Guide

Linux Driver support for Brainboxes US/VX products

Assignment II Linux Dictionary Device. Beycan KAHRAMAN Turgut UYAR

7.4 Simple example of Linux drivers

CIS c. University of Pennsylvania Zachary Goldberg. Notes

Loadable Kernel Modules Juan-Mariano de Goyeneche and Elena Apolinario Fernández de Sousa, Technical University of Madrid

Linux Kernel Compilation

LINUX KERNEL AND DEVICE DRIVER PROGRAMMING A SIMPLER APPROACH TO LINUX KERNEL

Oracle Linux System AdministrationNEW

Oracle Linux 5 & 6 System Administration TOD

Oracle Linux 7: System Administration Ed 2 NE

ASE++ : Linux Kernel Programming

CHAPTER 7 IMPLEMENTATION OF DYNAMIC VOLTAGE SCALING IN LINUX SCHEDULER

Oracle Linux 5 & 6 System Administration

Linux Kernel Development (LKD)

PCICC32 Linux Driver Quick Installation and Usage Guide

Embedded Linux Primer, Second Edition

PCIe Hot-Swap Device Driver

Oracle - Oracle Linux 5 6 System Administration Ed 3

Oracle Linux 7: System Administration Ed 1

Chapter 12 IoT Projects Case Studies. Lesson-01: Introduction

Oracle Linux 7: System Administration Ed 1

Eclipse-Based CodeWarrior Debugger

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Oracle Linux 7: System Administration Ed 1 LVC

First order of Business

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 0449 Project 4: /dev/rps Due: Friday, December 8, 2017, at 11:59pm

Helping Developers Help You. Tom Caputi

DS-5 ARM. Getting Started with DS-5. Version 5.6. Copyright 2010, 2011 ARM. All rights reserved. ARM DUI 0478F (ID071411)

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Modules in 2.6: Breaking The Kernel, and What I Learned

Makefiles SE 2XA3. Term I, 2018/19

Lecture 4: Extensibility (and finishing virtual machines) CSC 469H1F Fall 2006 Angela Demke Brown

Netfilter & Packet Dropping

Lab 1: Loadable Kernel Modules (LKMs)

List of Linux Commands in an IPm

Reviewing gcc, make, gdb, and Linux Editors 1


John A. Ronciak Staff Engineer NCG/NID/LAO Intel Corp.

Kernel Module Programming

Advantech General FAQ. How to change ubuntu specific kernel for quick cross test

Transcription:

Loadable Kernel Modules Kevin Dankwardt, Ph.D. kevin.dankwardt@gmail.com

Topics 1. Why loadable kernel modules? 2. Using Modules 3. Writing modules 4. Compiling & Installing Modules 5. Example : Simple Module 6. Command line parameters 7. Modules are not binary compatible with different kernels 8. vermagic 9. tainting 10. modinfo 11. Export symbols 12. Stacking Modules 13. kernel/module.c 14. kernel/kmod.c 15. scripts/makefile.modpost 16. Statically linked modules 17. References etc

Why Loadable Kernel Modules Makes booting kernel smaller and faster Need to include just functionality, like drivers, that one needs. Faster and easier to update functionality No need to reboot to add or remove functionality Note that removing is dangerous and maybe you shouldn't do it.

Using modules insmod modprobe lsmod depmod modinfo rmmod weak-modules make modules available for more than one kernel version (yikes)

Writing modules Modules require many header files, e.g., #include <linux/module.h> #include <linux/sched.h> These header files will come from the kernel source directory. Use module_init() and module_exit() macros. Use MODULE_LICENSE() macro.

Compiling & Installing Modules make modules # in kernel source directory make make modules_install Or, for your own directory: make -C /lib/modules/$(uname -r)/build M=$PWD modules make modules_install INSTALL_MOD_PATH=~kevin/myrootfs Makefile: obj-m := := mod.o

# cat simplemodule.c #include <linux/init.h> #include <linux/module.h> #include <linux/sched.h> int init_simple(void) { printk("in init module simple\n"); return 0; } void cleanup_simple (void) { printk ("in cleanup module simple\n"); } module_init(init_simple); module_exit(cleanup_simple); MODULE_LICENSE("GPL"); # echo obj-m := simplemodule.o > Makefile Example : Simple Module # make -C /lib/modules/$(uname -r)/build M=$PWD modules # insmod./simplemodule.ko # rmmod simplemodule # dmesg tail

Command line parameters insmod module.ko parm=value module_param(name, type, permissions) module_param_array(name, type, number, perm) MODULE_PARM_DESC() Module parameters are listed in /sys/module under the module name if perm is non-zero.

Modules are not binary compatible with different kernels struct S { int f1; int f1b; int f2; } thes; module.c... thes.f2 = 5; Will module.o be portable if f1b is added or removed?

vermagic include/linux/vermagic.h Checks for SMP, PREEMPT, MODULE_UNLOAD, MODVERSIONS Includes UTS_RELEASE and strings based on the above. Although what is in vermagic is kernel version dependent. Different than just the kernel version (UTS_RELEASE). (UTS comes from Unix Timesharing System, and is related to uname(2).) Use modinfo <modulename> to see an example vermagic string vermagic is put into vmlinux (in its Read only Data section), (see include/asmgeneric/vmlinux.lds.h, for example) vermagic is put into each module via:q scripts/mod/modpost.c Kernel compares vermagic when loading the module: sys_init_module () load_module() if (!same_magic(modmagic, vermagic, versindex)) { printk(kern_err "%s: version magic '%s' should be '%s'\n", mod->name, modmagic, vermagic); err = -ENOEXEC; goto free_hdr;

Tainting Tainting is used to indicate that a non GPL (open source) module was loaded. If ndiswrapper is used, the kernel is tainted. If driverloader was used, the kernel is tainted Forcing loading or unloading of modules Module is proprietary - i.e., not GPL

modinfo modinfo <module name> author, description, license, parm, depends, and alias modprobe uses the depends info in files created by depmod

Export Symbols To register entries in the symbol table: Add driver code to kernel source Use EXPORT_SYMBOL(symbol) to export symbol EXPORT_SYMBOL_GPL(symbol) to restrict symbol use to only GPL d modules.

Stacking Modules Modules link to symbols and can provide symbols that other modules require. A module that uses symbols from another module, depends on that module. These modules form a stack. modpost puts depends info in the modinfo section

kernel/module.c 3100+ LOC Does tainting Handle module reference counting Resolve symbols

kernel/kmod.c request_module() macro request_module() calls this about 175 calls to request_module() in the kernel Allows for the kernel to quest a module to be loaded invokes a user space helper e.g. modprobe to load the module

scripts/makefile.modpost # Stage one of module building created the following: # a) The individual.o files used for the module # b) A <module>.o file which is the.o files above linked together # c) A <module>.mod file in $(MODVERDIR)/, listing the name of the # the preliminary <module>.o file, plus all.o files # Stage 2 is handled by this file and does the following # 1) Find all modules from the files listed in $(MODVERDIR)/ # 2) modpost is then used to # 3) create one <module>.mod.c file pr. module # 4) create one Module.symvers file with CRC for all exported symbols # 5) compile all <module>.mod.c files # 6) final link of the module to a <module.ko> (or <module.unsigned>) file # 7) signs the modules to a <module.ko> file # Step 3 is used to place certain information in the module's ELF # section, including information such as: # Version magic (see include/vermagic.h for full details) # - Kernel release # - SMP is CONFIG_SMP # - PREEMPT is CONFIG_PREEMPT # - GCC Version # Module info # - Module version (MODULE_VERSION) # - Module alias'es (MODULE_ALIAS) # - Module license (MODULE_LICENSE) # - See include/linux/module.h for more details

Statically Linked Drivers Add driver code to kernel source Mark initialization function with module_init() Modify the file Makefile in the same directory as your driver code by adding: obj-y += your_code.o

References etc http://tldp.org/howto/module-howto scripts/makefile.modpost kernel source code Contact Contact Kevin Kevin Dankwardt Dankwardt for for info info on on consulting consulting and and training. training. kevin.dankwardt@gmail.com Kernel Kernel internals internals Device Device driver driver development Application development Debugging Debugging Performance Virtualization Embedded Embedded Linux Linux System System admin admin