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

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

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

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

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

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

Loadable Kernel Modules

Kernel Modules. Kartik Gopalan

Linux Kernel Modules & Device Drivers April 9, 2012

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

CPSC Tutorial 17

Linux drivers - Exercise

Advanced Operating Systems #13

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

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

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.

Linux Device Driver. Analog/Digital Signal Interfacing

7.3 Simplest module for embedded Linux drivers

Loadable Kernel Module

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

Introduction to Supercomputing

Developing Real-Time Applications

Device Drivers. CS449 Fall 2017

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

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

Creating a system call in Linux. Tushar B. Kute,

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

Part A: Setup your kernel development environment

Introduction to Linux

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

CS240: Programming in C

Unix (Linux) Device Drivers

Topic 6: A Quick Intro To C

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Embedded Linux driver development

Introduction to Linux

A Fast Review of C Essentials Part I

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

TestOut Linux Pro - English 4.0.x OBJECTIVE MAPPING: CompTIA Linux+ LX0-103

Itron Riva Kernel Module Building

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

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

CS240: Programming in C. Lecture 2: Overview

CST8177 Linux II. Linux Boot Process

Programming refresher and intro to C programming

Manually Mount Usb Flash Drive Linux Command Line Redhat

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

Reliable C++ development - session 1: From C to C++ (and some C++ features)

Kernel Module Programming

RocketRAID 2310/2300 Controller Fedora Linux Installation Guide

OS In Action. Linux. /proc

PRINCIPLES OF OPERATING SYSTEMS

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

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

EE516: Embedded Software Project 1. Setting Up Environment for Projects

Introduction to Linux

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

LPIC-1 System Administrator

Lectures 5-6: Introduction to C

Make Your Own Linux Module CS 444/544

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

RocketRAID 2522 SATA Controller Ubuntu Linux Installation Guide

Ubuntu Linux Installation Guide

C Introduction. Comparison w/ Java, Memory Model, and Pointers

SuSE Labs / Novell.

OS Structure. Kevin Webb Swarthmore College January 25, Relevant xkcd:

CS 1550 Project 3: File Systems Directories Due: Sunday, July 22, 2012, 11:59pm Completed Due: Sunday, July 29, 2012, 11:59pm

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

Introduction to Supercomputing

RocketRAID 231x/230x SATA Controller Debian Linux Installation Guide

If you don't care about how it works but you just would like that it works read here. Other wise jump to the next chapter.

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

CHAPTER 2: SYSTEM STRUCTURES. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

*nix Crash Course. Presented by: Virginia Tech Linux / Unix Users Group VTLUUG

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Project #1 Exceptions and Simple System Calls

CS3210: Crash consistency

Kthreads, Mutexes, and Debugging. Sarah Diesburg CS 3430 Operating Systems

Release Note MPI/XMP-Linux Support Package

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

RocketRAID 231x/230x SATA Controller Fedora Linux Installation Guide

Introduction to Computer Systems

BLM2031 Structured Programming. Zeyneb KURT

Chapter 1 Getting Started

CS5460/6460: Operating Systems. Lecture 24: Device drivers. Anton Burtsev April, 2014

MV V310 Android 4.0 Compilation

2 Compiling a C program

2 Setting up the RDMA Framework for Development

QEMU and the Linux Kernel

Linux Kernel Development (LKD)

Linux Files and the File System

EL2310 Scientific Programming

Working with Kernel Modules Lab Wind River Systems, Inc

Filesystem Hierarchy Operating systems I800 Edmund Laugasson

Operating Systems, Fall

Lecture 03 Bits, Bytes and Data Types

An introduction to Docker

Introduction of Linux

CSC231 C Tutorial Fall 2018 Introduction to C

Transcription:

Modules Mail: Stephane.Lavirotte@unice.fr Web: http://stephane.lavirotte.com/ University of Nice - Sophia Antipolis

Introduction For a good start 2

Where does the Modules come from? Principle based on micro kernel (ex: Mach) : The (micro) kernel only contains generic code Synchronization Scheduling Inter-Process Communications (IPC)... Each layer/function/driver of the OS is written independently Obligation to define and use interfaces / protocols clearly defined Strong partitioning (MACH module = process) Better management of resources Only active modules consume resources 3

