windows kernel internals io architecture.pdf

Upload: tamthientai

Post on 03-Apr-2018

244 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    1/32

    Microsoft Corporation 1

    Windows Kernel InternalsI/O Architecture

    David B. Probert, Ph.D.

    Windows Kernel DevelopmentMicrosoft Corporation

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    2/32

    Microsoft Corporation 2

    I/O Management

    Asynchronous with multiple completion

    mechanisms Apc completion (callback)

    Set event completion

    I/O Completion port

    Packet based (not stack based)

    Stackable Driver model Fast path, synchronous in some situations

    Integrated buffer cache management

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    3/32

    Microsoft Corporation 3

    I/O Request Packets

    UserEventUserIoStatusThread

    CancelRoutineIoStatus

    IRP Header

    Completio

    n Context

    Completio

    n Routine

    Para

    m

    Para

    m

    Para

    m

    Para

    m

    Minor

    Code

    Major

    Code

    PVOIDfunc()IRP_MN_IRP_MJ_

    Stack Pointer

    IRP_MJ_POWER

    IRP_MJ_PNP

    IRP_MJ_DEVICE_CONTROL

    IRP_MJ_WRITEIRP_MJ_READ

    IRP_MJ_CLOSE

    IRP_MJ_CREATE

    DRIVER_OBJECT

    Dispatch TableDEVICE_OBJECT

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    4/32

    Microsoft Corporation 4

    IO Architecture Goals

    Provide consistent I/O abstraction to

    applications. Provide a framework to do the following.

    Dynamic loading/unloading of drivers. Driver layering.

    Asynchronous I/O.

    Uniform enforcement of security.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    5/32

    Microsoft Corporation 5

    IO Manager Objects

    Driver objects represent loaded drivers.

    Drivers create device objects to representdevices.

    All IO requests are made to device objects. File objects represent open instances of

    device objects.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    6/32

    Microsoft Corporation 6

    Layering Drivers

    Device objects can be attached one on top

    of another using IoAttachDevice* APIs tocreate device stacks.

    IO manager sends IRP to top of the stack.

    Drivers store next lower device object in

    their private data structure. Stack tear down done using

    IoDetachDevice and IoDeleteDevice.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    7/32

    Microsoft Corporation 7

    Object Relationships

    Driver Object Device

    Object

    Device

    Object

    Device

    Object

    Device

    Object

    Device

    ObjectDriver Object File Object

    File Object

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    8/32

    Microsoft Corporation 8

    Loading Device Drivers

    Drivers can be loaded by,

    The boot loader at boot time. The IO manager at system initialization.

    The service control manager or PNP.

    Driver details are obtained from the registry. Driver object is created and DriverEntry for the

    driver is invoked. Drivers provide dispatch routines for various IO

    operations. (E.G., Create, read, write). Drivers can optionally provide fast path entry

    points.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    9/32

    Microsoft Corporation 9

    Device Deletion and Driver Unload

    Drivers delete devices using IoDeleteDevice.

    Drivers are unloaded by calling NtUnloadDriveror by PNP.

    No further opens/attaches allowed after a device

    is marked for deletion or unload. Driver unload function is invoked when all its

    device objects have no handles/attaches.

    Driver is unloaded when last reference to driverobject goes away.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    10/32

    Microsoft Corporation 10

    IO Request Packet (IRP)

    IO operations encapsulated in IRPs.

    IO requests travel down a driver stack in an IRP. Each driver gets a stack location which contains

    parameters for that IO request.

    IRP has major and minor codes to describe IOoperations.

    Major codes include create, read, write, PNP,

    devioctl, cleanup and close. Irps are associated with a thread that made the

    IO request.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    11/32

    Microsoft Corporation 11

    Path of an Async IO request

    NtReadFile(Handle,

    ..)User APCs

    Completion ports

    File

    object

    Devobj1

    Devobj2

    Security and

    access validation

    Allocate IRP

    Devobj1

    Dispatch routine

    Devobj2

    Dispatch routine Interrupt serviceroutine

    DPC routine

    IoCompleteRequest

    HandleIO Special APC

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    12/32

    Microsoft Corporation 12

    Async IO (contd.)

    Applications can issue asynchronous IO requests to files

    opened with FILE_FLAG_OVERLAPPED and passing

    an LPOVERLAPPED parameter to the IO API (e.g.,

    ReadFile())

    Methods available to wait for IO completion,

    Wait on the file handle

    Wait on an event handle passed in the overlapped

    structure (e.g., GetOverlappedResult())

    Specify a routine to be called on IO completion.

    Use completion ports.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    13/32

    Microsoft Corporation 13

    Security and Access checks

    Detailed security and access check is done atthe time of opening the file. Allowed access

    rights are store in the object manager andvalidated when an operation is performed onthe handle.

    If filename exactly matches a device objectthen the IO manager performs the securitycheck or if the driver sets the

    FILE_DEVICE_SECURE_OPEN in thedevice object.

    Otherwise the security check should be done

    by the driver.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    14/32

    Microsoft Corporation 14

    Canceling IRPs

    IO manager provides a mechanism to cancelIRPs queued for a long time. Canceling is doneon a per IRP basis.

    IO is canceled when a thread exits or when

    CancelIo is called on the thread. Drivers can cancel IRPs using IoCancelIrp().

    Drivers which queue IRPs that can wait a long

    time should set a cancel routine. Driver clears cancel routine before completing

    IRP.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    15/32

    Microsoft Corporation 15

    NT IO APIsEstablish IO handles

    NtCreateFile

    NtOpenFile NtCreateNamedPipeFile

    NtCreateMailslotFile

    IO Completion APIs NtCreateIoCompletion

    NtOpenIoCompletion

    NtQueryIoCompletion

    NtSetIoCompletion

    NtRemoveIoCompletion

    Actual IO operations

    NtReadFile

    NtReadFileScatter NtWriteFile

    NtWriteFileGather

    NtCancelIoFile NtFlushBuffersFile

    File operations

    NtLockFile

    NtUnlockFile

    NtDeleteFile

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    16/32

    Microsoft Corporation 16

    NT IO APIsMeta IO operations

    NtFsControlFile

    NtDeviceIoControlFile NtQueryDirectoryFile

    NtQueryAttributesFile

    NtQueryFullAttributesFile NtQueryEaFile

    NtSetEaFile

    NtQueryInformationFile

    NtSetInformationFile

    NtNotifyChangeDirectoryFile

    Administrative operations

    NtLoadDriver

    NtUnloadDriver NtQueryVolumeInformationFile

    NtSetVolumeInformationFile

    NtQueryQuotaInformationFile NtSetQuotaInformationFile

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    17/32

    Microsoft Corporation 17

    Debugging I/O Problems

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    18/32

    Microsoft Corporation 18

    IO Architecture Overview

    IO APIs come from many components Classic IO manager

    Plug and play manager

    Power manager

    Dump and Partition APIs

    Why is the IO model complex ? Large number of devices to be supported

    PnP and Power mgmt state machines Lots of miniport APIs (storage, NDIS, TDI, Audio,

    USB, 1394)

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    19/32

    Microsoft Corporation 19

    IO Manager Objects

    Driver objects represent loaded drivers.

    Drivers create device objects to representdevices.

    All IO requests are made to device objects.

    Device objects have names

    Device interface symbolic links to PDOs

    File objects represent open instances ofdevice objects.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    20/32

    Microsoft Corporation 20

    IO verifier

    IO verifier started off by thunking driver calls

    to IO APIs (IO Verifier level 1)

    Allocates IRP from special pool

    Validates parameters to IO APIs

    No performance hit

    Path changes are local to the driver

    Support added to unload all drivers at shutdown

    Memory leaks caught when driver image isunloaded

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    21/32

    Microsoft Corporation 21

    IO verifier (contd.)

    IO verifier level 2

    Leverages IRP tracking code that was in checkedbuild.

    Allocates memory for each IRP and keeps track of its

    path and validates the parameters. Catches lots of inconsistencies in the stack

    Perf impact in terms of memory usage and long paths

    Global to the system. Tests all drivers and reportsresults for driver to be verified.

    C

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    22/32

    Microsoft Corporation 22

    Common driver problems that

    crash in IO IOCTL system buffer already freed or

    overrun Events in IRP are allocated from stacks

    that have unwound. Kernel stacks pagable.

    Cancellation races

    Multiple IRP complete requests

    Driver left locked pages Hangs

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    23/32

    Microsoft Corporation 23

    Driver loading issues

    Two drivers cannot be loaded from the

    same image A driver which is also a DLL

    (EXPORT_DRIVER) should be loaded as

    a driver first If a component is loaded as a DLL

    DllInitialize and DllUnload routines areinvoked

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    24/32

    Microsoft Corporation 24

    Driver unloading issues

    Driver unload routine cannot fail

    Driver image can still remain after invocation ofunload routine

    Driver unload routine can race with other driver

    routines Legacy drivers should properly detach and

    delete device objects.

    Verifier checks for uncanceled timers and workerthreads after unload

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    25/32

    Microsoft Corporation 25

    Miscellaneous Crashes

    Multiple IRP completes. Cancellation issue.

    Pending flag not set correctly. If a driver returns

    STATUS_PENDING it should mark the IRP pending.

    System buffer already freed.

    MDL already freed.

    STATUS_MORE_PROCESSING_REQUIRED shouldbe used carefully.

    Drivers should watch out for IRP and MDL ownership.

    Spinlocks held in pageable code (verifier catches this)

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    26/32

    Microsoft Corporation 26

    Crashes (Contd.)

    DRIVER_LEFT_LOCKED_PAGES bug check. Caused by lack of cancel routine

    Driver locked the pages and forgot to unlock it incompletion routine

    Memory leaks of IO tags

    File object leaks (caused by process not closinghandles)

    Completion packet leaks (caused by user process notreading completion queues)

    Lack of quota enforcement with pool tagging causesthis.

    MDL and IRP leaks (Use !irpfind)

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    27/32

    Microsoft Corporation 27

    Crashes (contd.)

    INACCESSIBLE_BOOT_DEVICE bug

    check

    Status code is a bug check parameter that

    helps narrow it to a file system or storage

    problem KERNEL_DATA_INPAGE_ERROR bug

    check Status code in bug check parameter helpsdetermine the problem

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    28/32

    Microsoft Corporation 28

    Hangs

    Critical section timeouts. Process stuck in

    kernel inside IO manager. !thread shows IRP and identifies driver. NPFS IRPs are usually hung because the

    consumer is another process (Services hungon another user process RPC).

    Not marking Pending flag causes hangs

    (verifier catches this) Recursive locking (fs filter problems)

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    29/32

    Microsoft Corporation 29

    Hangs (contd.)

    APC deadlocks. IO system uses APCs to

    complete IRPs and will deadlock if IO isissued at IRQL >= APC_LEVEL

    KeStackAttachProcess can also cause

    deadlocks as it blocks APCs.

    DMA API hangs on PAE systems. Caused

    by map registers allocated and not freed.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    30/32

    Microsoft Corporation 30

    Canceling IRPs

    IO manager provides a mechanism to cancelIRPs queued for a long time. Canceling is doneon a per IRP basis.

    IO is canceled when a thread exits or whenCancelIo is called on the thread. IO managerwaits for 5 minutes for IRP to get canceled.

    Drivers can cancel IRPs using IoCancelIrp().

    Drivers which queue IRPs for a long time (> 1second) should set a cancel routine in the IRP.

    Driver clears cancel routine before completing

    IRP.

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    31/32

    Microsoft Corporation 31

    Debugging notes

    !thread lists IRPs pending for thread

    !drvobj prints out driver object !devobj prints out device object

    !object ?? prints out interesting symboliclinks

    dt FILE_OBJECT dumps fileobjects

  • 7/28/2019 Windows Kernel Internals IO Architecture.pdf

    32/32

    Microsoft Corporation 32

    Discussion