12057
|
1 /******************************************************************************
|
|
2 * dhahelper.c: direct hardware access under Windows NT/2000/XP
|
|
3 * Copyright (c) 2004 Sascha Sommer <saschasommer@freenet.de>.
|
|
4 *
|
23277
|
5 * This file is part of MPlayer.
|
|
6 *
|
|
7 * MPlayer is free software; you can redistribute it and/or modify
|
12057
|
8 * it under the terms of the GNU General Public License as published by
|
|
9 * the Free Software Foundation; either version 2 of the License, or
|
|
10 * (at your option) any later version.
|
|
11 *
|
23277
|
12 * MPlayer is distributed in the hope that it will be useful,
|
12057
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
23277
|
18 * along with MPlayer; if not, write to the Free Software
|
19614
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
12057
|
20 *
|
|
21 *****************************************************************************/
|
|
22
|
|
23
|
|
24 #include <ntddk.h>
|
|
25 #include "dhahelper.h"
|
|
26
|
|
27 #define OutputDebugString DbgPrint
|
|
28
|
|
29 #define IOPM_SIZE 0x2000
|
|
30 typedef char IOPM[IOPM_SIZE];
|
|
31 static IOPM *pIOPM = NULL;
|
|
32
|
|
33
|
|
34
|
|
35 typedef struct {
|
|
36 PMDL Mdl;
|
|
37 PVOID SystemVirtualAddress;
|
|
38 PVOID UserVirtualAddress;
|
|
39 ULONG PhysMemSizeInBytes;
|
|
40 }alloc_priv;
|
|
41 static alloc_priv* alloclist;
|
|
42 static unsigned int alloccount=0;
|
|
43
|
|
44
|
|
45
|
|
46
|
|
47
|
|
48
|
|
49
|
|
50 static NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp);
|
|
51 static void dhahelperunload(IN PDRIVER_OBJECT DriverObject);
|
|
52 static NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress);
|
|
53 static NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin);
|
|
54
|
|
55 void Ke386SetIoAccessMap(int, IOPM *);
|
|
56 void Ke386QueryIoAccessMap(int, IOPM *);
|
|
57 void Ke386IoSetAccessProcess(PEPROCESS, int);
|
|
58
|
|
59
|
|
60
|
|
61
|
|
62 //entry point
|
|
63 NTSTATUS DriverEntry (IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath){
|
|
64 UNICODE_STRING DeviceNameUnicodeString;
|
|
65 UNICODE_STRING DeviceLinkUnicodeString;
|
|
66 NTSTATUS ntStatus;
|
|
67 PDEVICE_OBJECT DeviceObject = NULL;
|
|
68
|
|
69 OutputDebugString ("dhahelper: entering DriverEntry");
|
|
70
|
|
71 RtlInitUnicodeString (&DeviceNameUnicodeString, L"\\Device\\DHAHELPER");
|
|
72
|
|
73 // Create an EXCLUSIVE device object (only 1 thread at a time
|
|
74 // can make requests to this device).
|
|
75
|
|
76 ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_DHAHELPER,0,TRUE,&DeviceObject);
|
|
77
|
|
78 if (NT_SUCCESS(ntStatus)){
|
|
79 // Create dispatch points for device control, create, close.
|
|
80 DriverObject->MajorFunction[IRP_MJ_CREATE] =
|
|
81 DriverObject->MajorFunction[IRP_MJ_CLOSE] =
|
|
82 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = dhahelperdispatch;
|
|
83 DriverObject->DriverUnload = dhahelperunload;
|
|
84
|
|
85 // Create a symbolic link, e.g. a name that a Win32 app can specify
|
|
86 // to open the device.
|
|
87
|
|
88 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER");
|
|
89
|
|
90 ntStatus = IoCreateSymbolicLink(&DeviceLinkUnicodeString,&DeviceNameUnicodeString);
|
|
91
|
|
92 if (!NT_SUCCESS(ntStatus)){
|
|
93 // Symbolic link creation failed- note this & then delete the
|
|
94 // device object (it's useless if a Win32 app can't get at it).
|
|
95 OutputDebugString ("dhahelper: IoCreateSymbolicLink failed");
|
|
96 IoDeleteDevice (DeviceObject);
|
|
97 }
|
|
98 }
|
|
99 else{
|
|
100 OutputDebugString ("dhahelper: IoCreateDevice failed");
|
|
101 }
|
|
102 OutputDebugString ("dhahelper: leaving DriverEntry");
|
|
103 return ntStatus;
|
|
104 }
|
|
105
|
|
106
|
|
107 // Process the IRPs sent to this device
|
|
108
|
|
109 static NTSTATUS dhahelperdispatch(IN PDEVICE_OBJECT DeviceObject,IN PIRP Irp){
|
|
110 PIO_STACK_LOCATION IrpStack;
|
|
111 ULONG dwInputBufferLength;
|
|
112 ULONG dwOutputBufferLength;
|
|
113 ULONG dwIoControlCode;
|
|
114 PVOID pvIOBuffer;
|
|
115 NTSTATUS ntStatus;
|
|
116 dhahelper_t dhahelper_priv;
|
|
117
|
|
118 OutputDebugString ("dhahelper: entering dhahelperdispatch");
|
|
119
|
|
120 // Init to default settings
|
|
121
|
|
122 Irp->IoStatus.Status = STATUS_SUCCESS;
|
|
123 Irp->IoStatus.Information = 0;
|
|
124
|
|
125 IrpStack = IoGetCurrentIrpStackLocation(Irp);
|
|
126
|
|
127 // Get the pointer to the input/output buffer and it's length
|
|
128
|
|
129 pvIOBuffer = Irp->AssociatedIrp.SystemBuffer;
|
|
130 dwInputBufferLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
|
|
131 dwOutputBufferLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
|
|
132
|
|
133 switch (IrpStack->MajorFunction){
|
|
134 case IRP_MJ_CREATE:
|
|
135 OutputDebugString("dhahelper: IRP_MJ_CREATE");
|
|
136 break;
|
|
137 case IRP_MJ_CLOSE:
|
|
138 OutputDebugString("dhahelper: IRP_MJ_CLOSE");
|
|
139 break;
|
|
140 case IRP_MJ_DEVICE_CONTROL:
|
|
141 OutputDebugString("dhahelper: IRP_MJ_DEVICE_CONTROL");
|
|
142 dwIoControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
|
|
143 switch (dwIoControlCode){
|
|
144 case IOCTL_DHAHELPER_ENABLEDIRECTIO:
|
|
145 OutputDebugString("dhahelper: IOCTL_DHAHELPER_ENABLEDIRECTIO");
|
|
146 pIOPM = MmAllocateNonCachedMemory(sizeof(IOPM));
|
|
147 if (pIOPM){
|
|
148 RtlZeroMemory(pIOPM, sizeof(IOPM));
|
|
149 Ke386IoSetAccessProcess(PsGetCurrentProcess(), 1);
|
|
150 Ke386SetIoAccessMap(1, pIOPM);
|
|
151 }
|
|
152 else Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
|
|
153 break;
|
|
154 case IOCTL_DHAHELPER_DISABLEDIRECTIO:
|
|
155 OutputDebugString("dhahelper: IOCTL_DHAHELPER_DISABLEDIRECTIO");
|
|
156 if (pIOPM){
|
|
157 Ke386IoSetAccessProcess(PsGetCurrentProcess(), 0);
|
|
158 Ke386SetIoAccessMap(1, pIOPM);
|
|
159 MmFreeNonCachedMemory(pIOPM, sizeof(IOPM));
|
|
160 pIOPM = NULL;
|
|
161 }
|
|
162 break;
|
|
163 case IOCTL_DHAHELPER_MAPPHYSTOLIN:
|
|
164 OutputDebugString("dhahelper: IOCTL_DHAHELPER_MAPPHYSTOLIN");
|
|
165 if (dwInputBufferLength){
|
|
166 memcpy (&dhahelper_priv, pvIOBuffer, dwInputBufferLength);
|
|
167 ntStatus = MapPhysicalMemoryToLinearSpace(dhahelper_priv.base,dhahelper_priv.size,&dhahelper_priv.ptr);
|
|
168 if (NT_SUCCESS(ntStatus)){
|
|
169 memcpy (pvIOBuffer, &dhahelper_priv, dwInputBufferLength);
|
|
170 Irp->IoStatus.Information = dwInputBufferLength;
|
|
171 }
|
|
172 Irp->IoStatus.Status = ntStatus;
|
|
173 }
|
|
174 else Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
|
|
175 break;
|
|
176 case IOCTL_DHAHELPER_UNMAPPHYSADDR:
|
|
177 OutputDebugString("dhahelper: IOCTL_DHAHELPER_UNMAPPHYSADDR");
|
|
178 if (dwInputBufferLength){
|
|
179 memcpy (&dhahelper_priv, pvIOBuffer, dwInputBufferLength);
|
|
180 ntStatus = UnmapPhysicalMemory(dhahelper_priv.ptr);
|
|
181 Irp->IoStatus.Status = ntStatus;
|
|
182 }
|
|
183 else
|
|
184 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
|
|
185 break;
|
|
186 default:
|
|
187 OutputDebugString("dhahelper: unknown IRP_MJ_DEVICE_CONTROL");
|
|
188 Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
|
|
189 break;
|
|
190 }
|
|
191 break;
|
|
192 }
|
|
193
|
|
194 // DON'T get cute and try to use the status field of the irp in the
|
|
195 // return status. That IRP IS GONE as soon as you call IoCompleteRequest.
|
|
196
|
|
197 ntStatus = Irp->IoStatus.Status;
|
|
198
|
|
199 IoCompleteRequest (Irp, IO_NO_INCREMENT);
|
|
200
|
|
201 // We never have pending operation so always return the status code.
|
|
202
|
|
203 OutputDebugString("dhahelper: leaving dhahelperdispatch");
|
|
204
|
|
205 return ntStatus;
|
|
206 }
|
|
207
|
|
208 // Delete the associated device and return
|
|
209
|
|
210 static void dhahelperunload(IN PDRIVER_OBJECT DriverObject){
|
|
211 UNICODE_STRING DeviceLinkUnicodeString;
|
|
212 NTSTATUS ntStatus=STATUS_SUCCESS;
|
|
213 OutputDebugString ("dhahelper: entering dhahelperunload");
|
|
214 OutputDebugString ("dhahelper: unmapping remaining memory");
|
|
215
|
|
216 while(alloccount && (ntStatus==STATUS_SUCCESS))ntStatus = UnmapPhysicalMemory(alloclist[alloccount-1].UserVirtualAddress);
|
|
217 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER");
|
|
218 ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString);
|
|
219
|
|
220 if (NT_SUCCESS(ntStatus)){
|
|
221 IoDeleteDevice (DriverObject->DeviceObject);
|
|
222 }
|
|
223 else {
|
|
224 OutputDebugString ("dhahelper: IoDeleteSymbolicLink failed");
|
|
225 }
|
|
226 OutputDebugString ("dhahelper: leaving dhahelperunload");
|
|
227 }
|
|
228
|
|
229
|
|
230
|
|
231
|
|
232
|
|
233
|
|
234 /************************* memory mapping functions ******************************/
|
|
235 //unlike the functions of other io helpers these functions allow to map adapter memory on windows xp
|
|
236 //even if it has alread been mapped by the original driver
|
|
237 //the technique used is described in
|
|
238 //http://support.microsoft.com/default.aspx?scid=kb;en-us;q189327
|
|
239 //furthermore it keeps a list of mapped areas to free them when the driver gets unloaded
|
|
240 //I'm not sure what the limitations of ZwMapViewOfSection are but mapping 128MB videoram (that is probably already mapped by the gfxcard driver)
|
|
241 //won't work so it is generally a good idea to map only the memory you really need
|
|
242
|
|
243 static NTSTATUS MapPhysicalMemoryToLinearSpace(PVOID pPhysAddress,ULONG PhysMemSizeInBytes,PVOID *PhysMemLin){
|
|
244 alloc_priv* alloclisttmp;
|
|
245 PMDL Mdl=NULL;
|
|
246 PVOID SystemVirtualAddress=NULL;
|
|
247 PVOID UserVirtualAddress=NULL;
|
|
248 PHYSICAL_ADDRESS pStartPhysAddress;
|
|
249 OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace");
|
|
250
|
|
251 pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress;
|
|
252 __try {
|
|
253 SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, /*MmWriteCombined*/MmNonCached);
|
|
254 if(!SystemVirtualAddress){
|
|
255 OutputDebugString("dhahelper: MmMapIoSpace failed");
|
|
256 return STATUS_INVALID_PARAMETER;
|
|
257 }
|
|
258 OutputDebugString("dhahelper: SystemVirtualAddress 0x%x",SystemVirtualAddress);
|
|
259 Mdl=IoAllocateMdl(SystemVirtualAddress, PhysMemSizeInBytes, FALSE, FALSE,NULL);
|
|
260 if(!Mdl){
|
|
261 OutputDebugString("dhahelper: IoAllocateMdl failed");
|
|
262 return STATUS_INSUFFICIENT_RESOURCES;
|
|
263 }
|
|
264 OutputDebugString("dhahelper: Mdl 0x%x",Mdl);
|
|
265 MmBuildMdlForNonPagedPool(Mdl);
|
|
266 UserVirtualAddress = (PVOID)(((ULONG)PAGE_ALIGN(MmMapLockedPages(Mdl,UserMode))) + MmGetMdlByteOffset(Mdl));
|
|
267 if(!UserVirtualAddress){
|
|
268 OutputDebugString("dhahelper: MmMapLockedPages failed");
|
|
269 return STATUS_INSUFFICIENT_RESOURCES;
|
|
270 }
|
|
271 OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress);
|
|
272 }__except(EXCEPTION_EXECUTE_HANDLER){
|
|
273 NTSTATUS ntStatus;
|
|
274 ntStatus = GetExceptionCode();
|
|
275 OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus);
|
|
276 return ntStatus;
|
|
277 }
|
|
278
|
|
279
|
|
280 OutputDebugString("dhahelper: adding data to internal allocation list");
|
|
281 alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv));
|
|
282
|
|
283
|
|
284 if(!alloclisttmp){
|
|
285 OutputDebugString("dhahelper: not enough memory to create temporary allocation list");
|
|
286 MmUnmapLockedPages(UserVirtualAddress, Mdl);
|
|
287 IoFreeMdl(Mdl);
|
|
288 return STATUS_INSUFFICIENT_RESOURCES;
|
|
289 }
|
|
290 if(alloccount){
|
|
291 memcpy(alloclisttmp,alloclist,alloccount * sizeof(alloc_priv));
|
|
292 MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv));
|
|
293 }
|
|
294 alloclist=alloclisttmp;
|
|
295 alloclist[alloccount].Mdl=Mdl;
|
|
296 alloclist[alloccount].SystemVirtualAddress=SystemVirtualAddress;
|
|
297 alloclist[alloccount].UserVirtualAddress=UserVirtualAddress;
|
|
298 alloclist[alloccount].PhysMemSizeInBytes=PhysMemSizeInBytes;
|
|
299 ++alloccount;
|
|
300
|
|
301 *PhysMemLin=UserVirtualAddress;
|
|
302
|
|
303 OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace");
|
|
304 return STATUS_SUCCESS;
|
|
305 }
|
|
306
|
|
307 static NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){
|
|
308 unsigned int i;
|
|
309 unsigned int x=0;
|
|
310 unsigned int alloccounttmp=alloccount;
|
|
311 OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress);
|
|
312 if(!alloccount){
|
|
313 OutputDebugString("dhahelper: UnmapPhysicalMemory: nothing todo -> leaving...");
|
|
314 return STATUS_SUCCESS;
|
|
315 }
|
|
316
|
|
317 for(i=0;i<alloccount;i++){
|
|
318 if(alloclist[i].UserVirtualAddress!=UserVirtualAddress){
|
|
319 if(x!=i){
|
|
320 alloclist[x].Mdl=alloclist[i].Mdl;
|
|
321 alloclist[x].SystemVirtualAddress=alloclist[i].SystemVirtualAddress;
|
|
322 alloclist[x].UserVirtualAddress=alloclist[i].UserVirtualAddress;
|
|
323 alloclist[x].PhysMemSizeInBytes=alloclist[i].PhysMemSizeInBytes;
|
|
324
|
|
325 }
|
|
326 x++;
|
|
327 }
|
|
328 else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){
|
|
329 if(x==i){
|
|
330 __try {
|
|
331 MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl);
|
|
332 IoFreeMdl(alloclist[x].Mdl);
|
|
333 MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes);
|
|
334 }__except(EXCEPTION_EXECUTE_HANDLER){
|
|
335 NTSTATUS ntStatus;
|
|
336 ntStatus = GetExceptionCode();
|
|
337 OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl);
|
|
338 return ntStatus;
|
|
339 }
|
|
340 }
|
|
341 alloccounttmp--;
|
|
342 }
|
|
343
|
|
344 }
|
|
345
|
|
346 if(alloccounttmp){
|
|
347 alloc_priv* alloclisttmp;
|
|
348 alloclisttmp=MmAllocateNonCachedMemory(alloccounttmp*sizeof(alloc_priv));
|
|
349 if(!alloclisttmp){
|
|
350 OutputDebugString("dhahelper: not enough memory to create temporary allocation list");
|
|
351 return STATUS_INSUFFICIENT_RESOURCES;
|
|
352 }
|
|
353 memcpy(alloclisttmp,alloclist,alloccounttmp * sizeof(alloc_priv));
|
|
354 MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv));
|
|
355 alloclist=alloclisttmp;
|
|
356 }
|
|
357 alloccount=alloccounttmp;
|
|
358
|
|
359 OutputDebugString("dhahelper: leaving UnmapPhysicalMemory");
|
|
360 return STATUS_SUCCESS;
|
|
361 }
|