d3d8/tests: Fix some error messages.
[wine] / dlls / ntoskrnl.exe / ntoskrnl.c
1 /*
2  * ntoskrnl.exe implementation
3  *
4  * Copyright (C) 2007 Alexandre Julliard
5  * Copyright (C) 2010 Damjan Jovanovic
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
34 #include "excpt.h"
35 #include "ddk/ntddk.h"
36 #include "wine/unicode.h"
37 #include "wine/server.h"
38 #include "wine/list.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl);
42 WINE_DECLARE_DEBUG_CHANNEL(relay);
43
44 BOOLEAN KdDebuggerEnabled = FALSE;
45
46 extern LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs );
47
48 KSYSTEM_TIME KeTickCount = { 0, 0, 0 };
49
50 typedef struct _KSERVICE_TABLE_DESCRIPTOR
51 {
52     PULONG_PTR Base;
53     PULONG Count;
54     ULONG Limit;
55     PUCHAR Number;
56 } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
57
58 KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[4] = { { 0 } };
59
60 typedef void (WINAPI *PCREATE_PROCESS_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
61 typedef void (WINAPI *PCREATE_THREAD_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
62
63 static struct list Irps = LIST_INIT(Irps);
64
65 struct IrpInstance
66 {
67     struct list entry;
68     IRP *irp;
69 };
70
71 #ifdef __i386__
72 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
73     __ASM_STDCALL_FUNC( name, 4, \
74                        "popl %eax\n\t" \
75                        "pushl %ecx\n\t" \
76                        "pushl %eax\n\t" \
77                        "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(4))
78 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
79     __ASM_STDCALL_FUNC( name, 8, \
80                        "popl %eax\n\t" \
81                        "pushl %edx\n\t" \
82                        "pushl %ecx\n\t" \
83                        "pushl %eax\n\t" \
84                        "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(8))
85 #define DEFINE_FASTCALL3_ENTRYPOINT( name ) \
86     __ASM_STDCALL_FUNC( name, 12, \
87                        "popl %eax\n\t" \
88                        "pushl %edx\n\t" \
89                        "pushl %ecx\n\t" \
90                        "pushl %eax\n\t" \
91                        "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(12))
92 #endif
93
94 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
95 {
96     if (!us) return "<null>";
97     return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
98 }
99
100 static HANDLE get_device_manager(void)
101 {
102     static HANDLE device_manager;
103     HANDLE handle = 0, ret = device_manager;
104
105     if (!ret)
106     {
107         SERVER_START_REQ( create_device_manager )
108         {
109             req->access     = SYNCHRONIZE;
110             req->attributes = 0;
111             if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
112         }
113         SERVER_END_REQ;
114
115         if (!handle)
116         {
117             ERR( "failed to create the device manager\n" );
118             return 0;
119         }
120         if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
121             ret = handle;
122         else
123             NtClose( handle );  /* somebody beat us to it */
124     }
125     return ret;
126 }
127
128 /* process an ioctl request for a given device */
129 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
130                                void *out_buff, ULONG *out_size )
131 {
132     IRP irp;
133     MDL mdl;
134     IO_STACK_LOCATION irpsp;
135     PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
136     NTSTATUS status;
137     LARGE_INTEGER count;
138
139     TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
140
141     /* so we can spot things that we should initialize */
142     memset( &irp, 0x55, sizeof(irp) );
143     memset( &irpsp, 0x66, sizeof(irpsp) );
144     memset( &mdl, 0x77, sizeof(mdl) );
145
146     irp.RequestorMode = UserMode;
147     irp.AssociatedIrp.SystemBuffer = in_buff;
148     irp.UserBuffer = out_buff;
149     irp.MdlAddress = &mdl;
150     irp.Tail.Overlay.s.u2.CurrentStackLocation = &irpsp;
151     irp.UserIosb = NULL;
152
153     irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
154     irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
155     irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
156     irpsp.Parameters.DeviceIoControl.IoControlCode = code;
157     irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
158     irpsp.DeviceObject = device;
159     irpsp.CompletionRoutine = NULL;
160
161     mdl.Next = NULL;
162     mdl.Size = 0;
163     mdl.StartVa = out_buff;
164     mdl.ByteCount = *out_size;
165     mdl.ByteOffset = 0;
166
167     device->CurrentIrp = &irp;
168
169     KeQueryTickCount( &count );  /* update the global KeTickCount */
170
171     if (TRACE_ON(relay))
172         DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
173                  GetCurrentThreadId(), dispatch, device, &irp );
174
175     status = dispatch( device, &irp );
176
177     if (TRACE_ON(relay))
178         DPRINTF( "%04x:Ret  driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
179                  GetCurrentThreadId(), dispatch, device, &irp, status );
180
181     *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
182     return irp.IoStatus.u.Status;
183 }
184
185
186 /***********************************************************************
187  *           wine_ntoskrnl_main_loop   (Not a Windows API)
188  */
189 NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event )
190 {
191     HANDLE manager = get_device_manager();
192     obj_handle_t ioctl = 0;
193     NTSTATUS status = STATUS_SUCCESS;
194     ULONG code = 0;
195     void *in_buff, *out_buff = NULL;
196     DEVICE_OBJECT *device = NULL;
197     ULONG in_size = 4096, out_size = 0;
198     HANDLE handles[2];
199
200     if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
201     {
202         ERR( "failed to allocate buffer\n" );
203         return STATUS_NO_MEMORY;
204     }
205
206     handles[0] = stop_event;
207     handles[1] = manager;
208
209     for (;;)
210     {
211         SERVER_START_REQ( get_next_device_request )
212         {
213             req->manager = wine_server_obj_handle( manager );
214             req->prev = ioctl;
215             req->status = status;
216             wine_server_add_data( req, out_buff, out_size );
217             wine_server_set_reply( req, in_buff, in_size );
218             if (!(status = wine_server_call( req )))
219             {
220                 code     = reply->code;
221                 ioctl    = reply->next;
222                 device   = wine_server_get_ptr( reply->user_ptr );
223                 in_size  = reply->in_size;
224                 out_size = reply->out_size;
225             }
226             else
227             {
228                 ioctl = 0; /* no previous ioctl */
229                 out_size = 0;
230                 in_size = reply->in_size;
231             }
232         }
233         SERVER_END_REQ;
234
235         switch(status)
236         {
237         case STATUS_SUCCESS:
238             HeapFree( GetProcessHeap(), 0, out_buff );
239             if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
240             else out_buff = NULL;
241             status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
242             break;
243         case STATUS_BUFFER_OVERFLOW:
244             HeapFree( GetProcessHeap(), 0, in_buff );
245             in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
246             /* restart with larger buffer */
247             break;
248         case STATUS_PENDING:
249             if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
250             {
251                 HeapFree( GetProcessHeap(), 0, in_buff );
252                 HeapFree( GetProcessHeap(), 0, out_buff );
253                 return STATUS_SUCCESS;
254             }
255             break;
256         }
257     }
258 }
259
260
261 /***********************************************************************
262  *           IoAllocateDriverObjectExtension  (NTOSKRNL.EXE.@)
263  */
264 NTSTATUS WINAPI IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject,
265                                                  PVOID ClientIdentificationAddress,
266                                                  ULONG DriverObjectExtensionSize,
267                                                  PVOID *DriverObjectExtension )
268 {
269     FIXME( "stub: %p, %p, %u, %p\n", DriverObject, ClientIdentificationAddress,
270             DriverObjectExtensionSize, DriverObjectExtension );
271     return STATUS_NOT_IMPLEMENTED;
272 }
273
274
275 /***********************************************************************
276  *           IoGetDriverObjectExtension  (NTOSKRNL.EXE.@)
277  */
278 PVOID WINAPI IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject,
279                                          PVOID ClientIdentificationAddress )
280 {
281     FIXME( "stub: %p, %p\n", DriverObject, ClientIdentificationAddress );
282     return NULL;
283 }
284
285
286 /***********************************************************************
287  *           IoInitializeIrp  (NTOSKRNL.EXE.@)
288  */
289 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
290 {
291     TRACE( "%p, %u, %d\n", irp, size, stack_size );
292
293     RtlZeroMemory( irp, size );
294
295     irp->Type = IO_TYPE_IRP;
296     irp->Size = size;
297     InitializeListHead( &irp->ThreadListEntry );
298     irp->StackCount = stack_size;
299     irp->CurrentLocation = stack_size + 1;
300     irp->Tail.Overlay.s.u2.CurrentStackLocation =
301             (PIO_STACK_LOCATION)(irp + 1) + stack_size;
302 }
303
304
305 /***********************************************************************
306  *           IoInitializeTimer   (NTOSKRNL.EXE.@)
307  */
308 NTSTATUS WINAPI IoInitializeTimer(PDEVICE_OBJECT DeviceObject,
309                                   PIO_TIMER_ROUTINE TimerRoutine,
310                                   PVOID Context)
311 {
312     FIXME( "stub: %p, %p, %p\n", DeviceObject, TimerRoutine, Context );
313     return STATUS_NOT_IMPLEMENTED;
314 }
315
316
317 /***********************************************************************
318  *           IoStartTimer   (NTOSKRNL.EXE.@)
319  */
320 void WINAPI IoStartTimer(PDEVICE_OBJECT DeviceObject)
321 {
322     FIXME( "stub: %p\n", DeviceObject );
323 }
324
325
326 /***********************************************************************
327  *           IoAllocateIrp  (NTOSKRNL.EXE.@)
328  */
329 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
330 {
331     SIZE_T size;
332     PIRP irp;
333
334     TRACE( "%d, %d\n", stack_size, charge_quota );
335
336     size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
337     irp = ExAllocatePool( NonPagedPool, size );
338     if (irp == NULL)
339         return NULL;
340     IoInitializeIrp( irp, size, stack_size );
341     irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
342     if (charge_quota)
343         irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
344     return irp;
345 }
346
347
348 /***********************************************************************
349  *           IoFreeIrp  (NTOSKRNL.EXE.@)
350  */
351 void WINAPI IoFreeIrp( IRP *irp )
352 {
353     TRACE( "%p\n", irp );
354
355     ExFreePool( irp );
356 }
357
358
359 /***********************************************************************
360  *           IoAllocateErrorLogEntry  (NTOSKRNL.EXE.@)
361  */
362 PVOID WINAPI IoAllocateErrorLogEntry( PVOID IoObject, UCHAR EntrySize )
363 {
364     FIXME( "stub: %p, %u\n", IoObject, EntrySize );
365     return NULL;
366 }
367
368
369 /***********************************************************************
370  *           IoAllocateMdl  (NTOSKRNL.EXE.@)
371  */
372 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
373 {
374     FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
375     return NULL;
376 }
377
378
379 /***********************************************************************
380  *           IoAllocateWorkItem  (NTOSKRNL.EXE.@)
381  */
382 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
383 {
384     FIXME( "stub: %p\n", DeviceObject );
385     return NULL;
386 }
387
388
389 /***********************************************************************
390  *           IoAttachDeviceToDeviceStack  (NTOSKRNL.EXE.@)
391  */
392 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
393                                                    DEVICE_OBJECT *target )
394 {
395     TRACE( "%p, %p\n", source, target );
396     target->AttachedDevice = source;
397     source->StackSize = target->StackSize + 1;
398     return target;
399 }
400
401
402 /***********************************************************************
403  *           IoBuildDeviceIoControlRequest  (NTOSKRNL.EXE.@)
404  */
405 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
406                                            PDEVICE_OBJECT DeviceObject,
407                                            PVOID InputBuffer,
408                                            ULONG InputBufferLength,
409                                            PVOID OutputBuffer,
410                                            ULONG OutputBufferLength,
411                                            BOOLEAN InternalDeviceIoControl,
412                                            PKEVENT Event,
413                                            PIO_STATUS_BLOCK IoStatusBlock )
414 {
415     PIRP irp;
416     PIO_STACK_LOCATION irpsp;
417     struct IrpInstance *instance;
418
419     TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
420            IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
421            OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
422            Event, IoStatusBlock );
423
424     if (DeviceObject == NULL)
425         return NULL;
426
427     irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
428     if (irp == NULL)
429         return NULL;
430
431     instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
432     if (instance == NULL)
433     {
434         IoFreeIrp( irp );
435         return NULL;
436     }
437     instance->irp = irp;
438     list_add_tail( &Irps, &instance->entry );
439
440     irpsp = IoGetNextIrpStackLocation( irp );
441     irpsp->MajorFunction = InternalDeviceIoControl ?
442             IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
443     irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
444     irp->UserIosb = IoStatusBlock;
445     irp->UserEvent = Event;
446
447     return irp;
448 }
449
450
451 /***********************************************************************
452  *           IoCreateDriver   (NTOSKRNL.EXE.@)
453  */
454 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
455 {
456     DRIVER_OBJECT *driver;
457     DRIVER_EXTENSION *extension;
458     NTSTATUS status;
459
460     if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
461                                     sizeof(*driver) + sizeof(*extension) )))
462         return STATUS_NO_MEMORY;
463
464     if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
465     {
466         RtlFreeHeap( GetProcessHeap(), 0, driver );
467         return status;
468     }
469
470     extension = (DRIVER_EXTENSION *)(driver + 1);
471     driver->Size            = sizeof(*driver);
472     driver->DriverInit      = init;
473     driver->DriverExtension = extension;
474     extension->DriverObject   = driver;
475     extension->ServiceKeyName = driver->DriverName;
476
477     status = driver->DriverInit( driver, name );
478
479     if (status)
480     {
481         RtlFreeUnicodeString( &driver->DriverName );
482         RtlFreeHeap( GetProcessHeap(), 0, driver );
483     }
484     return status;
485 }
486
487
488 /***********************************************************************
489  *           IoDeleteDriver   (NTOSKRNL.EXE.@)
490  */
491 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
492 {
493     RtlFreeUnicodeString( &driver->DriverName );
494     RtlFreeHeap( GetProcessHeap(), 0, driver );
495 }
496
497
498 /***********************************************************************
499  *           IoCreateDevice   (NTOSKRNL.EXE.@)
500  */
501 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
502                                 UNICODE_STRING *name, DEVICE_TYPE type,
503                                 ULONG characteristics, BOOLEAN exclusive,
504                                 DEVICE_OBJECT **ret_device )
505 {
506     NTSTATUS status;
507     DEVICE_OBJECT *device;
508     HANDLE handle = 0;
509     HANDLE manager = get_device_manager();
510
511     TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
512            driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
513
514     if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
515         return STATUS_NO_MEMORY;
516
517     SERVER_START_REQ( create_device )
518     {
519         req->access     = 0;
520         req->attributes = 0;
521         req->rootdir    = 0;
522         req->manager    = wine_server_obj_handle( manager );
523         req->user_ptr   = wine_server_client_ptr( device );
524         if (name) wine_server_add_data( req, name->Buffer, name->Length );
525         if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
526     }
527     SERVER_END_REQ;
528
529     if (status == STATUS_SUCCESS)
530     {
531         device->DriverObject    = driver;
532         device->DeviceExtension = device + 1;
533         device->DeviceType      = type;
534         device->StackSize       = 1;
535         device->Reserved        = handle;
536
537         device->NextDevice   = driver->DeviceObject;
538         driver->DeviceObject = device;
539
540         *ret_device = device;
541     }
542     else HeapFree( GetProcessHeap(), 0, device );
543
544     return status;
545 }
546
547
548 /***********************************************************************
549  *           IoDeleteDevice   (NTOSKRNL.EXE.@)
550  */
551 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
552 {
553     NTSTATUS status;
554
555     TRACE( "%p\n", device );
556
557     SERVER_START_REQ( delete_device )
558     {
559         req->handle = wine_server_obj_handle( device->Reserved );
560         status = wine_server_call( req );
561     }
562     SERVER_END_REQ;
563
564     if (status == STATUS_SUCCESS)
565     {
566         DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
567         while (*prev && *prev != device) prev = &(*prev)->NextDevice;
568         if (*prev) *prev = (*prev)->NextDevice;
569         NtClose( device->Reserved );
570         HeapFree( GetProcessHeap(), 0, device );
571     }
572 }
573
574
575 /***********************************************************************
576  *           IoCreateSymbolicLink   (NTOSKRNL.EXE.@)
577  */
578 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
579 {
580     HANDLE handle;
581     OBJECT_ATTRIBUTES attr;
582
583     attr.Length                   = sizeof(attr);
584     attr.RootDirectory            = 0;
585     attr.ObjectName               = name;
586     attr.Attributes               = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
587     attr.SecurityDescriptor       = NULL;
588     attr.SecurityQualityOfService = NULL;
589
590     TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
591     /* FIXME: store handle somewhere */
592     return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
593 }
594
595
596 /***********************************************************************
597  *           IoDeleteSymbolicLink   (NTOSKRNL.EXE.@)
598  */
599 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
600 {
601     HANDLE handle;
602     OBJECT_ATTRIBUTES attr;
603     NTSTATUS status;
604
605     attr.Length                   = sizeof(attr);
606     attr.RootDirectory            = 0;
607     attr.ObjectName               = name;
608     attr.Attributes               = OBJ_CASE_INSENSITIVE;
609     attr.SecurityDescriptor       = NULL;
610     attr.SecurityQualityOfService = NULL;
611
612     if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
613     {
614         SERVER_START_REQ( unlink_object )
615         {
616             req->handle = wine_server_obj_handle( handle );
617             status = wine_server_call( req );
618         }
619         SERVER_END_REQ;
620         NtClose( handle );
621     }
622     return status;
623 }
624
625
626 /***********************************************************************
627  *           IoGetDeviceInterfaces   (NTOSKRNL.EXE.@)
628  */
629 NTSTATUS WINAPI IoGetDeviceInterfaces( CONST GUID *InterfaceClassGuid,
630                                        PDEVICE_OBJECT PhysicalDeviceObject,
631                                        ULONG Flags, PWSTR *SymbolicLinkList )
632 {
633     FIXME( "stub: %s %p %x %p\n", debugstr_guid(InterfaceClassGuid),
634            PhysicalDeviceObject, Flags, SymbolicLinkList );
635     return STATUS_NOT_IMPLEMENTED;
636 }
637
638
639 /***********************************************************************
640  *           IoGetDeviceObjectPointer   (NTOSKRNL.EXE.@)
641  */
642 NTSTATUS  WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
643 {
644     FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
645     return STATUS_NOT_IMPLEMENTED;
646 }
647
648
649 /***********************************************************************
650  *           IoGetDeviceProperty   (NTOSKRNL.EXE.@)
651  */
652 NTSTATUS WINAPI IoGetDeviceProperty( DEVICE_OBJECT *device, DEVICE_REGISTRY_PROPERTY device_property,
653                                      ULONG buffer_length, PVOID property_buffer, PULONG result_length )
654 {
655     FIXME( "%p %d %u %p %p: stub\n", device, device_property, buffer_length,
656            property_buffer, result_length );
657     return STATUS_NOT_IMPLEMENTED;
658 }
659
660
661 /***********************************************************************
662  *           IoCallDriver   (NTOSKRNL.EXE.@)
663  */
664 NTSTATUS WINAPI IoCallDriver( DEVICE_OBJECT *device, IRP *irp )
665 {
666     PDRIVER_DISPATCH dispatch;
667     IO_STACK_LOCATION *irpsp;
668     NTSTATUS status;
669
670     TRACE( "%p %p\n", device, irp );
671
672     --irp->CurrentLocation;
673     irpsp = --irp->Tail.Overlay.s.u2.CurrentStackLocation;
674     dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
675     status = dispatch( device, irp );
676
677     return status;
678 }
679
680
681 /***********************************************************************
682  *           IofCallDriver   (NTOSKRNL.EXE.@)
683  */
684 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
685 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
686 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
687 #else
688 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
689 #endif
690 {
691     TRACE( "%p %p\n", device, irp );
692     return IoCallDriver( device, irp );
693 }
694
695
696 /***********************************************************************
697  *           IoGetRelatedDeviceObject    (NTOSKRNL.EXE.@)
698  */
699 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
700 {
701     FIXME( "stub: %p\n", obj );
702     return NULL;
703 }
704
705 static CONFIGURATION_INFORMATION configuration_information;
706
707 /***********************************************************************
708  *           IoGetConfigurationInformation    (NTOSKRNL.EXE.@)
709  */
710 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
711 {
712     FIXME( "partial stub\n" );
713     /* FIXME: return actual devices on system */
714     return &configuration_information;
715 }
716
717
718 /***********************************************************************
719  *           IoIsWdmVersionAvailable     (NTOSKRNL.EXE.@)
720  */
721 NTSTATUS WINAPI IoIsWdmVersionAvailable(UCHAR MajorVersion, UCHAR MinorVersion)
722 {
723     DWORD version;
724     DWORD major;
725     DWORD minor;
726
727     TRACE( "%d, 0x%X\n", MajorVersion, MinorVersion );
728
729     version = GetVersion();
730     major = LOBYTE(version);
731     minor = HIBYTE(LOWORD(version));
732
733     if (MajorVersion == 6 && MinorVersion == 0)
734     {
735         /* Windows Vista, Windows Server 2008, Windows 7 */
736     }
737     else if (MajorVersion == 1)
738     {
739         if (MinorVersion == 0x30)
740         {
741             /* Windows server 2003 */
742             MajorVersion = 6;
743             MinorVersion = 0;
744         }
745         else if (MinorVersion == 0x20)
746         {
747             /* Windows XP */
748             MajorVersion = 5;
749             MinorVersion = 1;
750         }
751         else if (MinorVersion == 0x10)
752         {
753             /* Windows 2000 */
754             MajorVersion = 5;
755             MinorVersion = 0;
756         }
757         else if (MinorVersion == 0x05)
758         {
759             /* Windows ME */
760             MajorVersion = 4;
761             MinorVersion = 0x5a;
762         }
763         else if (MinorVersion == 0x00)
764         {
765             /* Windows 98 */
766             MajorVersion = 4;
767             MinorVersion = 0x0a;
768         }
769         else
770         {
771             FIXME( "unknown major %d minor 0x%X\n", MajorVersion, MinorVersion );
772             return FALSE;
773         }
774     }
775     else
776     {
777         FIXME( "unknown major %d minor 0x%X\n", MajorVersion, MinorVersion );
778         return FALSE;
779     }
780     return major > MajorVersion || (major == MajorVersion && minor >= MinorVersion);
781 }
782
783
784 /***********************************************************************
785  *           IoQueryDeviceDescription    (NTOSKRNL.EXE.@)
786  */
787 NTSTATUS WINAPI IoQueryDeviceDescription(PINTERFACE_TYPE itype, PULONG bus, PCONFIGURATION_TYPE ctype,
788                                      PULONG cnum, PCONFIGURATION_TYPE ptype, PULONG pnum,
789                                      PIO_QUERY_DEVICE_ROUTINE callout, PVOID context)
790 {
791     FIXME( "(%p %p %p %p %p %p %p %p)\n", itype, bus, ctype, cnum, ptype, pnum, callout, context);
792     return STATUS_NOT_IMPLEMENTED;
793 }
794
795
796 /***********************************************************************
797  *           IoRegisterDriverReinitialization    (NTOSKRNL.EXE.@)
798  */
799 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
800 {
801     FIXME( "stub: %p %p %p\n", obj, reinit, context );
802 }
803
804
805 /***********************************************************************
806  *           IoRegisterShutdownNotification    (NTOSKRNL.EXE.@)
807  */
808 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
809 {
810     FIXME( "stub: %p\n", obj );
811     return STATUS_SUCCESS;
812 }
813
814
815 /***********************************************************************
816  *           IoReportResourceUsage    (NTOSKRNL.EXE.@)
817  */
818 NTSTATUS WINAPI IoReportResourceUsage(PUNICODE_STRING name, PDRIVER_OBJECT drv_obj, PCM_RESOURCE_LIST drv_list,
819                                       ULONG drv_size, PDRIVER_OBJECT dev_obj, PCM_RESOURCE_LIST dev_list,
820                                       ULONG dev_size, BOOLEAN overwrite, PBOOLEAN detected)
821 {
822     FIXME("(%s %p %p %u %p %p %u %d %p) stub\n", debugstr_w(name? name->Buffer : NULL),
823           drv_obj, drv_list, drv_size, dev_obj, dev_list, dev_size, overwrite, detected);
824     return STATUS_NOT_IMPLEMENTED;
825 }
826
827
828 /***********************************************************************
829  *           IoCompleteRequest   (NTOSKRNL.EXE.@)
830  */
831 VOID WINAPI IoCompleteRequest( IRP *irp, UCHAR priority_boost )
832 {
833     IO_STACK_LOCATION *irpsp;
834     PIO_COMPLETION_ROUTINE routine;
835     IO_STATUS_BLOCK *iosb;
836     struct IrpInstance *instance;
837     NTSTATUS status, stat;
838     int call_flag = 0;
839
840     TRACE( "%p %u\n", irp, priority_boost );
841
842     iosb = irp->UserIosb;
843     status = irp->IoStatus.u.Status;
844     while (irp->CurrentLocation <= irp->StackCount)
845     {
846         irpsp = irp->Tail.Overlay.s.u2.CurrentStackLocation;
847         routine = irpsp->CompletionRoutine;
848         call_flag = 0;
849         /* FIXME: add SL_INVOKE_ON_CANCEL support */
850         if (routine)
851         {
852             if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
853                 call_flag = 1;
854             if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
855                 call_flag = 1;
856         }
857         ++irp->CurrentLocation;
858         ++irp->Tail.Overlay.s.u2.CurrentStackLocation;
859         if (call_flag)
860         {
861             TRACE( "calling %p( %p, %p, %p )\n", routine,
862                     irpsp->DeviceObject, irp, irpsp->Context );
863             stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
864             TRACE( "CompletionRoutine returned %x\n", stat );
865             if (STATUS_MORE_PROCESSING_REQUIRED == stat)
866                 return;
867         }
868     }
869     if (iosb && STATUS_SUCCESS == status)
870     {
871         iosb->u.Status = irp->IoStatus.u.Status;
872         iosb->Information = irp->IoStatus.Information;
873     }
874     LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
875     {
876         if (instance->irp == irp)
877         {
878             list_remove( &instance->entry );
879             HeapFree( GetProcessHeap(), 0, instance );
880             IoFreeIrp( irp );
881             break;
882         }
883     }
884 }
885
886
887 /***********************************************************************
888  *           IofCompleteRequest   (NTOSKRNL.EXE.@)
889  */
890 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
891 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
892 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
893 #else
894 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
895 #endif
896 {
897     TRACE( "%p %u\n", irp, priority_boost );
898     IoCompleteRequest( irp, priority_boost );
899 }
900
901
902 /***********************************************************************
903  *           InterlockedCompareExchange   (NTOSKRNL.EXE.@)
904  */
905 #ifdef DEFINE_FASTCALL3_ENTRYPOINT
906 DEFINE_FASTCALL3_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
907 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
908 #else
909 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
910 #endif
911 {
912     return InterlockedCompareExchange( dest, xchg, compare );
913 }
914
915
916 /***********************************************************************
917  *           InterlockedDecrement   (NTOSKRNL.EXE.@)
918  */
919 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
920 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
921 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
922 #else
923 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
924 #endif
925 {
926     return InterlockedDecrement( dest );
927 }
928
929
930 /***********************************************************************
931  *           InterlockedExchange   (NTOSKRNL.EXE.@)
932  */
933 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
934 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
935 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
936 #else
937 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
938 #endif
939 {
940     return InterlockedExchange( dest, val );
941 }
942
943
944 /***********************************************************************
945  *           InterlockedExchangeAdd   (NTOSKRNL.EXE.@)
946  */
947 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
948 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
949 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
950 #else
951 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
952 #endif
953 {
954     return InterlockedExchangeAdd( dest, incr );
955 }
956
957
958 /***********************************************************************
959  *           InterlockedIncrement   (NTOSKRNL.EXE.@)
960  */
961 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
962 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
963 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
964 #else
965 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
966 #endif
967 {
968     return InterlockedIncrement( dest );
969 }
970
971
972 /***********************************************************************
973  *           ExAllocatePool   (NTOSKRNL.EXE.@)
974  */
975 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
976 {
977     return ExAllocatePoolWithTag( type, size, 0 );
978 }
979
980
981 /***********************************************************************
982  *           ExAllocatePoolWithQuota   (NTOSKRNL.EXE.@)
983  */
984 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
985 {
986     return ExAllocatePoolWithTag( type, size, 0 );
987 }
988
989
990 /***********************************************************************
991  *           ExAllocatePoolWithTag   (NTOSKRNL.EXE.@)
992  */
993 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
994 {
995     /* FIXME: handle page alignment constraints */
996     void *ret = HeapAlloc( GetProcessHeap(), 0, size );
997     TRACE( "%lu pool %u -> %p\n", size, type, ret );
998     return ret;
999 }
1000
1001
1002 /***********************************************************************
1003  *           ExAllocatePoolWithQuotaTag   (NTOSKRNL.EXE.@)
1004  */
1005 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
1006 {
1007     return ExAllocatePoolWithTag( type, size, tag );
1008 }
1009
1010
1011 /***********************************************************************
1012  *           ExFreePool   (NTOSKRNL.EXE.@)
1013  */
1014 void WINAPI ExFreePool( void *ptr )
1015 {
1016     ExFreePoolWithTag( ptr, 0 );
1017 }
1018
1019
1020 /***********************************************************************
1021  *           ExFreePoolWithTag   (NTOSKRNL.EXE.@)
1022  */
1023 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
1024 {
1025     TRACE( "%p\n", ptr );
1026     HeapFree( GetProcessHeap(), 0, ptr );
1027 }
1028
1029
1030 /***********************************************************************
1031  *           ExInitializeResourceLite   (NTOSKRNL.EXE.@)
1032  */
1033 NTSTATUS WINAPI ExInitializeResourceLite(PERESOURCE Resource)
1034 {
1035     FIXME( "stub: %p\n", Resource );
1036     return STATUS_NOT_IMPLEMENTED;
1037 }
1038
1039
1040 /***********************************************************************
1041  *           ExInitializeNPagedLookasideList   (NTOSKRNL.EXE.@)
1042  */
1043 void WINAPI ExInitializeNPagedLookasideList(PNPAGED_LOOKASIDE_LIST Lookaside,
1044                                             PALLOCATE_FUNCTION Allocate,
1045                                             PFREE_FUNCTION Free,
1046                                             ULONG Flags,
1047                                             SIZE_T Size,
1048                                             ULONG Tag,
1049                                             USHORT Depth)
1050 {
1051     FIXME( "stub: %p, %p, %p, %u, %lu, %u, %u\n", Lookaside, Allocate, Free, Flags, Size, Tag, Depth );
1052 }
1053
1054
1055 /***********************************************************************
1056  *           ExInitializeZone   (NTOSKRNL.EXE.@)
1057  */
1058 NTSTATUS WINAPI ExInitializeZone(PZONE_HEADER Zone,
1059                                  ULONG BlockSize,
1060                                  PVOID InitialSegment,
1061                                  ULONG InitialSegmentSize)
1062 {
1063     FIXME( "stub: %p, %u, %p, %u\n", Zone, BlockSize, InitialSegment, InitialSegmentSize );
1064     return STATUS_NOT_IMPLEMENTED;
1065 }
1066
1067 /***********************************************************************
1068 *           FsRtlRegisterUncProvider   (NTOSKRNL.EXE.@)
1069 */
1070 NTSTATUS WINAPI FsRtlRegisterUncProvider(PHANDLE MupHandle, PUNICODE_STRING RedirDevName,
1071                                          BOOLEAN MailslotsSupported)
1072 {
1073     FIXME("(%p %p %d): stub\n", MupHandle, RedirDevName, MailslotsSupported);
1074     return STATUS_NOT_IMPLEMENTED;
1075 }
1076
1077 /***********************************************************************
1078  *           KeGetCurrentThread / PsGetCurrentThread   (NTOSKRNL.EXE.@)
1079  */
1080 PRKTHREAD WINAPI KeGetCurrentThread(void)
1081 {
1082     FIXME("() stub\n");
1083     return NULL;
1084 }
1085
1086 /***********************************************************************
1087  *           KeInitializeEvent   (NTOSKRNL.EXE.@)
1088  */
1089 void WINAPI KeInitializeEvent( PRKEVENT Event, EVENT_TYPE Type, BOOLEAN State )
1090 {
1091     FIXME( "stub: %p %d %d\n", Event, Type, State );
1092 }
1093
1094
1095  /***********************************************************************
1096  *           KeInitializeMutex   (NTOSKRNL.EXE.@)
1097  */
1098 void WINAPI KeInitializeMutex(PRKMUTEX Mutex, ULONG Level)
1099 {
1100     FIXME( "stub: %p, %u\n", Mutex, Level );
1101 }
1102
1103
1104  /***********************************************************************
1105  *           KeWaitForMutexObject   (NTOSKRNL.EXE.@)
1106  */
1107 NTSTATUS WINAPI KeWaitForMutexObject(PRKMUTEX Mutex, KWAIT_REASON WaitReason, KPROCESSOR_MODE WaitMode,
1108                                      BOOLEAN Alertable, PLARGE_INTEGER Timeout)
1109 {
1110     FIXME( "stub: %p, %d, %d, %d, %p\n", Mutex, WaitReason, WaitMode, Alertable, Timeout );
1111     return STATUS_NOT_IMPLEMENTED;
1112 }
1113
1114
1115  /***********************************************************************
1116  *           KeReleaseMutex   (NTOSKRNL.EXE.@)
1117  */
1118 LONG WINAPI KeReleaseMutex(PRKMUTEX Mutex, BOOLEAN Wait)
1119 {
1120     FIXME( "stub: %p, %d\n", Mutex, Wait );
1121     return STATUS_NOT_IMPLEMENTED;
1122 }
1123
1124
1125 /***********************************************************************
1126  *           KeInitializeSemaphore   (NTOSKRNL.EXE.@)
1127  */
1128 void WINAPI KeInitializeSemaphore( PRKSEMAPHORE Semaphore, LONG Count, LONG Limit )
1129 {
1130     FIXME( "(%p %d %d) stub\n", Semaphore , Count, Limit );
1131 }
1132
1133
1134 /***********************************************************************
1135  *           KeInitializeSpinLock   (NTOSKRNL.EXE.@)
1136  */
1137 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
1138 {
1139     FIXME( "stub: %p\n", SpinLock );
1140 }
1141
1142
1143 /***********************************************************************
1144  *           KeInitializeTimerEx   (NTOSKRNL.EXE.@)
1145  */
1146 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
1147 {
1148     FIXME( "stub: %p %d\n", Timer, Type );
1149 }
1150
1151
1152 /***********************************************************************
1153  *           KeInitializeTimer   (NTOSKRNL.EXE.@)
1154  */
1155 void WINAPI KeInitializeTimer( PKTIMER Timer )
1156 {
1157     KeInitializeTimerEx(Timer, NotificationTimer);
1158 }
1159
1160
1161 /**********************************************************************
1162  *           KeQueryActiveProcessors   (NTOSKRNL.EXE.@)
1163  *
1164  * Return the active Processors as bitmask
1165  *
1166  * RETURNS
1167  *   active Processors as bitmask
1168  *
1169  */
1170 KAFFINITY WINAPI KeQueryActiveProcessors( void )
1171 {
1172     DWORD_PTR AffinityMask;
1173
1174     GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
1175     return AffinityMask;
1176 }
1177
1178
1179 /**********************************************************************
1180  *           KeQueryInterruptTime   (NTOSKRNL.EXE.@)
1181  *
1182  * Return the interrupt time count
1183  *
1184  */
1185 ULONGLONG WINAPI KeQueryInterruptTime( void )
1186 {
1187     LARGE_INTEGER totaltime;
1188
1189     KeQueryTickCount(&totaltime);
1190     return totaltime.QuadPart;
1191 }
1192
1193
1194 /***********************************************************************
1195  *           KeQuerySystemTime   (NTOSKRNL.EXE.@)
1196  */
1197 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
1198 {
1199     NtQuerySystemTime( time );
1200 }
1201
1202
1203 /***********************************************************************
1204  *           KeQueryTickCount   (NTOSKRNL.EXE.@)
1205  */
1206 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
1207 {
1208     count->QuadPart = NtGetTickCount();
1209     /* update the global variable too */
1210     KeTickCount.LowPart   = count->u.LowPart;
1211     KeTickCount.High1Time = count->u.HighPart;
1212     KeTickCount.High2Time = count->u.HighPart;
1213 }
1214
1215
1216 /***********************************************************************
1217  *           KeReleaseSemaphore   (NTOSKRNL.EXE.@)
1218  */
1219 LONG WINAPI KeReleaseSemaphore( PRKSEMAPHORE Semaphore, KPRIORITY Increment,
1220                                 LONG Adjustment, BOOLEAN Wait )
1221 {
1222     FIXME("(%p %d %d %d) stub\n", Semaphore, Increment, Adjustment, Wait );
1223     return 0;
1224 }
1225
1226
1227 /***********************************************************************
1228  *           KeQueryTimeIncrement   (NTOSKRNL.EXE.@)
1229  */
1230 ULONG WINAPI KeQueryTimeIncrement(void)
1231 {
1232     return 10000;
1233 }
1234
1235
1236 /***********************************************************************
1237  *           KeResetEvent   (NTOSKRNL.EXE.@)
1238  */
1239 LONG WINAPI KeResetEvent( PRKEVENT Event )
1240 {
1241     FIXME("(%p): stub\n", Event);
1242     return 0;
1243 }
1244
1245
1246 /***********************************************************************
1247  *           KeSetEvent   (NTOSKRNL.EXE.@)
1248  */
1249 LONG WINAPI KeSetEvent( PRKEVENT Event, KPRIORITY Increment, BOOLEAN Wait )
1250 {
1251     FIXME("(%p, %d, %d): stub\n", Event, Increment, Wait);
1252     return 0;
1253 }
1254
1255
1256 /***********************************************************************
1257  *           KeSetPriorityThread   (NTOSKRNL.EXE.@)
1258  */
1259 KPRIORITY WINAPI KeSetPriorityThread( PKTHREAD Thread, KPRIORITY Priority )
1260 {
1261     FIXME("(%p %d)\n", Thread, Priority);
1262     return Priority;
1263 }
1264
1265
1266 /***********************************************************************
1267  *           KeWaitForSingleObject   (NTOSKRNL.EXE.@)
1268  */
1269 NTSTATUS WINAPI KeWaitForSingleObject(PVOID Object,
1270                                       KWAIT_REASON WaitReason,
1271                                       KPROCESSOR_MODE WaitMode,
1272                                       BOOLEAN Alertable,
1273                                       PLARGE_INTEGER Timeout)
1274 {
1275     FIXME( "stub: %p, %d, %d, %d, %p\n", Object, WaitReason, WaitMode, Alertable, Timeout );
1276     return STATUS_NOT_IMPLEMENTED;
1277 }
1278
1279 /***********************************************************************
1280  *           IoRegisterFileSystem   (NTOSKRNL.EXE.@)
1281  */
1282 VOID WINAPI IoRegisterFileSystem(PDEVICE_OBJECT DeviceObject)
1283 {
1284     FIXME("(%p): stub\n", DeviceObject);
1285 }
1286
1287 /***********************************************************************
1288 *           IoUnregisterFileSystem   (NTOSKRNL.EXE.@)
1289 */
1290 VOID WINAPI IoUnregisterFileSystem(PDEVICE_OBJECT DeviceObject)
1291 {
1292     FIXME("(%p): stub\n", DeviceObject);
1293 }
1294
1295 /***********************************************************************
1296  *           MmAllocateNonCachedMemory   (NTOSKRNL.EXE.@)
1297  */
1298 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
1299 {
1300     TRACE( "%lu\n", size );
1301     return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
1302 }
1303
1304 /***********************************************************************
1305  *           MmAllocateContiguousMemory   (NTOSKRNL.EXE.@)
1306  */
1307 PVOID WINAPI MmAllocateContiguousMemory( SIZE_T size, PHYSICAL_ADDRESS highest_valid_address )
1308 {
1309     FIXME( "%lu, %s stub\n", size, wine_dbgstr_longlong(highest_valid_address.QuadPart) );
1310     return NULL;
1311 }
1312
1313 /***********************************************************************
1314  *           MmAllocateContiguousMemorySpecifyCache   (NTOSKRNL.EXE.@)
1315  */
1316 PVOID WINAPI MmAllocateContiguousMemorySpecifyCache( SIZE_T size,
1317                                                      PHYSICAL_ADDRESS lowest_valid_address,
1318                                                      PHYSICAL_ADDRESS highest_valid_address,
1319                                                      PHYSICAL_ADDRESS BoundaryAddressMultiple,
1320                                                      MEMORY_CACHING_TYPE CacheType )
1321 {
1322     FIXME(": stub\n");
1323     return NULL;
1324 }
1325
1326 /***********************************************************************
1327  *           MmAllocatePagesForMdl   (NTOSKRNL.EXE.@)
1328  */
1329 PMDL WINAPI MmAllocatePagesForMdl(PHYSICAL_ADDRESS lowaddress, PHYSICAL_ADDRESS highaddress,
1330                                   PHYSICAL_ADDRESS skipbytes, SIZE_T size)
1331 {
1332     FIXME("%s %s %s %lu: stub\n", wine_dbgstr_longlong(lowaddress.QuadPart), wine_dbgstr_longlong(highaddress.QuadPart),
1333                                   wine_dbgstr_longlong(skipbytes.QuadPart), size);
1334     return NULL;
1335 }
1336
1337 /***********************************************************************
1338  *           MmFreeNonCachedMemory   (NTOSKRNL.EXE.@)
1339  */
1340 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
1341 {
1342     TRACE( "%p %lu\n", addr, size );
1343     VirtualFree( addr, 0, MEM_RELEASE );
1344 }
1345
1346 /***********************************************************************
1347  *           MmIsAddressValid   (NTOSKRNL.EXE.@)
1348  *
1349  * Check if the process can access the virtual address without a pagefault
1350  *
1351  * PARAMS
1352  *  VirtualAddress [I] Address to check
1353  *
1354  * RETURNS
1355  *  Failure: FALSE
1356  *  Success: TRUE  (Accessing the Address works without a Pagefault)
1357  *
1358  */
1359 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
1360 {
1361     TRACE("(%p)\n", VirtualAddress);
1362     return !IsBadWritePtr(VirtualAddress, 1);
1363 }
1364
1365 /***********************************************************************
1366  *           MmMapIoSpace   (NTOSKRNL.EXE.@)
1367  */
1368 PVOID WINAPI MmMapIoSpace( PHYSICAL_ADDRESS PhysicalAddress, DWORD NumberOfBytes, DWORD CacheType )
1369 {
1370     FIXME( "stub: 0x%08x%08x, %d, %d\n", PhysicalAddress.u.HighPart, PhysicalAddress.u.LowPart, NumberOfBytes, CacheType );
1371     return NULL;
1372 }
1373
1374 /***********************************************************************
1375  *           MmPageEntireDriver   (NTOSKRNL.EXE.@)
1376  */
1377 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1378 {
1379     TRACE("%p\n", AddrInSection);
1380     return AddrInSection;
1381 }
1382
1383 /***********************************************************************
1384  *           MmResetDriverPaging   (NTOSKRNL.EXE.@)
1385  */
1386 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1387 {
1388     TRACE("%p\n", AddrInSection);
1389 }
1390
1391 /***********************************************************************
1392  *           MmUnmapIoSpace   (NTOSKRNL.EXE.@)
1393  */
1394 VOID WINAPI MmUnmapIoSpace( PVOID BaseAddress, SIZE_T NumberOfBytes )
1395 {
1396     FIXME( "stub: %p, %lu\n", BaseAddress, NumberOfBytes );
1397 }
1398
1399 /***********************************************************************
1400  *           ObfReferenceObject   (NTOSKRNL.EXE.@)
1401  */
1402 VOID WINAPI ObfReferenceObject(PVOID Object)
1403 {
1404     FIXME("(%p): stub\n", Object);
1405 }
1406
1407  /***********************************************************************
1408  *           ObReferenceObjectByHandle    (NTOSKRNL.EXE.@)
1409  */
1410 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1411                                            POBJECT_TYPE type,
1412                                            KPROCESSOR_MODE mode, PVOID* ptr,
1413                                            POBJECT_HANDLE_INFORMATION info)
1414 {
1415     FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1416     return STATUS_NOT_IMPLEMENTED;
1417 }
1418
1419  /***********************************************************************
1420  *           ObReferenceObjectByName    (NTOSKRNL.EXE.@)
1421  */
1422 NTSTATUS WINAPI ObReferenceObjectByName( UNICODE_STRING *ObjectName,
1423                                          ULONG Attributes,
1424                                          ACCESS_STATE *AccessState,
1425                                          ACCESS_MASK DesiredAccess,
1426                                          POBJECT_TYPE ObjectType,
1427                                          KPROCESSOR_MODE AccessMode,
1428                                          void *ParseContext,
1429                                          void **Object)
1430 {
1431     FIXME("stub\n");
1432     return STATUS_NOT_IMPLEMENTED;
1433 }
1434
1435 /***********************************************************************
1436  *           ObfDereferenceObject   (NTOSKRNL.EXE.@)
1437  */
1438 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1439 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1440 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1441 #else
1442 void WINAPI ObfDereferenceObject( VOID *obj )
1443 #endif
1444 {
1445     FIXME( "stub: %p\n", obj );
1446 }
1447
1448
1449 /***********************************************************************
1450  *           PsCreateSystemThread   (NTOSKRNL.EXE.@)
1451  */
1452 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1453                                      POBJECT_ATTRIBUTES ObjectAttributes,
1454                                      HANDLE ProcessHandle, PCLIENT_ID ClientId,
1455                                      PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1456 {
1457     if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1458     return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1459                                0, StartRoutine, StartContext,
1460                                ThreadHandle, ClientId);
1461 }
1462
1463 /***********************************************************************
1464  *           PsGetCurrentProcessId   (NTOSKRNL.EXE.@)
1465  */
1466 HANDLE WINAPI PsGetCurrentProcessId(void)
1467 {
1468     return UlongToHandle(GetCurrentProcessId());  /* FIXME: not quite right... */
1469 }
1470
1471
1472 /***********************************************************************
1473  *           PsGetCurrentThreadId   (NTOSKRNL.EXE.@)
1474  */
1475 HANDLE WINAPI PsGetCurrentThreadId(void)
1476 {
1477     return UlongToHandle(GetCurrentThreadId());  /* FIXME: not quite right... */
1478 }
1479
1480
1481 /***********************************************************************
1482  *           PsGetVersion   (NTOSKRNL.EXE.@)
1483  */
1484 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1485 {
1486     RTL_OSVERSIONINFOEXW info;
1487
1488     info.dwOSVersionInfoSize = sizeof(info);
1489     RtlGetVersion( &info );
1490     if (major) *major = info.dwMajorVersion;
1491     if (minor) *minor = info.dwMinorVersion;
1492     if (build) *build = info.dwBuildNumber;
1493
1494     if (version)
1495     {
1496 #if 0  /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1497         size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1498         memcpy( version->Buffer, info.szCSDVersion, len );
1499         if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1500         version->Length = len;
1501 #endif
1502     }
1503     return TRUE;
1504 }
1505
1506
1507 /***********************************************************************
1508  *           PsSetCreateProcessNotifyRoutine   (NTOSKRNL.EXE.@)
1509  */
1510 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1511 {
1512     FIXME( "stub: %p %d\n", callback, remove );
1513     return STATUS_SUCCESS;
1514 }
1515
1516
1517 /***********************************************************************
1518  *           PsSetCreateThreadNotifyRoutine   (NTOSKRNL.EXE.@)
1519  */
1520 NTSTATUS WINAPI PsSetCreateThreadNotifyRoutine( PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine )
1521 {
1522     FIXME( "stub: %p\n", NotifyRoutine );
1523     return STATUS_SUCCESS;
1524 }
1525
1526
1527 /***********************************************************************
1528  *           PsTerminateSystemThread   (NTOSKRNL.EXE.@)
1529  */
1530 NTSTATUS WINAPI PsTerminateSystemThread(NTSTATUS ExitStatus)
1531 {
1532     FIXME( "stub: %u\n", ExitStatus );
1533     return STATUS_NOT_IMPLEMENTED;
1534 }
1535
1536
1537 /***********************************************************************
1538  *           MmGetSystemRoutineAddress   (NTOSKRNL.EXE.@)
1539  */
1540 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1541 {
1542     HMODULE hMod;
1543     STRING routineNameA;
1544     PVOID pFunc = NULL;
1545
1546     static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1547     static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1548
1549     if (!SystemRoutineName) return NULL;
1550
1551     if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1552     {
1553         /* We only support functions exported from ntoskrnl.exe or hal.dll */
1554         hMod = GetModuleHandleW( ntoskrnlW );
1555         pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1556         if (!pFunc)
1557         {
1558            hMod = GetModuleHandleW( halW );
1559            if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1560         }
1561         RtlFreeAnsiString( &routineNameA );
1562     }
1563
1564     if (pFunc)
1565         TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1566     else
1567         FIXME( "%s not found\n", debugstr_us(SystemRoutineName) );
1568     return pFunc;
1569 }
1570
1571
1572 /***********************************************************************
1573  *           MmQuerySystemSize   (NTOSKRNL.EXE.@)
1574  */
1575 MM_SYSTEMSIZE WINAPI MmQuerySystemSize(void)
1576 {
1577     FIXME("stub\n");
1578     return MmLargeSystem;
1579 }
1580
1581 /***********************************************************************
1582  *           KeInitializeDpc   (NTOSKRNL.EXE.@)
1583  */
1584 VOID WINAPI KeInitializeDpc(PRKDPC Dpc, PKDEFERRED_ROUTINE DeferredRoutine, PVOID DeferredContext)
1585 {
1586     FIXME("stub\n");
1587 }
1588
1589 /***********************************************************************
1590  *           READ_REGISTER_BUFFER_UCHAR   (NTOSKRNL.EXE.@)
1591  */
1592 VOID WINAPI READ_REGISTER_BUFFER_UCHAR(PUCHAR Register, PUCHAR Buffer, ULONG Count)
1593 {
1594     FIXME("stub\n");
1595 }
1596
1597 /*****************************************************
1598  *           PoSetPowerState   (NTOSKRNL.EXE.@)
1599  */
1600 POWER_STATE WINAPI PoSetPowerState(PDEVICE_OBJECT DeviceObject, POWER_STATE_TYPE Type, POWER_STATE State)
1601 {
1602     FIXME("(%p %u %u) stub\n", DeviceObject, Type, State.DeviceState);
1603     return State;
1604 }
1605
1606 /*****************************************************
1607  *           IoWMIRegistrationControl   (NTOSKRNL.EXE.@)
1608  */
1609 NTSTATUS WINAPI IoWMIRegistrationControl(PDEVICE_OBJECT DeviceObject, ULONG Action)
1610 {
1611     FIXME("(%p %u ) stub\n", DeviceObject, Action);
1612     return STATUS_SUCCESS;
1613 }
1614
1615 /*****************************************************
1616  *           PsSetLoadImageNotifyRoutine   (NTOSKRNL.EXE.@)
1617  */
1618 NTSTATUS WINAPI PsSetLoadImageNotifyRoutine(PLOAD_IMAGE_NOTIFY_ROUTINE routine)
1619 {
1620     FIXME("(%p) stub\n", routine);
1621     return STATUS_SUCCESS;
1622 }
1623
1624 /*****************************************************
1625  *           PsLookupProcessByProcessId  (NTOSKRNL.EXE.@)
1626  */
1627 NTSTATUS WINAPI PsLookupProcessByProcessId(HANDLE processid, PEPROCESS *process)
1628 {
1629     FIXME("(%p %p) stub\n", processid, process);
1630     return STATUS_NOT_IMPLEMENTED;
1631 }
1632
1633
1634 /*****************************************************
1635  *           IoSetThreadHardErrorMode  (NTOSKRNL.EXE.@)
1636  */
1637 BOOLEAN WINAPI IoSetThreadHardErrorMode(BOOLEAN EnableHardErrors)
1638 {
1639     FIXME("stub\n");
1640     return FALSE;
1641 }
1642
1643 /*****************************************************
1644  *           DllMain
1645  */
1646 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1647 {
1648     static void *handler;
1649     LARGE_INTEGER count;
1650
1651     switch(reason)
1652     {
1653     case DLL_PROCESS_ATTACH:
1654         DisableThreadLibraryCalls( inst );
1655 #ifdef __i386__
1656         handler = RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1657 #endif
1658         KeQueryTickCount( &count );  /* initialize the global KeTickCount */
1659         break;
1660     case DLL_PROCESS_DETACH:
1661         RtlRemoveVectoredExceptionHandler( handler );
1662         break;
1663     }
1664     return TRUE;
1665 }