Linux Modules Module = object file Very partial implementation of the concept of micro-kernel Code can be dynamically linked at runtime During boot (rc scripts) At the request (kernel configured with optional CONFIG_KMOD) But the code can also generally be statically linked in kernel code (traditional monolithic approach) Benefits Adding features to the kernel (drivers, support FS...) Develop drivers without rebooting: load, test, unload, recompilation, loading... Support incompatibility between drivers Useful to keep the kernel image to a minimum size Once loaded, any access to the kernel. No special protection. 4

Dynamic Linking 1/3 The code of a library is not copied into the executable at compile It remains in a separate file (file.ko in Linux 2.6) The linker does virtually nothing to compile It noted that libraries need an executable (see Dependencies Modules) Most work is done during loading or execution By the OS loader The loader looks for/loads libraries that a program needs Adds necessary elements for the address space of process 5

Dynamic Linking 2/3 Using dynamic linking involves relocations The jumps addresses are not known at compile time They are only known when the program and libraries are loaded Unable to pre allocate space Conflicts Limitations of space in 32 bit memory Using a table of indirection An empty table is added to the program at compile All references go through this table (import directory) Libraries have a table similar to the symbols they export (entry points) This table is filled by loading the loader Slower than static linking 6

How to find the library at runtime? Dynamic Linking 3/3 Unix Known/defined directories Specified in /etc/ld.so.conf Environment variables (LD_PRELOAD, LD_LIBRARY_PATH) Windows Using the registry for ActiveX Well-known directories (System32, System ) Adding directories SetDllDirectory () Current path and PATH 7

Constraints on Linux License No constraints on redistribution You can share your changes early, in your own interest, but this is not forced! Constraints upon distribution For embedded devices Linux and Free Software, you must cast your sources to the end user. You have no obligation to distribute them to anyone else! The proprietary modules are tolerated (but not recommended) as they are not considered derivatives of GPL code The proprietary drivers can not be linked statically into the kernel No worries for the drivers available under a license compatible with the GPL (details in the section on writing modules) 8

Necessary Modules for Booting Compiling a kernel If "everything" is compiled as a module how to start the system? Example: Partition containing the root filesystem as ext3 and ext3 is not compiled statically into the kernel How to access the root filesystem? The solution: initrd 9

initrd: Initial Ram Disk Initrd (Initial RAM disk) Root file system (/) with minimalist use of RAM Traditionally used to minimize the numbers of drivers are compiled statically into the kernel. Useful for launching complex initialization scripts Useful for loading proprietary modules (which can be linked statically to the kernel) For more information: Read Documentation/initrd.txt in the kernel sources! Also covers changing root file system (pivot_root) 10

Manually Create an initrd Image mkdir /mnt/initrd dd if=/dev/zero of=initrd.img bs=1k count=2048 mkfs.ext2 F initrd.img mount o loop initrd.img /mnt/initrd <Remplir avec: les modules, le script linuxrc > umount /mnt/initrd gzip --best c initrd.img > initrd 11

Modules Adding functionalities to Kernel 12

Dynamic Modules Principles The kernel can handle a large number of components They may not be active at the same time Modules to load the kernel components when they are needed (and if necessary) Benefits: Less memory consumed by the kernel, so more memory for users Avoiding a full kernel compilation when adding code for a device driver Facilitates the debugging of a driver: it allows to unload and reload the module (if it has not crashed the machine ;-) 13

«Hello World» Module «Hello World» Module : hello.c #include <linux/module.h> /* Nécessaire pour tout module */ int init_module(void) { printk("hello World!\n"); return 0; } void cleanup_module(void) { printk("goodbye, cruel world!\n"); } MODULE_LICENSE("GPL"); 14

Dynamic Modules Schema 15

Module Initialization From Kernel 2.2: Macros init and exit init: if «built-in», free memory after initialization From Kernel 2.4: Possibility of giving different names to init clean_module() #include <linux/init.h> /* necessary for macros */ static int init_function (void) { } static void exit_function (void) { } module_init(init_function); module_exit(exit_function); static int init init_module(void) exit: if «built-in», omit this function static void exit cleaup_module(void) d 16

Documentation Macros Module s Documentation MODULE_AUTHOR("..."); MODULE_DESCRIPTION("..."); MODULE_SUPPORTED_DEVICE("..."); From Kernel 2.4: Need to define a license for a module, else: > insmod module.o Warning: loading module.o will taint the kernel: no licence See http://www.tux.org/lkml/#export-tainted for information about tainted modules On entre dans le module Module module loaded, with warnings 17

