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
|
/*++
Copyright (C) Microsoft Corporation. All rights reserved.
Module Name:
mmc.h
Abstract:
Functions/macros for using the scratch buffer and scratch request/SRB
in the serial I/O queue context
Author:
Environment:
kernel mode only
Notes:
Revision History:
--*/
#ifndef __SCRATCH_H__
#define __SCRATCH_H__
_IRQL_requires_max_(APC_LEVEL)
VOID
ScratchBuffer_Deallocate(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
_IRQL_requires_max_(APC_LEVEL)
BOOLEAN
ScratchBuffer_Allocate(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension
);
VOID
ScratchBuffer_ResetItems(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN ResetRequestHistory
);
_IRQL_requires_max_(APC_LEVEL)
VOID
ScratchBuffer_SetupSrb(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ WDFREQUEST OriginalRequest,
_In_ ULONG MaximumTransferLength,
_In_ BOOLEAN GetDataFromDevice
);
VOID
ScratchBuffer_SetupReadWriteSrb(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ WDFREQUEST OriginalRequest,
_In_ LARGE_INTEGER StartingOffset,
_In_ ULONG RequiredLength,
_Inout_updates_bytes_(RequiredLength) UCHAR* DataBuffer,
_In_ BOOLEAN IsReadRequest,
_In_ BOOLEAN UsePartialMdl
);
NTSTATUS
ScratchBuffer_SendSrb(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN SynchronousSrb,
_When_(SynchronousSrb, _Pre_null_)
_When_(!SynchronousSrb, _In_opt_)
PSRB_HISTORY_ITEM *SrbHistoryItem
);
NTSTATUS
ScratchBuffer_PerformNextReadWrite(
_In_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_ BOOLEAN FirstTry
);
#if DBG
#define ScratchBuffer_BeginUse(context) ScratchBuffer_BeginUseX((context), __FILE__, __LINE__)
#else
#define ScratchBuffer_BeginUse(context) ScratchBuffer_BeginUseX((context), NULL, (ULONG)-1)
#endif
__inline VOID ScratchBuffer_BeginUseX(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension, _In_opt_ LPCSTR File, ULONG Line)
{
// NOTE: these are not "real" locks. They are simply to help
// avoid multiple uses of the scratch buffer. Thus, it
// is not critical to have atomic operations here.
PVOID tmp = InterlockedCompareExchangePointer((PVOID)&(DeviceExtension->ScratchContext.ScratchInUse), (PVOID)-1, NULL);
NT_ASSERT(tmp == NULL);
UNREFERENCED_PARAMETER(tmp); //defensive coding, avoid PREFAST warning.
DeviceExtension->ScratchContext.ScratchInUseFileName = File;
DeviceExtension->ScratchContext.ScratchInUseLineNumber = Line;
ScratchBuffer_ResetItems(DeviceExtension, TRUE);
RequestClearSendTime(DeviceExtension->ScratchContext.ScratchRequest);
return;
}
__inline VOID ScratchBuffer_EndUse(_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension)
{
// NOTE: these are not "real" locks. They are simply to help
// avoid multiple uses of the scratch buffer. Thus, it
// is not critical to have atomic operations here.
// On lock release, we erase ScratchInUseFileName and ScratchInUseLineNumber _before_ releasing ScratchInUse,
// because otherwise we may erase these after the lock has been acquired again by another thread. We store the
// old values of ScratchInUseFileName and ScratchInUseLineNumber in local variables to facilitate debugging,
// if the ASSERT at the end of the function is hit.
PCSTR scratchInUseFileName;
ULONG scratchInUseLineNumber;
PVOID tmp;
scratchInUseFileName = DeviceExtension->ScratchContext.ScratchInUseFileName;
scratchInUseLineNumber = DeviceExtension->ScratchContext.ScratchInUseLineNumber;
UNREFERENCED_PARAMETER(scratchInUseFileName);
UNREFERENCED_PARAMETER(scratchInUseLineNumber);
DeviceExtension->ScratchContext.ScratchInUseFileName = NULL;
DeviceExtension->ScratchContext.ScratchInUseLineNumber = 0;
//
// If we have used the PartialMdl in the scratch context we should notify MM that we will be reusing it
// otherwise it may leak System VA if some one below us has mapped the same.
//
if (DeviceExtension->ScratchContext.PartialMdlIsBuilt != FALSE)
{
MmPrepareMdlForReuse(DeviceExtension->ScratchContext.PartialMdl);
DeviceExtension->ScratchContext.PartialMdlIsBuilt = FALSE;
}
tmp = InterlockedCompareExchangePointer((PVOID)&(DeviceExtension->ScratchContext.ScratchInUse), NULL, (PVOID)-1);
NT_ASSERT(tmp == ((PVOID)-1));
UNREFERENCED_PARAMETER(tmp); //defensive coding, avoid PREFAST warning.
return;
}
VOID
CompressSrbHistoryData(
_Inout_ PSRB_HISTORY RequestHistory
);
VOID
ValidateSrbHistoryDataPresumptions(
_In_ SRB_HISTORY const* RequestHistory
);
_IRQL_requires_max_(APC_LEVEL)
NTSTATUS
ScratchBuffer_ExecuteCdbEx(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ WDFREQUEST OriginalRequest,
_In_ ULONG TransferSize,
_In_ BOOLEAN GetDataFromDevice,
_In_ PCDB Cdb,
_In_ UCHAR OprationLength,
_In_ ULONG TimeoutValue
);
_IRQL_requires_max_(APC_LEVEL)
__inline
NTSTATUS
ScratchBuffer_ExecuteCdb(
_Inout_ PCDROM_DEVICE_EXTENSION DeviceExtension,
_In_opt_ WDFREQUEST OriginalRequest,
_In_ ULONG TransferSize,
_In_ BOOLEAN GetDataFromDevice,
_In_ PCDB Cdb,
_In_ UCHAR OprationLength
)
{
return ScratchBuffer_ExecuteCdbEx(DeviceExtension,
OriginalRequest,
TransferSize,
GetDataFromDevice,
Cdb,
OprationLength,
0);
}
KDEFERRED_ROUTINE ScratchBuffer_ReadWriteTimerRoutine;
#endif //__SCRATCH_H__
|