push 460a69df99a5ad9f5823a97170ef7a215171c033
[wine/hacks.git] / dlls / ntoskrnl.exe / ntoskrnl.c
blob81c4f33c1043199c211d8761374013ee85d076f6
1 /*
2 * ntoskrnl.exe implementation
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33 #include "excpt.h"
34 #include "ddk/ntddk.h"
35 #include "wine/unicode.h"
36 #include "wine/server.h"
37 #include "wine/list.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl);
41 WINE_DECLARE_DEBUG_CHANNEL(relay);
44 KSYSTEM_TIME KeTickCount = { 0, 0, 0 };
46 typedef struct _KSERVICE_TABLE_DESCRIPTOR
48 PULONG_PTR Base;
49 PULONG Count;
50 ULONG Limit;
51 PUCHAR Number;
52 } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
54 KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[4] = { { 0 } };
56 typedef void (WINAPI *PCREATE_PROCESS_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
57 typedef void (WINAPI *PCREATE_THREAD_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
59 static struct list Irps = LIST_INIT(Irps);
61 struct IrpInstance
63 struct list entry;
64 IRP *irp;
67 #ifdef __i386__
68 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
69 __ASM_GLOBAL_FUNC( name, \
70 "popl %eax\n\t" \
71 "pushl %ecx\n\t" \
72 "pushl %eax\n\t" \
73 "jmp " __ASM_NAME("__regs_") #name )
74 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
75 __ASM_GLOBAL_FUNC( name, \
76 "popl %eax\n\t" \
77 "pushl %edx\n\t" \
78 "pushl %ecx\n\t" \
79 "pushl %eax\n\t" \
80 "jmp " __ASM_NAME("__regs_") #name )
81 #endif
83 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
85 if (!us) return "<null>";
86 return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
89 static HANDLE get_device_manager(void)
91 static HANDLE device_manager;
92 HANDLE handle = 0, ret = device_manager;
94 if (!ret)
96 SERVER_START_REQ( create_device_manager )
98 req->access = SYNCHRONIZE;
99 req->attributes = 0;
100 if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
102 SERVER_END_REQ;
104 if (!handle)
106 ERR( "failed to create the device manager\n" );
107 return 0;
109 if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
110 ret = handle;
111 else
112 NtClose( handle ); /* somebody beat us to it */
114 return ret;
117 /* exception handler for emulation of privileged instructions */
118 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
120 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
122 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
123 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
125 #ifdef __i386__
126 CONTEXT *context = ptrs->ContextRecord;
127 extern DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT *context );
129 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
130 return EXCEPTION_CONTINUE_EXECUTION;
131 #else
132 FIXME( "Privileged instruction emulation not implemented on this CPU\n" );
133 #endif
135 return EXCEPTION_CONTINUE_SEARCH;
138 /* process an ioctl request for a given device */
139 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
140 void *out_buff, ULONG *out_size )
142 IRP irp;
143 MDL mdl;
144 IO_STACK_LOCATION irpsp;
145 PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
146 NTSTATUS status;
147 LARGE_INTEGER count;
149 TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
151 /* so we can spot things that we should initialize */
152 memset( &irp, 0x55, sizeof(irp) );
153 memset( &irpsp, 0x66, sizeof(irpsp) );
154 memset( &mdl, 0x77, sizeof(mdl) );
156 irp.RequestorMode = UserMode;
157 irp.AssociatedIrp.SystemBuffer = in_buff;
158 irp.UserBuffer = out_buff;
159 irp.MdlAddress = &mdl;
160 irp.Tail.Overlay.s.u.CurrentStackLocation = &irpsp;
161 irp.UserIosb = NULL;
163 irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
164 irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
165 irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
166 irpsp.Parameters.DeviceIoControl.IoControlCode = code;
167 irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
168 irpsp.DeviceObject = device;
169 irpsp.CompletionRoutine = NULL;
171 mdl.Next = NULL;
172 mdl.Size = 0;
173 mdl.StartVa = out_buff;
174 mdl.ByteCount = *out_size;
175 mdl.ByteOffset = 0;
177 device->CurrentIrp = &irp;
179 KeQueryTickCount( &count ); /* update the global KeTickCount */
181 if (TRACE_ON(relay))
182 DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
183 GetCurrentThreadId(), dispatch, device, &irp );
185 status = dispatch( device, &irp );
187 if (TRACE_ON(relay))
188 DPRINTF( "%04x:Ret driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
189 GetCurrentThreadId(), dispatch, device, &irp, status );
191 *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
192 return irp.IoStatus.u.Status;
196 /***********************************************************************
197 * wine_ntoskrnl_main_loop (Not a Windows API)
199 NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event )
201 HANDLE manager = get_device_manager();
202 obj_handle_t ioctl = 0;
203 NTSTATUS status = STATUS_SUCCESS;
204 ULONG code = 0;
205 void *in_buff, *out_buff = NULL;
206 DEVICE_OBJECT *device = NULL;
207 ULONG in_size = 4096, out_size = 0;
208 HANDLE handles[2];
210 if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
212 ERR( "failed to allocate buffer\n" );
213 return STATUS_NO_MEMORY;
216 handles[0] = stop_event;
217 handles[1] = manager;
219 for (;;)
221 SERVER_START_REQ( get_next_device_request )
223 req->manager = wine_server_obj_handle( manager );
224 req->prev = ioctl;
225 req->status = status;
226 wine_server_add_data( req, out_buff, out_size );
227 wine_server_set_reply( req, in_buff, in_size );
228 if (!(status = wine_server_call( req )))
230 code = reply->code;
231 ioctl = reply->next;
232 device = wine_server_get_ptr( reply->user_ptr );
233 in_size = reply->in_size;
234 out_size = reply->out_size;
236 else
238 ioctl = 0; /* no previous ioctl */
239 out_size = 0;
240 in_size = reply->in_size;
243 SERVER_END_REQ;
245 switch(status)
247 case STATUS_SUCCESS:
248 HeapFree( GetProcessHeap(), 0, out_buff );
249 if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
250 else out_buff = NULL;
251 status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
252 break;
253 case STATUS_BUFFER_OVERFLOW:
254 HeapFree( GetProcessHeap(), 0, in_buff );
255 in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
256 /* restart with larger buffer */
257 break;
258 case STATUS_PENDING:
259 if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
260 return STATUS_SUCCESS;
261 break;
267 /***********************************************************************
268 * IoAllocateDriverObjectExtension (NTOSKRNL.EXE.@)
270 NTSTATUS WINAPI IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject,
271 PVOID ClientIdentificationAddress,
272 ULONG DriverObjectExtensionSize,
273 PVOID *DriverObjectExtension )
275 FIXME( "stub: %p, %p, %u, %p\n", DriverObject, ClientIdentificationAddress,
276 DriverObjectExtensionSize, DriverObjectExtension );
277 return STATUS_NOT_IMPLEMENTED;
281 /***********************************************************************
282 * IoGetDriverObjectExtension (NTOSKRNL.EXE.@)
284 PVOID WINAPI IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject,
285 PVOID ClientIdentificationAddress )
287 FIXME( "stub: %p, %p\n", DriverObject, ClientIdentificationAddress );
288 return NULL;
292 /***********************************************************************
293 * IoInitializeIrp (NTOSKRNL.EXE.@)
295 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
297 TRACE( "%p, %u, %d\n", irp, size, stack_size );
299 RtlZeroMemory( irp, size );
301 irp->Type = IO_TYPE_IRP;
302 irp->Size = size;
303 InitializeListHead( &irp->ThreadListEntry );
304 irp->StackCount = stack_size;
305 irp->CurrentLocation = stack_size + 1;
306 irp->Tail.Overlay.s.u.CurrentStackLocation =
307 (PIO_STACK_LOCATION)(irp + 1) + stack_size;
311 /***********************************************************************
312 * IoAllocateIrp (NTOSKRNL.EXE.@)
314 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
316 SIZE_T size;
317 PIRP irp;
319 TRACE( "%d, %d\n", stack_size, charge_quota );
321 size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
322 irp = ExAllocatePool( NonPagedPool, size );
323 if (irp == NULL)
324 return NULL;
325 IoInitializeIrp( irp, size, stack_size );
326 irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
327 if (charge_quota)
328 irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
329 return irp;
333 /***********************************************************************
334 * IoFreeIrp (NTOSKRNL.EXE.@)
336 void WINAPI IoFreeIrp( IRP *irp )
338 TRACE( "%p\n", irp );
340 ExFreePool( irp );
344 /***********************************************************************
345 * IoAllocateMdl (NTOSKRNL.EXE.@)
347 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
349 FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
350 return NULL;
354 /***********************************************************************
355 * IoAllocateWorkItem (NTOSKRNL.EXE.@)
357 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
359 FIXME( "stub: %p\n", DeviceObject );
360 return NULL;
364 /***********************************************************************
365 * IoAttachDeviceToDeviceStack (NTOSKRNL.EXE.@)
367 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
368 DEVICE_OBJECT *target )
370 TRACE( "%p, %p\n", source, target );
371 target->AttachedDevice = source;
372 source->StackSize = target->StackSize + 1;
373 return target;
377 /***********************************************************************
378 * IoBuildDeviceIoControlRequest (NTOSKRNL.EXE.@)
380 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
381 PDEVICE_OBJECT DeviceObject,
382 PVOID InputBuffer,
383 ULONG InputBufferLength,
384 PVOID OutputBuffer,
385 ULONG OutputBufferLength,
386 BOOLEAN InternalDeviceIoControl,
387 PKEVENT Event,
388 PIO_STATUS_BLOCK IoStatusBlock )
390 PIRP irp;
391 PIO_STACK_LOCATION irpsp;
392 struct IrpInstance *instance;
394 TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
395 IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
396 OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
397 Event, IoStatusBlock );
399 if (DeviceObject == NULL)
400 return NULL;
402 irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
403 if (irp == NULL)
404 return NULL;
406 instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
407 if (instance == NULL)
409 IoFreeIrp( irp );
410 return NULL;
412 instance->irp = irp;
413 list_add_tail( &Irps, &instance->entry );
415 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation - 1;
416 irpsp->MajorFunction = InternalDeviceIoControl ?
417 IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
418 irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
419 irp->UserIosb = IoStatusBlock;
420 irp->UserEvent = Event;
422 return irp;
426 /***********************************************************************
427 * IoCreateDriver (NTOSKRNL.EXE.@)
429 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
431 DRIVER_OBJECT *driver;
432 DRIVER_EXTENSION *extension;
433 NTSTATUS status;
435 if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
436 sizeof(*driver) + sizeof(*extension) )))
437 return STATUS_NO_MEMORY;
439 if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
441 RtlFreeHeap( GetProcessHeap(), 0, driver );
442 return status;
445 extension = (DRIVER_EXTENSION *)(driver + 1);
446 driver->Size = sizeof(*driver);
447 driver->DriverInit = init;
448 driver->DriverExtension = extension;
449 extension->DriverObject = driver;
450 extension->ServiceKeyName = driver->DriverName;
452 status = driver->DriverInit( driver, name );
454 if (status)
456 RtlFreeUnicodeString( &driver->DriverName );
457 RtlFreeHeap( GetProcessHeap(), 0, driver );
459 return status;
463 /***********************************************************************
464 * IoDeleteDriver (NTOSKRNL.EXE.@)
466 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
468 RtlFreeUnicodeString( &driver->DriverName );
469 RtlFreeHeap( GetProcessHeap(), 0, driver );
473 /***********************************************************************
474 * IoCreateDevice (NTOSKRNL.EXE.@)
476 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
477 UNICODE_STRING *name, DEVICE_TYPE type,
478 ULONG characteristics, BOOLEAN exclusive,
479 DEVICE_OBJECT **ret_device )
481 NTSTATUS status;
482 DEVICE_OBJECT *device;
483 HANDLE handle = 0;
484 HANDLE manager = get_device_manager();
486 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
487 driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
489 if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
490 return STATUS_NO_MEMORY;
492 SERVER_START_REQ( create_device )
494 req->access = 0;
495 req->attributes = 0;
496 req->rootdir = 0;
497 req->manager = wine_server_obj_handle( manager );
498 req->user_ptr = wine_server_client_ptr( device );
499 if (name) wine_server_add_data( req, name->Buffer, name->Length );
500 if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
502 SERVER_END_REQ;
504 if (status == STATUS_SUCCESS)
506 device->DriverObject = driver;
507 device->DeviceExtension = device + 1;
508 device->DeviceType = type;
509 device->StackSize = 1;
510 device->Reserved = handle;
512 device->NextDevice = driver->DeviceObject;
513 driver->DeviceObject = device;
515 *ret_device = device;
517 else HeapFree( GetProcessHeap(), 0, device );
519 return status;
523 /***********************************************************************
524 * IoDeleteDevice (NTOSKRNL.EXE.@)
526 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
528 NTSTATUS status;
530 TRACE( "%p\n", device );
532 SERVER_START_REQ( delete_device )
534 req->handle = wine_server_obj_handle( device->Reserved );
535 status = wine_server_call( req );
537 SERVER_END_REQ;
539 if (status == STATUS_SUCCESS)
541 DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
542 while (*prev && *prev != device) prev = &(*prev)->NextDevice;
543 if (*prev) *prev = (*prev)->NextDevice;
544 NtClose( device->Reserved );
545 HeapFree( GetProcessHeap(), 0, device );
550 /***********************************************************************
551 * IoCreateSymbolicLink (NTOSKRNL.EXE.@)
553 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
555 HANDLE handle;
556 OBJECT_ATTRIBUTES attr;
558 attr.Length = sizeof(attr);
559 attr.RootDirectory = 0;
560 attr.ObjectName = name;
561 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
562 attr.SecurityDescriptor = NULL;
563 attr.SecurityQualityOfService = NULL;
565 TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
566 /* FIXME: store handle somewhere */
567 return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
571 /***********************************************************************
572 * IoDeleteSymbolicLink (NTOSKRNL.EXE.@)
574 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
576 HANDLE handle;
577 OBJECT_ATTRIBUTES attr;
578 NTSTATUS status;
580 attr.Length = sizeof(attr);
581 attr.RootDirectory = 0;
582 attr.ObjectName = name;
583 attr.Attributes = OBJ_CASE_INSENSITIVE;
584 attr.SecurityDescriptor = NULL;
585 attr.SecurityQualityOfService = NULL;
587 if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
589 SERVER_START_REQ( unlink_object )
591 req->handle = wine_server_obj_handle( handle );
592 status = wine_server_call( req );
594 SERVER_END_REQ;
595 NtClose( handle );
597 return status;
601 /***********************************************************************
602 * IoGetDeviceObjectPointer (NTOSKRNL.EXE.@)
604 NTSTATUS WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
606 FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
607 return STATUS_NOT_IMPLEMENTED;
611 /***********************************************************************
612 * IofCallDriver (NTOSKRNL.EXE.@)
614 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
615 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
616 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
617 #else
618 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
619 #endif
621 PDRIVER_DISPATCH dispatch;
622 IO_STACK_LOCATION *irpsp;
623 NTSTATUS status;
625 TRACE( "%p %p\n", device, irp );
627 --irp->CurrentLocation;
628 irpsp = --irp->Tail.Overlay.s.u.CurrentStackLocation;
629 dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
630 status = dispatch( device, irp );
632 return status;
636 /***********************************************************************
637 * IoGetRelatedDeviceObject (NTOSKRNL.EXE.@)
639 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
641 FIXME( "stub: %p\n", obj );
642 return NULL;
645 static CONFIGURATION_INFORMATION configuration_information;
647 /***********************************************************************
648 * IoGetConfigurationInformation (NTOSKRNL.EXE.@)
650 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
652 FIXME( "partial stub\n" );
653 /* FIXME: return actual devices on system */
654 return &configuration_information;
658 /***********************************************************************
659 * IoRegisterDriverReinitialization (NTOSKRNL.EXE.@)
661 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
663 FIXME( "stub: %p %p %p\n", obj, reinit, context );
667 /***********************************************************************
668 * IoRegisterShutdownNotification (NTOSKRNL.EXE.@)
670 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
672 FIXME( "stub: %p\n", obj );
673 return STATUS_SUCCESS;
677 /***********************************************************************
678 * IofCompleteRequest (NTOSKRNL.EXE.@)
680 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
681 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
682 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
683 #else
684 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
685 #endif
687 IO_STACK_LOCATION *irpsp;
688 PIO_COMPLETION_ROUTINE routine;
689 IO_STATUS_BLOCK *iosb;
690 struct IrpInstance *instance;
691 NTSTATUS status, stat;
692 int call_flag = 0;
694 TRACE( "%p %u\n", irp, priority_boost );
696 iosb = irp->UserIosb;
697 status = irp->IoStatus.u.Status;
698 while (irp->CurrentLocation <= irp->StackCount)
700 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
701 routine = irpsp->CompletionRoutine;
702 call_flag = 0;
703 /* FIXME: add SL_INVOKE_ON_CANCEL support */
704 if (routine)
706 if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
707 call_flag = 1;
708 if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
709 call_flag = 1;
711 ++irp->CurrentLocation;
712 ++irp->Tail.Overlay.s.u.CurrentStackLocation;
713 if (call_flag)
715 TRACE( "calling %p( %p, %p, %p )\n", routine,
716 irpsp->DeviceObject, irp, irpsp->Context );
717 stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
718 TRACE( "CompletionRoutine returned %x\n", stat );
719 if (STATUS_MORE_PROCESSING_REQUIRED == stat)
720 return;
723 if (iosb && STATUS_SUCCESS == status)
725 iosb->u.Status = irp->IoStatus.u.Status;
726 iosb->Information = irp->IoStatus.Information;
728 LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
730 if (instance->irp == irp)
732 list_remove( &instance->entry );
733 HeapFree( GetProcessHeap(), 0, instance );
734 IoFreeIrp( irp );
735 break;
741 /***********************************************************************
742 * InterlockedCompareExchange (NTOSKRNL.EXE.@)
744 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
745 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
746 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
747 #else
748 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
749 #endif
751 return InterlockedCompareExchange( dest, xchg, compare );
755 /***********************************************************************
756 * InterlockedDecrement (NTOSKRNL.EXE.@)
758 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
759 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
760 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
761 #else
762 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
763 #endif
765 return InterlockedDecrement( dest );
769 /***********************************************************************
770 * InterlockedExchange (NTOSKRNL.EXE.@)
772 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
773 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
774 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
775 #else
776 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
777 #endif
779 return InterlockedExchange( dest, val );
783 /***********************************************************************
784 * InterlockedExchangeAdd (NTOSKRNL.EXE.@)
786 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
787 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
788 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
789 #else
790 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
791 #endif
793 return InterlockedExchangeAdd( dest, incr );
797 /***********************************************************************
798 * InterlockedIncrement (NTOSKRNL.EXE.@)
800 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
801 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
802 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
803 #else
804 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
805 #endif
807 return InterlockedIncrement( dest );
811 /***********************************************************************
812 * ExAllocatePool (NTOSKRNL.EXE.@)
814 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
816 return ExAllocatePoolWithTag( type, size, 0 );
820 /***********************************************************************
821 * ExAllocatePoolWithQuota (NTOSKRNL.EXE.@)
823 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
825 return ExAllocatePoolWithTag( type, size, 0 );
829 /***********************************************************************
830 * ExAllocatePoolWithTag (NTOSKRNL.EXE.@)
832 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
834 /* FIXME: handle page alignment constraints */
835 void *ret = HeapAlloc( GetProcessHeap(), 0, size );
836 TRACE( "%lu pool %u -> %p\n", size, type, ret );
837 return ret;
841 /***********************************************************************
842 * ExAllocatePoolWithQuotaTag (NTOSKRNL.EXE.@)
844 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
846 return ExAllocatePoolWithTag( type, size, tag );
850 /***********************************************************************
851 * ExFreePool (NTOSKRNL.EXE.@)
853 void WINAPI ExFreePool( void *ptr )
855 ExFreePoolWithTag( ptr, 0 );
859 /***********************************************************************
860 * ExFreePoolWithTag (NTOSKRNL.EXE.@)
862 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
864 TRACE( "%p\n", ptr );
865 HeapFree( GetProcessHeap(), 0, ptr );
869 /***********************************************************************
870 * KeInitializeEvent (NTOSKRNL.EXE.@)
872 void WINAPI KeInitializeEvent( PRKEVENT Event, EVENT_TYPE Type, BOOLEAN State )
874 FIXME( "stub: %p %d %d\n", Event, Type, State );
878 /***********************************************************************
879 * KeInitializeSpinLock (NTOSKRNL.EXE.@)
881 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
883 FIXME( "stub: %p\n", SpinLock );
887 /***********************************************************************
888 * KeInitializeTimerEx (NTOSKRNL.EXE.@)
890 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
892 FIXME( "stub: %p %d\n", Timer, Type );
896 /***********************************************************************
897 * KeInitializeTimer (NTOSKRNL.EXE.@)
899 void WINAPI KeInitializeTimer( PKTIMER Timer )
901 KeInitializeTimerEx(Timer, NotificationTimer);
905 /**********************************************************************
906 * KeQueryActiveProcessors (NTOSKRNL.EXE.@)
908 * Return the active Processors as bitmask
910 * RETURNS
911 * active Processors as bitmask
914 KAFFINITY WINAPI KeQueryActiveProcessors( void )
916 DWORD_PTR AffinityMask;
918 GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
919 return AffinityMask;
923 /**********************************************************************
924 * KeQueryInterruptTime (NTOSKRNL.EXE.@)
926 * Return the interrupt time count
929 ULONGLONG WINAPI KeQueryInterruptTime( void )
931 LARGE_INTEGER totaltime;
933 KeQueryTickCount(&totaltime);
934 return totaltime.QuadPart;
938 /***********************************************************************
939 * KeQuerySystemTime (NTOSKRNL.EXE.@)
941 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
943 NtQuerySystemTime( time );
947 /***********************************************************************
948 * KeQueryTickCount (NTOSKRNL.EXE.@)
950 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
952 count->QuadPart = NtGetTickCount();
953 /* update the global variable too */
954 KeTickCount.LowPart = count->u.LowPart;
955 KeTickCount.High1Time = count->u.HighPart;
956 KeTickCount.High2Time = count->u.HighPart;
960 /***********************************************************************
961 * KeQueryTimeIncrement (NTOSKRNL.EXE.@)
963 ULONG WINAPI KeQueryTimeIncrement(void)
965 return 10000;
969 /***********************************************************************
970 * MmAllocateNonCachedMemory (NTOSKRNL.EXE.@)
972 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
974 TRACE( "%lu\n", size );
975 return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
979 /***********************************************************************
980 * MmFreeNonCachedMemory (NTOSKRNL.EXE.@)
982 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
984 TRACE( "%p %lu\n", addr, size );
985 VirtualFree( addr, 0, MEM_RELEASE );
988 /***********************************************************************
989 * MmIsAddressValid (NTOSKRNL.EXE.@)
991 * Check if the process can access the virtual address without a pagefault
993 * PARAMS
994 * VirtualAddress [I] Address to check
996 * RETURNS
997 * Failure: FALSE
998 * Success: TRUE (Accessing the Address works without a Pagefault)
1001 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
1003 TRACE("(%p)\n", VirtualAddress);
1004 return !IsBadWritePtr(VirtualAddress, 1);
1007 /***********************************************************************
1008 * MmPageEntireDriver (NTOSKRNL.EXE.@)
1010 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1012 TRACE("%p\n", AddrInSection);
1013 return AddrInSection;
1016 /***********************************************************************
1017 * MmResetDriverPaging (NTOSKRNL.EXE.@)
1019 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1021 TRACE("%p\n", AddrInSection);
1025 /***********************************************************************
1026 * ObReferenceObjectByHandle (NTOSKRNL.EXE.@)
1028 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1029 POBJECT_TYPE type,
1030 KPROCESSOR_MODE mode, PVOID* ptr,
1031 POBJECT_HANDLE_INFORMATION info)
1033 FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1034 return STATUS_NOT_IMPLEMENTED;
1038 /***********************************************************************
1039 * ObfDereferenceObject (NTOSKRNL.EXE.@)
1041 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1042 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1043 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1044 #else
1045 void WINAPI ObfDereferenceObject( VOID *obj )
1046 #endif
1048 FIXME( "stub: %p\n", obj );
1052 /***********************************************************************
1053 * PsCreateSystemThread (NTOSKRNL.EXE.@)
1055 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1056 POBJECT_ATTRIBUTES ObjectAttributes,
1057 HANDLE ProcessHandle, PCLIENT_ID ClientId,
1058 PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1060 if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1061 return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1062 0, StartRoutine, StartContext,
1063 ThreadHandle, ClientId);
1066 /***********************************************************************
1067 * PsGetCurrentProcessId (NTOSKRNL.EXE.@)
1069 HANDLE WINAPI PsGetCurrentProcessId(void)
1071 return UlongToHandle(GetCurrentProcessId()); /* FIXME: not quite right... */
1075 /***********************************************************************
1076 * PsGetCurrentThreadId (NTOSKRNL.EXE.@)
1078 HANDLE WINAPI PsGetCurrentThreadId(void)
1080 return UlongToHandle(GetCurrentThreadId()); /* FIXME: not quite right... */
1084 /***********************************************************************
1085 * PsGetVersion (NTOSKRNL.EXE.@)
1087 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1089 RTL_OSVERSIONINFOEXW info;
1091 RtlGetVersion( &info );
1092 if (major) *major = info.dwMajorVersion;
1093 if (minor) *minor = info.dwMinorVersion;
1094 if (build) *build = info.dwBuildNumber;
1096 if (version)
1098 #if 0 /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1099 size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1100 memcpy( version->Buffer, info.szCSDVersion, len );
1101 if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1102 version->Length = len;
1103 #endif
1105 return TRUE;
1109 /***********************************************************************
1110 * PsSetCreateProcessNotifyRoutine (NTOSKRNL.EXE.@)
1112 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1114 FIXME( "stub: %p %d\n", callback, remove );
1115 return STATUS_SUCCESS;
1119 /***********************************************************************
1120 * PsSetCreateThreadNotifyRoutine (NTOSKRNL.EXE.@)
1122 NTSTATUS WINAPI PsSetCreateThreadNotifyRoutine( PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine )
1124 FIXME( "stub: %p\n", NotifyRoutine );
1125 return STATUS_SUCCESS;
1129 /***********************************************************************
1130 * MmGetSystemRoutineAddress (NTOSKRNL.EXE.@)
1132 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1134 HMODULE hMod;
1135 STRING routineNameA;
1136 PVOID pFunc = NULL;
1138 static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1139 static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1141 if (!SystemRoutineName) return NULL;
1143 if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1145 /* We only support functions exported from ntoskrnl.exe or hal.dll */
1146 hMod = GetModuleHandleW( ntoskrnlW );
1147 pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1148 if (!pFunc)
1150 hMod = GetModuleHandleW( halW );
1151 if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1153 RtlFreeAnsiString( &routineNameA );
1156 TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1157 return pFunc;
1160 /*****************************************************
1161 * DllMain
1163 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1165 LARGE_INTEGER count;
1167 switch(reason)
1169 case DLL_PROCESS_ATTACH:
1170 DisableThreadLibraryCalls( inst );
1171 RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1172 KeQueryTickCount( &count ); /* initialize the global KeTickCount */
1173 break;
1175 return TRUE;