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

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

Device Drivers. CS449 Fall 2017

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 Kernel Module Programming. Tushar B. Kute,

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

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

Linux Device Drivers

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

Loadable Kernel Modules

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

Developing Real-Time Applications

Linux drivers - Exercise

7.3 Simplest module for embedded Linux drivers

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

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

Finish up OS topics Group plans

Advanced Operating Systems #13

CPSC Tutorial 17

ASE++ : Linux Kernel Programming

Itron Riva Kernel Module Building

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

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

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

OPERATING SYSTEM OVERVIEW

Kernel Module Programming

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!

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

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

CIS c. University of Pennsylvania Zachary Goldberg. Notes

Linux Kernel Development (LKD)

Operating Systems. Review ENCE 360

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

Make Your Own Linux Module CS 444/544

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

ECE 598 Advanced Operating Systems Lecture 19

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

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

UNIX input and output

CSE 451: Operating Systems. Sec$on 2 Interrupts, system calls, and project 1

Unix (Linux) Device Drivers

OPERATING SYSTEMS ASSIGNMENT 4 FILE SYSTEMS

esi-risc Development Suite Getting Started Guide

Operating Systems. Steven Hand. Michaelmas / Lent Term 2008/ lectures for CST IA. Handout 4. Operating Systems

SuSE Labs / Novell.

CS 322 Operating Systems Practice Midterm Questions

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

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

Visual Studio.NET.NET Framework. Web Services Web Forms Windows Forms. Data and XML classes. Framework Base Classes. Common Language Runtime

C Refresher, Advance C, Coding Standard, Misra C Compliance & Real-time Programming

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

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

Embedded Linux kernel and driver development training 5-day session

REVISION HISTORY NUMBER DATE DESCRIPTION NAME

Operating System Labs. Yuanbin Wu

Lecture 19: File System Implementation. Mythili Vutukuru IIT Bombay

Processes. Johan Montelius KTH

CIT 480: Securing Computer Systems. Operating System Concepts

A process. the stack

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College March 31, 2015

MP3: VIRTUAL MEMORY PAGE FAULT MEASUREMENT

Introduction. CS3026 Operating Systems Lecture 01

Introduction to Linux

CompTIA Linux+/LPIC-1 COPYRIGHTED MATERIAL

5 Lecture: Intro. to Concurrency

Outline. 1) Introduction to Linux Kernel 2) How system calls work 3) Kernel-space programming 4) Networking in kernel 2/34

Alexander Warg. Complex Lab Operating System 2007 Winter Term. Introduction

Operating Systems. Key Questions in System Design. Review: What is An Operating System? Key Points. The OS provides an Extended Machine

Creating a Shell or Command Interperter Program CSCI411 Lab

Introduction of Linux

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Control Abstraction. Hwansoo Han

The Kernel Abstraction. Chapter 2 OSPP Part I

OS Security. Authorization. Radboud University Nijmegen, The Netherlands. Winter 2015/2016

Segmentation with Paging. Review. Segmentation with Page (MULTICS) Segmentation with Page (MULTICS) Segmentation with Page (MULTICS)

Ausgewählte Betriebssysteme - Mark Russinovich & David Solomon (used with permission of authors)

Virtual Memory Paging

Power Aware Operating Systems: Task-Specific CPU Throttling

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College November 8, 2018

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

Hardware OS & OS- Application interface

Interrupts & System Calls

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

Processes. Dr. Yingwu Zhu

File System Case Studies. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Linux Loadable Kernel Modules (LKM)

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

Linux Device Driver. Analog/Digital Signal Interfacing

Outline. Classic races: files in /tmp. Race conditions. TOCTTOU example. TOCTTOU gaps. Vulnerabilities in OS interaction

Table of Contents. Preface... xi

Chapter 4. File Systems. Part 1

Introduction to Supercomputing

COMP 3430 Robert Guderian

ADVANCED OPERATING SYSTEMS

3. Process Management in xv6

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

Process Control. Philipp Koehn. 23 April 2018

Subverting the Linux Kernel Linux Kernel Rootkits 101

Operating System Structure

Transcription:

Software Layers Device Drivers User-level I/O software & libraries Device-independent OS software Device drivers Interrupt handlers Hardware User Operating system (kernel) Abstraction via the OS Device Drivers User space User program Kernel space Rest of the OS Keyboard driver Disk driver Keyboard controller Disk controller 1

