Chapter 7 Device Drivers and Software Interface Design

  • Slides: 31
Download presentation
Chapter 7 Device Drivers and Software Interface Design Professor Ching-Lung Su E-mail: kevinsu@yuntech. edu.

Chapter 7 Device Drivers and Software Interface Design Professor Ching-Lung Su E-mail: kevinsu@yuntech. edu. tw Http: //www. eecs. yuntech. edu. tw NYUST/EL

Outline 7. 1 Introduction to Device Driver 7. 2 Linux Driver 7. 3 Efficiency

Outline 7. 1 Introduction to Device Driver 7. 2 Linux Driver 7. 3 Efficiency Optimization of the Driver 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-2/80

7. 1 Introduction to Device Driver 7. 2 Linux Driver 7. 3 Efficiency Optimization

7. 1 Introduction to Device Driver 7. 2 Linux Driver 7. 3 Efficiency Optimization of the Driver 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-3/80

7. 1 Introduction to Device Driver u Introduction to Linux Device Driver u Introduction

7. 1 Introduction to Device Driver u Introduction to Linux Device Driver u Introduction to Win. CE Device Driver 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-4/80

7. 1 Introduction to Device Driver u A Device Driver has Three Sides: n

7. 1 Introduction to Device Driver u A Device Driver has Three Sides: n The Rest of the Kernel Side n The Hardware Side n The User Side 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-5/80

7. 1 Introduction to Linux Device Driver u Splitting the Kernel Subsystems Features Implemented

7. 1 Introduction to Linux Device Driver u Splitting the Kernel Subsystems Features Implemented Sofware Support Hardware 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-6/80

7. 1 Introduction to Linux Device Driver u Process management n The kernel is

7. 1 Introduction to Linux Device Driver u Process management n The kernel is in charge of creating and destroying processes and handling their connection to the outside world (input and output). n Communication among different processes (through signals, pipes, or inter-process communication primitives) is basic to the overall system functionality and is also handled by the kernel. n The scheduler which controls how processes share the CPU, is part of process management. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-7/80

7. 1 Introduction to Linux Device Driver n The kernel’s process management activity implements

7. 1 Introduction to Linux Device Driver n The kernel’s process management activity implements the abstraction of several processes on top of a single CPU or a few of them. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-8/80

7. 1 Introduction to Linux Device Driver u Memory management n The computer’s memory

7. 1 Introduction to Linux Device Driver u Memory management n The computer’s memory is a major resource, and the policy used to deal with it is a critical one for system performance. n The kernel builds up a virtual addressing space for any and all processes on top of the limited available resources. n The different parts of the kernel interact with the memorymanagement subsystem through a set of function calls, ranging from the simple malloc/free pair to much more complex functionalities. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-9/80

7. 1 Introduction to Linux Device Driver u Filesystems n Unix is heavily based

7. 1 Introduction to Linux Device Driver u Filesystems n Unix is heavily based on the filesystem concept. n Almost everything in Unix can be treated as a file. n The kernel builds a structured filesystem on top of unstructured hardware, and the resulting file abstraction is heavily used throughout the whole system. n Linux supports multiple filesystem types, that is different ways of organizing data on the physical medium. n For example, disks may be formatted with the Linux-standard ext 3 filesystem, the commonly used FAT filesystem or several others. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-10/80

7. 1 Introduction to Linux Device Driver u Device control n Almost every system

7. 1 Introduction to Linux Device Driver u Device control n Almost every system operation eventually maps to a physical device. n With the exception of the processor, memory, and a very few other entities, any and all device control operations are performed by code that is specific to the device being addressed. n That code is called a device driver. n The kernel must have embedded in it a device driver for every peripheral present on a system, from the hard drive to the keyboard and the tape drive. n This aspect of the kernel’s functions is our primary interest in this book. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-11/80

7. 1 Introduction to Linux Device Driver u Networking n Networking must be managed

