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