ntoskrnl.exe: Initialize StackSize field of DEVICE_OBJECT structure.
[wine] / dlls / ntoskrnl.exe / ntoskrnl.c
1 /*
2  * ntoskrnl.exe implementation
3  *
4  * Copyright (C) 2007 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33 #include "excpt.h"
34 #include "ddk/ntddk.h"
35 #include "wine/unicode.h"
36 #include "wine/server.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl);
40 WINE_DECLARE_DEBUG_CHANNEL(relay);
41
42
43 KSYSTEM_TIME KeTickCount = { 0, 0, 0 };
44
45 typedef struct _KSERVICE_TABLE_DESCRIPTOR
46 {
47     PULONG_PTR Base;
48     PULONG Count;
49     ULONG Limit;
50     PUCHAR Number;
51 } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
52
53 KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[4] = { { 0 } };
54
55 typedef void (WINAPI *PCREATE_PROCESS_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
56
57 #ifdef __i386__
58 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
59     __ASM_GLOBAL_FUNC( name, \
60                        "popl %eax\n\t" \
61                        "pushl %ecx\n\t" \
62                        "pushl %eax\n\t" \
63                        "jmp " __ASM_NAME("__regs_") #name )
64 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
65     __ASM_GLOBAL_FUNC( name, \
66                        "popl %eax\n\t" \
67                        "pushl %edx\n\t" \
68                        "pushl %ecx\n\t" \
69                        "pushl %eax\n\t" \
70                        "jmp " __ASM_NAME("__regs_") #name )
71 #endif
72
73 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
74 {
75     if (!us) return "<null>";
76     return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
77 }
78
79 static HANDLE get_device_manager(void)
80 {
81     static HANDLE device_manager;
82     HANDLE handle = 0, ret = device_manager;
83
84     if (!ret)
85     {
86         SERVER_START_REQ( create_device_manager )
87         {
88             req->access     = SYNCHRONIZE;
89             req->attributes = 0;
90             if (!wine_server_call( req )) handle = reply->handle;
91         }
92         SERVER_END_REQ;
93
94         if (!handle)
95         {
96             ERR( "failed to create the device manager\n" );
97             return 0;
98         }
99         if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
100             ret = handle;
101         else
102             NtClose( handle );  /* somebody beat us to it */
103     }
104     return ret;
105 }
106
107 /* exception handler for emulation of privileged instructions */
108 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
109 {
110     extern DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context );
111
112     EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
113     CONTEXT86 *context = ptrs->ContextRecord;
114
115     if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
116         record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
117     {
118         if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
119             return EXCEPTION_CONTINUE_EXECUTION;
120     }
121     return EXCEPTION_CONTINUE_SEARCH;
122 }
123
124 /* process an ioctl request for a given device */
125 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
126                                void *out_buff, ULONG *out_size )
127 {
128     IRP irp;
129     MDL mdl;
130     IO_STACK_LOCATION irpsp;
131     PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
132     NTSTATUS status;
133     LARGE_INTEGER count;
134
135     TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
136
137     /* so we can spot things that we should initialize */
138     memset( &irp, 0x55, sizeof(irp) );
139     memset( &irpsp, 0x66, sizeof(irpsp) );
140     memset( &mdl, 0x77, sizeof(mdl) );
141
142     irp.RequestorMode = UserMode;
143     irp.AssociatedIrp.SystemBuffer = in_buff;
144     irp.UserBuffer = out_buff;
145     irp.MdlAddress = &mdl;
146     irp.Tail.Overlay.s.u.CurrentStackLocation = &irpsp;
147
148     irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
149     irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
150     irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
151     irpsp.Parameters.DeviceIoControl.IoControlCode = code;
152     irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
153     irpsp.DeviceObject = device;
154
155     mdl.Next = NULL;
156     mdl.Size = 0;
157     mdl.StartVa = out_buff;
158     mdl.ByteCount = *out_size;
159     mdl.ByteOffset = 0;
160
161     device->CurrentIrp = &irp;
162
163     KeQueryTickCount( &count );  /* update the global KeTickCount */
164
165     if (TRACE_ON(relay))
166         DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
167                  GetCurrentThreadId(), dispatch, device, &irp );
168
169     status = dispatch( device, &irp );
170
171     if (TRACE_ON(relay))
172         DPRINTF( "%04x:Ret  driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
173                  GetCurrentThreadId(), dispatch, device, &irp, status );
174
175     *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
176     return irp.IoStatus.u.Status;
177 }
178
179
180 /***********************************************************************
181  *           wine_ntoskrnl_main_loop   (Not a Windows API)
182  */
183 NTSTATUS wine_ntoskrnl_main_loop( HANDLE stop_event )
184 {
185     HANDLE manager = get_device_manager();
186     HANDLE ioctl = 0;
187     NTSTATUS status = STATUS_SUCCESS;
188     ULONG code = 0;
189     void *in_buff, *out_buff = NULL;
190     DEVICE_OBJECT *device = NULL;
191     ULONG in_size = 4096, out_size = 0;
192     HANDLE handles[2];
193
194     if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
195     {
196         ERR( "failed to allocate buffer\n" );
197         return STATUS_NO_MEMORY;
198     }
199
200     handles[0] = stop_event;
201     handles[1] = manager;
202
203     for (;;)
204     {
205         SERVER_START_REQ( get_next_device_request )
206         {
207             req->manager = manager;
208             req->prev = ioctl;
209             req->status = status;
210             wine_server_add_data( req, out_buff, out_size );
211             wine_server_set_reply( req, in_buff, in_size );
212             if (!(status = wine_server_call( req )))
213             {
214                 code     = reply->code;
215                 ioctl    = reply->next;
216                 device   = reply->user_ptr;
217                 in_size  = reply->in_size;
218                 out_size = reply->out_size;
219             }
220             else
221             {
222                 ioctl = 0; /* no previous ioctl */
223                 out_size = 0;
224                 in_size = reply->in_size;
225             }
226         }
227         SERVER_END_REQ;
228
229         switch(status)
230         {
231         case STATUS_SUCCESS:
232             HeapFree( GetProcessHeap(), 0, out_buff );
233             if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
234             else out_buff = NULL;
235             status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
236             break;
237         case STATUS_BUFFER_OVERFLOW:
238             HeapFree( GetProcessHeap(), 0, in_buff );
239             in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
240             /* restart with larger buffer */
241             break;
242         case STATUS_PENDING:
243             if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
244                 return STATUS_SUCCESS;
245             break;
246         }
247     }
248 }
249
250
251 /***********************************************************************
252  *           IoInitializeIrp  (NTOSKRNL.EXE.@)
253  */
254 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
255 {
256     FIXME( "%p, %u, %d\n", irp, size, stack_size );
257 }
258
259
260 /***********************************************************************
261  *           IoAllocateIrp  (NTOSKRNL.EXE.@)
262  */
263 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
264 {
265     FIXME( "%d, %d\n", stack_size, charge_quota );
266     return NULL;
267 }
268
269
270 /***********************************************************************
271  *           IoFreeIrp  (NTOSKRNL.EXE.@)
272  */
273 void WINAPI IoFreeIrp( IRP *irp )
274 {
275     FIXME( "%p\n", irp );
276 }
277
278
279 /***********************************************************************
280  *           IoAllocateMdl  (NTOSKRNL.EXE.@)
281  */
282 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
283 {
284     FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
285     return NULL;
286 }
287
288
289 /***********************************************************************
290  *           IoAllocateWorkItem  (NTOSKRNL.EXE.@)
291  */
292 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
293 {
294     FIXME( "stub: %p\n", DeviceObject );
295     return NULL;
296 }
297
298
299 /***********************************************************************
300  *           IoCreateDriver   (NTOSKRNL.EXE.@)
301  */
302 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
303 {
304     DRIVER_OBJECT *driver;
305     DRIVER_EXTENSION *extension;
306     NTSTATUS status;
307
308     if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
309                                     sizeof(*driver) + sizeof(*extension) )))
310         return STATUS_NO_MEMORY;
311
312     if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
313     {
314         RtlFreeHeap( GetProcessHeap(), 0, driver );
315         return status;
316     }
317
318     extension = (DRIVER_EXTENSION *)(driver + 1);
319     driver->Size            = sizeof(*driver);
320     driver->DriverInit      = init;
321     driver->DriverExtension = extension;
322     extension->DriverObject   = driver;
323     extension->ServiceKeyName = driver->DriverName;
324
325     status = driver->DriverInit( driver, name );
326
327     if (status)
328     {
329         RtlFreeUnicodeString( &driver->DriverName );
330         RtlFreeHeap( GetProcessHeap(), 0, driver );
331     }
332     return status;
333 }
334
335
336 /***********************************************************************
337  *           IoDeleteDriver   (NTOSKRNL.EXE.@)
338  */
339 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
340 {
341     RtlFreeUnicodeString( &driver->DriverName );
342     RtlFreeHeap( GetProcessHeap(), 0, driver );
343 }
344
345
346 /***********************************************************************
347  *           IoCreateDevice   (NTOSKRNL.EXE.@)
348  */
349 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
350                                 UNICODE_STRING *name, DEVICE_TYPE type,
351                                 ULONG characteristics, BOOLEAN exclusive,
352                                 DEVICE_OBJECT **ret_device )
353 {
354     NTSTATUS status;
355     DEVICE_OBJECT *device;
356     HANDLE handle = 0;
357     HANDLE manager = get_device_manager();
358
359     TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
360            driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
361
362     if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
363         return STATUS_NO_MEMORY;
364
365     SERVER_START_REQ( create_device )
366     {
367         req->access     = 0;
368         req->attributes = 0;
369         req->rootdir    = 0;
370         req->manager    = manager;
371         req->user_ptr   = device;
372         if (name) wine_server_add_data( req, name->Buffer, name->Length );
373         if (!(status = wine_server_call( req ))) handle = reply->handle;
374     }
375     SERVER_END_REQ;
376
377     if (status == STATUS_SUCCESS)
378     {
379         device->DriverObject    = driver;
380         device->DeviceExtension = device + 1;
381         device->DeviceType      = type;
382         device->StackSize       = 1;
383         device->Reserved        = handle;
384
385         device->NextDevice   = driver->DeviceObject;
386         driver->DeviceObject = device;
387
388         *ret_device = device;
389     }
390     else HeapFree( GetProcessHeap(), 0, device );
391
392     return status;
393 }
394
395
396 /***********************************************************************
397  *           IoDeleteDevice   (NTOSKRNL.EXE.@)
398  */
399 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
400 {
401     NTSTATUS status;
402
403     TRACE( "%p\n", device );
404
405     SERVER_START_REQ( delete_device )
406     {
407         req->handle = device->Reserved;
408         status = wine_server_call( req );
409     }
410     SERVER_END_REQ;
411
412     if (status == STATUS_SUCCESS)
413     {
414         DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
415         while (*prev && *prev != device) prev = &(*prev)->NextDevice;
416         if (*prev) *prev = (*prev)->NextDevice;
417         NtClose( device->Reserved );
418         HeapFree( GetProcessHeap(), 0, device );
419     }
420 }
421
422
423 /***********************************************************************
424  *           IoCreateSymbolicLink   (NTOSKRNL.EXE.@)
425  */
426 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
427 {
428     HANDLE handle;
429     OBJECT_ATTRIBUTES attr;
430
431     attr.Length                   = sizeof(attr);
432     attr.RootDirectory            = 0;
433     attr.ObjectName               = name;
434     attr.Attributes               = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
435     attr.SecurityDescriptor       = NULL;
436     attr.SecurityQualityOfService = NULL;
437
438     TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
439     /* FIXME: store handle somewhere */
440     return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
441 }
442
443
444 /***********************************************************************
445  *           IoDeleteSymbolicLink   (NTOSKRNL.EXE.@)
446  */
447 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
448 {
449     FIXME( "%s\n", debugstr_us(name) );
450     return STATUS_SUCCESS;
451 }
452
453
454 /***********************************************************************
455  *           IoGetDeviceObjectPointer   (NTOSKRNL.EXE.@)
456  */
457 NTSTATUS  WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
458 {
459     FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
460     return STATUS_NOT_IMPLEMENTED;
461 }
462
463
464 /***********************************************************************
465  *           IoGetRelatedDeviceObject    (NTOSKRNL.EXE.@)
466  */
467 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
468 {
469     FIXME( "stub: %p\n", obj );
470     return NULL;
471 }
472
473 static CONFIGURATION_INFORMATION configuration_information;
474
475 /***********************************************************************
476  *           IoGetConfigurationInformation    (NTOSKRNL.EXE.@)
477  */
478 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
479 {
480     FIXME( "partial stub\n" );
481     /* FIXME: return actual devices on system */
482     return &configuration_information;
483 }
484
485
486 /***********************************************************************
487  *           IoRegisterDriverReinitialization    (NTOSKRNL.EXE.@)
488  */
489 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
490 {
491     FIXME( "stub: %p %p %p\n", obj, reinit, context );
492 }
493
494
495 /***********************************************************************
496  *           IoRegisterShutdownNotification    (NTOSKRNL.EXE.@)
497  */
498 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
499 {
500     FIXME( "stub: %p\n", obj );
501     return STATUS_SUCCESS;
502 }
503
504
505 /***********************************************************************
506  *           IofCompleteRequest   (NTOSKRNL.EXE.@)
507  */
508 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
509 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
510 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
511 #else
512 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
513 #endif
514 {
515     TRACE( "%p %u\n", irp, priority_boost );
516     /* nothing to do for now */
517 }
518
519
520 /***********************************************************************
521  *           InterlockedCompareExchange   (NTOSKRNL.EXE.@)
522  */
523 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
524 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
525 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
526 #else
527 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
528 #endif
529 {
530     return InterlockedCompareExchange( dest, xchg, compare );
531 }
532
533
534 /***********************************************************************
535  *           InterlockedDecrement   (NTOSKRNL.EXE.@)
536  */
537 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
538 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
539 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
540 #else
541 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
542 #endif
543 {
544     return InterlockedDecrement( dest );
545 }
546
547
548 /***********************************************************************
549  *           InterlockedExchange   (NTOSKRNL.EXE.@)
550  */
551 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
552 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
553 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
554 #else
555 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
556 #endif
557 {
558     return InterlockedExchange( dest, val );
559 }
560
561
562 /***********************************************************************
563  *           InterlockedExchangeAdd   (NTOSKRNL.EXE.@)
564  */
565 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
566 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
567 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
568 #else
569 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
570 #endif
571 {
572     return InterlockedExchangeAdd( dest, incr );
573 }
574
575
576 /***********************************************************************
577  *           InterlockedIncrement   (NTOSKRNL.EXE.@)
578  */
579 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
580 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
581 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
582 #else
583 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
584 #endif
585 {
586     return InterlockedIncrement( dest );
587 }
588
589
590 /***********************************************************************
591  *           ExAllocatePool   (NTOSKRNL.EXE.@)
592  */
593 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
594 {
595     return ExAllocatePoolWithTag( type, size, 0 );
596 }
597
598
599 /***********************************************************************
600  *           ExAllocatePoolWithQuota   (NTOSKRNL.EXE.@)
601  */
602 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
603 {
604     return ExAllocatePoolWithTag( type, size, 0 );
605 }
606
607
608 /***********************************************************************
609  *           ExAllocatePoolWithTag   (NTOSKRNL.EXE.@)
610  */
611 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
612 {
613     /* FIXME: handle page alignment constraints */
614     void *ret = HeapAlloc( GetProcessHeap(), 0, size );
615     TRACE( "%lu pool %u -> %p\n", size, type, ret );
616     return ret;
617 }
618
619
620 /***********************************************************************
621  *           ExAllocatePoolWithQuotaTag   (NTOSKRNL.EXE.@)
622  */
623 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
624 {
625     return ExAllocatePoolWithTag( type, size, tag );
626 }
627
628
629 /***********************************************************************
630  *           ExFreePool   (NTOSKRNL.EXE.@)
631  */
632 void WINAPI ExFreePool( void *ptr )
633 {
634     ExFreePoolWithTag( ptr, 0 );
635 }
636
637
638 /***********************************************************************
639  *           ExFreePoolWithTag   (NTOSKRNL.EXE.@)
640  */
641 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
642 {
643     TRACE( "%p\n", ptr );
644     HeapFree( GetProcessHeap(), 0, ptr );
645 }
646
647
648 /***********************************************************************
649  *           KeInitializeSpinLock   (NTOSKRNL.EXE.@)
650  */
651 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
652 {
653     FIXME("%p\n", SpinLock);
654 }
655
656
657 /***********************************************************************
658  *           KeInitializeTimerEx   (NTOSKRNL.EXE.@)
659  */
660 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
661 {
662     FIXME("%p %d\n", Timer, Type);
663 }
664
665
666 /***********************************************************************
667  *           KeInitializeTimer   (NTOSKRNL.EXE.@)
668  */
669 void WINAPI KeInitializeTimer( PKTIMER Timer )
670 {
671     KeInitializeTimerEx(Timer, NotificationTimer);
672 }
673
674
675 /**********************************************************************
676  *           KeQueryActiveProcessors   (NTOSKRNL.EXE.@)
677  *
678  * Return the active Processors as bitmask
679  *
680  * RETURNS
681  *   active Processors as bitmask
682  *
683  */
684 KAFFINITY WINAPI KeQueryActiveProcessors( void )
685 {
686     DWORD_PTR AffinityMask;
687
688     GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
689     return AffinityMask;
690 }
691
692
693 /**********************************************************************
694  *           KeQueryInterruptTime   (NTOSKRNL.EXE.@)
695  *
696  * Return the interrupt time count
697  *
698  */
699 ULONGLONG WINAPI KeQueryInterruptTime( void )
700 {
701     LARGE_INTEGER totaltime;
702
703     KeQueryTickCount(&totaltime);
704     return totaltime.QuadPart;
705 }
706
707
708 /***********************************************************************
709  *           KeQuerySystemTime   (NTOSKRNL.EXE.@)
710  */
711 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
712 {
713     NtQuerySystemTime( time );
714 }
715
716
717 /***********************************************************************
718  *           KeQueryTickCount   (NTOSKRNL.EXE.@)
719  */
720 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
721 {
722     count->QuadPart = NtGetTickCount();
723     /* update the global variable too */
724     KeTickCount.LowPart   = count->u.LowPart;
725     KeTickCount.High1Time = count->u.HighPart;
726     KeTickCount.High2Time = count->u.HighPart;
727 }
728
729
730 /***********************************************************************
731  *           KeQueryTimeIncrement   (NTOSKRNL.EXE.@)
732  */
733 ULONG WINAPI KeQueryTimeIncrement(void)
734 {
735     return 10000;
736 }
737
738
739 /***********************************************************************
740  *           MmAllocateNonCachedMemory   (NTOSKRNL.EXE.@)
741  */
742 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
743 {
744     TRACE( "%lu\n", size );
745     return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
746 }
747
748
749 /***********************************************************************
750  *           MmFreeNonCachedMemory   (NTOSKRNL.EXE.@)
751  */
752 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
753 {
754     TRACE( "%p %lu\n", addr, size );
755     VirtualFree( addr, 0, MEM_RELEASE );
756 }
757
758 /***********************************************************************
759  *           MmIsAddressValid   (NTOSKRNL.EXE.@)
760  *
761  * Check if the process can access the virtual address without a pagefault
762  *
763  * PARAMS
764  *  VirtualAddress [I] Address to check
765  *
766  * RETURNS
767  *  Failure: FALSE
768  *  Success: TRUE  (Accessing the Address works without a Pagefault)
769  *
770  */
771 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
772 {
773     TRACE("(%p)\n", VirtualAddress);
774     return !IsBadWritePtr(VirtualAddress, 1);
775 }
776
777 /***********************************************************************
778  *           MmPageEntireDriver   (NTOSKRNL.EXE.@)
779  */
780 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
781 {
782     TRACE("%p\n", AddrInSection);
783     return AddrInSection;
784 }
785
786 /***********************************************************************
787  *           MmResetDriverPaging   (NTOSKRNL.EXE.@)
788  */
789 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
790 {
791     TRACE("%p\n", AddrInSection);
792 }
793
794
795  /***********************************************************************
796  *           ObReferenceObjectByHandle    (NTOSKRNL.EXE.@)
797  */
798 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
799                                            POBJECT_TYPE type,
800                                            KPROCESSOR_MODE mode, PVOID* ptr,
801                                            POBJECT_HANDLE_INFORMATION info)
802 {
803     FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
804     return STATUS_NOT_IMPLEMENTED;
805 }
806
807
808 /***********************************************************************
809  *           ObfDereferenceObject   (NTOSKRNL.EXE.@)
810  */
811 void WINAPI ObfDereferenceObject( VOID *obj )
812 {
813     FIXME( "stub: %p\n", obj );
814 }
815
816
817 /***********************************************************************
818  *           PsCreateSystemThread   (NTOSKRNL.EXE.@)
819  */
820 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
821                                      POBJECT_ATTRIBUTES ObjectAttributes,
822                                      HANDLE ProcessHandle, PCLIENT_ID ClientId,
823                                      PKSTART_ROUTINE StartRoutine, PVOID StartContext)
824 {
825     if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
826     return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
827                                0, StartRoutine, StartContext,
828                                ThreadHandle, ClientId);
829 }
830
831 /***********************************************************************
832  *           PsGetCurrentProcessId   (NTOSKRNL.EXE.@)
833  */
834 HANDLE WINAPI PsGetCurrentProcessId(void)
835 {
836     return (HANDLE)GetCurrentProcessId();  /* FIXME: not quite right... */
837 }
838
839
840 /***********************************************************************
841  *           PsGetCurrentThreadId   (NTOSKRNL.EXE.@)
842  */
843 HANDLE WINAPI PsGetCurrentThreadId(void)
844 {
845     return (HANDLE)GetCurrentThreadId();  /* FIXME: not quite right... */
846 }
847
848
849 /***********************************************************************
850  *           PsGetVersion   (NTOSKRNL.EXE.@)
851  */
852 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
853 {
854     RTL_OSVERSIONINFOEXW info;
855
856     RtlGetVersion( &info );
857     if (major) *major = info.dwMajorVersion;
858     if (minor) *minor = info.dwMinorVersion;
859     if (build) *build = info.dwBuildNumber;
860
861     if (version)
862     {
863 #if 0  /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
864         size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
865         memcpy( version->Buffer, info.szCSDVersion, len );
866         if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
867         version->Length = len;
868 #endif
869     }
870     return TRUE;
871 }
872
873
874 /***********************************************************************
875  *           PsSetCreateProcessNotifyRoutine   (NTOSKRNL.EXE.@)
876  */
877 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
878 {
879     FIXME( "stub: %p %d\n", callback, remove );
880     return STATUS_SUCCESS;
881 }
882
883
884 /*****************************************************
885  *           DllMain
886  */
887 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
888 {
889     LARGE_INTEGER count;
890
891     switch(reason)
892     {
893     case DLL_PROCESS_ATTACH:
894         DisableThreadLibraryCalls( inst );
895         RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
896         KeQueryTickCount( &count );  /* initialize the global KeTickCount */
897         break;
898     }
899     return TRUE;
900 }