Sleeping and waking An introduction to character-mode device-driver modules for Linux.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
Another device-driver? Getting ready to program the network interface.
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
Creating a device-file node An introduction to some privileged Linux system-calls (needed for an upcoming programming exercise)
EEE 435 Principles of Operating Systems Principles and Structure of I/O Software (Modern Operating Systems 5.2 & 5.3) 5/22/20151Dr Alain Beaulieu.
The ‘system-call’ interface We see how an application program can invoke privileged kernel services.
UNIX’s “grand illusion” How Linux makes a hardware device appear to be a ‘file’
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
04/14/2008CSCI 315 Operating Systems Design1 I/O Systems Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Character Driver Issues Implementing ‘/dev/physmem’
SiS 315 Graphics Engine Introduction to some capabilities of graphics accelerator hardware.
Standard C Libraries Application Programmming Interface to System-Calls.
A ‘ringbuffer’ application Introduction to process ‘blocking’ and the Linux kernel’s support for ‘sleeping’ and ‘waking’
Standard C Library Application Programming Interface to System-Calls.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Embedded Real-time Systems The Linux kernel. The Operating System Kernel Resident in memory, privileged mode System calls offer general purpose services.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
Device Management.
Linux Device Drivers Inroduction. What’s a ‘device-driver’? A special kind of computer program Intended to control a peripheral device Needs to execute.
1 Today I/O Systems Storage. 2 I/O Devices Many different kinds of I/O devices Software that controls them: device drivers.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Systems Software Operating Systems.
An Introduction to Operating Systems. Definition  An Operating System, or OS, is low-level software that enables a user and higher-level application.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Operating System Program 5 I/O System DMA Device Driver.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
System Calls 1.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
1 I/O Management and Disk Scheduling Chapter Categories of I/O Devices Human readable Used to communicate with the user Printers Video display terminals.
Discussion Week 8 TA: Kyle Dewey. Overview Exams Interrupt priority Direct memory access (DMA) Different kinds of I/O calls Caching What I/O looks like.
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Hardware Definitions –Port: Point of connection –Bus: Interface Daisy Chain (A=>B=>…=>X) Shared Direct Device Access –Controller: Device Electronics –Registers:
Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze Unit OS6: Device Management 6.1. Principles of I/O.
OS provide a user-friendly environment and manage resources of the computer system. Operating systems manage: –Processes –Memory –Storage –I/O subsystem.
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
1 Comp 104: Operating Systems Concepts Devices. 2 Today Devices –Introduction –Handling I/O Device handling Buffering and caching.
Systems Software Operating Systems. What is software? Software is the term that we use for all the programs and data that we use with a computer system.
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
© Paradigm Publishing Inc. 4-1 OPERATING SYSTEMS.
UNIX Files File organization and a few primitives.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Chapter 13: I/O Systems. 13.2/34 Chapter 13: I/O Systems I/O Hardware Application I/O Interface Kernel I/O Subsystem Transforming I/O Requests to Hardware.
OS, , Part I Operating - System Structures Department of Computer Engineering, PSUWannarat Suntiamorntut.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 12: I/O Systems I/O hardwared Application I/O Interface Kernel I/O.
I/O Software CS 537 – Introduction to Operating Systems.
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Introduction to Operating Systems Concepts
Input/Output (I/O) Important OS function – control I/O
Introduction to Kernel
Chapter 13: I/O Systems Modified by Dr. Neerja Mhaskar for CS 3SH3.
Operating System Review
Linux Kernel Module Programming
Lesson Objectives Aims Key Words Interrupt, Buffer, Priority, Stack
CS 3305 System Calls Lecture 7.
Operating System Review
Lecture Topics: 11/1 General Operating System Concepts Processes
Chapter 13: I/O Systems “The two main jobs of a computer are I/O and [CPU] processing. In many cases, the main job is I/O, and the [CPU] processing is.
Presentation transcript:

Sleeping and waking An introduction to character-mode device-driver modules for Linux

What’s a ‘device-driver’? A special kind of computer program Intended to control a peripheral device Needs to execute ‘privileged’ instructions Must be integrated into the OS kernel Interfaces both to kernel and to hardware Program-format specific to a particular OS

Linux device-drivers A package mainly of ‘service functions’ The package is conceptually an ‘object’ But in C this means it’s a ‘struct’ Specifically:struct file_operations { …; }; Definition is found in a kernel-header: ‘/usr/src/linux/include/linux/fs.h’