Modules Licenses MODULE_LICENSE("..."); voir include/linux/module.h GPL GNU Public License v2 or greater GPL v2 GNU Public License v2 GPL and additional rights Dual BSD/GPL Choice between GNU Public License v2 and BSD Dual MIT/GPL Choice between GNU Public License v2 and MIT Dual MPL/GPL Choice between GNU Public License v2 and Mozilla Proprietary Non free products 18

Usefulness of Licenses Used by kernel developers to identify Problems from proprietary drivers, they will not try to solve Allows users To verify that their system is 100% free Allows GNU / Linux distributors To verify compliance with their licensing policy 19

Includes C: Writing a Module: Some Rules 1/2 The C library is implemented above the kernel and not the reverse Unable to use the features of the standard C library (printf(), strcat(), etc.). Linux has some useful functions like C printk() which has an interface similar to printf() Therefore, only the header files of the kernel are allowed. Floating Point: Never use floating point numbers in the kernel code. Your code can be run on a processor without calculation unit float (like ARM) The emulation by the kernel is possible but very slow 20

Scope: Writing a Module: Some Rules Define all symbols as local/static, except those that are exported (to prevent pollution of the namespace) 2/2 Check: In the source code: Documentation/CodingStyle It is always good to know and to apply the GNU coding rules: http://www.gnu.org/prep/standards.html 21

Compiling a Module Commande ligne compilation until 2.4: gcc -DMODULE -D KERNEL -c hello.c A Makefile is needed from 2.6: # Makefile pour le module hello obj-m := hello.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: $(MAKE) C $(KDIR) M=$(PWD) clean This will result in building a file hello.ko (Kernel Object) 22

Key Commands: insmod: load a module into memory Commands for Modules modprobe: load a module and all is dependencies rmmod: unload a module from memory lsmod: list all loaded modules (cat /proc/modules) modinfo: see information on a module Example: ~> lsmod Module Size Used by Not tainted mousedev 4308 1 (autoclean) input 3648 0 (autoclean) [mousedev] usb-storage 70048 0 (unused) usb-uhci 23568 0 (unused) usbcore 63756 1 [usb-storage usb-uhci] eepro100 20276 1 mii 2464 0 [eepro100] vfat 10892 0 fat 32792 0 [vfat] ext3 82536 0 (autoclean) jbd 42980 0 (autoclean) [ext3] Management 23

Modules Dependencies 1/2 The dependencies of a module does not need to be specified explicitly by the creator of the module They are automatically deducted when compiling the kernel, through the symbols exported by the module : ModuleB depends on ModuleA if moduleb used a symbol exported by modulea. The modules dependencies are stored in: /lib/modules/<version>/modules.dep This file is updated (as a root user) with: depmod a [<version>] 24

Modules Dependencies To be used by another module, you must export the symbols By default all the symbols are exported Not to export anything: EXPORT_NO_SYMBOL; To enable export mechanism EXPORT_SYMTAB To define before including module.h Using macros in source code to export symbols EXPORT_SYMBOL(symbol_name); EXPORT_SYMBOL_NOVERS(symbol_name); EXPORT_SYMBOL_GPL(symbol_name); The symbols are placed in symbol table module 2/2 25

Using Static Symbols defined in In principle, only the exported symbols Declared "extern" The symbols that have been planned for this API provided by the kernel to modules What to do if you need a static symbol? Reasonable solution 1 : do not use it! Reasonable solution 2 : modify the source of kernel Add "extern the Kernel But you need to recompile the kernel (and this become a specific kernel) Bad solution (but efficient one ) : cheat Find the address in the symbol table /proc/ksyms /boot/system.mapxxx 26

Example of Using Static Symbol Hypothesis: one wishes to use the variable module_list Problem : this symbol is not exported by the kernel Solution to avoid (but efficient) : Look for this symbol in System.map > grep module_list /boot/system.map 2.4.27 c01180d3 T get_module_list c023edc0 D module_list Declare this symbol in your module /* /boot/system.map nous donne l'adresse du symbole 'module_list' * Comme module_list est un pointeur sur struct module, ce que nous * récupérons est de type struct module ** */ struct module **module_list_ptr = (struct module **)0xc023edc0; Why "avoid" anyway?? We must check (and alter) the code of each module to recompile the kernel (the symbols address change) 27

