annotate libdha/dhahelperwin/dhahelper.c @ 21007:ae51c2fe7310

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