Types of Device-Drivers Character drivers: - the device processes individual bytes (e.g., keyboard, printer, modem) Block drivers: - the device processes groups of bytes (e.g., hard disks, CD-ROM drives)

Linux has other driver-types Network drivers Mouse drivers SCSI drivers USB drivers Video drivers ‘Hot-swap’ drivers … and others

Developing a device-driver Clarify your requirements Devise a design to achieve them Test your design-concept (‘prototype’) ‘Debug’ your prototype (as needed) Build your final driver iteratively Document your work for future use

‘Open Source’ Hardware Some equipment manufactures regard their designs as ‘intellectual property’ They don’t want to ‘give away’ their info They believe ‘secrecy’ is an advantage They fear others might copy their designs BUT: This hinders systems programmers!

Non-Disclosure Agreements Sometimes manufacturers will let ‘trusted’ individuals, or commercial ‘partners’, look at their design-specs and manuals College professors often are ‘trusted’ BUT: Just to be sure, an NDA is required -- which prevents professors from teaching students the design-details that they learn

Some designs are ‘open’ The IBM-PC designs were published Then other companies copied them And those companies prospered! While IBM lost market-share! An unfortunate ‘lesson’ was learned

Advantage of ‘open’ designs Microsoft and Apple used to provide lots of technical information to programmers They wanted to encourage innovations that made their products more valuable Imagine hundreds of unpaid ‘volunteers’ creating applications for your platform! BUT: Were they ‘giving away the store’?

A ‘virtual device’ To avoid NDA hassles, we can work with a ‘pseudo’ device (i.e., no special hardware) We can use a portion of physical memory to hold some data that we ‘read’ or ‘write’ We refer to our pseudo-device as a ‘stash’ This allows us to illustrate the main issues that a simple device-driver will encounter

How system-calls work Application Program User-spaceKernel-space C Runtime Library Operating System Kernel Device Driver

How a ring buffer works data tail head where to put the next data-element where to get the next data-element

Linux treats devices as files Programmers accustomed to the file API open(), lseek(), read(), write(), close(),... Requires creating a filename in a directory (special ‘/dev’ directory is for devices)

Driver Identification Character/Block drivers: Use ‘major-number’ to identify the driver Use ‘minor-numbers’ to distinguish among several devices the same driver controls Kernel also needs a driver-name Users need a device-node as ‘interface’

Our module: ‘stash.c’ We can create a device-driver module for our ‘virtual’ device (we named it ‘stash’) It allows an application to save some data in a kernel-space buffer (a ‘ring’ buffer) by ‘writing’ to the device-file ‘/dev/stash’ Any application can retrieve this stashed data, by reading from this device-file It works like a FIFO (First In, First Out)

Creating our device node The ‘mknod’ command creates the node: $ mknod /dev/stash c 40 0 The ‘chmod’ command changes the node access-permissions (if that’s needed): $ chmod a+rw /dev/stash Both commands normally are ‘privileged’

Module ‘Boilerplate’ Must have ‘init_module()’ function (to ‘register’ service-functions with kernel) Must have ‘cleanup_module()’ function (to ‘unregister’ our service-functions)

