WINDOWS NT INTERNALS – 05
Computer Call
September 22, 1997
DRIVER DEVELOPMENT ISSUES
Driver design strategies
• Used formal design methods
• Data flow diagram
• State machine models
• Analysis of expected data repetition
• List of external events and driver actions
• Use of incremented development
• Kind of kernel mode objects
• Context and state information
• Driver entry routine and unload routine
• Use registry editor
• Dispatch routines that process IRP_MJ_CREATE,
IRP_MJ_CLOSE
• Dispatch routines that process IRP_MJ_XXX,START I/O
logic without starting the device
• Real start I/O logic, interrupt service routine and
DPC routine
CODING CONVENTIONS
General recommendations
• Avoid use of assembly language
• Put platform specific code in its own module or within
#if def /#end if statements
• Use RtlXxx routines instead of standard C runtime library
• Comment the code
Naming conventions
• Add driver specific prefix to each routines
• Name the routine that describes what it does
DRIVER MEMORY
ALLOCATION
Memory available to drivers
• Kernel stack
• Paged pool-
• Non paged pool-
WORKING WITH KERNEL STACK
• Avoid kernel stack overflow by the guidelines
• Don’t make deeply nested calls to internal routines
• Limit the depth of recursion
• Don’t build large temporary data structures
• Working with the pool areas
• Non paged pool- for routines running at or above
DISPATCH_LEVEL IRQL
• Non paged pool must succeed- memory for emergencies only
• Non paged pool cache aligned- aligned on the natural
boundary of a CPU data-cache line
• Non paged pool cache aligned musts- temporary I/O buffer
• Paged pool- for routines running below DISPATCH_LEVEL IRQL
• Paged pool cache aligned- I/O buffer used by FSD
ZONE BUFFERS
A zone buffer is a piece of driver allocated pool
Steps to manage the zone buffers
• Call exallocate pool and initialize zone buffer with exinitialize zone
• Call either exallocate from zone or interlocked allocate from zone
• Use either exfree to zone exinterlocked free to zone
• Call exfree pool
LOOKASIDE LIST
A linked list of fixed size memory blocks
Steps to manage Lookaside list
• Use exinitialize Xxx Lookaside list in drive entry routine
• Call exallocate from Xxx Lookaside list
• Call exfree to Xxx Lookaside list
• Use exdelete Xxx Lookaside list
UNICODE STRING
Unicode-string , * punicode-string
Field
USHORT Length
USHORT Maximum Length
PWSTR Buffer
SYNCHRONIZING MULTIPLE CPU’S
• NT uses synchronization objects called spinlocks
Spin Locks Operation
CPU A CPU B
Raise IRQL
Repeat
Request Spinlock
Until Acquired
f00,x=1
f00,y=2
Release Spinlock
Restore IRQL
Raise IRQL
Repeat
Request Spinlock
Until Acquired
f00,x=10
f00,y=20
Release Spinlock
Restore IRQL
Spinlock
for “f00”
Struct foo
Int x;
int y;
WRITING A DRIVER ENTRY ROUTINE
• Driver entry routine runs at PASSIVE_LEVEL IRQL
Parameters
1 . Pointer to drive object
2 .UNICODE_STRING containing path to the driver’s service key
in registry e,g HKEY_LOCAL MACHINEsystem
current control setservicesdrive
Steps in driver entry routine
1.Finding and allocating hardware
2.Initialize the driver object
3.Call IO create controller in case of multiunit controller
4.Call IO create device to create device object
5.Make device visible by calling IO create symbolic link
6.Connect device to interrupt object and DPC objects
7.Repeat steps 3-6 for all controllers/devices
8.Return STATUS_SUCCESS to I/O Manager
INITIALIZING DRIVER ENTRY
POINTS
• Routine responsible for setting up function pointers
• Function pointers
•Functions with explicit slots in the drive object
•IRP dispatch functions
• Creating device objects
• IO create device takes description of your device and
returns a device object
•IO create device links device object into list of devices
• Choosing a buffering strategy
• Buffered I/O or direct I/O
• Done by Oring flags field of device object
•DO_BUFFERD_IO
•DO_DIRECT_IO
WRITING REINITIALIZE
ROUTINES
• Registers a reinitialize routine by calling IO register
device reinitialization
• Routines runs at PASSIVE_LEVEL IRQL
PARAMETERS
• Pointer to driver object
• Context block specified at registration
• Count of reinitialization calls
WRITING AN UNLOAD ROUTINE
• Routine runs at PASSIVE_LEVEL IRQL
PARAMETERS
• Pointer to driver object
Steps in unload routine
• Save the state of the device in registry
• Disable interrupts from the device
• Deallocate hardware
• Use IO delete symbol link
• Remove device object using IO delete device
• Repeat step 4-5 in case of multiunit controller and delete
controller object
• Repeat steps 4-6 for all controllers and devices
• Deallocate any pool
WRITING SHUTDOWN
ROUTINES
• Routine runs at PASSIVE_LEVEL IRQL
• Put the device into a known state
PARAMETERS
• Pointer to driver object
• Pointer to shutdown IRP
• To receive shutdown notifications,call IO register
shutdown notification