I/O System Types of Devices User Process User Process User Process OS File System I/O System Device Driver Device Controller Disk Block Devices A device that stores data in fixed-sized blocks, each uniquely addressed, and can be randomly accessed E.g., Disks, Flash Drives Character Devices Device that delivers or accepts a stream of characters E.g., Keyboard, mouse, terminal Mechanism vs. Policy Mechanism What capabilities to have Policy How to use a mechanism Drivers should be flexible by only providing mechanisms not policies Policies should generally be pushed up as much as possible Device Drivers in Linux Can be compiled into the kernel or dynamically loaded as a module Allow access to exported module interface Kernel level API Allows access to common kernel functions E.g. Printing to the system log Allows modules to connect to kernel subsystems Register handlers for a specific device Add a new file system Allows modules to export interfaces to user space 2

File Operations Opening Files Create file with given pathname /a/b/file Traverse pathname, allocate meta-data and directory entry Read-from/write-to offset in file Find (or allocate) blocks on disk, update meta-data Delete Remove directory entry, free disk blocks Rename Change directory entry Copy Allocate new directory entry, allocate new space on disk, copy Change permissions Change directory entry, or ACL meta-data Files must be opened before accessing data User must specify access mode: read and/or write OS tracks open files for each process Table containing open file s metadata How do you know which table entry belongs to a file? File descriptor = index into open file table Table contains meta-data + current position and access mode UNIX file operations File operations are implemented inside the kernel Kernel maps operations to the specific file in question Data flows from process memory to/from files on disk But now we have a better picture /dev Character and block devices can be exposed via a filesystem /dev/ typically contains files that represent the different devices on a system Data path: process kernel disk Kernel can intercept data before it reaches the disk Easy mechanism for copying to/from kernel space (device drivers) /dev/console the console /dev/fd/ a process s open file descriptors /dev/hda first hard disk 3

ProcFS and Sysfs ProcFS -- /proc Virtual filesystem with information about the system and running programs e.g /proc/cpuinfo text file containing CPU information Sysfs -- /sys Exports information about devices and drivers to userspace, Can configure aspects of device 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); module_exit(hello_exit); Build and Run Module Helper Programs % make make[1]: Entering directory `/usr/src/linux-2.6.10' CC [M] /home/ldd3/src/misc-modules/hello.o Building modules, stage 2. MODPOST CC /home/ldd3/src/misc-modules/hello.mod.o LD [M] /home/ldd3/src/misc-modules/hello.ko make[1]: Leaving directory `/usr/src/linux-2.6.10' % su root# insmod./hello.ko Hello, world root# rmmod hello Goodbye cruel world root# insmod loads a module rmmod unloads a module lsmod lists what modules are loaded modprobe loads a module checking dependencies Modprobe -r -- removes a module and dependencies 4

Method of operation Device drivers respond to requests They are part of the kernel Interrupts from hardware devices Requests from user space Loading a Module During module initialization you register for certain events Interrupts from a specific device File operations on a specific file system The Kernel s main responsibility is to route events to the correct device drivers Module linking The module calls the kernel level API The kernel calls the module init function There must be a linking step Kernel must be able to find init() Module must be able to find kernel API functions When a module is loaded the kernel performs a linking operation on the module Similar to user level linking (ELF format) Error Handling 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 */ 5

Race Conditions The kernel will make calls into your module while your initialization function is still running Why printk? The kernel does not have access to libraries Can t use printf or many other standard functions (FILE stuff, strtok, etc.) Modules are linked against the kernel only Kernel provides useful set of common functions like strcpy, strcat, etc. Things you can t do in the kernel Stack allocate big arrays The stack is small, maybe only a single page (4KB) Use kmalloc to allocate heap space Driver Stacking Floating point arithmetic Context switch into the kernel does not save floating point registers 6

Module Parameters Permissions bool, invbool (int), charp int, long, short, int, ulong, ushort Array parameters, where the values are supplied as a commaseparated list: module_param_array(name,type,num,perm); insmod hellop howmany=10 whom= Mom static char *whom = "world"; static int howmany = 1; module_param(howmany, int, S_IRUGO); module_param(whom, charp, S_IRUGO); Module parameters show up as files in the sysfs entry If perm is set to 0, there is no sysfs entry at all S_IRUGO Read Only S_IRUGO S_IWUSR Writeable by root Makefile obj-m := hello_dev.o KERN_SRC := /u/syslab/shared/linux-2.6.23.1 PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) M=$(PWD) modules 7