annotate vidix/dhahelperwin/dhahelper.c @ 23277:32873ccfb007

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