comparison vidix/dhahelperwin/dhahelper.c @ 29263:0f1b5b68af32

whitespace cosmetics: Remove all trailing whitespace.
author diego
date Wed, 13 May 2009 02:58:57 +0000
parents 2391228b7ff0
children
comparison
equal deleted inserted replaced
29262:7d545a6b8aff 29263:0f1b5b68af32
85 85
86 // Create an EXCLUSIVE device object (only 1 thread at a time 86 // Create an EXCLUSIVE device object (only 1 thread at a time
87 // can make requests to this device). 87 // can make requests to this device).
88 88
89 ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_DHAHELPER,0,TRUE,&DeviceObject); 89 ntStatus = IoCreateDevice(DriverObject,0,&DeviceNameUnicodeString,FILE_DEVICE_DHAHELPER,0,TRUE,&DeviceObject);
90 90
91 if (NT_SUCCESS(ntStatus)){ 91 if (NT_SUCCESS(ntStatus)){
92 // Create dispatch points for device control, create, close. 92 // Create dispatch points for device control, create, close.
93 DriverObject->MajorFunction[IRP_MJ_CREATE] = 93 DriverObject->MajorFunction[IRP_MJ_CREATE] =
94 DriverObject->MajorFunction[IRP_MJ_CLOSE] = 94 DriverObject->MajorFunction[IRP_MJ_CLOSE] =
95 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = dhahelperdispatch; 95 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = dhahelperdispatch;
132 132
133 // Init to default settings 133 // Init to default settings
134 134
135 Irp->IoStatus.Status = STATUS_SUCCESS; 135 Irp->IoStatus.Status = STATUS_SUCCESS;
136 Irp->IoStatus.Information = 0; 136 Irp->IoStatus.Information = 0;
137 137
138 IrpStack = IoGetCurrentIrpStackLocation(Irp); 138 IrpStack = IoGetCurrentIrpStackLocation(Irp);
139 139
140 // Get the pointer to the input/output buffer and it's length 140 // Get the pointer to the input/output buffer and it's length
141 141
142 pvIOBuffer = Irp->AssociatedIrp.SystemBuffer; 142 pvIOBuffer = Irp->AssociatedIrp.SystemBuffer;
223 static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject){ 223 static STDCALL void dhahelperunload(IN PDRIVER_OBJECT DriverObject){
224 UNICODE_STRING DeviceLinkUnicodeString; 224 UNICODE_STRING DeviceLinkUnicodeString;
225 NTSTATUS ntStatus=STATUS_SUCCESS; 225 NTSTATUS ntStatus=STATUS_SUCCESS;
226 OutputDebugString ("dhahelper: entering dhahelperunload"); 226 OutputDebugString ("dhahelper: entering dhahelperunload");
227 OutputDebugString ("dhahelper: unmapping remaining memory"); 227 OutputDebugString ("dhahelper: unmapping remaining memory");
228 228
229 while(alloccount && (ntStatus==STATUS_SUCCESS))ntStatus = UnmapPhysicalMemory(alloclist[alloccount-1].UserVirtualAddress); 229 while(alloccount && (ntStatus==STATUS_SUCCESS))ntStatus = UnmapPhysicalMemory(alloclist[alloccount-1].UserVirtualAddress);
230 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER"); 230 RtlInitUnicodeString (&DeviceLinkUnicodeString, L"\\DosDevices\\DHAHELPER");
231 ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString); 231 ntStatus = IoDeleteSymbolicLink (&DeviceLinkUnicodeString);
232 232
233 if (NT_SUCCESS(ntStatus)){ 233 if (NT_SUCCESS(ntStatus)){
258 PMDL Mdl=NULL; 258 PMDL Mdl=NULL;
259 PVOID SystemVirtualAddress=NULL; 259 PVOID SystemVirtualAddress=NULL;
260 PVOID UserVirtualAddress=NULL; 260 PVOID UserVirtualAddress=NULL;
261 PHYSICAL_ADDRESS pStartPhysAddress; 261 PHYSICAL_ADDRESS pStartPhysAddress;
262 OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace"); 262 OutputDebugString ("dhahelper: entering MapPhysicalMemoryToLinearSpace");
263 263
264 #ifdef _WIN64 264 #ifdef _WIN64
265 pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress; 265 pStartPhysAddress.QuadPart = (ULONGLONG)pPhysAddress;
266 #else 266 #else
267 pStartPhysAddress.QuadPart = (ULONGLONG)(ULONG)pPhysAddress; 267 pStartPhysAddress.QuadPart = (ULONGLONG)(ULONG)pPhysAddress;
268 #endif 268 #endif
271 #endif 271 #endif
272 SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, /*MmWriteCombined*/MmNonCached); 272 SystemVirtualAddress=MmMapIoSpace(pStartPhysAddress,PhysMemSizeInBytes, /*MmWriteCombined*/MmNonCached);
273 if(!SystemVirtualAddress){ 273 if(!SystemVirtualAddress){
274 OutputDebugString("dhahelper: MmMapIoSpace failed"); 274 OutputDebugString("dhahelper: MmMapIoSpace failed");
275 return STATUS_INVALID_PARAMETER; 275 return STATUS_INVALID_PARAMETER;
276 } 276 }
277 OutputDebugString("dhahelper: SystemVirtualAddress 0x%x",SystemVirtualAddress); 277 OutputDebugString("dhahelper: SystemVirtualAddress 0x%x",SystemVirtualAddress);
278 Mdl=IoAllocateMdl(SystemVirtualAddress, PhysMemSizeInBytes, FALSE, FALSE,NULL); 278 Mdl=IoAllocateMdl(SystemVirtualAddress, PhysMemSizeInBytes, FALSE, FALSE,NULL);
279 if(!Mdl){ 279 if(!Mdl){
280 OutputDebugString("dhahelper: IoAllocateMdl failed"); 280 OutputDebugString("dhahelper: IoAllocateMdl failed");
281 return STATUS_INSUFFICIENT_RESOURCES; 281 return STATUS_INSUFFICIENT_RESOURCES;
291 OutputDebugString("dhahelper: MmMapLockedPages failed"); 291 OutputDebugString("dhahelper: MmMapLockedPages failed");
292 return STATUS_INSUFFICIENT_RESOURCES; 292 return STATUS_INSUFFICIENT_RESOURCES;
293 } 293 }
294 OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress); 294 OutputDebugString("dhahelper: UserVirtualAddress 0x%x",UserVirtualAddress);
295 #ifndef NO_SEH 295 #ifndef NO_SEH
296 }__except(EXCEPTION_EXECUTE_HANDLER){ 296 }__except(EXCEPTION_EXECUTE_HANDLER){
297 NTSTATUS ntStatus; 297 NTSTATUS ntStatus;
298 ntStatus = GetExceptionCode(); 298 ntStatus = GetExceptionCode();
299 OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus); 299 OutputDebugString("dhahelper: MapPhysicalMemoryToLinearSpace failed due to exception 0x%0x\n", ntStatus);
300 return ntStatus; 300 return ntStatus;
301 } 301 }
302 #endif 302 #endif
303 303
304 304
305 OutputDebugString("dhahelper: adding data to internal allocation list"); 305 OutputDebugString("dhahelper: adding data to internal allocation list");
306 alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv)); 306 alloclisttmp=MmAllocateNonCachedMemory((alloccount+1)*sizeof(alloc_priv));
307 307
308 308
309 if(!alloclisttmp){ 309 if(!alloclisttmp){
310 OutputDebugString("dhahelper: not enough memory to create temporary allocation list"); 310 OutputDebugString("dhahelper: not enough memory to create temporary allocation list");
311 MmUnmapLockedPages(UserVirtualAddress, Mdl); 311 MmUnmapLockedPages(UserVirtualAddress, Mdl);
312 IoFreeMdl(Mdl); 312 IoFreeMdl(Mdl);
313 return STATUS_INSUFFICIENT_RESOURCES; 313 return STATUS_INSUFFICIENT_RESOURCES;
314 } 314 }
315 if(alloccount){ 315 if(alloccount){
316 memcpy(alloclisttmp,alloclist,alloccount * sizeof(alloc_priv)); 316 memcpy(alloclisttmp,alloclist,alloccount * sizeof(alloc_priv));
320 alloclist[alloccount].Mdl=Mdl; 320 alloclist[alloccount].Mdl=Mdl;
321 alloclist[alloccount].SystemVirtualAddress=SystemVirtualAddress; 321 alloclist[alloccount].SystemVirtualAddress=SystemVirtualAddress;
322 alloclist[alloccount].UserVirtualAddress=UserVirtualAddress; 322 alloclist[alloccount].UserVirtualAddress=UserVirtualAddress;
323 alloclist[alloccount].PhysMemSizeInBytes=PhysMemSizeInBytes; 323 alloclist[alloccount].PhysMemSizeInBytes=PhysMemSizeInBytes;
324 ++alloccount; 324 ++alloccount;
325 325
326 *PhysMemLin=UserVirtualAddress; 326 *PhysMemLin=UserVirtualAddress;
327 327
328 OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace"); 328 OutputDebugString("dhahelper: leaving MapPhysicalMemoryToLinearSpace");
329 return STATUS_SUCCESS; 329 return STATUS_SUCCESS;
330 } 330 }
331 331
332 static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){ 332 static STDCALL NTSTATUS UnmapPhysicalMemory(PVOID UserVirtualAddress){
336 OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress); 336 OutputDebugString("dhahelper: entering UnmapPhysicalMemory to unmapp 0x%x",UserVirtualAddress);
337 if(!alloccount){ 337 if(!alloccount){
338 OutputDebugString("dhahelper: UnmapPhysicalMemory: nothing todo -> leaving..."); 338 OutputDebugString("dhahelper: UnmapPhysicalMemory: nothing todo -> leaving...");
339 return STATUS_SUCCESS; 339 return STATUS_SUCCESS;
340 } 340 }
341 341
342 for(i=0;i<alloccount;i++){ 342 for(i=0;i<alloccount;i++){
343 if(alloclist[i].UserVirtualAddress!=UserVirtualAddress){ 343 if(alloclist[i].UserVirtualAddress!=UserVirtualAddress){
344 if(x!=i){ 344 if(x!=i){
345 alloclist[x].Mdl=alloclist[i].Mdl; 345 alloclist[x].Mdl=alloclist[i].Mdl;
346 alloclist[x].SystemVirtualAddress=alloclist[i].SystemVirtualAddress; 346 alloclist[x].SystemVirtualAddress=alloclist[i].SystemVirtualAddress;
347 alloclist[x].UserVirtualAddress=alloclist[i].UserVirtualAddress; 347 alloclist[x].UserVirtualAddress=alloclist[i].UserVirtualAddress;
348 alloclist[x].PhysMemSizeInBytes=alloclist[i].PhysMemSizeInBytes; 348 alloclist[x].PhysMemSizeInBytes=alloclist[i].PhysMemSizeInBytes;
349 349
350 } 350 }
351 x++; 351 x++;
352 } 352 }
353 else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){ 353 else if(alloclist[i].UserVirtualAddress==UserVirtualAddress){
354 if(x==i){ 354 if(x==i){
355 #ifndef NO_SEH 355 #ifndef NO_SEH
356 __try { 356 __try {
357 #endif 357 #endif
358 MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl); 358 MmUnmapLockedPages(alloclist[x].UserVirtualAddress, alloclist[x].Mdl);
359 IoFreeMdl(alloclist[x].Mdl); 359 IoFreeMdl(alloclist[x].Mdl);
360 MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes); 360 MmUnmapIoSpace(alloclist[x].SystemVirtualAddress,alloclist[x].PhysMemSizeInBytes);
361 #ifndef NO_SEH 361 #ifndef NO_SEH
362 }__except(EXCEPTION_EXECUTE_HANDLER){ 362 }__except(EXCEPTION_EXECUTE_HANDLER){
363 NTSTATUS ntStatus; 363 NTSTATUS ntStatus;
364 ntStatus = GetExceptionCode(); 364 ntStatus = GetExceptionCode();
365 OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl); 365 OutputDebugString("dhahelper: UnmapPhysicalMemory failed due to exception 0x%0x (Mdl 0x%x)\n", ntStatus,alloclist[x].Mdl);
366 return ntStatus; 366 return ntStatus;
367 } 367 }
368 #endif 368 #endif
369 } 369 }
370 alloccounttmp--; 370 alloccounttmp--;
371 } 371 }
372 372
373 } 373 }
374 374
375 if(alloccounttmp){ 375 if(alloccounttmp){
376 alloc_priv* alloclisttmp; 376 alloc_priv* alloclisttmp;
377 alloclisttmp=MmAllocateNonCachedMemory(alloccounttmp*sizeof(alloc_priv)); 377 alloclisttmp=MmAllocateNonCachedMemory(alloccounttmp*sizeof(alloc_priv));
378 if(!alloclisttmp){ 378 if(!alloclisttmp){
379 OutputDebugString("dhahelper: not enough memory to create temporary allocation list"); 379 OutputDebugString("dhahelper: not enough memory to create temporary allocation list");
382 memcpy(alloclisttmp,alloclist,alloccounttmp * sizeof(alloc_priv)); 382 memcpy(alloclisttmp,alloclist,alloccounttmp * sizeof(alloc_priv));
383 MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv)); 383 MmFreeNonCachedMemory(alloclist,alloccount*sizeof(alloc_priv));
384 alloclist=alloclisttmp; 384 alloclist=alloclisttmp;
385 } 385 }
386 alloccount=alloccounttmp; 386 alloccount=alloccounttmp;
387 387
388 OutputDebugString("dhahelper: leaving UnmapPhysicalMemory"); 388 OutputDebugString("dhahelper: leaving UnmapPhysicalMemory");
389 return STATUS_SUCCESS; 389 return STATUS_SUCCESS;
390 } 390 }