Linux Kernel Modules & Device Drivers April 9, 2012

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

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

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

Linux Kernel Module Programming. Tushar B. Kute,

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

Device Drivers. CS449 Fall 2017

Kernel Modules. Kartik Gopalan

Linux drivers - Exercise

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

Linux Device Drivers

Linux Device Driver. Analog/Digital Signal Interfacing

/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

Finish up OS topics Group plans

Loadable Kernel Modules

CPSC Tutorial 17

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

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

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

7.3 Simplest module for embedded Linux drivers

ASE++ : Linux Kernel Programming

Linux Device Driver in Action (LDDiA)

Workspace for '5-linux' Page 1 (row 1, column 1)

Linux Kernel Development (LKD)

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Unix (Linux) Device Drivers

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

Linux Device Drivers. 3. Char Drivers. 1. Introduction 2. Kernel Modules 3. Char Drivers 4. Advanced Char Drivers 5. Interrupts

Make Your Own Linux Module CS 444/544

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

Linux Loadable Kernel Modules (LKM)

MP3: VIRTUAL MEMORY PAGE FAULT MEASUREMENT

Loadable Kernel Module

Files. Eric McCreath

The Embedded I/O Company TIP700-SW-82 Linux Device Driver User Manual TEWS TECHNOLOGIES GmbH TEWS TECHNOLOGIES LLC

TIP675-SW-82. Linux Device Driver. 48 TTL I/O Lines with Interrupts Version 1.2.x. User Manual. Issue November 2013

- Knowledge of basic computer architecture and organization, ECE 445

BLOCK DEVICES. Philipp Dollst

SuSE Labs / Novell.

Linux kernel. Tutorials to discover how to do some stuff on linux kernel. If you have any suggestions or if you find any problem, please report it!

Linux Kernel Development (LKD)

First order of Business

What is a Linux Device Driver? Kevin Dankwardt, Ph.D. VP Technology Open Source Careers

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

Developing Real-Time Applications

CS 378 (Spring 2003)

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

Interrupt handling. Interrupt handling. Deferred work. Interrupt handling. Remove an interrupt handler. 1. Link a struct work to a function

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

CS 453: Operating Systems Programming Project 5 (100 points) Linux Device Driver

TPMC860-SW-82. Linux Device Driver. 4 Channel Isolated Serial Interface RS232 Version 1.4.x. User Manual. Issue 1.4.

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

CARRIER-SW-82. Linux Device Driver. IPAC Carrier Version 2.2.x. User Manual. Issue November 2017

USB. Development of a USB device driver working on Linux and Control Interface. Takeshi Fukutani, Shoji Kodani and Tomokazu Takahashi

Android 多核心嵌入式多媒體系統設計與實作

Virtual File System. Don Porter CSE 306

Itron Riva Kernel Module Building

Assignment II Linux Dictionary Device. Beycan KAHRAMAN Turgut UYAR

FAME Operatinf Systems - Modules

To understand this, let's build a layered model from the bottom up. Layers include: device driver filesystem file

Laboratory Assignment #3. Extending scull, a char pseudo-device

Operating systems for embedded systems. Embedded Operating Systems

UNIX Kernel. UNIX History

Assignment 4 cpe 453 Fall 2018

Simple char driver. for Linux. my_first.c: headers. my_first.c: file structure. Calcolatori Elettronici e Sistemi Operativi.

CS 134. Operating Systems. April 8, 2013 Lecture 20. Input/Output. Instructor: Neil Rhodes. Monday, April 7, 14

Advanced Operating Systems #13

Operating systems for embedded systems

Designing and developing device drivers. Coding drivers

Introduction Reading Writing scull. Linux Device Drivers - char driver

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

NPTEL Course Jan K. Gopinath Indian Institute of Science

NPTEL Course Jan K. Gopinath Indian Institute of Science

TPMC917-SW-82. Linux Device Driver. User Manual. The Embedded I/O Company. 4 MB SRAM with Battery Backup and 4 Channel Serial Interface Version 1.0.

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

7.4 Simple example of Linux drivers

Laboratory Assignment #3. Extending scull, a char pseudo-device. Summary: Objectives: Tasks:

Outline. Background. Embedded Linux (common but not required attributes) Legal issues, versioning, building, user basis, FHS, booting

OPTO32A 24 Input Bit, 8 Output Bit Optical Isolator Board

Inter-Process Communication

Logical disks. Bach 2.2.1

Table of Contents. Preface... xi

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

Lecture on Storage Systems

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

Using the Kernel Security Module Interface

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

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Working with Kernel Modules Lab Wind River Systems, Inc

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

we are here I/O & Storage Layers Recall: C Low level I/O Recall: C Low Level Operations CS162 Operating Systems and Systems Programming Lecture 18

2 Setting up the RDMA Framework for Development

TPMC680-SW-82. Linux Device Driver. 64 Digital Input/Output Version 1.1.x. User Manual. Issue April 2010

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

3 Character Drivers. 3.1 The Design of scullc. Linux Device Drivers in Assembly

File Systems: Consistency Issues

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

TDRV011-SW-82. Linux Device Driver. Extended CAN Bus Version 1.0.x. User Manual. Issue April 2018

What is an Operating System? A Whirlwind Tour of Operating Systems. How did OS evolve? How did OS evolve?

Linux Device Drivers. 3. Char Drivers cont. 3. Char Drivers. 1. Introduction 2. Kernel Modules 3. Char Drivers 4. Advanced Char Drivers 5.

Transcription:

Linux Kernel Modules & Device Drivers April 9, 2012 Pacific University 1

