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_GLOBAL_FUNC( name, \
73 "jmp " __ASM_NAME("__regs_") #name )
74 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
75 __ASM_GLOBAL_FUNC( name, \
80 "jmp " __ASM_NAME("__regs_") #name )
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;
96 SERVER_START_REQ( create_device_manager )
98 req->access = SYNCHRONIZE;
100 if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
106 ERR( "failed to create the device manager\n" );
109 if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
112 NtClose( handle ); /* somebody beat us to it */
117 /* exception handler for emulation of privileged instructions */
118 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
120 extern DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context );
122 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
123 CONTEXT86 *context = ptrs->ContextRecord;
125 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
126 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
128 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
129 return EXCEPTION_CONTINUE_EXECUTION;
131 return EXCEPTION_CONTINUE_SEARCH;
134 /* process an ioctl request for a given device */
135 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
136 void *out_buff, ULONG *out_size )
140 IO_STACK_LOCATION irpsp;
141 PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
145 TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
147 /* so we can spot things that we should initialize */
148 memset( &irp, 0x55, sizeof(irp) );
149 memset( &irpsp, 0x66, sizeof(irpsp) );
150 memset( &mdl, 0x77, sizeof(mdl) );
152 irp.RequestorMode = UserMode;
153 irp.AssociatedIrp.SystemBuffer = in_buff;
154 irp.UserBuffer = out_buff;
155 irp.MdlAddress = &mdl;
156 irp.Tail.Overlay.s.u.CurrentStackLocation = &irpsp;
159 irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
160 irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
161 irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
162 irpsp.Parameters.DeviceIoControl.IoControlCode = code;
163 irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
164 irpsp.DeviceObject = device;
165 irpsp.CompletionRoutine = NULL;
169 mdl.StartVa = out_buff;
170 mdl.ByteCount = *out_size;
173 device->CurrentIrp = &irp;
175 KeQueryTickCount( &count ); /* update the global KeTickCount */
178 DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
179 GetCurrentThreadId(), dispatch, device, &irp );
181 status = dispatch( device, &irp );
184 DPRINTF( "%04x:Ret driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
185 GetCurrentThreadId(), dispatch, device, &irp, status );
187 *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
188 return irp.IoStatus.u.Status;
192 /***********************************************************************
193 * wine_ntoskrnl_main_loop (Not a Windows API)
195 NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event )
197 HANDLE manager = get_device_manager();
198 obj_handle_t ioctl = 0;
199 NTSTATUS status = STATUS_SUCCESS;
201 void *in_buff, *out_buff = NULL;
202 DEVICE_OBJECT *device = NULL;
203 ULONG in_size = 4096, out_size = 0;
206 if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
208 ERR( "failed to allocate buffer\n" );
209 return STATUS_NO_MEMORY;
212 handles[0] = stop_event;
213 handles[1] = manager;
217 SERVER_START_REQ( get_next_device_request )
219 req->manager = wine_server_obj_handle( manager );
221 req->status = status;
222 wine_server_add_data( req, out_buff, out_size );
223 wine_server_set_reply( req, in_buff, in_size );
224 if (!(status = wine_server_call( req )))
228 device = wine_server_get_ptr( reply->user_ptr );
229 in_size = reply->in_size;
230 out_size = reply->out_size;
234 ioctl = 0; /* no previous ioctl */
236 in_size = reply->in_size;
244 HeapFree( GetProcessHeap(), 0, out_buff );
245 if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
246 else out_buff = NULL;
247 status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
249 case STATUS_BUFFER_OVERFLOW:
250 HeapFree( GetProcessHeap(), 0, in_buff );
251 in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
252 /* restart with larger buffer */
255 if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
256 return STATUS_SUCCESS;
263 /***********************************************************************
264 * IoAllocateDriverObjectExtension (NTOSKRNL.EXE.@)
266 NTSTATUS WINAPI IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject,
267 PVOID ClientIdentificationAddress,
268 ULONG DriverObjectExtensionSize,
269 PVOID *DriverObjectExtension )
271 FIXME( "stub: %p, %p, %u, %p\n", DriverObject, ClientIdentificationAddress,
272 DriverObjectExtensionSize, DriverObjectExtension );
273 return STATUS_NOT_IMPLEMENTED;
277 /***********************************************************************
278 * IoGetDriverObjectExtension (NTOSKRNL.EXE.@)
280 PVOID WINAPI IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject,
281 PVOID ClientIdentificationAddress )
283 FIXME( "stub: %p, %p\n", DriverObject, ClientIdentificationAddress );
288 /***********************************************************************
289 * IoInitializeIrp (NTOSKRNL.EXE.@)
291 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
293 TRACE( "%p, %u, %d\n", irp, size, stack_size );
295 RtlZeroMemory( irp, size );
297 irp->Type = IO_TYPE_IRP;
299 InitializeListHead( &irp->ThreadListEntry );
300 irp->StackCount = stack_size;
301 irp->CurrentLocation = stack_size + 1;
302 irp->Tail.Overlay.s.u.CurrentStackLocation =
303 (PIO_STACK_LOCATION)(irp + 1) + stack_size;
307 /***********************************************************************
308 * IoAllocateIrp (NTOSKRNL.EXE.@)
310 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
315 TRACE( "%d, %d\n", stack_size, charge_quota );
317 size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
318 irp = ExAllocatePool( NonPagedPool, size );
321 IoInitializeIrp( irp, size, stack_size );
322 irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
324 irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
329 /***********************************************************************
330 * IoFreeIrp (NTOSKRNL.EXE.@)
332 void WINAPI IoFreeIrp( IRP *irp )
334 TRACE( "%p\n", irp );
340 /***********************************************************************
341 * IoAllocateMdl (NTOSKRNL.EXE.@)
343 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
345 FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
350 /***********************************************************************
351 * IoAllocateWorkItem (NTOSKRNL.EXE.@)
353 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
355 FIXME( "stub: %p\n", DeviceObject );
360 /***********************************************************************
361 * IoAttachDeviceToDeviceStack (NTOSKRNL.EXE.@)
363 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
364 DEVICE_OBJECT *target )
366 TRACE( "%p, %p\n", source, target );
367 target->AttachedDevice = source;
368 source->StackSize = target->StackSize + 1;
373 /***********************************************************************
374 * IoBuildDeviceIoControlRequest (NTOSKRNL.EXE.@)
376 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
377 PDEVICE_OBJECT DeviceObject,
379 ULONG InputBufferLength,
381 ULONG OutputBufferLength,
382 BOOLEAN InternalDeviceIoControl,
384 PIO_STATUS_BLOCK IoStatusBlock )
387 PIO_STACK_LOCATION irpsp;
388 struct IrpInstance *instance;
390 TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
391 IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
392 OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
393 Event, IoStatusBlock );
395 if (DeviceObject == NULL)
398 irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
402 instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
403 if (instance == NULL)
409 list_add_tail( &Irps, &instance->entry );
411 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation - 1;
412 irpsp->MajorFunction = InternalDeviceIoControl ?
413 IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
414 irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
415 irp->UserIosb = IoStatusBlock;
416 irp->UserEvent = Event;
422 /***********************************************************************
423 * IoCreateDriver (NTOSKRNL.EXE.@)
425 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
427 DRIVER_OBJECT *driver;
428 DRIVER_EXTENSION *extension;
431 if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
432 sizeof(*driver) + sizeof(*extension) )))
433 return STATUS_NO_MEMORY;
435 if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
437 RtlFreeHeap( GetProcessHeap(), 0, driver );
441 extension = (DRIVER_EXTENSION *)(driver + 1);
442 driver->Size = sizeof(*driver);
443 driver->DriverInit = init;
444 driver->DriverExtension = extension;
445 extension->DriverObject = driver;
446 extension->ServiceKeyName = driver->DriverName;
448 status = driver->DriverInit( driver, name );
452 RtlFreeUnicodeString( &driver->DriverName );
453 RtlFreeHeap( GetProcessHeap(), 0, driver );
459 /***********************************************************************
460 * IoDeleteDriver (NTOSKRNL.EXE.@)
462 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
464 RtlFreeUnicodeString( &driver->DriverName );
465 RtlFreeHeap( GetProcessHeap(), 0, driver );
469 /***********************************************************************
470 * IoCreateDevice (NTOSKRNL.EXE.@)
472 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
473 UNICODE_STRING *name, DEVICE_TYPE type,
474 ULONG characteristics, BOOLEAN exclusive,
475 DEVICE_OBJECT **ret_device )
478 DEVICE_OBJECT *device;
480 HANDLE manager = get_device_manager();
482 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
483 driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
485 if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
486 return STATUS_NO_MEMORY;
488 SERVER_START_REQ( create_device )
493 req->manager = wine_server_obj_handle( manager );
494 req->user_ptr = wine_server_client_ptr( device );
495 if (name) wine_server_add_data( req, name->Buffer, name->Length );
496 if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
500 if (status == STATUS_SUCCESS)
502 device->DriverObject = driver;
503 device->DeviceExtension = device + 1;
504 device->DeviceType = type;
505 device->StackSize = 1;
506 device->Reserved = handle;
508 device->NextDevice = driver->DeviceObject;
509 driver->DeviceObject = device;
511 *ret_device = device;
513 else HeapFree( GetProcessHeap(), 0, device );
519 /***********************************************************************
520 * IoDeleteDevice (NTOSKRNL.EXE.@)
522 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
526 TRACE( "%p\n", device );
528 SERVER_START_REQ( delete_device )
530 req->handle = wine_server_obj_handle( device->Reserved );
531 status = wine_server_call( req );
535 if (status == STATUS_SUCCESS)
537 DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
538 while (*prev && *prev != device) prev = &(*prev)->NextDevice;
539 if (*prev) *prev = (*prev)->NextDevice;
540 NtClose( device->Reserved );
541 HeapFree( GetProcessHeap(), 0, device );
546 /***********************************************************************
547 * IoCreateSymbolicLink (NTOSKRNL.EXE.@)
549 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
552 OBJECT_ATTRIBUTES attr;
554 attr.Length = sizeof(attr);
555 attr.RootDirectory = 0;
556 attr.ObjectName = name;
557 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
558 attr.SecurityDescriptor = NULL;
559 attr.SecurityQualityOfService = NULL;
561 TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
562 /* FIXME: store handle somewhere */
563 return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
567 /***********************************************************************
568 * IoDeleteSymbolicLink (NTOSKRNL.EXE.@)
570 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
573 OBJECT_ATTRIBUTES attr;
576 attr.Length = sizeof(attr);
577 attr.RootDirectory = 0;
578 attr.ObjectName = name;
579 attr.Attributes = OBJ_CASE_INSENSITIVE;
580 attr.SecurityDescriptor = NULL;
581 attr.SecurityQualityOfService = NULL;
583 if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
585 SERVER_START_REQ( unlink_object )
587 req->handle = wine_server_obj_handle( handle );
588 status = wine_server_call( req );
597 /***********************************************************************
598 * IoGetDeviceObjectPointer (NTOSKRNL.EXE.@)
600 NTSTATUS WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
602 FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
603 return STATUS_NOT_IMPLEMENTED;
607 /***********************************************************************
608 * IofCallDriver (NTOSKRNL.EXE.@)
610 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
611 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
612 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
614 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
617 PDRIVER_DISPATCH dispatch;
618 IO_STACK_LOCATION *irpsp;
621 TRACE( "%p %p\n", device, irp );
623 --irp->CurrentLocation;
624 irpsp = --irp->Tail.Overlay.s.u.CurrentStackLocation;
625 dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
626 status = dispatch( device, irp );
632 /***********************************************************************
633 * IoGetRelatedDeviceObject (NTOSKRNL.EXE.@)
635 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
637 FIXME( "stub: %p\n", obj );
641 static CONFIGURATION_INFORMATION configuration_information;
643 /***********************************************************************
644 * IoGetConfigurationInformation (NTOSKRNL.EXE.@)
646 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
648 FIXME( "partial stub\n" );
649 /* FIXME: return actual devices on system */
650 return &configuration_information;
654 /***********************************************************************
655 * IoRegisterDriverReinitialization (NTOSKRNL.EXE.@)
657 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
659 FIXME( "stub: %p %p %p\n", obj, reinit, context );
663 /***********************************************************************
664 * IoRegisterShutdownNotification (NTOSKRNL.EXE.@)
666 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
668 FIXME( "stub: %p\n", obj );
669 return STATUS_SUCCESS;
673 /***********************************************************************
674 * IofCompleteRequest (NTOSKRNL.EXE.@)
676 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
677 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
678 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
680 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
683 IO_STACK_LOCATION *irpsp;
684 PIO_COMPLETION_ROUTINE routine;
685 IO_STATUS_BLOCK *iosb;
686 struct IrpInstance *instance;
687 NTSTATUS status, stat;
690 TRACE( "%p %u\n", irp, priority_boost );
692 iosb = irp->UserIosb;
693 status = irp->IoStatus.u.Status;
694 while (irp->CurrentLocation <= irp->StackCount)
696 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
697 routine = irpsp->CompletionRoutine;
699 /* FIXME: add SL_INVOKE_ON_CANCEL support */
702 if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
704 if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
707 ++irp->CurrentLocation;
708 ++irp->Tail.Overlay.s.u.CurrentStackLocation;
711 TRACE( "calling %p( %p, %p, %p )\n", routine,
712 irpsp->DeviceObject, irp, irpsp->Context );
713 stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
714 TRACE( "CompletionRoutine returned %x\n", stat );
715 if (STATUS_MORE_PROCESSING_REQUIRED == stat)
719 if (iosb && STATUS_SUCCESS == status)
721 iosb->u.Status = irp->IoStatus.u.Status;
722 iosb->Information = irp->IoStatus.Information;
724 LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
726 if (instance->irp == irp)
728 list_remove( &instance->entry );
729 HeapFree( GetProcessHeap(), 0, instance );
737 /***********************************************************************
738 * InterlockedCompareExchange (NTOSKRNL.EXE.@)
740 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
741 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
742 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
744 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
747 return InterlockedCompareExchange( dest, xchg, compare );
751 /***********************************************************************
752 * InterlockedDecrement (NTOSKRNL.EXE.@)
754 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
755 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
756 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
758 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
761 return InterlockedDecrement( dest );
765 /***********************************************************************
766 * InterlockedExchange (NTOSKRNL.EXE.@)
768 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
769 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
770 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
772 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
775 return InterlockedExchange( dest, val );
779 /***********************************************************************
780 * InterlockedExchangeAdd (NTOSKRNL.EXE.@)
782 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
783 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
784 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
786 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
789 return InterlockedExchangeAdd( dest, incr );
793 /***********************************************************************
794 * InterlockedIncrement (NTOSKRNL.EXE.@)
796 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
797 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
798 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
800 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
803 return InterlockedIncrement( dest );
807 /***********************************************************************
808 * ExAllocatePool (NTOSKRNL.EXE.@)
810 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
812 return ExAllocatePoolWithTag( type, size, 0 );
816 /***********************************************************************
817 * ExAllocatePoolWithQuota (NTOSKRNL.EXE.@)
819 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
821 return ExAllocatePoolWithTag( type, size, 0 );
825 /***********************************************************************
826 * ExAllocatePoolWithTag (NTOSKRNL.EXE.@)
828 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
830 /* FIXME: handle page alignment constraints */
831 void *ret = HeapAlloc( GetProcessHeap(), 0, size );
832 TRACE( "%lu pool %u -> %p\n", size, type, ret );
837 /***********************************************************************
838 * ExAllocatePoolWithQuotaTag (NTOSKRNL.EXE.@)
840 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
842 return ExAllocatePoolWithTag( type, size, tag );
846 /***********************************************************************
847 * ExFreePool (NTOSKRNL.EXE.@)
849 void WINAPI ExFreePool( void *ptr )
851 ExFreePoolWithTag( ptr, 0 );
855 /***********************************************************************
856 * ExFreePoolWithTag (NTOSKRNL.EXE.@)
858 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
860 TRACE( "%p\n", ptr );
861 HeapFree( GetProcessHeap(), 0, ptr );
865 /***********************************************************************
866 * KeInitializeEvent (NTOSKRNL.EXE.@)
868 void WINAPI KeInitializeEvent( PRKEVENT Event, EVENT_TYPE Type, BOOLEAN State )
870 FIXME( "stub: %p %d %d\n", Event, Type, State );
874 /***********************************************************************
875 * KeInitializeSpinLock (NTOSKRNL.EXE.@)
877 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
879 FIXME( "stub: %p\n", SpinLock );
883 /***********************************************************************
884 * KeInitializeTimerEx (NTOSKRNL.EXE.@)
886 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
888 FIXME( "stub: %p %d\n", Timer, Type );
892 /***********************************************************************
893 * KeInitializeTimer (NTOSKRNL.EXE.@)
895 void WINAPI KeInitializeTimer( PKTIMER Timer )
897 KeInitializeTimerEx(Timer, NotificationTimer);
901 /**********************************************************************
902 * KeQueryActiveProcessors (NTOSKRNL.EXE.@)
904 * Return the active Processors as bitmask
907 * active Processors as bitmask
910 KAFFINITY WINAPI KeQueryActiveProcessors( void )
912 DWORD_PTR AffinityMask;
914 GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
919 /**********************************************************************
920 * KeQueryInterruptTime (NTOSKRNL.EXE.@)
922 * Return the interrupt time count
925 ULONGLONG WINAPI KeQueryInterruptTime( void )
927 LARGE_INTEGER totaltime;
929 KeQueryTickCount(&totaltime);
930 return totaltime.QuadPart;
934 /***********************************************************************
935 * KeQuerySystemTime (NTOSKRNL.EXE.@)
937 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
939 NtQuerySystemTime( time );
943 /***********************************************************************
944 * KeQueryTickCount (NTOSKRNL.EXE.@)
946 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
948 count->QuadPart = NtGetTickCount();
949 /* update the global variable too */
950 KeTickCount.LowPart = count->u.LowPart;
951 KeTickCount.High1Time = count->u.HighPart;
952 KeTickCount.High2Time = count->u.HighPart;
956 /***********************************************************************
957 * KeQueryTimeIncrement (NTOSKRNL.EXE.@)
959 ULONG WINAPI KeQueryTimeIncrement(void)
965 /***********************************************************************
966 * MmAllocateNonCachedMemory (NTOSKRNL.EXE.@)
968 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
970 TRACE( "%lu\n", size );
971 return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
975 /***********************************************************************
976 * MmFreeNonCachedMemory (NTOSKRNL.EXE.@)
978 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
980 TRACE( "%p %lu\n", addr, size );
981 VirtualFree( addr, 0, MEM_RELEASE );
984 /***********************************************************************
985 * MmIsAddressValid (NTOSKRNL.EXE.@)
987 * Check if the process can access the virtual address without a pagefault
990 * VirtualAddress [I] Address to check
994 * Success: TRUE (Accessing the Address works without a Pagefault)
997 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
999 TRACE("(%p)\n", VirtualAddress);
1000 return !IsBadWritePtr(VirtualAddress, 1);
1003 /***********************************************************************
1004 * MmPageEntireDriver (NTOSKRNL.EXE.@)
1006 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1008 TRACE("%p\n", AddrInSection);
1009 return AddrInSection;
1012 /***********************************************************************
1013 * MmResetDriverPaging (NTOSKRNL.EXE.@)
1015 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1017 TRACE("%p\n", AddrInSection);
1021 /***********************************************************************
1022 * ObReferenceObjectByHandle (NTOSKRNL.EXE.@)
1024 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1026 KPROCESSOR_MODE mode, PVOID* ptr,
1027 POBJECT_HANDLE_INFORMATION info)
1029 FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1030 return STATUS_NOT_IMPLEMENTED;
1034 /***********************************************************************
1035 * ObfDereferenceObject (NTOSKRNL.EXE.@)
1037 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1038 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1039 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1041 void WINAPI ObfDereferenceObject( VOID *obj )
1044 FIXME( "stub: %p\n", obj );
1048 /***********************************************************************
1049 * PsCreateSystemThread (NTOSKRNL.EXE.@)
1051 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1052 POBJECT_ATTRIBUTES ObjectAttributes,
1053 HANDLE ProcessHandle, PCLIENT_ID ClientId,
1054 PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1056 if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1057 return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1058 0, StartRoutine, StartContext,
1059 ThreadHandle, ClientId);
1062 /***********************************************************************
1063 * PsGetCurrentProcessId (NTOSKRNL.EXE.@)
1065 HANDLE WINAPI PsGetCurrentProcessId(void)
1067 return (HANDLE)GetCurrentProcessId(); /* FIXME: not quite right... */
1071 /***********************************************************************
1072 * PsGetCurrentThreadId (NTOSKRNL.EXE.@)
1074 HANDLE WINAPI PsGetCurrentThreadId(void)
1076 return (HANDLE)GetCurrentThreadId(); /* FIXME: not quite right... */
1080 /***********************************************************************
1081 * PsGetVersion (NTOSKRNL.EXE.@)
1083 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1085 RTL_OSVERSIONINFOEXW info;
1087 RtlGetVersion( &info );
1088 if (major) *major = info.dwMajorVersion;
1089 if (minor) *minor = info.dwMinorVersion;
1090 if (build) *build = info.dwBuildNumber;
1094 #if 0 /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1095 size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1096 memcpy( version->Buffer, info.szCSDVersion, len );
1097 if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1098 version->Length = len;
1105 /***********************************************************************
1106 * PsSetCreateProcessNotifyRoutine (NTOSKRNL.EXE.@)
1108 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1110 FIXME( "stub: %p %d\n", callback, remove );
1111 return STATUS_SUCCESS;
1115 /***********************************************************************
1116 * PsSetCreateThreadNotifyRoutine (NTOSKRNL.EXE.@)
1118 NTSTATUS WINAPI PsSetCreateThreadNotifyRoutine( PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine )
1120 FIXME( "stub: %p\n", NotifyRoutine );
1121 return STATUS_SUCCESS;
1125 /***********************************************************************
1126 * MmGetSystemRoutineAddress (NTOSKRNL.EXE.@)
1128 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1131 STRING routineNameA;
1134 static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1135 static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1137 if (!SystemRoutineName) return NULL;
1139 if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1141 /* We only support functions exported from ntoskrnl.exe or hal.dll */
1142 hMod = GetModuleHandleW( ntoskrnlW );
1143 pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1146 hMod = GetModuleHandleW( halW );
1147 if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1149 RtlFreeAnsiString( &routineNameA );
1152 TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1156 /*****************************************************
1159 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1161 LARGE_INTEGER count;
1165 case DLL_PROCESS_ATTACH:
1166 DisableThreadLibraryCalls( inst );
1167 RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1168 KeQueryTickCount( &count ); /* initialize the global KeTickCount */