Device Drivers. CS449 Fall 2017

Similar documents
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

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

Kernel Modules. Kartik Gopalan

Linux Kernel Modules & Device Drivers April 9, 2012

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

Linux Device Drivers

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

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

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

Linux drivers - Exercise

Finish up OS topics Group plans

Unix (Linux) Device Drivers

Linux Kernel Module Programming. Tushar B. Kute,

System Calls & Signals. CS449 Spring 2016

File and Console I/O. CS449 Spring 2016

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

File and Console I/O. CS449 Fall 2017

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

CS 378 (Spring 2003)

ASE++ : Linux Kernel Programming

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

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

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

Fork() System Call and Processes. CS449 Fall 2017

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

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

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

BLOCK DEVICES. Philipp Dollst

7.4 Simple example of Linux drivers

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Designing and developing device drivers. Coding drivers

CPSC Tutorial 17

Operating systems for embedded systems

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

7.3 Simplest module for embedded Linux drivers

NPTEL Course Jan K. Gopinath Indian Institute of Science

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

Operating systems for embedded systems. Embedded Operating Systems

Sandboxing. CS-576 Systems Security Instructor: Georgios Portokalidis Spring 2018

Advanced Operating Systems #13

CSE 333 SECTION 3. POSIX I/O Functions

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

Loadable Kernel Module

Make Your Own Linux Module CS 444/544

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

MP3: VIRTUAL MEMORY PAGE FAULT MEASUREMENT

Character Device Drivers

Distribution Kernel Security Hardening with ftrace

Introduction Reading Writing scull. Linux Device Drivers - char driver

Linux Kernel Development (LKD)

Operating Systems. V. Input / Output

Linux Device Driver. Analog/Digital Signal Interfacing

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

SOLUTIONS TO THE FIRST 3360/6310 QUIZ. Jehan-François Pâris Summer 2016

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

Introduction to Operating Systems. Device Drivers. John Franco. Dept. of Electrical Engineering and Computing Systems University of Cincinnati

Introduction. Yolanda Becerra Fontal Juan José Costa Prats

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

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!

1 #include <linux/module.h> 2 #include <linux/sched.h> 3 #include <linux/interrupt.h> 4 #include <linux/init.h> 5 #include <linux/kernel.

Operating System Concepts Ch. 11: File System Implementation

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

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

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

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.

ADVANCED OPERATING SYSTEMS

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

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

CS510 Operating System Foundations. Jonathan Walpole

Design and Implementation of an Asymmetric Multiprocessor Environment Within an SMP System. Roberto Mijat, ARM Santa Clara, October 2, 2007

Embedded Linux. Session 4: Introduction to the GPIO Kernel API. Martin Aebersold BFH-TI Dipl. El.-Ing. FH.

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Lecture on Storage Systems

UNIX input and output

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CS333 Intro to Operating Systems. Jonathan Walpole

Linux Kernel Development (LKD)

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

Operating System Labs. Yuanbin Wu

CSE 333 SECTION 3. POSIX I/O Functions

Linux Loadable Kernel Modules (LKM)

COS 318: Operating Systems

Final Exam. Fall Semester 2016 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

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

Loadable Kernel Modules

CPSC 8220 FINAL EXAM, SPRING 2016

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Quiz I

Processes COMPSCI 386

Linux Device Drivers Interrupt Requests

CS2028 -UNIX INTERNALS

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

Motivation. Operating Systems. File Systems. Outline. Files: The User s Point of View. File System Concepts. Solution? Files!

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Processes. Dr. Yingwu Zhu

pthreads CS449 Fall 2017

Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

ECE 471 Embedded Systems Lecture 12

Transcription:

Device Drivers CS449 Fall 2017

Software Layers User-level I/O so7ware & libraries Device-independent OS so7ware Device drivers Interrupt handlers User OperaEng system (kernel) Hardware

Device Drivers User space User program Kernel space Rest of the OS Keyboard driver Disk driver Keyboard controller Disk controller

Device Drivers in Linux Can be compiled into the kernel Using staec linking just like any binary Can be loaded dynamically as Modules Using dynamic linking just like link loader Except Modules are linked to kernel (not applicaeons) Modules are loaded into kernel space (not user space)

Device Drivers as Plug-in Modules

Hello World Module #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { } printk(kern_alert "Hello, world\n"); return 0; static void hello_exit(void) { } printk(kern_alert "Goodbye, cruel world\n"); module_init(hello_init); //macro to specify init function module_exit(hello_exit); //macro to specify exit function

Why printk? Modules do not have access to user libraries Modules are linked to the kernel Kernel executes by itself w/o relying on user libraries Can t use prinu and many other standard funceons provided by C library (libc.so) Printk is a kernel funceon similar to prinu But implemented very differently (w/o write system call) Kernel provides own versions for other common funceons like strcpy, strcat, etc.