Resources Linux Device Drivers,3rd Edition, Corbet, Rubini, Kroah- Hartman; O'Reilly kernel 2.6.10 we will use 3.1.9 The current kernel APIs are different. http://lwn.net/kernel/ldd3/ Creative Commons Attribution-ShareAlike 2.0 license Chapters 1, 2, 3, 5, 18 Linker & Libraries Guide (Everything you ever wanted to know about ELF but never asked) http://docs.oracle.com/cd/e19963-01/html/819-0690/index.html http://docs.oracle.com/cd/e19963-01/pdf/819-0690.pdf http://kernelnewbies.org/kernelglossary http://kernel.org/ http://tldp.org/ldp/lkmpg/2.6/html/lkmpg.html Pacific University 2

Linux Loadable Modules Device Drivers character devices block devices network devices Looks like a file. You supply the implementation for each of the file operations that could be called on the device. Pacific University 3

Security Device Drivers run in the Kernel! Buffer Overflow http://faculty.ycp.edu/~dhovemey/fall2009/cs340/lecture/lecture23.html Pacific University 4

User Space Device Driver http://www.kernel.org/doc/htmldocs/uio-howto.html Pacific University 5

Implementation In the Kernel no libc Kernel Headers Pacific University 6

Structure Init/exit functions (loadable module) Register License Pacific University 7

Example http://lwn.net/images/pdf/ldd3/ch02.pdf http://lxr.linux.no/linux+v3.1.9/include/linux/module.h#l114 http://lxr.linux.no/linux+v3.1.9/include/linux/printk.h #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int init hello_init(void) { printk(kern_alert "Hello, world\n"); return 0; } static void exit hello_exit(void) { printk(kern_alert "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit); Pacific University 8

Makefile http://www.gnu.org/software/make/manual/make.html#toc_running obj-m += kbleds.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 -C change to directory before doing anything else M= creates a variable for use in the Makefile make -C /lib/modules/3.1.9-2-arch/build M=/home/cs460/kbleds modules ifeq ("$(origin M)", "command line") KBUILD_EXTMOD := $(M) endif http://www.gnu.org/software/make/manual/make.html#origin-function Pacific University 9

ELF! module.ko http://www.ibm.com/developerworks/linux/library/l-lkm/ Pacific University 10

Concurrency Must be threadsafe Functionality available as soon as registered races in init errors in init Pacific University 11

Linux Commands insmod modprobe rmmod lsmod modinfo Pacific University 12

Loading a Module http://www.ibm.com/developerworks/linux/library/l-lkm/ http://lxr.linux.no/linux+v3.1.9/kernel/module.c#l2803 http://lxr.linux.no/linux+v3.1.9/kernel/module.c#l2949 sys_init_module(mod_name, args) 1) /* Permissions Checks */ 2) mod = load_module (mod_name, args) 3) /* Add module to linked list */ 4) /* Call module notify list with state change */ 5) /* call the module's init function */ mod->init() 6) mod->state = MODULE_STATE_LIVE 7) return load_module(mod_name, args) 1) Allocate temp memory, read ELF 2) Sanity checks (bad object, arch, ) 3) Map ELF section headers to variables 4) Read in optional module args 5) mod->state = MODULE_STATE_COMING 6) Allocate Per CPU sections 7) Allocate final module memory 8) Move SHF_ALLOC sections from temp to final module memory 9) Fixup symbols and perform relocations 10) Flush instruction cache 11) Clean up (free temp memory) and return module Pacific University 13

Character Devices Read/write character (byte) streams ls -al /dev Look for c (character) or b (block) Major/minor number cat /proc/devices mknod /dev/x {c,b} Major# Minor# Pacific University 14

file_operations linux/fs.h http://lxr.linux.no/linux+v3.1.9/include/linux/fs.h#l1563 Function pointers to implementation for this Device Driver open release read write lseek mmap flush and more... Pacific University 15

Input/output control ioctl http://lxr.linux.no/linux+v3.1.9/include/asm-generic/ioctl.h Device specific system call man ioctl // POSIX #include <stropts.h> int ioctl(int fildes, int request,... /* arg */); #include <sys/ioctl.h> int ioctl(int d, int request,...); Pacific University 16

struct file linux/fs.h open file http://lxr.linux.no/linux+v3.1.9/include/linux/fs.h#l953 Pacific University 17

struct inode linux/fs.h http://lxr.linux.no/linux+v3.1.9/include/linux/fs.h#l748 ls -i stat data.txt a file many struct file per one inode http://www.ibm.com/developerworks/aix/library/au-speakingunix14/ Pacific University 18

Kernel Register your Device driver with the kernel Pacific University 19

Concurrency Primatives mutex/semphore (up, down_interruptable) Pacific University 20

struct timer_list http://lxr.linux.no/linux+v3.1.9/include/linux/timer.h#l12 init_timer add_timer del_timer http://lwn.net/images/pdf/ldd3/ch07.pdf Pacific University 21

struct tty_driver http://lxr.linux.no/linux+v3.1.9/include/linux/tty_driver.h http://lxr.linux.no/linux+v3.1.9/drivers/tty/vt/vt_ioctl.c tty: teletype connected to a console now a virtual console (vc) http://lwn.net/images/pdf/ldd3/ch18.pdf http://www.linusakesson.net/programming/tty/index.php Pacific University 22

Wednesday Run ArchLinux inside of VirtualBox http://archlinux.org/ https://wiki.archlinux.org/index.php/main_page You will have root access We will reuse this VirtualBox machine for a series of labs ArchLinux: a little more hacker-ish than OpenSUSE great for learning all the behinds the scene internals Pacific University 23