Unit OS6: Device Management - The windows I/O system coponents

ppt 28 trang huongle 4000
Bạn đang xem 20 trang mẫu của tài liệu "Unit OS6: Device Management - The windows I/O system coponents", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptunit_os6_device_management_the_windows_io_system_coponents.ppt

Nội dung text: Unit OS6: Device Management - The windows I/O system coponents

  1. Unit OS6: Device Management 6.2. The Windows I/O System Components Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze
  2. Copyright Notice © 2000-2005 David A. Solomon and Mark Russinovich These materials are part of the Windows Operating System Internals Curriculum Development Kit, developed by David A. Solomon and Mark E. Russinovich with Andreas Polze Microsoft has licensed these materials from David Solomon Expert Seminars, Inc. for distribution to academic organizations solely for use in academic environments (and not for commercial use) 2
  3. Roadmap for Section 6.2 I/OI/O SystemSystem ComponentsComponents FunctionsFunctions ofof thethe I/OI/O ManagerManager ControlControl flowflow forfor anan I/OI/O operationoperation PlugPlug andand PlayPlay (PnP)(PnP) andand PowerPower ManagerManager DriverDriver VerifierVerifier WindowsWindows DriverDriver ModelModel (WDM)(WDM) classificationclassification 3
  4. I/O System Design Goals Fast I/O processing on single / multiprocessor systems Protection for shareable resources Using Windows security mechanisms Meet requirements dictated by different subsystems Provide common services for device drivers Ease device driver development Allow drivers to be written in high-level language Dynamic addition/removal of device drivers Support multiple file systems (FAT, CDFS, UDF, NTFS) Provide mapped file I/O capabilities Windows Management Instrumentation support and diagnosability Drivers can be managed through WMI applications and scripts. 4
  5. I/O System Components Windows Applications Services User-Mode WMI Service SetupSetupSetup User PnP Manager ComponentsComponentsComponents Mode .inf files .cat files Kernel Registry Mode WDM WMI Power PnP Manager I/O Manager Routines Manager I/O System Drivers Driver 1 Driver 2 HAL 5
  6. I/O System Components The I/O manager Connects applications and system components to virtual, logical, and physical devices Windows APIs: ReadFile, WriteFile, CreateFile, CloseFile, DeviceIoControl Defines the infrastructure that supports device drivers A device driver typically provides an I/O interface for a particular type of device Device drivers receive commands routed to them by the I/O manager that are directed at devices they manage, and they inform the I/O manager when those commands complete Device drivers often use the I/O manager to forward I/O commands to other device drivers that share in the implementation of a device's interfaceinterface oror control.control. Several types: “ordinary”, file system, network, bus drivers, etc. More information in I/O subsystem section 6
  7. I/O Manager Framework for delivery of I/O request packets (IRPs) IRPs control processing of all I/O operations (exception: fast I/O does not use IRPs) I/O manager: creates an IRP for each I/O operation; passes IRP to correct drivers; deletes IRP when I/O operation is complete Driver: Receives IRP Performs operations specified by IRP Passes IRP back to I/O manager or to another driver (via I/O manager) for further processing 7
  8. I/O Manager (contd.) Supplies common code for different drivers: Drivers become simpler, more compact I/O manager: Allows driver to call other drivers Manages buffers for I/O requests Provides time-out support for drivers Records which installable file systems are loaded Provides flexible I/O services to environment subsystems (Windows/POSIX asynchronous I/O) Layered processing of I/O requests possible: Drivers can call each other (via I/O manager) 8
  9. I/O Functions Advanced features beyond open, close, read, write: Asynchronous I/O: May improve throughput/performance: continue program execution while I/O is in progress Must specify FILE_FLAG_OVERLAPPED on CreateFile() Programmer is responsible for synchronization of I/O requests Internally, all I/O is performed asynchronously I/O system returns to caller only if file was opened for asynch. I/O For synchronous I/O, wait is done in kernel mode depending on overlapped flag in file object Status of pending I/O can be tested: via Windows-API function: HasOverlappedIoCompleted() when using I/O completion ports: GetQueuedCompletionStatus() 9
  10. Control flow for an I/O operation Call ReadFile() Application ReadFile Call NtReadFile() Return to caller KERNEL32.DLL NtReadFile Int 2E NTDLL.DLL Return to caller User mode Kernel mode KiSystemService Call Nt ReadFile() Dismiss interrupt NTOSKRNL.EXE NtReadFile Call Invoke driver() Whether to wait depends Wait or return NTOSKRNL.EXE on overlapped flag to caller Initiate I/O operation Return to caller DRIVER.SYS 10
  11. Advanced I/O Functions Fast I/O Bypass generation of IRPs Go directly to file system driver or cache manager to complete I/O Mapped File I/O and File Caching Available through Windows-API CreateFileMapping() / MapViewOfFile() Used by OS for file caching and image activation Used by file systems via cache manager to improve performance Scatter/Gather I/O Windows-API functions ReadFileScatter()/WriteFileScatter() Read/write multiple buffers with a single system call File must be opened for non-cached, asynchronous I/O; buffers must be page-aligned 11
  12. HAL TheThe hardwarehardware abstractionabstraction layerlayer (HAL)(HAL) insulatesinsulates driversdrivers fromfrom thethe specificsspecifics ofof thethe processorprocessor andand interruptinterrupt controllercontroller byby providingproviding APIsAPIs thatthat hidehide differencesdifferences betweenbetween platformsplatforms in essence, the HAL is the bus driver for all the devices on the computer's motherboard that aren't controlled by other drivers By programming to the HAL drivers are source-level compatible across CPU architectures 12
  13. PnP and Power TheThe PnPPnP managermanager Handles driver loading and starting Performs resource arbitration It relies on the I/O Manager to load drivers and send them PnP-related commands TheThe powerpower managermanager controlscontrols thethe powerpower statestate ofof thethe systemsystem It relies on the I/O Manager to ask drivers if they can change power state and to inform them when they should 13
  14. The Driver Verifier Driver Verifier is a tool introduced in Windows 2000 that helps developers test their drivers and systems administrators identify faulty drivers Must be run from \windows\system32\verifier.exe (no shortcut) This is the Windows 2000 GUI to driver verifier 14
  15. Verification Options Special Pool The memory returned for driver memory allocations is bounded with invalid regions to catch buffer overrun and underrun To be described in Crash Analysis section Force IRQL checking Detects drivers that access paged memory when the system is in a state that can’t tolerate page faults Low Resource Simulation Randomly fails driver memory allocations Pool Tracking Associates memory with the driver that allocated it to help identify leaks I/O verification Ensures that I/O commands are properly formatted and handled 15
  16. Driver Verifier – XP/Server 2003 Enhancements Simpler wizard-style UI Default is verify unsigned drivers Four new verification options in XP: DMA verification – detects improper use of DMA buffers, adapters, and map registers Deadlock detection – detects lock hierarchy violations with spinlocks, mutexes, fast mutexes SCSI verification - monitors the interaction between a SCSI miniport driver and the port driver Enhanced I/O Verification tests drivers' support for power management, WMI, and filters One new on in Server 2003: Disk integrity checking - monitors a hard disk and detects whether the disk is preserving its data correctly (this is the Windows XP/2003 GUI) 16
  17. Kernel-Mode Drivers Windows kernel-mode drivers PnP Drivers: Integrate with the power manager and PnP manager Mass storage devices Input devices Non PnP Drivers: Don’t have to integrate with the PnP manager Protocol stacks Network adapters Virtual devices (Filemon, Regmon) File system drivers accept I/O requests to files and satisfy the requests by issuing their own, more explicit requests to mass storage or network device drivers Interact closely with MM and Cache Manager 17
  18. User-Mode Drivers Virtual device drivers (VDDs) are used to emulate 16-bit MS-DOS applications. User-mode can't access hardware directly and thus must go through a real kernel-mode device driver. They trap what an MS-DOS application thinks are references to I/O ports and translates them into native Windows I/O functions Windows subsystem printer drivers translate device-independent graphics requests to printer-specific commands. Commands are forwarded to a kernel-mode port driver such as the parallel port driver (Parport.sys) or the universal serial bus (USB) printer port driver (Usbprint.sys) 18
  19. WDM Driver Classification Windows Driver Model Unified architecture for drivers Originally intended to be Win9x/NT cross platform Most PnP Drivers are WDM drivers There are three types of WDM drivers: Bus drivers manage a logical or physical bus e.g. PCMCIA, PCI, Function drivers manage a particular type of device. Bus drivers present devices to function drivers via the PnP manager. Filter drivers logically layer above or below function drivers, augmenting or changing the behavior of a device or another driver. 19
  20. WDM Driver Classification In WDM, no one driver is responsible for controlling all aspects of a particular device. Function Upper Filter Function Driver Function Lower Filter Bus Filter Bus Driver 20
  21. Class/Port/Miniport Driver Classification Hardware support might be split between different modules that implement support for different levels of abstraction Microsoft typically provides the drivers for the higher levels of abstraction Hardware vendors provide the lowest level, which understands a particular device The conventional division is three levels: Class drivers implement the I/O processing for a particular class of devices, such as disk, tape, or CD-ROM. Port drivers implement the processing of an I/O request specific to a type of I/O port, such as SCSI, and are also implemented as kernel-mode libraries of functions rather than actual device drivers. Miniport drivers map a generic I/O request to a type of port into an adapter type, such as a specific SCSI adapter. Miniport drivers are actual device drivers that import the functions supplied by a port driver. 21
  22. Class/Port/Miniport Driver Classification Class Driver Port Driver Miniport Driver 22
  23. Layered Driver Structure Windows I/O system interface CD-ROM FAT file NTFS file system system CD-ROM Tape class FTDisk class driver driver driver (striping, mirroring) Disk class Relationships among driver various types of kernel-mode device drivers port driver MiniportMiniport driversdrivers 23
  24. Dynamically Layering a File System Driver and a Disk Driver Environment subsystem or DLL User mode Kernel mode NtWriteFile(file_handle, char_buffer) System services Write data at specified Adding a File system byte offset within a file layered driver driver Translate file-relative byte offset I/O Multi- into disk-relative byte offset, and manager volume call next driver (via I/O manager) disk driver Disk driver Call driver to write data at disk-relative byte offset Call next driver to write data to Translate disk-relative byte offset into Disk 3 at disk- physical location, and transfer data relative byte offset Disk 2 Disk 1 Disk 3 24
  25. Internal Structure of a Driver Dispatch routines Start I/O routine DispatchDispatch routines routines Add-device routine Interrupt service I/O routine (ISR) Initialization subsystem routine DPC routine I/O manager executes initialization routine when loading a driver PnP manager calls add-device routine on device detection Dispatch routines: open(), close(), read(), write() Start I/O routine initiates transfer from/to a device ISR runs in response to interrupt; schedules DPC DPC routine performs actual work of handling interrupt; starts next queued I/O operation on device 25
  26. Other components of device drivers Completion routines A layered driver may have completion routines that will notify it when a lower- levellevel driverdriver finishesfinishes processingprocessing anan IRPIRP (I/O Request Packet) Cancel I/O routine Unload routine Releases system resources System shutdown notification routine Error-logging routines Notify I/O manager to write record to error log file (e.g., bad disk block) Windows Driver Model (WDM) Plug & Play support Source compatible between Win98/ME 26
  27. Further Reading Mark E. Russinovich and David A. Solomon, Microsoft Windows Internals, 4th Edition, Microsoft Press, 2004. Chapter 9 - I/O System I/O System Components (from pp. 537) Typical I/O Processing (from pp. 540) Types of Device Drivers (from pp. 541) Driver Objects and Device Objects (from pp. 550) 27
  28. Source Code References WindowsWindows ResearchResearch KernelKernel sourcessources \base\ntos\io – I/O Manager \base\ntos\inc\io.h – additional structure definitions \base\ntos\verifer – Driver Verifier 28