Linux Device Drivers & Project3 preview CSC345. Project 3 Preview Write a device driver for a pseudo stack device Idea from

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Linux device-driver issues
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Device Drivers cs423, Fall 2007 Klara Nahrstedt/Sam King 6/6/20141.
Drivers and the kernel1-1 Drivers and the kernel UNIX system has three layers: m The hardware m The operating system kernel m The user-level programs Kernel.
RT_FIFO, Device driver.
Sogang University Advanced Operating Systems (Linux Device Drivers) Advanced Operating Systems (Linux Device Drivers) Sang Gue Oh, Ph.D.
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 - /
Computer System Laboratory
Devices and Drivers (Chapter 7) Khattab Alhabashi UNIX System Administration.
USERSPACE I/O Reporter: R 張凱富.
Tutorial 3 - Linux Interrupt Handling -
Building and Running Modules Sarah Diesburg COP 5641.
Building and Running Modules Linux Kernel Programming CIS 4930/COP 5641.
Using VMX within Linux We explore the feasibility of executing ROM-BIOS code within the Linux x86_64 kernel.
Chap 2 System Structures.
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.
Embedded Systems Programming Writing Device Drivers.
Introduction to Kernel
I/O Hardware n Incredible variety of I/O devices n Common concepts: – Port – connection point to the computer – Bus (daisy chain or shared direct access)
Embedded System Programming Introduction to Device Drivers.
Embedded Real-time Systems The Linux kernel. The Operating System Kernel Resident in memory, privileged mode System calls offer general purpose services.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
M. Muztaba Fuad Advanced Operating System Project Device Drivers.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
Operating System Program 5 I/O System DMA Device Driver.
Linux Installation and Administration – Lesson 5 Tutor: George Papamarkos Topic: Devices in Linux.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
CSC 322 Operating Systems Concepts Lecture - 25: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Comp 104: Operating Systems Concepts Devices. 2 Today Devices –Introduction –Handling I/O Device handling Buffering and caching.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
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.
Linux Architecture Overview 1. Initialization Uboot – hardware init, loads kernel Kernel – remaining initialization, calls “init” Init – 1 st process,
UNIX Files File organization and a few primitives.
Manage Directories and Files in Linux. 2 Objectives Understand the Filesystem Hierarchy Standard (FHS) Identify File Types in the Linux System Change.
LINUX System : Lecture 7 Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 13: I/O Systems I/O Hardware Application I/O Interface Kernel I/O Subsystem.
UNIX Unit 1- Architecture of Unix - By Pratima.
Interfacing Device Drivers with the Kernel
Silberschatz, Galvin and Gagne  Operating System Concepts Six Step Process to Perform DMA Transfer.
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
4P13 Week 12 Talking Points Device Drivers 1.Auto-configuration and initialization routines 2.Routines for servicing I/O requests (the top half)
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
Device Driver Concepts Digital UNIX Internals II Device Driver Concepts Chapter 13.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
Silberschatz, Galvin and Gagne ©2011 Operating System Concepts Essentials – 8 th Edition Chapter 2: The Linux System Part 2.
MINIX Presented by: Clinton Morse, Joseph Paetz, Theresa Sullivan, and Angela Volk.
Introduction to Operating Systems Concepts
Introduction to Kernel
Linux Kernel Module Programming
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
Operation System Program 4
Chapter 2: The Linux System Part 2
I/O Systems I/O Hardware Application I/O Interface
CS703 - Advanced Operating Systems
Mid Term review CSC345.
CS 6560 Operating System Design
Linux Architecture Overview.
CS 6560 Operating System Design Kernel Loadable Modules
LINUX System : Lecture 7 Lecture notes acknowledgement : The design of UNIX Operating System.
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Computer System Laboratory
Loadable Kernel Modules
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:

Linux Device Drivers & Project3 preview CSC345

Project 3 Preview Write a device driver for a pseudo stack device Idea from Linux character device type supports the following operations –Open: only one is allowed. –Write: writes an char string to top of the device stack. Error if stack is empty –Read: reads an item from top of the device stack. Error if stack is empty –Release: release the device Install with LKM. Test: It will be a dedicated standalone machine in the lab. Root password may be given out. If you mess up, you will re-install the

What is a device driver? A programming module with interfaces –Communication Medium between application/user and hardware In Unix, –Kernel module – device driver interface = file interface –What are normal operations? –Block vs. character

User program & Kernel interface Note: This picture is excerpted from Write a Linux Hardware Device Driver, Andrew OShauqhnessy, Unix world

Loadable Kernel Module (LKM) A new kernel module can be added on the fly (while OS is still running) LKMs are often called kernel modules They are not user program

Types of LKM Device drivers Filesystem driver (one for ext2, MSDOS FAT16, 32, NFS) System calls Network Drivers TTY line disciplines. special terminal devices. Executable interpreters.

Basic LKM (program) Every LKM consist of two basic functions (minimum) : int init_module(void) /*used for all initialition stuff*/ {... } void cleanup_module(void) /*used for a clean shutdown*/ {... } Loading a module - normally retricted to root - is managed by issuing the follwing command: # insmod module.o

LKM Utilities cmd insmod –Insert an LKM into the kernel. rmmod –Remove an LKM from the kernel. depmod –Determine interdependencies between LKMs. kerneld –Kerneld daemon program ksyms –Display symbols that are exported by the kernel for use by new LKMs. lsmod –List currently loaded LKMs. modinfo –Display contents of.modinfo section in an LKM object file. modprobe –Insert or remove an LKM or set of LKMs intelligently. For example, if you must load A before loading B, Modprobe will automatically load A when you tell it to load B.

