1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
cancel.c
Abstract: Demonstrates the use of new Cancel-Safe queue
APIs to perform queuing of IRPs without worrying about
any synchronization issues between cancel lock in the I/O
manager and the driver's queue lock.
This driver is written for an hypothetical data acquisition
device that requires polling at a regular interval.
The device has some settling period between two reads.
Upon user request the driver reads data and records the time.
When the next read request comes in, it checks the interval
to see if it's reading the device too soon. If so, it pends
the IRP and sleeps for while and tries again.
Upon arrival, IRPs are queued in a cancel-safe queue and a
semaphore is signaled. A polling thread indefinitely waits on the
semaphore to process queued IRPs sequentially.
This sample is adapted from the original cancel
sample (KB Q188276) available in MSDN.
Environment:
Kernel mode
--*/
#include "cancel.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text( INIT, DriverEntry )
#pragma alloc_text( PAGE, CsampCreateClose)
#pragma alloc_text( PAGE, CsampUnload)
#pragma alloc_text( PAGE, CsampRead)
#endif // ALLOC_PRAGMA
NTSTATUS
DriverEntry(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Installable driver initialization entry point.
This entry point is called directly by the I/O system.
Arguments:
DriverObject - pointer to the driver object
registryPath - pointer to a unicode string representing the path,
to driver-specific key in the registry.
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING unicodeDeviceName;
UNICODE_STRING unicodeDosDeviceName;
PDEVICE_OBJECT deviceObject;
PDEVICE_EXTENSION devExtension;
HANDLE threadHandle;
UNICODE_STRING sddlString;
UNREFERENCED_PARAMETER (RegistryPath);
CSAMP_KDPRINT(("DriverEntry Enter \n"));
//
// Opt-in to using non-executable pool memory on Windows 8 and later.
// https://msdn.microsoft.com/en-us/library/windows/hardware/hh920402(v=vs.85).aspx
//
ExInitializeDriverRuntime(DrvRtPoolNxOptIn);
(void) RtlInitUnicodeString(&unicodeDeviceName, CSAMP_DEVICE_NAME_U);
(void) RtlInitUnicodeString( &sddlString, L"D:P(A;;GA;;;SY)(A;;GA;;;BA)");
//
// We will create a secure deviceobject so that only processes running
// in admin and local system account can access the device. Refer
// "Security Descriptor String Format" section in the platform
// SDK documentation to understand the format of the sddl string.
// We need to do because this is a legacy driver and there is no INF
// involved in installing the driver. For PNP drivers, security descriptor
// is typically specified for the FDO in the INF file.
//
status = IoCreateDeviceSecure(
DriverObject,
sizeof(DEVICE_EXTENSION),
&unicodeDeviceName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
(BOOLEAN) FALSE,
&sddlString,
(LPCGUID)&GUID_DEVCLASS_CANCEL_SAMPLE,
&deviceObject
);
if (!NT_SUCCESS(status))
{
return status;
}
DbgPrint("DeviceObject %p\n", deviceObject);
//
// Allocate and initialize a Unicode String containing the Win32 name
// for our device.
//
(void)RtlInitUnicodeString( &unicodeDosDeviceName, CSAMP_DOS_DEVICE_NAME_U );
status = IoCreateSymbolicLink(
(PUNICODE_STRING) &unicodeDosDeviceName,
(PUNICODE_STRING) &unicodeDeviceName
);
if (!NT_SUCCESS(status))
{
IoDeleteDevice(deviceObject);
return status;
}
devExtension = deviceObject->DeviceExtension;
DriverObject->MajorFunction[IRP_MJ_CREATE]=
DriverObject->MajorFunction[IRP_MJ_CLOSE] = CsampCreateClose;
DriverObject->MajorFunction[IRP_MJ_READ] = CsampRead;
DriverObject->MajorFunction[IRP_MJ_CLEANUP] = CsampCleanup;
DriverObject->DriverUnload = CsampUnload;
//
// Set the flag signifying that we will do buffered I/O. This causes NT
// to allocate a buffer on a ReadFile operation which will then be copied
// back to the calling application by the I/O subsystem
//
deviceObject->Flags |= DO_BUFFERED_IO;
//
// This is used to serailize access to the queue.
//
KeInitializeSpinLock(&devExtension->QueueLock);
KeInitializeSemaphore(&devExtension->IrpQueueSemaphore, 0, MAXLONG );
//
// Initialize the pending Irp devicequeue
//
InitializeListHead( &devExtension->PendingIrpQueue );
//
// Initialize the cancel safe queue
//
IoCsqInitialize( &devExtension->CancelSafeQueue,
CsampInsertIrp,
CsampRemoveIrp,
CsampPeekNextIrp,
CsampAcquireLock,
CsampReleaseLock,
CsampCompleteCanceledIrp );
//
// 10 is multiplied because system time is specified in 100ns units
//
devExtension->PollingInterval.QuadPart = Int32x32To64(
CSAMP_RETRY_INTERVAL, -10);
//
// Note down system time
//
KeQuerySystemTime (&devExtension->LastPollTime);
//
// Start the polling thread.
//
devExtension->ThreadShouldStop = FALSE;
status = PsCreateSystemThread(&threadHandle,
(ACCESS_MASK)0,
NULL,
(HANDLE) 0,
NULL,
CsampPollingThread,
deviceObject );
if ( !NT_SUCCESS( status ))
{
IoDeleteSymbolicLink( &unicodeDosDeviceName );
IoDeleteDevice( deviceObject );
return status;
}
//
// Convert the Thread object handle into a pointer to the Thread object
// itself. Then close the handle.
//
ObReferenceObjectByHandle(threadHandle,
THREAD_ALL_ACCESS,
NULL,
KernelMode,
&devExtension->ThreadObject,
NULL );
ZwClose(threadHandle);
CSAMP_KDPRINT(("DriverEntry Exit = %x\n", status));
ASSERT(NT_SUCCESS(status));
return status;
}
_Use_decl_annotations_
NTSTATUS
CsampCreateClose(
PDEVICE_OBJECT DeviceObject,
PIRP Irp
)
/*++
Routine Description:
Process the Create and close IRPs sent to this device.
Arguments:
DeviceObject - pointer to a device object.
Irp - pointer to an I/O Request Packet.
Return Value:
NT Status code
--*/
{
PIO_STACK_LOCATION irpStack;
NTSTATUS status = STATUS_SUCCESS;
PFILE_CONTEXT fileContext;
UNREFERENCED_PARAMETER(DeviceObject);
PAGED_CODE ();
CSAMP_KDPRINT(("CsampCreateClose Enter\n"));
irpStack = IoGetCurrentIrpStackLocation(Irp);
ASSERT(irpStack->FileObject != NULL);
switch(irpStack->MajorFunction)
{
case IRP_MJ_CREATE:
//
// The dispatch routine for IRP_MJ_CREATE is called when a
// file object associated with the device is created.
// This is typically because of a call to CreateFile() in
// a user-mode program or because a another driver is
// layering itself over a this driver. A driver is
// required to supply a dispatch routine for IRP_MJ_CREATE.
//
fileContext = ExAllocatePoolQuotaZero(NonPagedPoolNx | POOL_QUOTA_FAIL_INSTEAD_OF_RAISE,
sizeof(FILE_CONTEXT),
TAG);
if (NULL == fileContext) {
status = STATUS_INSUFFICIENT_RESOURCES;
break;
}
IoInitializeRemoveLock(&fileContext->FileRundownLock, TAG, 0, 0);
//
// Make sure nobody is using the FsContext scratch area.
//
ASSERT(irpStack->FileObject->FsContext == NULL);
//
// Store the context in the FileObject's scratch area.
//
irpStack->FileObject->FsContext = (PVOID) fileContext;
CSAMP_KDPRINT(("IRP_MJ_CREATE\n"));
break;
case IRP_MJ_CLOSE:
//
// The IRP_MJ_CLOSE dispatch routine is called when a file object
// opened on the driver is being removed from the system; that is,
// all file object handles have been closed and the reference count
// of the file object is down to 0.
//
fileContext = irpStack->FileObject->FsContext;
ExFreePoolWithTag(fileContext, TAG);
CSAMP_KDPRINT(("IRP_MJ_CLOSE\n"));
break;
default:
CSAMP_KDPRINT((" Invalid CreateClose Parameter\n"));
status = STATUS_INVALID_PARAMETER;
break;
}
//
// Save Status for return and complete Irp
//
Irp->IoStatus.Status = status;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
CSAMP_KDPRINT((" CsampCreateClose Exit = %x\n", status));
return status;
}
_Use_decl_annotations_
NTSTATUS
CsampRead(
PDEVICE_OBJECT DeviceObject,
PIRP Irp
)
/*++
Routine Description:
Read disptach routine
Arguments:
DeviceObject - pointer to a device object.
Irp - pointer to current Irp
Return Value:
NT status code.
--*/
{
NTSTATUS status;
PDEVICE_EXTENSION devExtension;
PIO_STACK_LOCATION irpStack;
LARGE_INTEGER currentTime;
PFILE_CONTEXT fileContext;
PVOID readBuffer;
BOOLEAN inCriticalRegion;
PAGED_CODE();
CSAMP_KDPRINT(("CsampRead Enter:0x%p\n", Irp));
devExtension = DeviceObject->DeviceExtension;
inCriticalRegion = FALSE;
irpStack = IoGetCurrentIrpStackLocation(Irp);
ASSERT(irpStack->FileObject != NULL);
fileContext = irpStack->FileObject->FsContext;
status = IoAcquireRemoveLock(&fileContext->FileRundownLock, Irp);
if (!NT_SUCCESS(status)) {
//
// Lock is in a removed state. That means we have already received
// cleaned up request for this handle.
//
Irp->IoStatus.Status = status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
return status;
}
//
// First make sure there is enough room.
//
if (irpStack->Parameters.Read.Length < sizeof(INPUT_DATA))
{
Irp->IoStatus.Status = status = STATUS_BUFFER_TOO_SMALL;
Irp->IoStatus.Information = 0;
IoReleaseRemoveLock(&fileContext->FileRundownLock, Irp);
IoCompleteRequest (Irp, IO_NO_INCREMENT);
return status;
}
//
// FOR TESTING:
// Initialize the data to mod 2 of some random number.
// With this value you can control the number of times the
// Irp will be queued before completion. Check
// CsampPollDevice routine to know how this works.
//
KeQuerySystemTime(¤tTime);
readBuffer = Irp->AssociatedIrp.SystemBuffer;
*((PULONG)readBuffer) = ((currentTime.LowPart/13)%2);
//
// To avoid the thread from being suspended after it has queued the IRP and
// before it signalled the semaphore, we will enter critical region.
//
ASSERT(KeGetCurrentIrql() <= APC_LEVEL);
KeEnterCriticalRegion();
inCriticalRegion = TRUE;
//
// Queue the IRP and return STATUS_PENDING after signalling the
// polling thread.
// Note: IoCsqInsertIrp marks the IRP pending.
//
IoCsqInsertIrp(&devExtension->CancelSafeQueue, Irp, NULL);
//
// Do not touch the IRP once it has been queued because another thread
// could remove the IRP and complete it before this one gets to run.
//
//
// A semaphore remains signaled as long as its count is greater than
// zero, and non-signaled when the count is zero. Following function
// increments the semaphore count by 1.
//
KeReleaseSemaphore(&devExtension->IrpQueueSemaphore,
0,// No priority boost
1,// Increment semaphore by 1
FALSE );// No WaitForXxx after this call
if (inCriticalRegion == TRUE) {
KeLeaveCriticalRegion();
}
//
// We don't hold the lock for IRP that's pending in the list because this
// lock is meant to rundown currently dispatching threads when the cleanup
// is handled.
//
IoReleaseRemoveLock(&fileContext->FileRundownLock, Irp);
return STATUS_PENDING;
}
VOID
CsampPollingThread(
_In_ PVOID Context
)
/*++
Routine Description:
This is the main thread that removes IRP from the queue
and peforms I/O on it.
Arguments:
Context -- pointer to the device object
--*/
{
PDEVICE_OBJECT DeviceObject = Context;
PDEVICE_EXTENSION DevExtension = DeviceObject->DeviceExtension;
PIRP Irp;
NTSTATUS Status;
KeSetPriorityThread(KeGetCurrentThread(), LOW_REALTIME_PRIORITY );
//
// Now enter the main IRP-processing loop
//
for(;;)
{
//
// Wait indefinitely for an IRP to appear in the work queue or for
// the Unload routine to stop the thread. Every successful return
// from the wait decrements the semaphore count by 1.
//
KeWaitForSingleObject(&DevExtension->IrpQueueSemaphore,
Executive,
KernelMode,
FALSE,
NULL );
//
// See if thread was awakened because driver is unloading itself...
//
if ( DevExtension->ThreadShouldStop ) {
PsTerminateSystemThread( STATUS_SUCCESS );
}
//
// Remove a pending IRP from the queue.
//
Irp = IoCsqRemoveNextIrp(&DevExtension->CancelSafeQueue, NULL);
if (!Irp) {
CSAMP_KDPRINT(("Oops, a queued irp got cancelled\n"));
continue; // go back to waiting
}
for(;;) {
//
// Perform I/O
//
Status = CsampPollDevice(DeviceObject, Irp);
if (Status == STATUS_PENDING) {
//
// Device is not ready, so sleep for a while and try again.
//
KeDelayExecutionThread(KernelMode, FALSE,
&DevExtension->PollingInterval);
} else {
//
// I/O is successful, so complete the Irp.
//
Irp->IoStatus.Status = Status;
IoCompleteRequest (Irp, IO_NO_INCREMENT);
break;
}
}
//
// Go back to the top of the loop to see if there's another request waiting.
//
} // end of while-loop
}
_Use_decl_annotations_
NTSTATUS
CsampPollDevice(
PDEVICE_OBJECT DeviceObject,
PIRP Irp
)
/*++
Routine Description:
Polls for data
Arguments:
DeviceObject -- pointer to the device object
Irp -- pointer to the requesing Irp
Return Value:
STATUS_SUCCESS -- if the poll succeeded,
STATUS_TIMEOUT -- if the poll failed (timeout),
or the checksum was incorrect
STATUS_PENDING -- if polled too soon
--*/
{
PINPUT_DATA pInput;
UNREFERENCED_PARAMETER( DeviceObject );
pInput = (PINPUT_DATA)Irp->AssociatedIrp.SystemBuffer;
#ifdef REAL
RtlZeroMemory( pInput, sizeof(INPUT_DATA) );
//
// If currenttime is less than the lasttime polled plus
// minimum time required for the device to settle
// then don't poll and return STATUS_PENDING
//
KeQuerySystemTime(¤tTime);
if (currentTime->QuadPart < (TimeBetweenPolls +
devExtension->LastPollTime.QuadPart))
{
return STATUS_PENDING;
}
//
// Read/Write to the port here.
// Fill the INPUT structure
//
//
// Note down the current time as the last polled time
//
KeQuerySystemTime(&devExtension->LastPollTime);
return STATUS_SUCCESS;
#else
//
// With this conditional statement
// you can control the number of times the
// i/o should be retried before completing.
//
if (pInput->Data-- <= 0)
{
Irp->IoStatus.Information = sizeof(INPUT_DATA);
return STATUS_SUCCESS;
}
return STATUS_PENDING;
#endif
}
_Use_decl_annotations_
NTSTATUS
CsampCleanup(
PDEVICE_OBJECT DeviceObject,
PIRP Irp
)
/*++
Routine Description:
This dispatch routine is called when the last handle (in
the whole system) to a file object is closed. In other words, the open
handle count for the file object goes to 0. A driver that holds pending
IRPs internally must implement a routine for IRP_MJ_CLEANUP. When the
routine is called, the driver should cancel all the pending IRPs that
belong to the file object identified by the IRP_MJ_CLEANUP call. In other
words, it should cancel all the IRPs that have the same file-object pointer
as the one supplied in the current I/O stack location of the IRP for the
IRP_MJ_CLEANUP call. Of course, IRPs belonging to other file objects should
not be canceled. Also, if an outstanding IRP is completed immediately, the
driver does not have to cancel it.
Arguments:
DeviceObject -- pointer to the device object
Irp -- pointer to the requesing Irp
Return Value:
STATUS_SUCCESS -- if the poll succeeded,
--*/
{
PDEVICE_EXTENSION devExtension;
PIRP pendingIrp;
PIO_STACK_LOCATION irpStack;
PFILE_CONTEXT fileContext;
NTSTATUS status;
CSAMP_KDPRINT(("CsampCleanupIrp enter\n"));
devExtension = DeviceObject->DeviceExtension;
irpStack = IoGetCurrentIrpStackLocation(Irp);
ASSERT(irpStack->FileObject != NULL);
fileContext = irpStack->FileObject->FsContext;
//
// This acquire cannot fail because you cannot get more than one
// cleanup for the same handle.
//
status = IoAcquireRemoveLock(&fileContext->FileRundownLock, Irp);
ASSERT(NT_SUCCESS(status));
//
// Wait for all the threads that are currently dispatching to exit and
// prevent any threads dispatching I/O on the same handle beyond this point.
//
IoReleaseRemoveLockAndWait(&fileContext->FileRundownLock, Irp);
pendingIrp = IoCsqRemoveNextIrp(&devExtension->CancelSafeQueue,
irpStack->FileObject);
while(pendingIrp)
{
//
// Cancel the IRP
//
pendingIrp->IoStatus.Information = 0;
pendingIrp->IoStatus.Status = STATUS_CANCELLED;
CSAMP_KDPRINT(("Cleanup cancelled irp\n"));
IoCompleteRequest(pendingIrp, IO_NO_INCREMENT);
pendingIrp = IoCsqRemoveNextIrp(&devExtension->CancelSafeQueue,
irpStack->FileObject);
}
//
// Finally complete the cleanup IRP
//
Irp->IoStatus.Information = 0;
Irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
CSAMP_KDPRINT(("CsampCleanupIrp exit\n"));
return STATUS_SUCCESS;
}
VOID
CsampUnload(
_In_ PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Free all the allocated resources, etc.
Arguments:
DriverObject - pointer to a driver object.
Return Value:
VOID
--*/
{
PDEVICE_OBJECT deviceObject = DriverObject->DeviceObject;
UNICODE_STRING uniWin32NameString;
PDEVICE_EXTENSION devExtension = deviceObject->DeviceExtension;
PAGED_CODE();
CSAMP_KDPRINT(("CsampUnload Enter\n"));
//
// Set the Stop flag
//
devExtension->ThreadShouldStop = TRUE;
//
// Make sure the thread wakes up
//
#pragma prefast(suppress: __WARNING_ERROR, "Passing TRUE as last parameter of KeReleaseSemaphore is just a hint that a wait is next.")
KeReleaseSemaphore(&devExtension->IrpQueueSemaphore,
0, // No priority boost
1, // Increment semaphore by 1
TRUE );// WaitForXxx after this call
//
// Wait for the thread to terminate
//
KeWaitForSingleObject(devExtension->ThreadObject,
Executive,
KernelMode,
FALSE,
NULL );
ObDereferenceObject(devExtension->ThreadObject);
//
// Create counted string version of our Win32 device name.
//
RtlInitUnicodeString( &uniWin32NameString, CSAMP_DOS_DEVICE_NAME_U );
IoDeleteSymbolicLink( &uniWin32NameString );
IoDeleteDevice( deviceObject );
CSAMP_KDPRINT(("CsampUnload Exit\n"));
return;
}
VOID CsampInsertIrp (
_In_ PIO_CSQ Csq,
_In_ PIRP Irp
)
{
PDEVICE_EXTENSION devExtension;
devExtension = CONTAINING_RECORD(Csq,
DEVICE_EXTENSION, CancelSafeQueue);
InsertTailList(&devExtension->PendingIrpQueue,
&Irp->Tail.Overlay.ListEntry);
}
VOID CsampRemoveIrp(
_In_ PIO_CSQ Csq,
_In_ PIRP Irp
)
{
UNREFERENCED_PARAMETER(Csq);
RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
}
PIRP CsampPeekNextIrp(
_In_ PIO_CSQ Csq,
_In_ PIRP Irp,
_In_ PVOID PeekContext
)
{
PDEVICE_EXTENSION devExtension;
PIRP nextIrp = NULL;
PLIST_ENTRY nextEntry;
PLIST_ENTRY listHead;
PIO_STACK_LOCATION irpStack;
devExtension = CONTAINING_RECORD(Csq,
DEVICE_EXTENSION, CancelSafeQueue);
listHead = &devExtension->PendingIrpQueue;
//
// If the IRP is NULL, we will start peeking from the listhead, else
// we will start from that IRP onwards. This is done under the
// assumption that new IRPs are always inserted at the tail.
//
if (Irp == NULL) {
nextEntry = listHead->Flink;
} else {
nextEntry = Irp->Tail.Overlay.ListEntry.Flink;
}
while(nextEntry != listHead) {
nextIrp = CONTAINING_RECORD(nextEntry, IRP, Tail.Overlay.ListEntry);
irpStack = IoGetCurrentIrpStackLocation(nextIrp);
//
// If context is present, continue until you find a matching one.
// Else you break out as you got next one.
//
if (PeekContext) {
if (irpStack->FileObject == (PFILE_OBJECT) PeekContext) {
break;
}
} else {
break;
}
nextIrp = NULL;
nextEntry = nextEntry->Flink;
}
return nextIrp;
}
//
// CsampAcquireLock modifies the execution level of the current processor.
//
// KeAcquireSpinLock raises the execution level to Dispatch Level and stores
// the current execution level in the Irql parameter to be restored at a later
// time. KeAcqurieSpinLock also requires us to be running at no higher than
// Dispatch level when it is called.
//
// The annotations reflect these changes and requirments.
//
_IRQL_raises_(DISPATCH_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL)
_Acquires_lock_(CONTAINING_RECORD(Csq,DEVICE_EXTENSION, CancelSafeQueue)->QueueLock)
VOID CsampAcquireLock(
_In_ PIO_CSQ Csq,
_Out_ _At_(*Irql, _Post_ _IRQL_saves_) PKIRQL Irql
)
{
PDEVICE_EXTENSION devExtension;
devExtension = CONTAINING_RECORD(Csq,
DEVICE_EXTENSION, CancelSafeQueue);
//
// Suppressing because the address below csq is valid since it's
// part of DEVICE_EXTENSION structure.
//
#pragma prefast(suppress: __WARNING_BUFFER_UNDERFLOW, "Underflow using expression 'devExtension->QueueLock'")
KeAcquireSpinLock(&devExtension->QueueLock, Irql);
}
//
// CsampReleaseLock modifies the execution level of the current processor.
//
// KeReleaseSpinLock assumes we already hold the spin lock and are therefore
// running at Dispatch level. It will use the Irql parameter saved in a
// previous call to KeAcquireSpinLock to return the thread back to it's original
// execution level.
//
// The annotations reflect these changes and requirments.
//
_IRQL_requires_(DISPATCH_LEVEL)
_Releases_lock_(CONTAINING_RECORD(Csq,DEVICE_EXTENSION, CancelSafeQueue)->QueueLock)
VOID CsampReleaseLock(
_In_ PIO_CSQ Csq,
_In_ _IRQL_restores_ KIRQL Irql
)
{
PDEVICE_EXTENSION devExtension;
devExtension = CONTAINING_RECORD(Csq,
DEVICE_EXTENSION, CancelSafeQueue);
//
// Suppressing because the address below csq is valid since it's
// part of DEVICE_EXTENSION structure.
//
#pragma prefast(suppress: __WARNING_BUFFER_UNDERFLOW, "Underflow using expression 'devExtension->QueueLock'")
KeReleaseSpinLock(&devExtension->QueueLock, Irql);
}
VOID CsampCompleteCanceledIrp(
_In_ PIO_CSQ pCsq,
_In_ PIRP Irp
)
{
UNREFERENCED_PARAMETER(pCsq);
Irp->IoStatus.Status = STATUS_CANCELLED;
Irp->IoStatus.Information = 0;
CSAMP_KDPRINT(("cancelled irp\n"));
IoCompleteRequest(Irp, IO_NO_INCREMENT);
}
|