ntoskrnl.exe: Add stub for KeInitializeEvent.
[wine] / dlls / ntoskrnl.exe / ntoskrnl.c
1 /*
2  * ntoskrnl.exe implementation
3  *
4  * Copyright (C) 2007 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
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"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl);
41 WINE_DECLARE_DEBUG_CHANNEL(relay);
42
43
44 KSYSTEM_TIME KeTickCount = { 0, 0, 0 };
45
46 typedef struct _KSERVICE_TABLE_DESCRIPTOR
47 {
48     PULONG_PTR Base;
49     PULONG Count;
50     ULONG Limit;
51     PUCHAR Number;
52 } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
53
54 KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[4] = { { 0 } };
55
56 typedef void (WINAPI *PCREATE_PROCESS_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
57
58 static struct list Irps = LIST_INIT(Irps);
59
60 struct IrpInstance
61 {
62     struct list entry;
63     IRP *irp;
64 };
65
66 #ifdef __i386__
67 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
68     __ASM_GLOBAL_FUNC( name, \
69                        "popl %eax\n\t" \
70                        "pushl %ecx\n\t" \
71                        "pushl %eax\n\t" \
72                        "jmp " __ASM_NAME("__regs_") #name )
73 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
74     __ASM_GLOBAL_FUNC( name, \
75                        "popl %eax\n\t" \
76                        "pushl %edx\n\t" \
77                        "pushl %ecx\n\t" \
78                        "pushl %eax\n\t" \
79                        "jmp " __ASM_NAME("__regs_") #name )
80 #endif
81
82 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
83 {
84     if (!us) return "<null>";
85     return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
86 }
87
88 static HANDLE get_device_manager(void)
89 {
90     static HANDLE device_manager;
91     HANDLE handle = 0, ret = device_manager;
92
93     if (!ret)
94     {
95         SERVER_START_REQ( create_device_manager )
96         {
97             req->access     = SYNCHRONIZE;
98             req->attributes = 0;
99             if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
100         }
101         SERVER_END_REQ;
102
103         if (!handle)
104         {
105             ERR( "failed to create the device manager\n" );
106             return 0;
107         }
108         if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
109             ret = handle;
110         else
111             NtClose( handle );  /* somebody beat us to it */
112     }
113     return ret;
114 }
115
116 /* exception handler for emulation of privileged instructions */
117 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
118 {
119     extern DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context );
120
121     EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
122     CONTEXT86 *context = ptrs->ContextRecord;
123
124     if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
125         record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
126     {
127         if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
128             return EXCEPTION_CONTINUE_EXECUTION;
129     }
130     return EXCEPTION_CONTINUE_SEARCH;
131 }
132
133 /* process an ioctl request for a given device */
134 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
135                                void *out_buff, ULONG *out_size )
136 {
137     IRP irp;
138     MDL mdl;
139     IO_STACK_LOCATION irpsp;
140     PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
141     NTSTATUS status;
142     LARGE_INTEGER count;
143
144     TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
145
146     /* so we can spot things that we should initialize */
147     memset( &irp, 0x55, sizeof(irp) );
148     memset( &irpsp, 0x66, sizeof(irpsp) );
149     memset( &mdl, 0x77, sizeof(mdl) );
150
151     irp.RequestorMode = UserMode;
152     irp.AssociatedIrp.SystemBuffer = in_buff;
153     irp.UserBuffer = out_buff;
154     irp.MdlAddress = &mdl;
155     irp.Tail.Overlay.s.u.CurrentStackLocation = &irpsp;
156     irp.UserIosb = NULL;
157
158     irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
159     irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
160     irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
161     irpsp.Parameters.DeviceIoControl.IoControlCode = code;
162     irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
163     irpsp.DeviceObject = device;
164     irpsp.CompletionRoutine = NULL;
165
166     mdl.Next = NULL;
167     mdl.Size = 0;
168     mdl.StartVa = out_buff;
169     mdl.ByteCount = *out_size;
170     mdl.ByteOffset = 0;
171
172     device->CurrentIrp = &irp;
173
174     KeQueryTickCount( &count );  /* update the global KeTickCount */
175
176     if (TRACE_ON(relay))
177         DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
178                  GetCurrentThreadId(), dispatch, device, &irp );
179
180     status = dispatch( device, &irp );
181
182     if (TRACE_ON(relay))
183         DPRINTF( "%04x:Ret  driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
184                  GetCurrentThreadId(), dispatch, device, &irp, status );
185
186     *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
187     return irp.IoStatus.u.Status;
188 }
189
190
191 /***********************************************************************
192  *           wine_ntoskrnl_main_loop   (Not a Windows API)
193  */
194 NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event )
195 {
196     HANDLE manager = get_device_manager();
197     obj_handle_t ioctl = 0;
198     NTSTATUS status = STATUS_SUCCESS;
199     ULONG code = 0;
200     void *in_buff, *out_buff = NULL;
201     DEVICE_OBJECT *device = NULL;
202     ULONG in_size = 4096, out_size = 0;
203     HANDLE handles[2];
204
205     if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
206     {
207         ERR( "failed to allocate buffer\n" );
208         return STATUS_NO_MEMORY;
209     }
210
211     handles[0] = stop_event;
212     handles[1] = manager;
213
214     for (;;)
215     {
216         SERVER_START_REQ( get_next_device_request )
217         {
218             req->manager = wine_server_obj_handle( manager );
219             req->prev = ioctl;
220             req->status = status;
221             wine_server_add_data( req, out_buff, out_size );
222             wine_server_set_reply( req, in_buff, in_size );
223             if (!(status = wine_server_call( req )))
224             {
225                 code     = reply->code;
226                 ioctl    = reply->next;
227                 device   = reply->user_ptr;
228                 in_size  = reply->in_size;
229                 out_size = reply->out_size;
230             }
231             else
232             {
233                 ioctl = 0; /* no previous ioctl */
234                 out_size = 0;
235                 in_size = reply->in_size;
236             }
237         }
238         SERVER_END_REQ;
239
240         switch(status)
241         {
242         case STATUS_SUCCESS:
243             HeapFree( GetProcessHeap(), 0, out_buff );
244             if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
245             else out_buff = NULL;
246             status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
247             break;
248         case STATUS_BUFFER_OVERFLOW:
249             HeapFree( GetProcessHeap(), 0, in_buff );
250             in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
251             /* restart with larger buffer */
252             break;
253         case STATUS_PENDING:
254             if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
255                 return STATUS_SUCCESS;
256             break;
257         }
258     }
259 }
260
261
262 /***********************************************************************
263  *           IoAllocateDriverObjectExtension  (NTOSKRNL.EXE.@)
264  */
265 NTSTATUS WINAPI IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject,
266                                                  PVOID ClientIdentificationAddress,
267                                                  ULONG DriverObjectExtensionSize,
268                                                  PVOID *DriverObjectExtension )
269 {
270     FIXME( "%p, %p, %u, %p\n", DriverObject, ClientIdentificationAddress,
271             DriverObjectExtensionSize, DriverObjectExtension );
272     return STATUS_NOT_IMPLEMENTED;
273 }
274
275
276 /***********************************************************************
277  *           IoGetDriverObjectExtension  (NTOSKRNL.EXE.@)
278  */
279 PVOID WINAPI IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject,
280                                          PVOID ClientIdentificationAddress )
281 {
282     FIXME( "%p, %p\n", DriverObject, ClientIdentificationAddress );
283     return NULL;
284 }
285
286
287 /***********************************************************************
288  *           IoInitializeIrp  (NTOSKRNL.EXE.@)
289  */
290 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
291 {
292     TRACE( "%p, %u, %d\n", irp, size, stack_size );
293
294     RtlZeroMemory( irp, size );
295
296     irp->Type = IO_TYPE_IRP;
297     irp->Size = size;
298     InitializeListHead( &irp->ThreadListEntry );
299     irp->StackCount = stack_size;
300     irp->CurrentLocation = stack_size + 1;
301     irp->Tail.Overlay.s.u.CurrentStackLocation =
302             (PIO_STACK_LOCATION)(irp + 1) + stack_size;
303 }
304
305
306 /***********************************************************************
307  *           IoAllocateIrp  (NTOSKRNL.EXE.@)
308  */
309 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
310 {
311     SIZE_T size;
312     PIRP irp;
313
314     TRACE( "%d, %d\n", stack_size, charge_quota );
315
316     size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
317     irp = ExAllocatePool( NonPagedPool, size );
318     if (irp == NULL)
319         return NULL;
320     IoInitializeIrp( irp, size, stack_size );
321     irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
322     if (charge_quota)
323         irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
324     return irp;
325 }
326
327
328 /***********************************************************************
329  *           IoFreeIrp  (NTOSKRNL.EXE.@)
330  */
331 void WINAPI IoFreeIrp( IRP *irp )
332 {
333     TRACE( "%p\n", irp );
334
335     ExFreePool( irp );
336 }
337
338
339 /***********************************************************************
340  *           IoAllocateMdl  (NTOSKRNL.EXE.@)
341  */
342 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
343 {
344     FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
345     return NULL;
346 }
347
348
349 /***********************************************************************
350  *           IoAllocateWorkItem  (NTOSKRNL.EXE.@)
351  */
352 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
353 {
354     FIXME( "stub: %p\n", DeviceObject );
355     return NULL;
356 }
357
358
359 /***********************************************************************
360  *           IoAttachDeviceToDeviceStack  (NTOSKRNL.EXE.@)
361  */
362 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
363                                                    DEVICE_OBJECT *target )
364 {
365     TRACE( "%p, %p\n", source, target );
366     target->AttachedDevice = source;
367     source->StackSize = target->StackSize + 1;
368     return target;
369 }
370
371
372 /***********************************************************************
373  *           IoBuildDeviceIoControlRequest  (NTOSKRNL.EXE.@)
374  */
375 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
376                                            PDEVICE_OBJECT DeviceObject,
377                                            PVOID InputBuffer,
378                                            ULONG InputBufferLength,
379                                            PVOID OutputBuffer,
380                                            ULONG OutputBufferLength,
381                                            BOOLEAN InternalDeviceIoControl,
382                                            PKEVENT Event,
383                                            PIO_STATUS_BLOCK IoStatusBlock )
384 {
385     PIRP irp;
386     PIO_STACK_LOCATION irpsp;
387     struct IrpInstance *instance;
388
389     TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
390            IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
391            OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
392            Event, IoStatusBlock );
393
394     if (DeviceObject == NULL)
395         return NULL;
396
397     irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
398     if (irp == NULL)
399         return NULL;
400
401     instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
402     if (instance == NULL)
403     {
404         IoFreeIrp( irp );
405         return NULL;
406     }
407     instance->irp = irp;
408     list_add_tail( &Irps, &instance->entry );
409
410     irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation - 1;
411     irpsp->MajorFunction = InternalDeviceIoControl ?
412             IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
413     irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
414     irp->UserIosb = IoStatusBlock;
415     irp->UserEvent = Event;
416
417     return irp;
418 }
419
420
421 /***********************************************************************
422  *           IoCreateDriver   (NTOSKRNL.EXE.@)
423  */
424 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
425 {
426     DRIVER_OBJECT *driver;
427     DRIVER_EXTENSION *extension;
428     NTSTATUS status;
429
430     if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
431                                     sizeof(*driver) + sizeof(*extension) )))
432         return STATUS_NO_MEMORY;
433
434     if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
435     {
436         RtlFreeHeap( GetProcessHeap(), 0, driver );
437         return status;
438     }
439
440     extension = (DRIVER_EXTENSION *)(driver + 1);
441     driver->Size            = sizeof(*driver);
442     driver->DriverInit      = init;
443     driver->DriverExtension = extension;
444     extension->DriverObject   = driver;
445     extension->ServiceKeyName = driver->DriverName;
446
447     status = driver->DriverInit( driver, name );
448
449     if (status)
450     {
451         RtlFreeUnicodeString( &driver->DriverName );
452         RtlFreeHeap( GetProcessHeap(), 0, driver );
453     }
454     return status;
455 }
456
457
458 /***********************************************************************
459  *           IoDeleteDriver   (NTOSKRNL.EXE.@)
460  */
461 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
462 {
463     RtlFreeUnicodeString( &driver->DriverName );
464     RtlFreeHeap( GetProcessHeap(), 0, driver );
465 }
466
467
468 /***********************************************************************
469  *           IoCreateDevice   (NTOSKRNL.EXE.@)
470  */
471 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
472                                 UNICODE_STRING *name, DEVICE_TYPE type,
473                                 ULONG characteristics, BOOLEAN exclusive,
474                                 DEVICE_OBJECT **ret_device )
475 {
476     NTSTATUS status;
477     DEVICE_OBJECT *device;
478     HANDLE handle = 0;
479     HANDLE manager = get_device_manager();
480
481     TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
482            driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
483
484     if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
485         return STATUS_NO_MEMORY;
486
487     SERVER_START_REQ( create_device )
488     {
489         req->access     = 0;
490         req->attributes = 0;
491         req->rootdir    = 0;
492         req->manager    = wine_server_obj_handle( manager );
493         req->user_ptr   = device;
494         if (name) wine_server_add_data( req, name->Buffer, name->Length );
495         if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
496     }
497     SERVER_END_REQ;
498
499     if (status == STATUS_SUCCESS)
500     {
501         device->DriverObject    = driver;
502         device->DeviceExtension = device + 1;
503         device->DeviceType      = type;
504         device->StackSize       = 1;
505         device->Reserved        = handle;
506
507         device->NextDevice   = driver->DeviceObject;
508         driver->DeviceObject = device;
509
510         *ret_device = device;
511     }
512     else HeapFree( GetProcessHeap(), 0, device );
513
514     return status;
515 }
516
517
518 /***********************************************************************
519  *           IoDeleteDevice   (NTOSKRNL.EXE.@)
520  */
521 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
522 {
523     NTSTATUS status;
524
525     TRACE( "%p\n", device );
526
527     SERVER_START_REQ( delete_device )
528     {
529         req->handle = wine_server_obj_handle( device->Reserved );
530         status = wine_server_call( req );
531     }
532     SERVER_END_REQ;
533
534     if (status == STATUS_SUCCESS)
535     {
536         DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
537         while (*prev && *prev != device) prev = &(*prev)->NextDevice;
538         if (*prev) *prev = (*prev)->NextDevice;
539         NtClose( device->Reserved );
540         HeapFree( GetProcessHeap(), 0, device );
541     }
542 }
543
544
545 /***********************************************************************
546  *           IoCreateSymbolicLink   (NTOSKRNL.EXE.@)
547  */
548 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
549 {
550     HANDLE handle;
551     OBJECT_ATTRIBUTES attr;
552
553     attr.Length                   = sizeof(attr);
554     attr.RootDirectory            = 0;
555     attr.ObjectName               = name;
556     attr.Attributes               = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
557     attr.SecurityDescriptor       = NULL;
558     attr.SecurityQualityOfService = NULL;
559
560     TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
561     /* FIXME: store handle somewhere */
562     return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
563 }
564
565
566 /***********************************************************************
567  *           IoDeleteSymbolicLink   (NTOSKRNL.EXE.@)
568  */
569 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
570 {
571     HANDLE handle;
572     OBJECT_ATTRIBUTES attr;
573     NTSTATUS status;
574
575     attr.Length                   = sizeof(attr);
576     attr.RootDirectory            = 0;
577     attr.ObjectName               = name;
578     attr.Attributes               = OBJ_CASE_INSENSITIVE;
579     attr.SecurityDescriptor       = NULL;
580     attr.SecurityQualityOfService = NULL;
581
582     if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
583     {
584         SERVER_START_REQ( unlink_object )
585         {
586             req->handle = wine_server_obj_handle( handle );
587             status = wine_server_call( req );
588         }
589         SERVER_END_REQ;
590         NtClose( handle );
591     }
592     return status;
593 }
594
595
596 /***********************************************************************
597  *           IoGetDeviceObjectPointer   (NTOSKRNL.EXE.@)
598  */
599 NTSTATUS  WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
600 {
601     FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
602     return STATUS_NOT_IMPLEMENTED;
603 }
604
605
606 /***********************************************************************
607  *           IofCallDriver   (NTOSKRNL.EXE.@)
608  */
609 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
610 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
611 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
612 #else
613 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
614 #endif
615 {
616     PDRIVER_DISPATCH dispatch;
617     IO_STACK_LOCATION *irpsp;
618     NTSTATUS status;
619
620     TRACE( "%p %p\n", device, irp );
621
622     --irp->CurrentLocation;
623     irpsp = --irp->Tail.Overlay.s.u.CurrentStackLocation;
624     dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
625     status = dispatch( device, irp );
626
627     return status;
628 }
629
630
631 /***********************************************************************
632  *           IoGetRelatedDeviceObject    (NTOSKRNL.EXE.@)
633  */
634 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
635 {
636     FIXME( "stub: %p\n", obj );
637     return NULL;
638 }
639
640 static CONFIGURATION_INFORMATION configuration_information;
641
642 /***********************************************************************
643  *           IoGetConfigurationInformation    (NTOSKRNL.EXE.@)
644  */
645 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
646 {
647     FIXME( "partial stub\n" );
648     /* FIXME: return actual devices on system */
649     return &configuration_information;
650 }
651
652
653 /***********************************************************************
654  *           IoRegisterDriverReinitialization    (NTOSKRNL.EXE.@)
655  */
656 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
657 {
658     FIXME( "stub: %p %p %p\n", obj, reinit, context );
659 }
660
661
662 /***********************************************************************
663  *           IoRegisterShutdownNotification    (NTOSKRNL.EXE.@)
664  */
665 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
666 {
667     FIXME( "stub: %p\n", obj );
668     return STATUS_SUCCESS;
669 }
670
671
672 /***********************************************************************
673  *           IofCompleteRequest   (NTOSKRNL.EXE.@)
674  */
675 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
676 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
677 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
678 #else
679 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
680 #endif
681 {
682     IO_STACK_LOCATION *irpsp;
683     PIO_COMPLETION_ROUTINE routine;
684     IO_STATUS_BLOCK *iosb;
685     struct IrpInstance *instance;
686     NTSTATUS status, stat;
687     int call_flag = 0;
688
689     TRACE( "%p %u\n", irp, priority_boost );
690
691     iosb = irp->UserIosb;
692     status = irp->IoStatus.u.Status;
693     while (irp->CurrentLocation <= irp->StackCount)
694     {
695         irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
696         routine = irpsp->CompletionRoutine;
697         call_flag = 0;
698         /* FIXME: add SL_INVOKE_ON_CANCEL support */
699         if (routine)
700         {
701             if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
702                 call_flag = 1;
703             if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
704                 call_flag = 1;
705         }
706         ++irp->CurrentLocation;
707         ++irp->Tail.Overlay.s.u.CurrentStackLocation;
708         if (call_flag)
709         {
710             TRACE( "calling %p( %p, %p, %p )\n", routine,
711                     irpsp->DeviceObject, irp, irpsp->Context );
712             stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
713             TRACE( "CompletionRoutine returned %x\n", stat );
714             if (STATUS_MORE_PROCESSING_REQUIRED == stat)
715                 return;
716         }
717     }
718     if (iosb && STATUS_SUCCESS == status)
719     {
720         iosb->u.Status = irp->IoStatus.u.Status;
721         iosb->Information = irp->IoStatus.Information;
722     }
723     LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
724     {
725         if (instance->irp == irp)
726         {
727             list_remove( &instance->entry );
728             HeapFree( GetProcessHeap(), 0, instance );
729             IoFreeIrp( irp );
730             break;
731         }
732     }
733 }
734
735
736 /***********************************************************************
737  *           InterlockedCompareExchange   (NTOSKRNL.EXE.@)
738  */
739 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
740 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
741 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
742 #else
743 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
744 #endif
745 {
746     return InterlockedCompareExchange( dest, xchg, compare );
747 }
748
749
750 /***********************************************************************
751  *           InterlockedDecrement   (NTOSKRNL.EXE.@)
752  */
753 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
754 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
755 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
756 #else
757 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
758 #endif
759 {
760     return InterlockedDecrement( dest );
761 }
762
763
764 /***********************************************************************
765  *           InterlockedExchange   (NTOSKRNL.EXE.@)
766  */
767 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
768 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
769 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
770 #else
771 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
772 #endif
773 {
774     return InterlockedExchange( dest, val );
775 }
776
777
778 /***********************************************************************
779  *           InterlockedExchangeAdd   (NTOSKRNL.EXE.@)
780  */
781 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
782 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
783 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
784 #else
785 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
786 #endif
787 {
788     return InterlockedExchangeAdd( dest, incr );
789 }
790
791
792 /***********************************************************************
793  *           InterlockedIncrement   (NTOSKRNL.EXE.@)
794  */
795 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
796 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
797 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
798 #else
799 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
800 #endif
801 {
802     return InterlockedIncrement( dest );
803 }
804
805
806 /***********************************************************************
807  *           ExAllocatePool   (NTOSKRNL.EXE.@)
808  */
809 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
810 {
811     return ExAllocatePoolWithTag( type, size, 0 );
812 }
813
814
815 /***********************************************************************
816  *           ExAllocatePoolWithQuota   (NTOSKRNL.EXE.@)
817  */
818 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
819 {
820     return ExAllocatePoolWithTag( type, size, 0 );
821 }
822
823
824 /***********************************************************************
825  *           ExAllocatePoolWithTag   (NTOSKRNL.EXE.@)
826  */
827 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
828 {
829     /* FIXME: handle page alignment constraints */
830     void *ret = HeapAlloc( GetProcessHeap(), 0, size );
831     TRACE( "%lu pool %u -> %p\n", size, type, ret );
832     return ret;
833 }
834
835
836 /***********************************************************************
837  *           ExAllocatePoolWithQuotaTag   (NTOSKRNL.EXE.@)
838  */
839 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
840 {
841     return ExAllocatePoolWithTag( type, size, tag );
842 }
843
844
845 /***********************************************************************
846  *           ExFreePool   (NTOSKRNL.EXE.@)
847  */
848 void WINAPI ExFreePool( void *ptr )
849 {
850     ExFreePoolWithTag( ptr, 0 );
851 }
852
853
854 /***********************************************************************
855  *           ExFreePoolWithTag   (NTOSKRNL.EXE.@)
856  */
857 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
858 {
859     TRACE( "%p\n", ptr );
860     HeapFree( GetProcessHeap(), 0, ptr );
861 }
862
863
864 /***********************************************************************
865  *           KeInitializeEvent   (NTOSKRNL.EXE.@)
866  */
867 void WINAPI KeInitializeEvent( PRKEVENT Event, EVENT_TYPE Type, BOOLEAN State )
868 {
869     FIXME( "stub: %p %d %d\n", Event, Type, State );
870 }
871
872
873 /***********************************************************************
874  *           KeInitializeSpinLock   (NTOSKRNL.EXE.@)
875  */
876 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
877 {
878     FIXME("%p\n", SpinLock);
879 }
880
881
882 /***********************************************************************
883  *           KeInitializeTimerEx   (NTOSKRNL.EXE.@)
884  */
885 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
886 {
887     FIXME("%p %d\n", Timer, Type);
888 }
889
890
891 /***********************************************************************
892  *           KeInitializeTimer   (NTOSKRNL.EXE.@)
893  */
894 void WINAPI KeInitializeTimer( PKTIMER Timer )
895 {
896     KeInitializeTimerEx(Timer, NotificationTimer);
897 }
898
899
900 /**********************************************************************
901  *           KeQueryActiveProcessors   (NTOSKRNL.EXE.@)
902  *
903  * Return the active Processors as bitmask
904  *
905  * RETURNS
906  *   active Processors as bitmask
907  *
908  */
909 KAFFINITY WINAPI KeQueryActiveProcessors( void )
910 {
911     DWORD_PTR AffinityMask;
912
913     GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
914     return AffinityMask;
915 }
916
917
918 /**********************************************************************
919  *           KeQueryInterruptTime   (NTOSKRNL.EXE.@)
920  *
921  * Return the interrupt time count
922  *
923  */
924 ULONGLONG WINAPI KeQueryInterruptTime( void )
925 {
926     LARGE_INTEGER totaltime;
927
928     KeQueryTickCount(&totaltime);
929     return totaltime.QuadPart;
930 }
931
932
933 /***********************************************************************
934  *           KeQuerySystemTime   (NTOSKRNL.EXE.@)
935  */
936 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
937 {
938     NtQuerySystemTime( time );
939 }
940
941
942 /***********************************************************************
943  *           KeQueryTickCount   (NTOSKRNL.EXE.@)
944  */
945 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
946 {
947     count->QuadPart = NtGetTickCount();
948     /* update the global variable too */
949     KeTickCount.LowPart   = count->u.LowPart;
950     KeTickCount.High1Time = count->u.HighPart;
951     KeTickCount.High2Time = count->u.HighPart;
952 }
953
954
955 /***********************************************************************
956  *           KeQueryTimeIncrement   (NTOSKRNL.EXE.@)
957  */
958 ULONG WINAPI KeQueryTimeIncrement(void)
959 {
960     return 10000;
961 }
962
963
964 /***********************************************************************
965  *           MmAllocateNonCachedMemory   (NTOSKRNL.EXE.@)
966  */
967 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
968 {
969     TRACE( "%lu\n", size );
970     return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
971 }
972
973
974 /***********************************************************************
975  *           MmFreeNonCachedMemory   (NTOSKRNL.EXE.@)
976  */
977 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
978 {
979     TRACE( "%p %lu\n", addr, size );
980     VirtualFree( addr, 0, MEM_RELEASE );
981 }
982
983 /***********************************************************************
984  *           MmIsAddressValid   (NTOSKRNL.EXE.@)
985  *
986  * Check if the process can access the virtual address without a pagefault
987  *
988  * PARAMS
989  *  VirtualAddress [I] Address to check
990  *
991  * RETURNS
992  *  Failure: FALSE
993  *  Success: TRUE  (Accessing the Address works without a Pagefault)
994  *
995  */
996 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
997 {
998     TRACE("(%p)\n", VirtualAddress);
999     return !IsBadWritePtr(VirtualAddress, 1);
1000 }
1001
1002 /***********************************************************************
1003  *           MmPageEntireDriver   (NTOSKRNL.EXE.@)
1004  */
1005 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1006 {
1007     TRACE("%p\n", AddrInSection);
1008     return AddrInSection;
1009 }
1010
1011 /***********************************************************************
1012  *           MmResetDriverPaging   (NTOSKRNL.EXE.@)
1013  */
1014 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1015 {
1016     TRACE("%p\n", AddrInSection);
1017 }
1018
1019
1020  /***********************************************************************
1021  *           ObReferenceObjectByHandle    (NTOSKRNL.EXE.@)
1022  */
1023 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1024                                            POBJECT_TYPE type,
1025                                            KPROCESSOR_MODE mode, PVOID* ptr,
1026                                            POBJECT_HANDLE_INFORMATION info)
1027 {
1028     FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1029     return STATUS_NOT_IMPLEMENTED;
1030 }
1031
1032
1033 /***********************************************************************
1034  *           ObfDereferenceObject   (NTOSKRNL.EXE.@)
1035  */
1036 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1037 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1038 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1039 #else
1040 void WINAPI ObfDereferenceObject( VOID *obj )
1041 #endif
1042 {
1043     FIXME( "stub: %p\n", obj );
1044 }
1045
1046
1047 /***********************************************************************
1048  *           PsCreateSystemThread   (NTOSKRNL.EXE.@)
1049  */
1050 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1051                                      POBJECT_ATTRIBUTES ObjectAttributes,
1052                                      HANDLE ProcessHandle, PCLIENT_ID ClientId,
1053                                      PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1054 {
1055     if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1056     return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1057                                0, StartRoutine, StartContext,
1058                                ThreadHandle, ClientId);
1059 }
1060
1061 /***********************************************************************
1062  *           PsGetCurrentProcessId   (NTOSKRNL.EXE.@)
1063  */
1064 HANDLE WINAPI PsGetCurrentProcessId(void)
1065 {
1066     return (HANDLE)GetCurrentProcessId();  /* FIXME: not quite right... */
1067 }
1068
1069
1070 /***********************************************************************
1071  *           PsGetCurrentThreadId   (NTOSKRNL.EXE.@)
1072  */
1073 HANDLE WINAPI PsGetCurrentThreadId(void)
1074 {
1075     return (HANDLE)GetCurrentThreadId();  /* FIXME: not quite right... */
1076 }
1077
1078
1079 /***********************************************************************
1080  *           PsGetVersion   (NTOSKRNL.EXE.@)
1081  */
1082 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1083 {
1084     RTL_OSVERSIONINFOEXW info;
1085
1086     RtlGetVersion( &info );
1087     if (major) *major = info.dwMajorVersion;
1088     if (minor) *minor = info.dwMinorVersion;
1089     if (build) *build = info.dwBuildNumber;
1090
1091     if (version)
1092     {
1093 #if 0  /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1094         size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1095         memcpy( version->Buffer, info.szCSDVersion, len );
1096         if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1097         version->Length = len;
1098 #endif
1099     }
1100     return TRUE;
1101 }
1102
1103
1104 /***********************************************************************
1105  *           PsSetCreateProcessNotifyRoutine   (NTOSKRNL.EXE.@)
1106  */
1107 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1108 {
1109     FIXME( "stub: %p %d\n", callback, remove );
1110     return STATUS_SUCCESS;
1111 }
1112
1113 /***********************************************************************
1114  *           MmGetSystemRoutineAddress   (NTOSKRNL.EXE.@)
1115  */
1116 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1117 {
1118     HMODULE hMod;
1119     STRING routineNameA;
1120     PVOID pFunc = NULL;
1121
1122     static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1123     static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1124
1125     if (!SystemRoutineName) return NULL;
1126
1127     if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1128     {
1129         /* We only support functions exported from ntoskrnl.exe or hal.dll */
1130         hMod = GetModuleHandleW( ntoskrnlW );
1131         pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1132         if (!pFunc)
1133         {
1134            hMod = GetModuleHandleW( halW );
1135            if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1136         }
1137         RtlFreeAnsiString( &routineNameA );
1138     }
1139
1140     TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1141     return pFunc;
1142 }
1143
1144 /*****************************************************
1145  *           DllMain
1146  */
1147 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1148 {
1149     LARGE_INTEGER count;
1150
1151     switch(reason)
1152     {
1153     case DLL_PROCESS_ATTACH:
1154         DisableThreadLibraryCalls( inst );
1155         RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1156         KeQueryTickCount( &count );  /* initialize the global KeTickCount */
1157         break;
1158     }
1159     return TRUE;
1160 }