Module Configuration Some modules need information to run Hardware address for I/O Parameters to modify a driver behavior (DMA ) 2 ways to get theses data Given by the user Auto-detection Parameters are passed when loading a module insmod or modprobe insmod hello param1=5 param2="hello!" Possibility to put them in a configuration file (depending on distribution) Debian: /etc/modprobe.d/hello options hello param1=3 param2="hello!" RedHat: /etc/modules.conf 28

Passing Parameters to Modules Passing parameters to a module: Linux 2.4 The command line parameters are not passed to module as the classical argc/argv mechanism In source code, you need to use macro: MODULE_PARM(name, type) name = parameter name type = string containing type Supported types for variables: "b": byte "h": short int (2 bytes) "i": integer "l": long int "s": string (déclaration de la variable comme char*) Documenting parameters is strongly recommended MODULE_PARM_DESC("..."); 29

Passing Parameters to Modules Linux 2.6 Passing parameters to a module: Using macro: module_param(name, type, permission) name = parameter name type = string specifying the parameter type permission = exposed in sysfs if different than 0 (file permission) Supported variables types: bool: boolean charp: char * (chaîne de caractères) short: short ushort: unsigned short int: integer uint: unsigned integer long: long ulong: unsigned long Macro for importing tables: module_param_array(name, type, var pointer, permission) 30

Usefull References The Linux Kernel Module Programming Guide http://tldp.org/ldp/lkmpg/ 31

Kernel Debugging Or how to remove bugs in your kernel developments 32

Print Debugging Universal technique to debug used since the beginning of programming Printing message at key points in your source code For user programs: printf But in kernel space: printk Printed or not in the console or in /var/log/messages Depend on priority linux/kernel.h (see next slide) More the printing level is low, more the priority is high Strings "<[0-7]>" concatenated to compilation messages If there is no specified level, DEFAULT_MESSAGE_LOGLEVEL Else, levels defined in kernel/printk.c 33

Logging Levels # define KERN_EMERG "<0>" Urgent messages just before an unstable system # define KERN_ALERT "<1>" System needing an immediate action # define KERN_CRIT "<2>" Critical situation (software or hardware failure) # define KERN_ERR "<3>" Error situation, hardware problem # define KERN_WARNING "<4>" Problematic situation (not a big problem for the system) # define KERN_NOTICE "<5>" Normal situation but significant # define KERN_INFO "<6>" Information message (for example, name assigned to hardware) # define KERN_DEBUG "<7>" Debugging message 34

Oops We the kernel detects a problem (illegal operation) Kernel panic message: Oops message Kill the process that made the system fault Even if the system looks like to correctly, side effects might appear due to the stop of the process which generated the problem A Oops contains A textual description A Oops number The CPU number The CPU registers The stack Instructions that CPU executed before crash Do not contain any symbols, just addresses 35

Example of Kernel Oops 36

Recensement des Oops Web site dedicated to Oops references for each kernel version http://www.kerneloops.org/ Collected from: Messages from discussion lists as linux-kernel, bugzilla.kernel.org Software downloadable from web site Noyau Les 5 principaux Représentation problèmes Visuelle générant des Oops Nombre de Oops 2.6.32 rc 309 1. suspend_test_finish(resume 2 877 devices) 2.6.31 release 19 765 2. i915_gem_set_tiling 2.6.30 release 2 472 Failure in the tiling code 72 056 2.6.29 release 200 086 3. mtrr_trim_uncached_memory 1 706 BIOS bug (often in VMWare) where the 2.6.28 release MTRR's are set up incorrectly or 30 not 348at all 4. dquot_claim_space 2.6.27 release 665 Interaction between EXT4 and 232 the 292 quota code 2.6.26 release 163 374 5. dev_watchdog 597 2.6.25 release 79 039 37

ksymoops Help to decrypt Oops messages Convert addresses and code useful information Easy to use Copy/Paste the Oops text in a file Need the following information System.map of the kernel you use (and which had a problem) List of modules (by default uses /proc/modules) List of kernel symbols (/proc/ksyms) A copy of the kernel image With that, you get human readable addresses EIP; c88cd018 <[kbdb] memp_fget+158/b54> Example of command line ksymoops no-ksyms m System.map v vmlinuz oops.txt See Documentation/oops-tracing.txt and man ksymoops 38