More ‘boilerplate’ Must include certain kernel header-files (e.g., #include ) Must define certain compiler constants (e.g., #define __KERNEL__, MODULE) Alternatively these constants may be defined on the compiler’s command-line (using –D switch), and so be conveniently embedded in a Makefile

Important File I/O Functions int open( char *pathname, int flags ); int read( int fd, void *buf, size_t count ); int write( int fd, void *buf, size_t count ); loff_t lseek( int fd, loff_t off, int whence ); int close( int fd );

UNIX ‘man’ pages A convenient online guide to prototypes and semantics of the C Library Functions Example of usage: $ man 2 open

The ‘open’ function #include int open( const char *pathname, int flags ); Converts a pathname to a file-descriptor File-descriptor is a nonnegative integer Used as a file-ID in subsequent functions ‘flags’ is a symbolic constant: O_RDONLY, O_WRONLY, O_RDWR

The ‘close’ function #include int close( int fd ); Breaks link between file and file-descriptor Returns 0 on success, or -1 if an error

The ‘read’ function #include int read( int fd, void *buf, size_t count ); Attempts to read up to ‘count’ bytes Bytes are placed in ‘buf’ memory-buffer Returns the number of bytes read Or returns -1 if some error occurred Return-value 0 means ‘end-of-file’

The ‘write’ function #include int write( int fd, void *buf, size_t count ); Attempts to write up to ‘count’ bytes Bytes are taken from ‘buf’ memory-buffer Returns the number of bytes written Or returns -1 if some error occurred Return-value 0 means no data was written

The ‘lseek’ function #include loff_t lseek( int fd, loff_t off, int whence ); This function moves the file’s pointer Three ways to do the move: SEEK_SET: move from beginning position SEEK_CUR: move from current position SEEK_END: move from ending position (Could be used to determine a file’s size)

Default is ‘Blocking’ Mode The ‘read()’ function normally does not return 0 (unless ‘end-of-file’ is reached) The ‘write()’ function normally does not return 0 (unless there’s no more space) Instead, these functions ‘wait’ for data But ‘busy-waiting’ would waste CPU time, so the kernel will put the task to ‘sleep’ This means it won’t get scheduled again (until the kernel ‘wakes up’ this task)

How multitasking works Can be ‘cooperative’ or ‘preemptive’ ‘interrupted’ doesn’t mean ‘preempted’ ‘preempted’ implies a task was switched

Tasks have various ‘states’ A task may be ‘running’ A task may be ‘ready-to-run’ A task may be ‘blocked’

Kernel manages tasks Kernel uses ‘queues’ to manage tasks A queue of tasks that are ‘ready-to-run’ Other queues for tasks that are ‘blocked’

Special ‘wait’ queues Needed to avoid wasteful ‘busy waiting’ So Device-Drivers can put tasks to sleep And Drivers can ‘wake up’ sleeping tasks

How to use Linux wait-queues #include wait_queue_head_tmy_queue; init_waitqueue_head( &my_queue ); sleep_on( &my_queue ); wake_up( &my_queue ); But can’t unload driver if task stays asleep!

‘interruptible’ wait-queues Device-driver modules should use: interruptible_sleep_on( &my_queue ); wake_up_interruptible( &my_queue ); Then tasks can be awakened by ‘signals’

How ‘sleep’ works Our driver defines an instance of a kernel data-structure called a ‘wait queue head’ It will be the ‘anchor’ for a linked list of ‘task_struct’ objects It will initially be an empty-list If our driver wants to put a task to sleep, then its ‘task_struct’ will be taken off the runqueue and put onto our wait queue

How ‘wake up’ works If our driver detects that a task it had put to sleep (because no data-transfer could be done immediately) would now be allowed to proceed, it can execute a ‘wake up’ on its wait queue object All the task_struct objects that have been put onto that wait queue will be removed, and will be added to the CPU’s runqueue

Application to a ringbuffer A first-in first-out data-structure (FIFO) Uses a storage-array of finite length Uses two array-indices: ‘head’ and ‘tail’ Data is added at the current ‘tail’ position Data is removed from the ‘head’ position

Ringbuffer (continued) One array-position is always left unused Condition head == tail means “empty” Condition tail == head-1 means “full” Both ‘head’ and ‘tail’ will “wraparound” Calculation: next = ( next+1 )%RINGSIZE;

‘write’ algorithm for ‘stash.c’ while ( ringbuffer_is_full ) { interruptible_sleep_on( &wq ); If ( signal_pending( current ) ) return –EINTR; } Insert byte from user-space into ringbuffer; wake_up_interruptible( &wq ); return 1;

‘read’ algorithm for ‘stash.c’ while ( ringbuffer_is_empty ) { interruptible_sleep_on( &wq ); If ( signal_pending( current ) ) return –EINTR; } Remove byte from ringbuffer and store to user-space; wake_up_interruptible( &wq ); return 1;

The other driver-methods We can just omit definitions for other driver system-calls in this example (e.g., ‘open()’, ‘lseek()’, and ‘close()’) because suitable ‘default’ methods are available within the kernel for those cases in this example

Demonstration of ‘stash’ Quick demo: we can use I/O redirection For demonstrating ‘write’ to /dev/stash: $ echo “Hello” > /dev/stash For demonstrating ‘read’ from /dev/stash: $ cat /proc/stash

In-class exercise Can you modify the ‘stash.c’ example, to make it more efficient (fewer system calls), by arranging for its ‘read’ and ‘write’ to do larger-size data transfers (i.e., more than just one byte at a time)?