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
22 #include "wine/port.h"
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
30 #define WIN32_NO_STATUS
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
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);
68 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
69 __ASM_STDCALL_FUNC( name, 4, \
73 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(4))
74 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
75 __ASM_STDCALL_FUNC( name, 8, \
80 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(8))
81 #define DEFINE_FASTCALL3_ENTRYPOINT( name ) \
82 __ASM_STDCALL_FUNC( name, 12, \
87 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(12))
90 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
92 if (!us) return "<null>";
93 return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
96 static HANDLE get_device_manager(void)
98 static HANDLE device_manager;
99 HANDLE handle = 0, ret = device_manager;
103 SERVER_START_REQ( create_device_manager )
105 req->access = SYNCHRONIZE;
107 if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
113 ERR( "failed to create the device manager\n" );
116 if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
119 NtClose( handle ); /* somebody beat us to it */
124 /* exception handler for emulation of privileged instructions */
125 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
127 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
129 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
130 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
133 CONTEXT *context = ptrs->ContextRecord;
134 extern DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT *context );
136 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
137 return EXCEPTION_CONTINUE_EXECUTION;
139 FIXME( "Privileged instruction emulation not implemented on this CPU\n" );
142 return EXCEPTION_CONTINUE_SEARCH;
145 /* process an ioctl request for a given device */
146 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
147 void *out_buff, ULONG *out_size )
151 IO_STACK_LOCATION irpsp;
152 PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
156 TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
158 /* so we can spot things that we should initialize */
159 memset( &irp, 0x55, sizeof(irp) );
160 memset( &irpsp, 0x66, sizeof(irpsp) );
161 memset( &mdl, 0x77, sizeof(mdl) );
163 irp.RequestorMode = UserMode;
164 irp.AssociatedIrp.SystemBuffer = in_buff;
165 irp.UserBuffer = out_buff;
166 irp.MdlAddress = &mdl;
167 irp.Tail.Overlay.s.u.CurrentStackLocation = &irpsp;
170 irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
171 irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
172 irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
173 irpsp.Parameters.DeviceIoControl.IoControlCode = code;
174 irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
175 irpsp.DeviceObject = device;
176 irpsp.CompletionRoutine = NULL;
180 mdl.StartVa = out_buff;
181 mdl.ByteCount = *out_size;
184 device->CurrentIrp = &irp;
186 KeQueryTickCount( &count ); /* update the global KeTickCount */
189 DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
190 GetCurrentThreadId(), dispatch, device, &irp );
192 status = dispatch( device, &irp );
195 DPRINTF( "%04x:Ret driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
196 GetCurrentThreadId(), dispatch, device, &irp, status );
198 *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
199 return irp.IoStatus.u.Status;
203 /***********************************************************************
204 * wine_ntoskrnl_main_loop (Not a Windows API)
206 NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event )
208 HANDLE manager = get_device_manager();
209 obj_handle_t ioctl = 0;
210 NTSTATUS status = STATUS_SUCCESS;
212 void *in_buff, *out_buff = NULL;
213 DEVICE_OBJECT *device = NULL;
214 ULONG in_size = 4096, out_size = 0;
217 if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
219 ERR( "failed to allocate buffer\n" );
220 return STATUS_NO_MEMORY;
223 handles[0] = stop_event;
224 handles[1] = manager;
228 SERVER_START_REQ( get_next_device_request )
230 req->manager = wine_server_obj_handle( manager );
232 req->status = status;
233 wine_server_add_data( req, out_buff, out_size );
234 wine_server_set_reply( req, in_buff, in_size );
235 if (!(status = wine_server_call( req )))
239 device = wine_server_get_ptr( reply->user_ptr );
240 in_size = reply->in_size;
241 out_size = reply->out_size;
245 ioctl = 0; /* no previous ioctl */
247 in_size = reply->in_size;
255 HeapFree( GetProcessHeap(), 0, out_buff );
256 if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
257 else out_buff = NULL;
258 status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
260 case STATUS_BUFFER_OVERFLOW:
261 HeapFree( GetProcessHeap(), 0, in_buff );
262 in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
263 /* restart with larger buffer */
266 if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
268 HeapFree( GetProcessHeap(), 0, in_buff );
269 HeapFree( GetProcessHeap(), 0, out_buff );
270 return STATUS_SUCCESS;
278 /***********************************************************************
279 * IoAllocateDriverObjectExtension (NTOSKRNL.EXE.@)
281 NTSTATUS WINAPI IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject,
282 PVOID ClientIdentificationAddress,
283 ULONG DriverObjectExtensionSize,
284 PVOID *DriverObjectExtension )
286 FIXME( "stub: %p, %p, %u, %p\n", DriverObject, ClientIdentificationAddress,
287 DriverObjectExtensionSize, DriverObjectExtension );
288 return STATUS_NOT_IMPLEMENTED;
292 /***********************************************************************
293 * IoGetDriverObjectExtension (NTOSKRNL.EXE.@)
295 PVOID WINAPI IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject,
296 PVOID ClientIdentificationAddress )
298 FIXME( "stub: %p, %p\n", DriverObject, ClientIdentificationAddress );
303 /***********************************************************************
304 * IoInitializeIrp (NTOSKRNL.EXE.@)
306 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
308 TRACE( "%p, %u, %d\n", irp, size, stack_size );
310 RtlZeroMemory( irp, size );
312 irp->Type = IO_TYPE_IRP;
314 InitializeListHead( &irp->ThreadListEntry );
315 irp->StackCount = stack_size;
316 irp->CurrentLocation = stack_size + 1;
317 irp->Tail.Overlay.s.u.CurrentStackLocation =
318 (PIO_STACK_LOCATION)(irp + 1) + stack_size;
322 /***********************************************************************
323 * IoInitializeTimer (NTOSKRNL.EXE.@)
325 NTSTATUS WINAPI IoInitializeTimer(PDEVICE_OBJECT DeviceObject,
326 PIO_TIMER_ROUTINE TimerRoutine,
329 FIXME( "stub: %p, %p, %p\n", DeviceObject, TimerRoutine, Context );
330 return STATUS_NOT_IMPLEMENTED;
334 /***********************************************************************
335 * IoStartTimer (NTOSKRNL.EXE.@)
337 void WINAPI IoStartTimer(PDEVICE_OBJECT DeviceObject)
339 FIXME( "stub: %p\n", DeviceObject );
343 /***********************************************************************
344 * IoAllocateIrp (NTOSKRNL.EXE.@)
346 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
351 TRACE( "%d, %d\n", stack_size, charge_quota );
353 size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
354 irp = ExAllocatePool( NonPagedPool, size );
357 IoInitializeIrp( irp, size, stack_size );
358 irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
360 irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
365 /***********************************************************************
366 * IoFreeIrp (NTOSKRNL.EXE.@)
368 void WINAPI IoFreeIrp( IRP *irp )
370 TRACE( "%p\n", irp );
376 /***********************************************************************
377 * IoAllocateErrorLogEntry (NTOSKRNL.EXE.@)
379 PVOID WINAPI IoAllocateErrorLogEntry( PVOID IoObject, UCHAR EntrySize )
381 FIXME( "stub: %p, %u\n", IoObject, EntrySize );
386 /***********************************************************************
387 * IoAllocateMdl (NTOSKRNL.EXE.@)
389 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
391 FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
396 /***********************************************************************
397 * IoAllocateWorkItem (NTOSKRNL.EXE.@)
399 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
401 FIXME( "stub: %p\n", DeviceObject );
406 /***********************************************************************
407 * IoAttachDeviceToDeviceStack (NTOSKRNL.EXE.@)
409 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
410 DEVICE_OBJECT *target )
412 TRACE( "%p, %p\n", source, target );
413 target->AttachedDevice = source;
414 source->StackSize = target->StackSize + 1;
419 /***********************************************************************
420 * IoBuildDeviceIoControlRequest (NTOSKRNL.EXE.@)
422 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
423 PDEVICE_OBJECT DeviceObject,
425 ULONG InputBufferLength,
427 ULONG OutputBufferLength,
428 BOOLEAN InternalDeviceIoControl,
430 PIO_STATUS_BLOCK IoStatusBlock )
433 PIO_STACK_LOCATION irpsp;
434 struct IrpInstance *instance;
436 TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
437 IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
438 OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
439 Event, IoStatusBlock );
441 if (DeviceObject == NULL)
444 irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
448 instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
449 if (instance == NULL)
455 list_add_tail( &Irps, &instance->entry );
457 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation - 1;
458 irpsp->MajorFunction = InternalDeviceIoControl ?
459 IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
460 irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
461 irp->UserIosb = IoStatusBlock;
462 irp->UserEvent = Event;
468 /***********************************************************************
469 * IoCreateDriver (NTOSKRNL.EXE.@)
471 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
473 DRIVER_OBJECT *driver;
474 DRIVER_EXTENSION *extension;
477 if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
478 sizeof(*driver) + sizeof(*extension) )))
479 return STATUS_NO_MEMORY;
481 if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
483 RtlFreeHeap( GetProcessHeap(), 0, driver );
487 extension = (DRIVER_EXTENSION *)(driver + 1);
488 driver->Size = sizeof(*driver);
489 driver->DriverInit = init;
490 driver->DriverExtension = extension;
491 extension->DriverObject = driver;
492 extension->ServiceKeyName = driver->DriverName;
494 status = driver->DriverInit( driver, name );
498 RtlFreeUnicodeString( &driver->DriverName );
499 RtlFreeHeap( GetProcessHeap(), 0, driver );
505 /***********************************************************************
506 * IoDeleteDriver (NTOSKRNL.EXE.@)
508 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
510 RtlFreeUnicodeString( &driver->DriverName );
511 RtlFreeHeap( GetProcessHeap(), 0, driver );
515 /***********************************************************************
516 * IoCreateDevice (NTOSKRNL.EXE.@)
518 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
519 UNICODE_STRING *name, DEVICE_TYPE type,
520 ULONG characteristics, BOOLEAN exclusive,
521 DEVICE_OBJECT **ret_device )
524 DEVICE_OBJECT *device;
526 HANDLE manager = get_device_manager();
528 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
529 driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
531 if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
532 return STATUS_NO_MEMORY;
534 SERVER_START_REQ( create_device )
539 req->manager = wine_server_obj_handle( manager );
540 req->user_ptr = wine_server_client_ptr( device );
541 if (name) wine_server_add_data( req, name->Buffer, name->Length );
542 if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
546 if (status == STATUS_SUCCESS)
548 device->DriverObject = driver;
549 device->DeviceExtension = device + 1;
550 device->DeviceType = type;
551 device->StackSize = 1;
552 device->Reserved = handle;
554 device->NextDevice = driver->DeviceObject;
555 driver->DeviceObject = device;
557 *ret_device = device;
559 else HeapFree( GetProcessHeap(), 0, device );
565 /***********************************************************************
566 * IoDeleteDevice (NTOSKRNL.EXE.@)
568 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
572 TRACE( "%p\n", device );
574 SERVER_START_REQ( delete_device )
576 req->handle = wine_server_obj_handle( device->Reserved );
577 status = wine_server_call( req );
581 if (status == STATUS_SUCCESS)
583 DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
584 while (*prev && *prev != device) prev = &(*prev)->NextDevice;
585 if (*prev) *prev = (*prev)->NextDevice;
586 NtClose( device->Reserved );
587 HeapFree( GetProcessHeap(), 0, device );
592 /***********************************************************************
593 * IoCreateSymbolicLink (NTOSKRNL.EXE.@)
595 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
598 OBJECT_ATTRIBUTES attr;
600 attr.Length = sizeof(attr);
601 attr.RootDirectory = 0;
602 attr.ObjectName = name;
603 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
604 attr.SecurityDescriptor = NULL;
605 attr.SecurityQualityOfService = NULL;
607 TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
608 /* FIXME: store handle somewhere */
609 return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
613 /***********************************************************************
614 * IoDeleteSymbolicLink (NTOSKRNL.EXE.@)
616 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
619 OBJECT_ATTRIBUTES attr;
622 attr.Length = sizeof(attr);
623 attr.RootDirectory = 0;
624 attr.ObjectName = name;
625 attr.Attributes = OBJ_CASE_INSENSITIVE;
626 attr.SecurityDescriptor = NULL;
627 attr.SecurityQualityOfService = NULL;
629 if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
631 SERVER_START_REQ( unlink_object )
633 req->handle = wine_server_obj_handle( handle );
634 status = wine_server_call( req );
643 /***********************************************************************
644 * IoGetDeviceObjectPointer (NTOSKRNL.EXE.@)
646 NTSTATUS WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
648 FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
649 return STATUS_NOT_IMPLEMENTED;
653 /***********************************************************************
654 * IofCallDriver (NTOSKRNL.EXE.@)
656 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
657 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
658 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
660 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
663 PDRIVER_DISPATCH dispatch;
664 IO_STACK_LOCATION *irpsp;
667 TRACE( "%p %p\n", device, irp );
669 --irp->CurrentLocation;
670 irpsp = --irp->Tail.Overlay.s.u.CurrentStackLocation;
671 dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
672 status = dispatch( device, irp );
678 /***********************************************************************
679 * IoGetRelatedDeviceObject (NTOSKRNL.EXE.@)
681 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
683 FIXME( "stub: %p\n", obj );
687 static CONFIGURATION_INFORMATION configuration_information;
689 /***********************************************************************
690 * IoGetConfigurationInformation (NTOSKRNL.EXE.@)
692 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
694 FIXME( "partial stub\n" );
695 /* FIXME: return actual devices on system */
696 return &configuration_information;
700 /***********************************************************************
701 * IoQueryDeviceDescription (NTOSKRNL.EXE.@)
703 NTSTATUS WINAPI IoQueryDeviceDescription(PINTERFACE_TYPE itype, PULONG bus, PCONFIGURATION_TYPE ctype,
704 PULONG cnum, PCONFIGURATION_TYPE ptype, PULONG pnum,
705 PIO_QUERY_DEVICE_ROUTINE callout, PVOID context)
707 FIXME( "(%p %p %p %p %p %p %p %p)\n", itype, bus, ctype, cnum, ptype, pnum, callout, context);
708 return STATUS_NOT_IMPLEMENTED;
712 /***********************************************************************
713 * IoRegisterDriverReinitialization (NTOSKRNL.EXE.@)
715 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
717 FIXME( "stub: %p %p %p\n", obj, reinit, context );
721 /***********************************************************************
722 * IoRegisterShutdownNotification (NTOSKRNL.EXE.@)
724 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
726 FIXME( "stub: %p\n", obj );
727 return STATUS_SUCCESS;
731 /***********************************************************************
732 * IoReportResourceUsage (NTOSKRNL.EXE.@)
734 NTSTATUS WINAPI IoReportResourceUsage(PUNICODE_STRING name, PDRIVER_OBJECT drv_obj, PCM_RESOURCE_LIST drv_list,
735 ULONG drv_size, PDRIVER_OBJECT dev_obj, PCM_RESOURCE_LIST dev_list,
736 ULONG dev_size, BOOLEAN overwrite, PBOOLEAN detected)
738 FIXME("(%s %p %p %u %p %p %u %d %p) stub\n", debugstr_w(name? name->Buffer : NULL),
739 drv_obj, drv_list, drv_size, dev_obj, dev_list, dev_size, overwrite, detected);
740 return STATUS_NOT_IMPLEMENTED;
744 /***********************************************************************
745 * IofCompleteRequest (NTOSKRNL.EXE.@)
747 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
748 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
749 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
751 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
754 IO_STACK_LOCATION *irpsp;
755 PIO_COMPLETION_ROUTINE routine;
756 IO_STATUS_BLOCK *iosb;
757 struct IrpInstance *instance;
758 NTSTATUS status, stat;
761 TRACE( "%p %u\n", irp, priority_boost );
763 iosb = irp->UserIosb;
764 status = irp->IoStatus.u.Status;
765 while (irp->CurrentLocation <= irp->StackCount)
767 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
768 routine = irpsp->CompletionRoutine;
770 /* FIXME: add SL_INVOKE_ON_CANCEL support */
773 if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
775 if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
778 ++irp->CurrentLocation;
779 ++irp->Tail.Overlay.s.u.CurrentStackLocation;
782 TRACE( "calling %p( %p, %p, %p )\n", routine,
783 irpsp->DeviceObject, irp, irpsp->Context );
784 stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
785 TRACE( "CompletionRoutine returned %x\n", stat );
786 if (STATUS_MORE_PROCESSING_REQUIRED == stat)
790 if (iosb && STATUS_SUCCESS == status)
792 iosb->u.Status = irp->IoStatus.u.Status;
793 iosb->Information = irp->IoStatus.Information;
795 LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
797 if (instance->irp == irp)
799 list_remove( &instance->entry );
800 HeapFree( GetProcessHeap(), 0, instance );
808 /***********************************************************************
809 * InterlockedCompareExchange (NTOSKRNL.EXE.@)
811 #ifdef DEFINE_FASTCALL3_ENTRYPOINT
812 DEFINE_FASTCALL3_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
813 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
815 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
818 return InterlockedCompareExchange( dest, xchg, compare );
822 /***********************************************************************
823 * InterlockedDecrement (NTOSKRNL.EXE.@)
825 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
826 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
827 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
829 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
832 return InterlockedDecrement( dest );
836 /***********************************************************************
837 * InterlockedExchange (NTOSKRNL.EXE.@)
839 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
840 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
841 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
843 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
846 return InterlockedExchange( dest, val );
850 /***********************************************************************
851 * InterlockedExchangeAdd (NTOSKRNL.EXE.@)
853 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
854 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
855 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
857 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
860 return InterlockedExchangeAdd( dest, incr );
864 /***********************************************************************
865 * InterlockedIncrement (NTOSKRNL.EXE.@)
867 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
868 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
869 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
871 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
874 return InterlockedIncrement( dest );
878 /***********************************************************************
879 * ExAllocatePool (NTOSKRNL.EXE.@)
881 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
883 return ExAllocatePoolWithTag( type, size, 0 );
887 /***********************************************************************
888 * ExAllocatePoolWithQuota (NTOSKRNL.EXE.@)
890 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
892 return ExAllocatePoolWithTag( type, size, 0 );
896 /***********************************************************************
897 * ExAllocatePoolWithTag (NTOSKRNL.EXE.@)
899 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
901 /* FIXME: handle page alignment constraints */
902 void *ret = HeapAlloc( GetProcessHeap(), 0, size );
903 TRACE( "%lu pool %u -> %p\n", size, type, ret );
908 /***********************************************************************
909 * ExAllocatePoolWithQuotaTag (NTOSKRNL.EXE.@)
911 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
913 return ExAllocatePoolWithTag( type, size, tag );
917 /***********************************************************************
918 * ExFreePool (NTOSKRNL.EXE.@)
920 void WINAPI ExFreePool( void *ptr )
922 ExFreePoolWithTag( ptr, 0 );
926 /***********************************************************************
927 * ExFreePoolWithTag (NTOSKRNL.EXE.@)
929 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
931 TRACE( "%p\n", ptr );
932 HeapFree( GetProcessHeap(), 0, ptr );
936 /***********************************************************************
937 * ExInitializeResourceLite (NTOSKRNL.EXE.@)
939 NTSTATUS WINAPI ExInitializeResourceLite(PERESOURCE Resource)
941 FIXME( "stub: %p\n", Resource );
942 return STATUS_NOT_IMPLEMENTED;
946 /***********************************************************************
947 * ExInitializeNPagedLookasideList (NTOSKRNL.EXE.@)
949 void WINAPI ExInitializeNPagedLookasideList(PNPAGED_LOOKASIDE_LIST Lookaside,
950 PALLOCATE_FUNCTION Allocate,
957 FIXME( "stub: %p, %p, %p, %u, %lu, %u, %u\n", Lookaside, Allocate, Free, Flags, Size, Tag, Depth );
961 /***********************************************************************
962 * ExInitializeZone (NTOSKRNL.EXE.@)
964 NTSTATUS WINAPI ExInitializeZone(PZONE_HEADER Zone,
966 PVOID InitialSegment,
967 ULONG InitialSegmentSize)
969 FIXME( "stub: %p, %u, %p, %u\n", Zone, BlockSize, InitialSegment, InitialSegmentSize );
970 return STATUS_NOT_IMPLEMENTED;
973 /***********************************************************************
974 * FsRtlRegisterUncProvider (NTOSKRNL.EXE.@)
976 NTSTATUS WINAPI FsRtlRegisterUncProvider(PHANDLE MupHandle, PUNICODE_STRING RedirDevName,
977 BOOLEAN MailslotsSupported)
979 FIXME("(%p %p %d): stub\n", MupHandle, RedirDevName, MailslotsSupported);
980 return STATUS_NOT_IMPLEMENTED;
983 /***********************************************************************
984 * KeInitializeEvent (NTOSKRNL.EXE.@)
986 void WINAPI KeInitializeEvent( PRKEVENT Event, EVENT_TYPE Type, BOOLEAN State )
988 FIXME( "stub: %p %d %d\n", Event, Type, State );
992 /***********************************************************************
993 * KeInitializeMutex (NTOSKRNL.EXE.@)
995 void WINAPI KeInitializeMutex(PRKMUTEX Mutex, ULONG Level)
997 FIXME( "stub: %p, %u\n", Mutex, Level );
1001 /***********************************************************************
1002 * KeInitializeSpinLock (NTOSKRNL.EXE.@)
1004 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
1006 FIXME( "stub: %p\n", SpinLock );
1010 /***********************************************************************
1011 * KeInitializeTimerEx (NTOSKRNL.EXE.@)
1013 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
1015 FIXME( "stub: %p %d\n", Timer, Type );
1019 /***********************************************************************
1020 * KeInitializeTimer (NTOSKRNL.EXE.@)
1022 void WINAPI KeInitializeTimer( PKTIMER Timer )
1024 KeInitializeTimerEx(Timer, NotificationTimer);
1028 /**********************************************************************
1029 * KeQueryActiveProcessors (NTOSKRNL.EXE.@)
1031 * Return the active Processors as bitmask
1034 * active Processors as bitmask
1037 KAFFINITY WINAPI KeQueryActiveProcessors( void )
1039 DWORD_PTR AffinityMask;
1041 GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
1042 return AffinityMask;
1046 /**********************************************************************
1047 * KeQueryInterruptTime (NTOSKRNL.EXE.@)
1049 * Return the interrupt time count
1052 ULONGLONG WINAPI KeQueryInterruptTime( void )
1054 LARGE_INTEGER totaltime;
1056 KeQueryTickCount(&totaltime);
1057 return totaltime.QuadPart;
1061 /***********************************************************************
1062 * KeQuerySystemTime (NTOSKRNL.EXE.@)
1064 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
1066 NtQuerySystemTime( time );
1070 /***********************************************************************
1071 * KeQueryTickCount (NTOSKRNL.EXE.@)
1073 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
1075 count->QuadPart = NtGetTickCount();
1076 /* update the global variable too */
1077 KeTickCount.LowPart = count->u.LowPart;
1078 KeTickCount.High1Time = count->u.HighPart;
1079 KeTickCount.High2Time = count->u.HighPart;
1083 /***********************************************************************
1084 * KeQueryTimeIncrement (NTOSKRNL.EXE.@)
1086 ULONG WINAPI KeQueryTimeIncrement(void)
1092 /***********************************************************************
1093 * KeWaitForSingleObject (NTOSKRNL.EXE.@)
1095 NTSTATUS WINAPI KeWaitForSingleObject(PVOID Object,
1096 KWAIT_REASON WaitReason,
1097 KPROCESSOR_MODE WaitMode,
1099 PLARGE_INTEGER Timeout)
1101 FIXME( "stub: %p, %d, %d, %d, %p\n", Object, WaitReason, WaitMode, Alertable, Timeout );
1102 return STATUS_NOT_IMPLEMENTED;
1105 /***********************************************************************
1106 * IoRegisterFileSystem (NTOSKRNL.EXE.@)
1108 VOID WINAPI IoRegisterFileSystem(PDEVICE_OBJECT DeviceObject)
1110 FIXME("(%p): stub\n", DeviceObject);
1113 /***********************************************************************
1114 * IoUnregisterFileSystem (NTOSKRNL.EXE.@)
1116 VOID WINAPI IoUnregisterFileSystem(PDEVICE_OBJECT DeviceObject)
1118 FIXME("(%p): stub\n", DeviceObject);
1121 /***********************************************************************
1122 * MmAllocateNonCachedMemory (NTOSKRNL.EXE.@)
1124 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
1126 TRACE( "%lu\n", size );
1127 return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
1130 /***********************************************************************
1131 * MmAllocateContiguousMemory (NTOSKRNL.EXE.@)
1133 PVOID WINAPI MmAllocateContiguousMemory( SIZE_T size, PHYSICAL_ADDRESS highest_valid_address )
1135 FIXME( "%lu, %s stub\n", size, wine_dbgstr_longlong(highest_valid_address.QuadPart) );
1139 /***********************************************************************
1140 * MmAllocatePagesForMdl (NTOSKRNL.EXE.@)
1142 PMDL WINAPI MmAllocatePagesForMdl(PHYSICAL_ADDRESS lowaddress, PHYSICAL_ADDRESS highaddress,
1143 PHYSICAL_ADDRESS skipbytes, SIZE_T size)
1145 FIXME("%s %s %s %lu: stub\n", wine_dbgstr_longlong(lowaddress.QuadPart), wine_dbgstr_longlong(highaddress.QuadPart),
1146 wine_dbgstr_longlong(skipbytes.QuadPart), size);
1150 /***********************************************************************
1151 * MmFreeNonCachedMemory (NTOSKRNL.EXE.@)
1153 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
1155 TRACE( "%p %lu\n", addr, size );
1156 VirtualFree( addr, 0, MEM_RELEASE );
1159 /***********************************************************************
1160 * MmIsAddressValid (NTOSKRNL.EXE.@)
1162 * Check if the process can access the virtual address without a pagefault
1165 * VirtualAddress [I] Address to check
1169 * Success: TRUE (Accessing the Address works without a Pagefault)
1172 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
1174 TRACE("(%p)\n", VirtualAddress);
1175 return !IsBadWritePtr(VirtualAddress, 1);
1178 /***********************************************************************
1179 * MmPageEntireDriver (NTOSKRNL.EXE.@)
1181 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1183 TRACE("%p\n", AddrInSection);
1184 return AddrInSection;
1187 /***********************************************************************
1188 * MmResetDriverPaging (NTOSKRNL.EXE.@)
1190 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1192 TRACE("%p\n", AddrInSection);
1195 /***********************************************************************
1196 * ObfReferenceObject (NTOSKRNL.EXE.@)
1198 VOID WINAPI ObfReferenceObject(PVOID Object)
1200 FIXME("(%p): stub\n", Object);
1203 /***********************************************************************
1204 * ObReferenceObjectByHandle (NTOSKRNL.EXE.@)
1206 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1208 KPROCESSOR_MODE mode, PVOID* ptr,
1209 POBJECT_HANDLE_INFORMATION info)
1211 FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1212 return STATUS_NOT_IMPLEMENTED;
1216 /***********************************************************************
1217 * ObfDereferenceObject (NTOSKRNL.EXE.@)
1219 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1220 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1221 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1223 void WINAPI ObfDereferenceObject( VOID *obj )
1226 FIXME( "stub: %p\n", obj );
1230 /***********************************************************************
1231 * PsCreateSystemThread (NTOSKRNL.EXE.@)
1233 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1234 POBJECT_ATTRIBUTES ObjectAttributes,
1235 HANDLE ProcessHandle, PCLIENT_ID ClientId,
1236 PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1238 if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1239 return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1240 0, StartRoutine, StartContext,
1241 ThreadHandle, ClientId);
1244 /***********************************************************************
1245 * PsGetCurrentProcessId (NTOSKRNL.EXE.@)
1247 HANDLE WINAPI PsGetCurrentProcessId(void)
1249 return UlongToHandle(GetCurrentProcessId()); /* FIXME: not quite right... */
1253 /***********************************************************************
1254 * PsGetCurrentThreadId (NTOSKRNL.EXE.@)
1256 HANDLE WINAPI PsGetCurrentThreadId(void)
1258 return UlongToHandle(GetCurrentThreadId()); /* FIXME: not quite right... */
1262 /***********************************************************************
1263 * PsGetVersion (NTOSKRNL.EXE.@)
1265 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1267 RTL_OSVERSIONINFOEXW info;
1269 info.dwOSVersionInfoSize = sizeof(info);
1270 RtlGetVersion( &info );
1271 if (major) *major = info.dwMajorVersion;
1272 if (minor) *minor = info.dwMinorVersion;
1273 if (build) *build = info.dwBuildNumber;
1277 #if 0 /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1278 size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1279 memcpy( version->Buffer, info.szCSDVersion, len );
1280 if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1281 version->Length = len;
1288 /***********************************************************************
1289 * PsSetCreateProcessNotifyRoutine (NTOSKRNL.EXE.@)
1291 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1293 FIXME( "stub: %p %d\n", callback, remove );
1294 return STATUS_SUCCESS;
1298 /***********************************************************************
1299 * PsSetCreateThreadNotifyRoutine (NTOSKRNL.EXE.@)
1301 NTSTATUS WINAPI PsSetCreateThreadNotifyRoutine( PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine )
1303 FIXME( "stub: %p\n", NotifyRoutine );
1304 return STATUS_SUCCESS;
1308 /***********************************************************************
1309 * PsTerminateSystemThread (NTOSKRNL.EXE.@)
1311 NTSTATUS WINAPI PsTerminateSystemThread(NTSTATUS ExitStatus)
1313 FIXME( "stub: %u\n", ExitStatus );
1314 return STATUS_NOT_IMPLEMENTED;
1318 /***********************************************************************
1319 * MmGetSystemRoutineAddress (NTOSKRNL.EXE.@)
1321 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1324 STRING routineNameA;
1327 static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1328 static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1330 if (!SystemRoutineName) return NULL;
1332 if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1334 /* We only support functions exported from ntoskrnl.exe or hal.dll */
1335 hMod = GetModuleHandleW( ntoskrnlW );
1336 pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1339 hMod = GetModuleHandleW( halW );
1340 if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1342 RtlFreeAnsiString( &routineNameA );
1345 TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1350 /***********************************************************************
1351 * MmQuerySystemSize (NTOSKRNL.EXE.@)
1353 MM_SYSTEMSIZE WINAPI MmQuerySystemSize(void)
1356 return MmLargeSystem;
1360 /*****************************************************
1363 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1365 static void *handler;
1366 LARGE_INTEGER count;
1370 case DLL_PROCESS_ATTACH:
1371 DisableThreadLibraryCalls( inst );
1372 handler = RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1373 KeQueryTickCount( &count ); /* initialize the global KeTickCount */
1375 case DLL_PROCESS_DETACH:
1376 RtlRemoveVectoredExceptionHandler( handler );