7. 1 Introduction to Linux Device Driver u Networking n Networking must be managed by the operating system, because most network operations are not specific to a process: incoming packets are asynchronous events. n The packets must be collected, identified, and dispatched before a process takes care of them. n The system is in charge of delivering data packets across program and network interfaces, and it must control the execution of programs according to their network activity. n All the routing and address resolution issues are implemented within the kernel. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-12/80

7. 1 Introduction to Linux Device Driver u Classes of Devices and Modules n

7. 1 Introduction to Linux Device Driver u Classes of Devices and Modules n The three classes are: l Character device l Block device l Network interface 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-13/80

7. 1 Introduction to Linux Device Driver u Character device n Can be accessed

7. 1 Introduction to Linux Device Driver u Character device n Can be accessed as a stream of bytes (like a file). n A char driver is in charge of implementing this behavior. n Implements at least the l Open l Close l Read l Write system calls. n The examples of char devices are l The text console (/dev/console) l The serial ports (/dev/tty. S 0 and friends) 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-14/80

7. 1 Introduction to Linux Device Driver u Block device n Accessed by filesystem

7. 1 Introduction to Linux Device Driver u Block device n Accessed by filesystem nodes in the /dev directory. n Can host a filesystem. n Can only handle I/O operations that transfer one or more whole blocks, which are usually 512 bytes (or a larger power of two) bytes in length. n Allows the application to read and write a block device like a char device—it permits the transfer of any number of bytes at a time. n Block and char devices differ only in the way data is managed internally by the kernel, and thus in the kernel/driver software interface. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-15/80

7. 1 Introduction to Linux Device Driver n Like a char device, each block

7. 1 Introduction to Linux Device Driver n Like a char device, each block device is accessed through a filesystem node, and the difference between them is transparent to the user. n Block drivers have a completely different interface to the kernel than char drivers. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-16/80

7. 1 Introduction to Linux Device Driver u Network interface n Can exchange data

7. 1 Introduction to Linux Device Driver u Network interface n Can exchange data with other hosts. n In charge of sending and receiving data packets, driven by the network subsystem of the kernel, without knowing how individual transactions map to the actual packets being transmitted. n Many network connections (especially those using TCP) are stream-oriented, but network devices are designed around the transmission and receipt of packets. n A network driver knows nothing about in individual connections. n Only handles packets. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-17/80

7. 1 Introduction to Win. CE Device Driver u Win. CE Device Driver Architecture

7. 1 Introduction to Win. CE Device Driver u Win. CE Device Driver Architecture 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-18/80

7. 1 Introduction to Win. CE Device Driver u GWES: The Graphics, Windowing and

7. 1 Introduction to Win. CE Device Driver u GWES: The Graphics, Windowing and Event Subsystem u In CE can have two layers in a device driver n MDD (Model Device Driver) l The top level that interfaces to the OS using Device Driver Interface (DDI) – Idea is that you should not need to change the MDD layer for minor hardware changes. n PDD (Physical Device Driver) l The low level that interfaces to the hardware and talks to MDD using Device Driver Service-Provider Interface (DDSI). 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-19/80

7. 1 Introduction to Win. CE Device Driver u Supports two internal device driver

7. 1 Introduction to Win. CE Device Driver u Supports two internal device driver models n Native Device Driver n Stream Interface Device Driver 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-20/80

7. 1 Introduction to Win. CE Device Driver u Native Device Driver n Custom

7. 1 Introduction to Win. CE Device Driver u Native Device Driver n Custom interface for low level built-in hardware such as keyboards and displays. u Stream Interface Driver n Generic “file” type device driver. n Required entry points – all must be implemented l XXX_Init / XXX_De. Init l XXX_Power. Up / XXX_Power. Down l XXX_Open / XXX_Close l XXX_Read / XXX_Write l XXX_Seek l XXX_IOControl 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-21/80

7. 2 Linux Driver 7. 1 Introduction to Device Driver 7. 2 Linux Driver