Common LKM util cmd Create a special device file % mknode /dev/driver c 40 0 Insert a new module % insmod modname Remove a module %rmmod modname List module % lsmod Or % more /proc/modules audio cmpci soundcore [audio cmpci] nfsd (autoclean)

Linux Device Drivers A set of API subroutines (typically system calls) interface to hardware Hide implementation and hardware- specific details from a user program Typically use a file interface metaphor Device is a special file

Linux Device Drivers (continued) Manage data flow between a user program and devices A self-contained component (add/remove from kernel) A user can access the device via file name in /dev, e.g. /dev/lp0

General implementation steps 1.Understand the device characteristic and supported commands. 2.Map device specific operations to unix file operation 3.Select the device name (user interface) –Namespace (2-3 characters, /dev/lp0) 4.(optional) select a major number and minor (a device special file creation) for VFS interface –Mapping the number to right device sub-routines 5.Implement file interface subroutines 6.Compile the device driver 7.Install the device driver module with loadable kernel module (LKM) 8. or Rebuild (compile) the kernel

Read/write (I/O) Pooling (or synchronous) Interrupt based

Device Driver interface Note: This picture is excerpted from Write a Linux Hardware Device Driver, Andrew OShauqhnessy, Unix world

VSF & Major number principal interface between a device driver and Linux kernel

File operation structure struct file_operations Fops = { NULL, /* seek */ xxx_read, xxx_write, NULL, /* readdir */ NULL, /* select */ NULL, /* ioctl */ NULL, /* mmap */ xxx_open, NULL, /* flush */ xxx_release /* a.k.a. close */ }; struct file_operations Fops = { read: xxx_read, write: xxx_write, open: xxx_open, release: xxx_release, /* a.k.a. close */ }; Watch out compatibility issue with Linux version

Device special file Device number –Major (used to VFS mapping to right functions) –Minor (sub-devices) mknod /dev/stk c 38 0 ls –l /dev/tty –crw-rw-rw- 1 root root 5, 0 Apr 21 18:33 /dev/tty

Register and unregister device int init_module(void) /*used for all initialition stuff*/ { /* Register the character device (atleast try) */ Major = register_chrdev(0, DEVICE_NAME, &Fops); : } void cleanup_module(void) /*used for a clean shutdown*/ {ret = unregister_chrdev(Major, DEVICE_NAME);... }

Register and unregister device compile -Wall -DMODULE -D__KERNEL__ -DLINUX –DDEBUG -I /usr/include/linux/version.h -I/lib/modules/`uname -r`/build/include Install the module %insmod module.o List the module %lsmod If you let the system pick Major number, you can find the major number (for special creation) by % more /proc/devices Make a special file % mknod /dev/device_name c major minor

Device Driver Types A character device driver ( c ) –Most devices are this type (e.g.Modem, lp, USB –No buffer. A block device driver (b) – through a system buffer that acts as a data cache. –Hard drive controller and HDs

Implementation Assuming that your device name is Xxx Xxx_init() initialize the device when OS is booted Xxx_open() open a device Xxx_read() read from kernel memory Xxx_write() write Xxx_release() clean-up (close) init_module() cleanup_module()

kernel functions add_timer() –Causes a function to be executed when a given amount of time has passed cli() –Prevents interrupts from being acknowledged end_request() –Called when a request has been satisfied or aborted free_irq() –Frees an IRQ previously acquired with request_irq() or irqaction() get_user*() –Allows a driver to access data in user space, a memory area distinct from the kernel inb(), inb_p() –Reads a byte from a port. Here, inb() goes as fast as it can, while inb_p() pauses before returning. irqaction() –Registers an interrupt like a signal. IS_*(inode) –Tests if inode is on a file system mounted with the corresponding flag. kfree*() –Frees memory previously allocated with kmalloc() kmalloc() –Allocates a chu nk of memory no larger than 4096 bytes. MAJOR() –Reports the major device number for a device. MINOR() –Reports the minor device number for a device.

kernel functions memcpy_*fs() –Copies chunks of memory between user space and kernel space outb(), outb_p() –Writes a byte to a port. Here, outb() goes as fast as it can, while outb_p() pauses before returning. printk() –A version of printf() for the kernel. put_user*() –Allows a driver to write data in user space. register_*dev() –Registers a device with the kernel. request_irq() –Requests an IRQ from the kernel, and, if successful, installs an IRQ interrupt handler. select_wait() –Adds a process to the proper select_wait queue. *sleep_on() –Sleeps on an event, puts a wait_queue entry in the list so that the process can be awakened on that event. sti() –Allows interrupts to be acknowledged. sys_get*() –System calls used to get information regarding the process, user, or group. wake_up*() –Wakes up a process that has been put to sleep by the matching *sleep_on() function.

Stop here 5/1/2003

Pitfalls 1.Using standard libraries: can only use kernel functions, which are the functions you can see in /proc/ksyms. 2.Disabling interrupts You might need to do this for a short time and that is OK, but if you don't enable them afterwards, your system will be stuck 3.Changes from version to version

Resources Linux Kernel API: api/linuxkernelapi.html Kernel development tool Linux Device Drivers 2nd Edition by Rubini & Corbet, O'Reilly Pub, ISBN