ntoskrnl.exe: Stub for KeInitializeMutex.
[wine/hacks.git] / dlls / ntoskrnl.exe / ntoskrnl.c
blob50d7b03d9f46c3ac7eeb8351be2d8d9e12b673a0
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_STDCALL_FUNC( name, 4, \
70 "popl %eax\n\t" \
71 "pushl %ecx\n\t" \
72 "pushl %eax\n\t" \
73 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(4))
74 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
75 __ASM_STDCALL_FUNC( name, 8, \
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 __ASM_STDCALL(8))
81 #define DEFINE_FASTCALL3_ENTRYPOINT( name ) \
82 __ASM_STDCALL_FUNC( name, 12, \
83 "popl %eax\n\t" \
84 "pushl %edx\n\t" \
85 "pushl %ecx\n\t" \
86 "pushl %eax\n\t" \
87 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(12))
88 #endif
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;
101 if (!ret)
103 SERVER_START_REQ( create_device_manager )
105 req->access = SYNCHRONIZE;
106 req->attributes = 0;
107 if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
109 SERVER_END_REQ;
111 if (!handle)
113 ERR( "failed to create the device manager\n" );
114 return 0;
116 if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
117 ret = handle;
118 else
119 NtClose( handle ); /* somebody beat us to it */
121 return ret;
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)
132 #ifdef __i386__
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;
138 #else
139 FIXME( "Privileged instruction emulation not implemented on this CPU\n" );
140 #endif
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 )
149 IRP irp;
150 MDL mdl;
151 IO_STACK_LOCATION irpsp;
152 PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
153 NTSTATUS status;
154 LARGE_INTEGER count;
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;
168 irp.UserIosb = NULL;
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;
178 mdl.Next = NULL;
179 mdl.Size = 0;
180 mdl.StartVa = out_buff;
181 mdl.ByteCount = *out_size;
182 mdl.ByteOffset = 0;
184 device->CurrentIrp = &irp;
186 KeQueryTickCount( &count ); /* update the global KeTickCount */
188 if (TRACE_ON(relay))
189 DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
190 GetCurrentThreadId(), dispatch, device, &irp );
192 status = dispatch( device, &irp );
194 if (TRACE_ON(relay))
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;
211 ULONG code = 0;
212 void *in_buff, *out_buff = NULL;
213 DEVICE_OBJECT *device = NULL;
214 ULONG in_size = 4096, out_size = 0;
215 HANDLE handles[2];
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;
226 for (;;)
228 SERVER_START_REQ( get_next_device_request )
230 req->manager = wine_server_obj_handle( manager );
231 req->prev = ioctl;
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 )))
237 code = reply->code;
238 ioctl = reply->next;
239 device = wine_server_get_ptr( reply->user_ptr );
240 in_size = reply->in_size;
241 out_size = reply->out_size;
243 else
245 ioctl = 0; /* no previous ioctl */
246 out_size = 0;
247 in_size = reply->in_size;
250 SERVER_END_REQ;
252 switch(status)
254 case STATUS_SUCCESS:
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 );
259 break;
260 case STATUS_BUFFER_OVERFLOW:
261 HeapFree( GetProcessHeap(), 0, in_buff );
262 in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
263 /* restart with larger buffer */
264 break;
265 case STATUS_PENDING:
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;
272 break;
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 );
299 return NULL;
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;
313 irp->Size = size;
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 * IoAllocateIrp (NTOSKRNL.EXE.@)
325 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
327 SIZE_T size;
328 PIRP irp;
330 TRACE( "%d, %d\n", stack_size, charge_quota );
332 size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
333 irp = ExAllocatePool( NonPagedPool, size );
334 if (irp == NULL)
335 return NULL;
336 IoInitializeIrp( irp, size, stack_size );
337 irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
338 if (charge_quota)
339 irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
340 return irp;
344 /***********************************************************************
345 * IoFreeIrp (NTOSKRNL.EXE.@)
347 void WINAPI IoFreeIrp( IRP *irp )
349 TRACE( "%p\n", irp );
351 ExFreePool( irp );
355 /***********************************************************************
356 * IoAllocateMdl (NTOSKRNL.EXE.@)
358 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
360 FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
361 return NULL;
365 /***********************************************************************
366 * IoAllocateWorkItem (NTOSKRNL.EXE.@)
368 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
370 FIXME( "stub: %p\n", DeviceObject );
371 return NULL;
375 /***********************************************************************
376 * IoAttachDeviceToDeviceStack (NTOSKRNL.EXE.@)
378 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
379 DEVICE_OBJECT *target )
381 TRACE( "%p, %p\n", source, target );
382 target->AttachedDevice = source;
383 source->StackSize = target->StackSize + 1;
384 return target;
388 /***********************************************************************
389 * IoBuildDeviceIoControlRequest (NTOSKRNL.EXE.@)
391 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
392 PDEVICE_OBJECT DeviceObject,
393 PVOID InputBuffer,
394 ULONG InputBufferLength,
395 PVOID OutputBuffer,
396 ULONG OutputBufferLength,
397 BOOLEAN InternalDeviceIoControl,
398 PKEVENT Event,
399 PIO_STATUS_BLOCK IoStatusBlock )
401 PIRP irp;
402 PIO_STACK_LOCATION irpsp;
403 struct IrpInstance *instance;
405 TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
406 IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
407 OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
408 Event, IoStatusBlock );
410 if (DeviceObject == NULL)
411 return NULL;
413 irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
414 if (irp == NULL)
415 return NULL;
417 instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
418 if (instance == NULL)
420 IoFreeIrp( irp );
421 return NULL;
423 instance->irp = irp;
424 list_add_tail( &Irps, &instance->entry );
426 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation - 1;
427 irpsp->MajorFunction = InternalDeviceIoControl ?
428 IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
429 irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
430 irp->UserIosb = IoStatusBlock;
431 irp->UserEvent = Event;
433 return irp;
437 /***********************************************************************
438 * IoCreateDriver (NTOSKRNL.EXE.@)
440 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
442 DRIVER_OBJECT *driver;
443 DRIVER_EXTENSION *extension;
444 NTSTATUS status;
446 if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
447 sizeof(*driver) + sizeof(*extension) )))
448 return STATUS_NO_MEMORY;
450 if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
452 RtlFreeHeap( GetProcessHeap(), 0, driver );
453 return status;
456 extension = (DRIVER_EXTENSION *)(driver + 1);
457 driver->Size = sizeof(*driver);
458 driver->DriverInit = init;
459 driver->DriverExtension = extension;
460 extension->DriverObject = driver;
461 extension->ServiceKeyName = driver->DriverName;
463 status = driver->DriverInit( driver, name );
465 if (status)
467 RtlFreeUnicodeString( &driver->DriverName );
468 RtlFreeHeap( GetProcessHeap(), 0, driver );
470 return status;
474 /***********************************************************************
475 * IoDeleteDriver (NTOSKRNL.EXE.@)
477 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
479 RtlFreeUnicodeString( &driver->DriverName );
480 RtlFreeHeap( GetProcessHeap(), 0, driver );
484 /***********************************************************************
485 * IoCreateDevice (NTOSKRNL.EXE.@)
487 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
488 UNICODE_STRING *name, DEVICE_TYPE type,
489 ULONG characteristics, BOOLEAN exclusive,
490 DEVICE_OBJECT **ret_device )
492 NTSTATUS status;
493 DEVICE_OBJECT *device;
494 HANDLE handle = 0;
495 HANDLE manager = get_device_manager();
497 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
498 driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
500 if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
501 return STATUS_NO_MEMORY;
503 SERVER_START_REQ( create_device )
505 req->access = 0;
506 req->attributes = 0;
507 req->rootdir = 0;
508 req->manager = wine_server_obj_handle( manager );
509 req->user_ptr = wine_server_client_ptr( device );
510 if (name) wine_server_add_data( req, name->Buffer, name->Length );
511 if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
513 SERVER_END_REQ;
515 if (status == STATUS_SUCCESS)
517 device->DriverObject = driver;
518 device->DeviceExtension = device + 1;
519 device->DeviceType = type;
520 device->StackSize = 1;
521 device->Reserved = handle;
523 device->NextDevice = driver->DeviceObject;
524 driver->DeviceObject = device;
526 *ret_device = device;
528 else HeapFree( GetProcessHeap(), 0, device );
530 return status;
534 /***********************************************************************
535 * IoDeleteDevice (NTOSKRNL.EXE.@)
537 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
539 NTSTATUS status;
541 TRACE( "%p\n", device );
543 SERVER_START_REQ( delete_device )
545 req->handle = wine_server_obj_handle( device->Reserved );
546 status = wine_server_call( req );
548 SERVER_END_REQ;
550 if (status == STATUS_SUCCESS)
552 DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
553 while (*prev && *prev != device) prev = &(*prev)->NextDevice;
554 if (*prev) *prev = (*prev)->NextDevice;
555 NtClose( device->Reserved );
556 HeapFree( GetProcessHeap(), 0, device );
561 /***********************************************************************
562 * IoCreateSymbolicLink (NTOSKRNL.EXE.@)
564 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
566 HANDLE handle;
567 OBJECT_ATTRIBUTES attr;
569 attr.Length = sizeof(attr);
570 attr.RootDirectory = 0;
571 attr.ObjectName = name;
572 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
573 attr.SecurityDescriptor = NULL;
574 attr.SecurityQualityOfService = NULL;
576 TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
577 /* FIXME: store handle somewhere */
578 return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
582 /***********************************************************************
583 * IoDeleteSymbolicLink (NTOSKRNL.EXE.@)
585 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
587 HANDLE handle;
588 OBJECT_ATTRIBUTES attr;
589 NTSTATUS status;
591 attr.Length = sizeof(attr);
592 attr.RootDirectory = 0;
593 attr.ObjectName = name;
594 attr.Attributes = OBJ_CASE_INSENSITIVE;
595 attr.SecurityDescriptor = NULL;
596 attr.SecurityQualityOfService = NULL;
598 if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
600 SERVER_START_REQ( unlink_object )
602 req->handle = wine_server_obj_handle( handle );
603 status = wine_server_call( req );
605 SERVER_END_REQ;
606 NtClose( handle );
608 return status;
612 /***********************************************************************
613 * IoGetDeviceObjectPointer (NTOSKRNL.EXE.@)
615 NTSTATUS WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
617 FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
618 return STATUS_NOT_IMPLEMENTED;
622 /***********************************************************************
623 * IofCallDriver (NTOSKRNL.EXE.@)
625 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
626 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
627 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
628 #else
629 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
630 #endif
632 PDRIVER_DISPATCH dispatch;
633 IO_STACK_LOCATION *irpsp;
634 NTSTATUS status;
636 TRACE( "%p %p\n", device, irp );
638 --irp->CurrentLocation;
639 irpsp = --irp->Tail.Overlay.s.u.CurrentStackLocation;
640 dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
641 status = dispatch( device, irp );
643 return status;
647 /***********************************************************************
648 * IoGetRelatedDeviceObject (NTOSKRNL.EXE.@)
650 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
652 FIXME( "stub: %p\n", obj );
653 return NULL;
656 static CONFIGURATION_INFORMATION configuration_information;
658 /***********************************************************************
659 * IoGetConfigurationInformation (NTOSKRNL.EXE.@)
661 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
663 FIXME( "partial stub\n" );
664 /* FIXME: return actual devices on system */
665 return &configuration_information;
669 /***********************************************************************
670 * IoRegisterDriverReinitialization (NTOSKRNL.EXE.@)
672 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
674 FIXME( "stub: %p %p %p\n", obj, reinit, context );
678 /***********************************************************************
679 * IoRegisterShutdownNotification (NTOSKRNL.EXE.@)
681 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
683 FIXME( "stub: %p\n", obj );
684 return STATUS_SUCCESS;
688 /***********************************************************************
689 * IofCompleteRequest (NTOSKRNL.EXE.@)
691 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
692 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
693 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
694 #else
695 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
696 #endif
698 IO_STACK_LOCATION *irpsp;
699 PIO_COMPLETION_ROUTINE routine;
700 IO_STATUS_BLOCK *iosb;
701 struct IrpInstance *instance;
702 NTSTATUS status, stat;
703 int call_flag = 0;
705 TRACE( "%p %u\n", irp, priority_boost );
707 iosb = irp->UserIosb;
708 status = irp->IoStatus.u.Status;
709 while (irp->CurrentLocation <= irp->StackCount)
711 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
712 routine = irpsp->CompletionRoutine;
713 call_flag = 0;
714 /* FIXME: add SL_INVOKE_ON_CANCEL support */
715 if (routine)
717 if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
718 call_flag = 1;
719 if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
720 call_flag = 1;
722 ++irp->CurrentLocation;
723 ++irp->Tail.Overlay.s.u.CurrentStackLocation;
724 if (call_flag)
726 TRACE( "calling %p( %p, %p, %p )\n", routine,
727 irpsp->DeviceObject, irp, irpsp->Context );
728 stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
729 TRACE( "CompletionRoutine returned %x\n", stat );
730 if (STATUS_MORE_PROCESSING_REQUIRED == stat)
731 return;
734 if (iosb && STATUS_SUCCESS == status)
736 iosb->u.Status = irp->IoStatus.u.Status;
737 iosb->Information = irp->IoStatus.Information;
739 LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
741 if (instance->irp == irp)
743 list_remove( &instance->entry );
744 HeapFree( GetProcessHeap(), 0, instance );
745 IoFreeIrp( irp );
746 break;
752 /***********************************************************************
753 * InterlockedCompareExchange (NTOSKRNL.EXE.@)
755 #ifdef DEFINE_FASTCALL3_ENTRYPOINT
756 DEFINE_FASTCALL3_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
757 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
758 #else
759 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
760 #endif
762 return InterlockedCompareExchange( dest, xchg, compare );
766 /***********************************************************************
767 * InterlockedDecrement (NTOSKRNL.EXE.@)
769 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
770 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
771 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
772 #else
773 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
774 #endif
776 return InterlockedDecrement( dest );
780 /***********************************************************************
781 * InterlockedExchange (NTOSKRNL.EXE.@)
783 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
784 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
785 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
786 #else
787 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
788 #endif
790 return InterlockedExchange( dest, val );
794 /***********************************************************************
795 * InterlockedExchangeAdd (NTOSKRNL.EXE.@)
797 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
798 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
799 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
800 #else
801 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
802 #endif
804 return InterlockedExchangeAdd( dest, incr );
808 /***********************************************************************
809 * InterlockedIncrement (NTOSKRNL.EXE.@)
811 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
812 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
813 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
814 #else
815 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
816 #endif
818 return InterlockedIncrement( dest );
822 /***********************************************************************
823 * ExAllocatePool (NTOSKRNL.EXE.@)
825 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
827 return ExAllocatePoolWithTag( type, size, 0 );
831 /***********************************************************************
832 * ExAllocatePoolWithQuota (NTOSKRNL.EXE.@)
834 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
836 return ExAllocatePoolWithTag( type, size, 0 );
840 /***********************************************************************
841 * ExAllocatePoolWithTag (NTOSKRNL.EXE.@)
843 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
845 /* FIXME: handle page alignment constraints */
846 void *ret = HeapAlloc( GetProcessHeap(), 0, size );
847 TRACE( "%lu pool %u -> %p\n", size, type, ret );
848 return ret;
852 /***********************************************************************
853 * ExAllocatePoolWithQuotaTag (NTOSKRNL.EXE.@)
855 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
857 return ExAllocatePoolWithTag( type, size, tag );
861 /***********************************************************************
862 * ExFreePool (NTOSKRNL.EXE.@)
864 void WINAPI ExFreePool( void *ptr )
866 ExFreePoolWithTag( ptr, 0 );
870 /***********************************************************************
871 * ExFreePoolWithTag (NTOSKRNL.EXE.@)
873 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
875 TRACE( "%p\n", ptr );
876 HeapFree( GetProcessHeap(), 0, ptr );
880 /***********************************************************************
881 * KeInitializeEvent (NTOSKRNL.EXE.@)
883 void WINAPI KeInitializeEvent( PRKEVENT Event, EVENT_TYPE Type, BOOLEAN State )
885 FIXME( "stub: %p %d %d\n", Event, Type, State );
889 /***********************************************************************
890 * KeInitializeMutex (NTOSKRNL.EXE.@)
892 void WINAPI KeInitializeMutex(PRKMUTEX Mutex, ULONG Level)
894 FIXME( "stub: %p, %u\n", Mutex, Level );
898 /***********************************************************************
899 * KeInitializeSpinLock (NTOSKRNL.EXE.@)
901 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
903 FIXME( "stub: %p\n", SpinLock );
907 /***********************************************************************
908 * KeInitializeTimerEx (NTOSKRNL.EXE.@)
910 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
912 FIXME( "stub: %p %d\n", Timer, Type );
916 /***********************************************************************
917 * KeInitializeTimer (NTOSKRNL.EXE.@)
919 void WINAPI KeInitializeTimer( PKTIMER Timer )
921 KeInitializeTimerEx(Timer, NotificationTimer);
925 /**********************************************************************
926 * KeQueryActiveProcessors (NTOSKRNL.EXE.@)
928 * Return the active Processors as bitmask
930 * RETURNS
931 * active Processors as bitmask
934 KAFFINITY WINAPI KeQueryActiveProcessors( void )
936 DWORD_PTR AffinityMask;
938 GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
939 return AffinityMask;
943 /**********************************************************************
944 * KeQueryInterruptTime (NTOSKRNL.EXE.@)
946 * Return the interrupt time count
949 ULONGLONG WINAPI KeQueryInterruptTime( void )
951 LARGE_INTEGER totaltime;
953 KeQueryTickCount(&totaltime);
954 return totaltime.QuadPart;
958 /***********************************************************************
959 * KeQuerySystemTime (NTOSKRNL.EXE.@)
961 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
963 NtQuerySystemTime( time );
967 /***********************************************************************
968 * KeQueryTickCount (NTOSKRNL.EXE.@)
970 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
972 count->QuadPart = NtGetTickCount();
973 /* update the global variable too */
974 KeTickCount.LowPart = count->u.LowPart;
975 KeTickCount.High1Time = count->u.HighPart;
976 KeTickCount.High2Time = count->u.HighPart;
980 /***********************************************************************
981 * KeQueryTimeIncrement (NTOSKRNL.EXE.@)
983 ULONG WINAPI KeQueryTimeIncrement(void)
985 return 10000;
989 /***********************************************************************
990 * MmAllocateNonCachedMemory (NTOSKRNL.EXE.@)
992 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
994 TRACE( "%lu\n", size );
995 return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
999 /***********************************************************************
1000 * MmFreeNonCachedMemory (NTOSKRNL.EXE.@)
1002 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
1004 TRACE( "%p %lu\n", addr, size );
1005 VirtualFree( addr, 0, MEM_RELEASE );
1008 /***********************************************************************
1009 * MmIsAddressValid (NTOSKRNL.EXE.@)
1011 * Check if the process can access the virtual address without a pagefault
1013 * PARAMS
1014 * VirtualAddress [I] Address to check
1016 * RETURNS
1017 * Failure: FALSE
1018 * Success: TRUE (Accessing the Address works without a Pagefault)
1021 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
1023 TRACE("(%p)\n", VirtualAddress);
1024 return !IsBadWritePtr(VirtualAddress, 1);
1027 /***********************************************************************
1028 * MmPageEntireDriver (NTOSKRNL.EXE.@)
1030 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1032 TRACE("%p\n", AddrInSection);
1033 return AddrInSection;
1036 /***********************************************************************
1037 * MmResetDriverPaging (NTOSKRNL.EXE.@)
1039 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1041 TRACE("%p\n", AddrInSection);
1045 /***********************************************************************
1046 * ObReferenceObjectByHandle (NTOSKRNL.EXE.@)
1048 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1049 POBJECT_TYPE type,
1050 KPROCESSOR_MODE mode, PVOID* ptr,
1051 POBJECT_HANDLE_INFORMATION info)
1053 FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1054 return STATUS_NOT_IMPLEMENTED;
1058 /***********************************************************************
1059 * ObfDereferenceObject (NTOSKRNL.EXE.@)
1061 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1062 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1063 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1064 #else
1065 void WINAPI ObfDereferenceObject( VOID *obj )
1066 #endif
1068 FIXME( "stub: %p\n", obj );
1072 /***********************************************************************
1073 * PsCreateSystemThread (NTOSKRNL.EXE.@)
1075 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1076 POBJECT_ATTRIBUTES ObjectAttributes,
1077 HANDLE ProcessHandle, PCLIENT_ID ClientId,
1078 PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1080 if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1081 return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1082 0, StartRoutine, StartContext,
1083 ThreadHandle, ClientId);
1086 /***********************************************************************
1087 * PsGetCurrentProcessId (NTOSKRNL.EXE.@)
1089 HANDLE WINAPI PsGetCurrentProcessId(void)
1091 return UlongToHandle(GetCurrentProcessId()); /* FIXME: not quite right... */
1095 /***********************************************************************
1096 * PsGetCurrentThreadId (NTOSKRNL.EXE.@)
1098 HANDLE WINAPI PsGetCurrentThreadId(void)
1100 return UlongToHandle(GetCurrentThreadId()); /* FIXME: not quite right... */
1104 /***********************************************************************
1105 * PsGetVersion (NTOSKRNL.EXE.@)
1107 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1109 RTL_OSVERSIONINFOEXW info;
1111 RtlGetVersion( &info );
1112 if (major) *major = info.dwMajorVersion;
1113 if (minor) *minor = info.dwMinorVersion;
1114 if (build) *build = info.dwBuildNumber;
1116 if (version)
1118 #if 0 /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1119 size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1120 memcpy( version->Buffer, info.szCSDVersion, len );
1121 if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1122 version->Length = len;
1123 #endif
1125 return TRUE;
1129 /***********************************************************************
1130 * PsSetCreateProcessNotifyRoutine (NTOSKRNL.EXE.@)
1132 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1134 FIXME( "stub: %p %d\n", callback, remove );
1135 return STATUS_SUCCESS;
1139 /***********************************************************************
1140 * PsSetCreateThreadNotifyRoutine (NTOSKRNL.EXE.@)
1142 NTSTATUS WINAPI PsSetCreateThreadNotifyRoutine( PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine )
1144 FIXME( "stub: %p\n", NotifyRoutine );
1145 return STATUS_SUCCESS;
1149 /***********************************************************************
1150 * MmGetSystemRoutineAddress (NTOSKRNL.EXE.@)
1152 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1154 HMODULE hMod;
1155 STRING routineNameA;
1156 PVOID pFunc = NULL;
1158 static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1159 static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1161 if (!SystemRoutineName) return NULL;
1163 if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1165 /* We only support functions exported from ntoskrnl.exe or hal.dll */
1166 hMod = GetModuleHandleW( ntoskrnlW );
1167 pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1168 if (!pFunc)
1170 hMod = GetModuleHandleW( halW );
1171 if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1173 RtlFreeAnsiString( &routineNameA );
1176 TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1177 return pFunc;
1181 /***********************************************************************
1182 * MmQuerySystemSize (NTOSKRNL.EXE.@)
1184 MM_SYSTEMSIZE WINAPI MmQuerySystemSize(void)
1186 FIXME("stub\n");
1187 return MmLargeSystem;
1191 /*****************************************************
1192 * DllMain
1194 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1196 static void *handler;
1197 LARGE_INTEGER count;
1199 switch(reason)
1201 case DLL_PROCESS_ATTACH:
1202 DisableThreadLibraryCalls( inst );
1203 handler = RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1204 KeQueryTickCount( &count ); /* initialize the global KeTickCount */
1205 break;
1206 case DLL_PROCESS_DETACH:
1207 RtlRemoveVectoredExceptionHandler( handler );
1208 break;
1210 return TRUE;