7. 2 Linux Driver 7. 1 Introduction to Device Driver 7. 2 Linux Driver 7. 3 Efficiency Optimization of the Driver 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-22/80

7. 3 Efficiency Optimization of the Driver 7. 1 Introduction to Device Driver 7.

7. 3 Efficiency Optimization of the Driver 7. 1 Introduction to Device Driver 7. 2 Linux Driver 7. 3 Efficiency Optimization of the Driver 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-24/80

7. 3 Efficiency Optimization of the Driver u The inline assembler is a powerful

7. 3 Efficiency Optimization of the Driver u The inline assembler is a powerful new feature allowing the programmer to easily access the full functionality of the ARM instruction set within the C environment. u Assembly code can use any C or C++ variable or function name that is in scope, so it is easy to integrate it with your program's C and C++ code. u Assembly language serves many purposes, such as improving program speed, reducing memory needs, and controlling hardware. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-25/80

7. 3 Efficiency Optimization of the Driver u Example n C Code ------------#include<stdio. h>

7. 3 Efficiency Optimization of the Driver u Example n C Code ------------#include<stdio. h> int main(void) { int i, j=0; for(i=0; i<100; i++) j=j+i; printf("%d", j); } ------------ 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 n ARM Assembly --------------#include<stdio. h> int main(void) { int i, j=0; for(i=0; i<100; i++) __asm__ ( "mov %%r 1, %1nt" "mov %%r 2, %2nt" "add %%r 2, %%r 1, %%r 2nt" "mov %0, %%r 2" : "=r"(j) : "r"(i), "r"(j) : "0"); printf("%d", j); } -------------- 第七章:硬體驅動程式與軟體介面設計 P-26/80

7. 3 Efficiency Optimization of the Driver u Example(cont. ) n C Code ------------#include<stdio.

7. 3 Efficiency Optimization of the Driver u Example(cont. ) n C Code ------------#include<stdio. h> int main(void) { int i, j=0; for(i=0; i<100; i++) j=j+i; printf("%d", j); } ------------ 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 n X 86 Assembly ---------------#include<stdio. h> int main(void) { int i, j=0; for(i=0; i<100; i++) __asm__ ( "mov %1, %%eaxnt" "mov %2, %%ebxnt" "add %%eax, %%ebxnt" "mov %%ebx, %0" : "=r"(j) : "r"(i), "r"(j) : "0"); printf("%d", j); } --------------- 第七章:硬體驅動程式與軟體介面設計 P-27/80

Code motion for (i=0; i<N*M; i++) z[i] = a[i] + b[i]; i=0; Xi=0; =

Code motion for (i=0; i<N*M; i++) z[i] = a[i] + b[i]; i=0; Xi=0; = N*M i<X i<N*M Y N z[i] = a[i] + b[i]; i = i+1; © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-28/80

Array conflicts in cache a[0, 0] 1024 b[0, 0] . . . 4099 main

Array conflicts in cache a[0, 0] 1024 b[0, 0] . . . 4099 main memory © 2008 Wayne Wolf 4099 Overheads for Computers as Components 2 nd ed. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 cache 第七章:硬體驅動程式與軟體介面設計 P-29/80

Array conflicts, cont’d. u Array elements conflict because they are in the same line,

Array conflicts, cont’d. u Array elements conflict because they are in the same line, even if not mapped to same location. u Solutions: n move one array; n pad array. © 2008 Wayne Wolf Overheads for Computers as Components 2 nd ed. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-30/80

Reference u Linux Device Drivers, 3 rd Edition by Jonathan Corbet, Alessandro Rubini, Greg

Reference u Linux Device Drivers, 3 rd Edition by Jonathan Corbet, Alessandro Rubini, Greg Kroah-Hartman. u Linux Kernel Development, 2 nd edition, by Robert Love. 教育部顧問室PAL聯盟/系統雛型與軟硬體整合設計 第七章:硬體驅動程式與軟體介面設計 P-31/80