PDF Replacer Pro Crack 1.8.9 Free Download

  • 1.
    WINDOWS NT INTERNALS– 05 Computer Call September 22, 1997
  • 2.
    DRIVER DEVELOPMENT ISSUES Driverdesign strategies • Used formal design methods • Data flow diagram • State machine models • Analysis of expected data repetition • List of external events and driver actions • Use of incremented development • Kind of kernel mode objects • Context and state information • Driver entry routine and unload routine • Use registry editor • Dispatch routines that process IRP_MJ_CREATE, IRP_MJ_CLOSE • Dispatch routines that process IRP_MJ_XXX,START I/O logic without starting the device • Real start I/O logic, interrupt service routine and DPC routine
  • 3.
    CODING CONVENTIONS General recommendations •Avoid use of assembly language • Put platform specific code in its own module or within #if def /#end if statements • Use RtlXxx routines instead of standard C runtime library • Comment the code Naming conventions • Add driver specific prefix to each routines • Name the routine that describes what it does
  • 4.
    DRIVER MEMORY ALLOCATION Memory availableto drivers • Kernel stack • Paged pool- • Non paged pool-
  • 5.
    WORKING WITH KERNELSTACK • Avoid kernel stack overflow by the guidelines • Don’t make deeply nested calls to internal routines • Limit the depth of recursion • Don’t build large temporary data structures • Working with the pool areas • Non paged pool- for routines running at or above DISPATCH_LEVEL IRQL • Non paged pool must succeed- memory for emergencies only • Non paged pool cache aligned- aligned on the natural boundary of a CPU data-cache line • Non paged pool cache aligned musts- temporary I/O buffer • Paged pool- for routines running below DISPATCH_LEVEL IRQL • Paged pool cache aligned- I/O buffer used by FSD
  • 6.
    ZONE BUFFERS A zonebuffer is a piece of driver allocated pool Steps to manage the zone buffers • Call exallocate pool and initialize zone buffer with exinitialize zone • Call either exallocate from zone or interlocked allocate from zone • Use either exfree to zone exinterlocked free to zone • Call exfree pool
  • 7.
    LOOKASIDE LIST A linkedlist of fixed size memory blocks Steps to manage Lookaside list • Use exinitialize Xxx Lookaside list in drive entry routine • Call exallocate from Xxx Lookaside list • Call exfree to Xxx Lookaside list • Use exdelete Xxx Lookaside list
  • 8.
    UNICODE STRING Unicode-string ,* punicode-string Field USHORT Length USHORT Maximum Length PWSTR Buffer
  • 9.
    SYNCHRONIZING MULTIPLE CPU’S •NT uses synchronization objects called spinlocks Spin Locks Operation CPU A CPU B Raise IRQL Repeat Request Spinlock Until Acquired f00,x=1 f00,y=2 Release Spinlock Restore IRQL Raise IRQL Repeat Request Spinlock Until Acquired f00,x=10 f00,y=20 Release Spinlock Restore IRQL Spinlock for “f00” Struct foo Int x; int y;
  • 10.
    WRITING A DRIVERENTRY ROUTINE • Driver entry routine runs at PASSIVE_LEVEL IRQL Parameters 1 . Pointer to drive object 2 .UNICODE_STRING containing path to the driver’s service key in registry e,g HKEY_LOCAL MACHINEsystem current control setservicesdrive Steps in driver entry routine 1.Finding and allocating hardware 2.Initialize the driver object 3.Call IO create controller in case of multiunit controller 4.Call IO create device to create device object 5.Make device visible by calling IO create symbolic link 6.Connect device to interrupt object and DPC objects 7.Repeat steps 3-6 for all controllers/devices 8.Return STATUS_SUCCESS to I/O Manager
  • 11.
    INITIALIZING DRIVER ENTRY POINTS •Routine responsible for setting up function pointers • Function pointers •Functions with explicit slots in the drive object •IRP dispatch functions
  • 12.
    • Creating deviceobjects • IO create device takes description of your device and returns a device object •IO create device links device object into list of devices • Choosing a buffering strategy • Buffered I/O or direct I/O • Done by Oring flags field of device object •DO_BUFFERD_IO •DO_DIRECT_IO
  • 13.
    WRITING REINITIALIZE ROUTINES • Registersa reinitialize routine by calling IO register device reinitialization • Routines runs at PASSIVE_LEVEL IRQL PARAMETERS • Pointer to driver object • Context block specified at registration • Count of reinitialization calls
  • 14.
    WRITING AN UNLOADROUTINE • Routine runs at PASSIVE_LEVEL IRQL PARAMETERS • Pointer to driver object Steps in unload routine • Save the state of the device in registry • Disable interrupts from the device • Deallocate hardware • Use IO delete symbol link • Remove device object using IO delete device • Repeat step 4-5 in case of multiunit controller and delete controller object • Repeat steps 4-6 for all controllers and devices • Deallocate any pool
  • 15.
    WRITING SHUTDOWN ROUTINES • Routineruns at PASSIVE_LEVEL IRQL • Put the device into a known state PARAMETERS • Pointer to driver object • Pointer to shutdown IRP • To receive shutdown notifications,call IO register shutdown notification