Building & Running % make make[1]: Entering directory `/usr/src/linux-2.6.10' % su CC [M] /home/ldd3/src/misc-modules/hello.o Building modules, stage 2. [sic] root# insmod./hello.ko Hello, world root# rmmod hello Goodbye cruel world root# insmod/rmmod calls module init / exit funceons Need root privileges to insmod/rmmod since you are modifying kernel

Module Helper Programs insmod loads a module to memory Dynamically links a module to kernel Calls module init funceon to iniealize device rmmod unloads a module from memory Calls module exit funceon to perform clean up lsmod lists what modules are loaded Need root privileges since kernel is modified

Types of Devices in Linux Block Devices Device that can host a file system by storing data in randomly accessible fixed-sized blocks E.g. Disks, Flash Drives Character Devices Device that delivers/accepts a stream of characters E.g. Keyboard, mouse, terminal

Devfs and Device Files Devfs (mounted on /dev/) contains device files: thoth $ ls -l /proc/self/fd total 0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:37 0 -> /dev/pts/0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:37 1 -> /dev/pts/0 lrwx------ 1 wahn UNKNOWN1 64 Sep 11 13:37 2 -> /dev/pts/0 lr-x------ 1 wahn UNKNOWN1 64 Sep 11 13:37 3 -> /proc/16970/fd thoth $ ls -l /dev/pts/0 crw------- 1 wahn tty 136, 7 Sep 11 11:37 /dev/pts/0 All read/write system calls to stdin, stdout, stderr routed to pseudo terminal device file /dev/pts/0 The c in crw in /dev/pts/0 permissions means it is a character device. If b, block device.

Sysfs and Configuration Files Sysfs (mounted on /sys/) contains configuraeon files thoth $ cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq 1999998 thoth $ cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq 249999 /sys/devices/system/cpu: all CPUs available to system Shows CPU 0 can operate between max frequency 1999998 KHz (2 GHz) and min frequency 249999 KHz (250 MHz)

How Device Drivers Interface with OS A well-defined interface for each type. E.g. Block devices have an interface suitable for storage media Char devices have an interface suitable for stream-oriented devices This interface consists of two parts: FuncEons that the device driver implements (that the OS can call) FuncEons that the OS implements (that the device driver can call) All device types have interfaces to: Register device: OS calls device driver init funceon on insmod Unregister device: OS calls device driver exit funceon on rmmod Service OS requests made in response to user system calls Service interrupts coming from HW device

Block Device Driver

Block Device Init Function static void disk_request(struct request_queue *q){ } static struct block_device_operations disk_fops = { }; static int init disk_init(void) { /* 1. Ask kernel to create queue for disk_request */ struct request_queue *disk_queue = blk_init_queue(disk_request, &lock); /* 2. Ask kernel to allocate disk structure */ struct gendisk *disk = alloc_disk(disk_minors); /* 3. Attach block device operations to disk */ disk->fops = &disk_fops; /* 4. Attach request queue to disk */ disk->queue = disk_queue; /* 5. Make disk available to the system */ add_disk(disk); } Red: Interface implemented by device driver Blue: Interface implemented by OS

disk_fops struct struct block_device_operations { struct module *owner; int (*open) (struct inode*, struct file*); int (*ioctl)(struct inode*, struct file*, unsigned int, unsigned long); int (*release)(struct inode*, struct file*);... }; Contains funceon pointers passed to OS when driver is loaded OS calls funceons on corresponding system call Note: no entries for read and write system calls Handled by disk_request () associated with the request queue

disk_request() function static void disk_request(struct request_queue *q); OS calls disk_request() a7er enqueuing a batch of read/ write requests from a series of read/write system calls Device driver dequeues requests from q and services them Performance of many storage media is sensieve to order of processing requests (e.g. amount of arm movement in a hard disk) Servicing batches of requests allows device driver to reorder them to best suit the characterisecs of device

Character Device Operations struct file_operations { struct module *owner; int (*open) (struct inode*, struct file*); ssize_t (*read) (struct file*, char*, size_t, loff_t*); ssize_t (*write) (struct file*, const char*, size_t, loff_t*); int (*release)(struct inode*, struct file*);... }; Note the presence of read and write system calls Read/write system calls forwarded directly to driver (No need to batch together requests as in block devices) Different Device classes (block, char, ) have different interfaces

User Space Drivers Drivers where most of the code is run in user space Small kernel module forwards all requests to a user-level driver program that handles the requests For drivers that rarely (never) need to interact with HW Example: FUSE (Filesystem in User Space) Used for implemeneng virtual file systems (File systems that do not actually store data themselves) Data is sent to another (remote) storage system Cloud storage, FTP server, Email server, etc.

FUSE hello: User-level file system program using libfuse FUSE kernel module forwards all requests to hello

User Space Drivers Advantages: Beoer programming environment Full Standard C Library can be linked in Can use a conveneonal debugger like GDB Problems with driver will not crash OS Disadvantages: Less efficient HW interaceon Needs to go through user/kernel boundary every Eme Data obtained from device must be copied from kernel space to user space, just to make it available to driver

Pitfall 1: Resource Leaks int init my_init_function(void) { int err; /* registration takes a pointer and a name */ err = register_this(ptr1, driver ); if (err) goto fail_this; err = register_that(ptr2, driver ); if (err) goto fail_that; err = register_those(ptr3, driver ); if (err) goto fail_those; return 0; /* success */ fail_those: unregister_that(ptr2, driver ); fail_that: unregister_this(ptr1, driver ); fail_this: return err; /* propagate the error */ } Always remember to clean up state before returning on error!

Pitfall 2: Race Conditions Race: situaeon where two tasks race to complete resuleng in an illegal interleaving of operaeons MulEple CPUs may access device driver simultaneously E.g. CPU 1 reads from device, CPU 2 writes to device E.g. CPU 1 reads from device, CPU 2 handles an interrupt SoluEon: use synchronizaeon to disallow interleaving We will learn more about synchronizaeon soon