IOCTL

There is a user process A that sends an IOCTL down to a driver. Till the
time the IOCTL is processed by the driver and the data structure (of the
IOCTL) is filled up there is a context switch and anotehr user process B
starts execution, at this time the driver returns the IOCTL back, how is
this scenario handled internally, how does the OS ensure that the data
reaches the right process(thread). Are DeviceIoctl calls blocking?

IOCTL processing is asynchrous within the kernel, your process may be using
synchronous IO, but that is above the kernel level. The OS uses the APC
mechanism to deliver IO completion context to the appropriate thread.
Essentially Thread A will be rescheduled and start running with the
appropriate data.

Google APC NT IO Completion

=====================
Mark Roddy DDK MVP
Windows 2003/XP/2000 Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com


From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of A P
Sent: Thursday, April 20, 2006 2:51 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] IOCTL

There is a user process A that sends an IOCTL down to a driver. Till the
time the IOCTL is processed by the driver and the data structure (of the
IOCTL) is filled up there is a context switch and anotehr user process B
starts execution, at this time the driver returns the IOCTL back, how is
this scenario handled internally, how does the OS ensure that the data
reaches the right process(thread). Are DeviceIoctl calls blocking? —
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 To unsubscribe, visit the List
Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Device I/O control calls may or may not be blocking - it doesn’t really
matter in the end.

When user process A “sends an IOCTL down to a driver” it does so by
calling DeviceIoControl() which in turn calls the kernel I/O manager to
start the request. The I/O manager allocates an I/O Request Packet
(IRP) which encapsulates everything about the I/O request, including the
thread and process which initiated it. The IRP is dispatched to the
driver stack by calling its dispatch routines in the thread which called
DeviceIoControl(). The drivers may block that thread (forcing the I/O
to be synchronous) or they may return STATUS_PENDING and process the I/O
asynchronously.

Let’s look at the asynchronous case first. The driver gets an I/O
request packet & the thread returns back to the application before the
I/O is done. Some time later, some other thread (or a DPC which
interrupts some other thread) will complete the request. The I/O
manager uses the IRP to determine which process sent the I/O. The I/O
manager sends an APC to the thread which originated the I/O. When that
thread is next scheduled the APC routine interrupts it and can copy data
back into the context of the initiating process. There are some other
completion options, like completion ports, but they all involve getting
a thread in the process which initiated the I/O to run some portion of
the completion code.

Now think about the synchronous case. If thread A starts processing an
I/O operation, but is preempted by thread B (in process B), the I/O
can’t complete until thread A starts running again. When thread A is
finally done and completes the request, the I/O manager can detect (by
watching the PENDING_RETURNED flag in the IRP) whether the APC is
necessary … if it’s the same thread which initiated the request then
the I/O manager can copy results back immediately rather than needing to
schedule an APC.

-p


From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of A P
Sent: Wednesday, April 19, 2006 11:51 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] IOCTL

There is a user process A that sends an IOCTL down to a driver. Till the
time the IOCTL is processed by the driver and the data structure (of the
IOCTL) is filled up there is a context switch and anotehr user process B
starts execution, at this time the driver returns the IOCTL back, how is
this scenario handled internally, how does the OS ensure that the data
reaches the right process(thread). Are DeviceIoctl calls blocking? —
Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256 To unsubscribe, visit the
List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer