kernel32: Make __wine_emulate_instruction CDECL and i386 only
[wine/wine64.git] / dlls / ntoskrnl.exe / ntoskrnl.c
blob1e33a2937d588f88be7bac7ce150db9f0e1143a7
1 /*
2 * ntoskrnl.exe implementation
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
33 #include "excpt.h"
34 #include "ddk/ntddk.h"
35 #include "wine/unicode.h"
36 #include "wine/server.h"
37 #include "wine/list.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ntoskrnl);
41 WINE_DECLARE_DEBUG_CHANNEL(relay);
44 KSYSTEM_TIME KeTickCount = { 0, 0, 0 };
46 typedef struct _KSERVICE_TABLE_DESCRIPTOR
48 PULONG_PTR Base;
49 PULONG Count;
50 ULONG Limit;
51 PUCHAR Number;
52 } KSERVICE_TABLE_DESCRIPTOR, *PKSERVICE_TABLE_DESCRIPTOR;
54 KSERVICE_TABLE_DESCRIPTOR KeServiceDescriptorTable[4] = { { 0 } };
56 typedef void (WINAPI *PCREATE_PROCESS_NOTIFY_ROUTINE)(HANDLE,HANDLE,BOOLEAN);
58 static struct list Irps = LIST_INIT(Irps);
60 struct IrpInstance
62 struct list entry;
63 IRP *irp;
66 #ifdef __i386__
67 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
68 __ASM_GLOBAL_FUNC( name, \
69 "popl %eax\n\t" \
70 "pushl %ecx\n\t" \
71 "pushl %eax\n\t" \
72 "jmp " __ASM_NAME("__regs_") #name )
73 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
74 __ASM_GLOBAL_FUNC( name, \
75 "popl %eax\n\t" \
76 "pushl %edx\n\t" \
77 "pushl %ecx\n\t" \
78 "pushl %eax\n\t" \
79 "jmp " __ASM_NAME("__regs_") #name )
80 #endif
82 static inline LPCSTR debugstr_us( const UNICODE_STRING *us )
84 if (!us) return "<null>";
85 return debugstr_wn( us->Buffer, us->Length / sizeof(WCHAR) );
88 static HANDLE get_device_manager(void)
90 static HANDLE device_manager;
91 HANDLE handle = 0, ret = device_manager;
93 if (!ret)
95 SERVER_START_REQ( create_device_manager )
97 req->access = SYNCHRONIZE;
98 req->attributes = 0;
99 if (!wine_server_call( req )) handle = wine_server_ptr_handle( reply->handle );
101 SERVER_END_REQ;
103 if (!handle)
105 ERR( "failed to create the device manager\n" );
106 return 0;
108 if (!(ret = InterlockedCompareExchangePointer( &device_manager, handle, 0 )))
109 ret = handle;
110 else
111 NtClose( handle ); /* somebody beat us to it */
113 return ret;
116 /* exception handler for emulation of privileged instructions */
117 static LONG CALLBACK vectored_handler( EXCEPTION_POINTERS *ptrs )
119 #ifdef __i386__
120 extern DWORD __wine_emulate_instruction( EXCEPTION_RECORD *rec, CONTEXT86 *context );
122 EXCEPTION_RECORD *record = ptrs->ExceptionRecord;
123 CONTEXT86 *context = ptrs->ContextRecord;
125 if (record->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ||
126 record->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
128 if (__wine_emulate_instruction( record, context ) == ExceptionContinueExecution)
129 return EXCEPTION_CONTINUE_EXECUTION;
131 #endif
132 return EXCEPTION_CONTINUE_SEARCH;
135 /* process an ioctl request for a given device */
136 static NTSTATUS process_ioctl( DEVICE_OBJECT *device, ULONG code, void *in_buff, ULONG in_size,
137 void *out_buff, ULONG *out_size )
139 IRP irp;
140 MDL mdl;
141 IO_STACK_LOCATION irpsp;
142 PDRIVER_DISPATCH dispatch = device->DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL];
143 NTSTATUS status;
144 LARGE_INTEGER count;
146 TRACE( "ioctl %x device %p in_size %u out_size %u\n", code, device, in_size, *out_size );
148 /* so we can spot things that we should initialize */
149 memset( &irp, 0x55, sizeof(irp) );
150 memset( &irpsp, 0x66, sizeof(irpsp) );
151 memset( &mdl, 0x77, sizeof(mdl) );
153 irp.RequestorMode = UserMode;
154 irp.AssociatedIrp.SystemBuffer = in_buff;
155 irp.UserBuffer = out_buff;
156 irp.MdlAddress = &mdl;
157 irp.Tail.Overlay.s.u.CurrentStackLocation = &irpsp;
158 irp.UserIosb = NULL;
160 irpsp.MajorFunction = IRP_MJ_DEVICE_CONTROL;
161 irpsp.Parameters.DeviceIoControl.OutputBufferLength = *out_size;
162 irpsp.Parameters.DeviceIoControl.InputBufferLength = in_size;
163 irpsp.Parameters.DeviceIoControl.IoControlCode = code;
164 irpsp.Parameters.DeviceIoControl.Type3InputBuffer = in_buff;
165 irpsp.DeviceObject = device;
166 irpsp.CompletionRoutine = NULL;
168 mdl.Next = NULL;
169 mdl.Size = 0;
170 mdl.StartVa = out_buff;
171 mdl.ByteCount = *out_size;
172 mdl.ByteOffset = 0;
174 device->CurrentIrp = &irp;
176 KeQueryTickCount( &count ); /* update the global KeTickCount */
178 if (TRACE_ON(relay))
179 DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
180 GetCurrentThreadId(), dispatch, device, &irp );
182 status = dispatch( device, &irp );
184 if (TRACE_ON(relay))
185 DPRINTF( "%04x:Ret driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
186 GetCurrentThreadId(), dispatch, device, &irp, status );
188 *out_size = (irp.IoStatus.u.Status >= 0) ? irp.IoStatus.Information : 0;
189 return irp.IoStatus.u.Status;
193 /***********************************************************************
194 * wine_ntoskrnl_main_loop (Not a Windows API)
196 NTSTATUS CDECL wine_ntoskrnl_main_loop( HANDLE stop_event )
198 HANDLE manager = get_device_manager();
199 obj_handle_t ioctl = 0;
200 NTSTATUS status = STATUS_SUCCESS;
201 ULONG code = 0;
202 void *in_buff, *out_buff = NULL;
203 DEVICE_OBJECT *device = NULL;
204 ULONG in_size = 4096, out_size = 0;
205 HANDLE handles[2];
207 if (!(in_buff = HeapAlloc( GetProcessHeap(), 0, in_size )))
209 ERR( "failed to allocate buffer\n" );
210 return STATUS_NO_MEMORY;
213 handles[0] = stop_event;
214 handles[1] = manager;
216 for (;;)
218 SERVER_START_REQ( get_next_device_request )
220 req->manager = wine_server_obj_handle( manager );
221 req->prev = ioctl;
222 req->status = status;
223 wine_server_add_data( req, out_buff, out_size );
224 wine_server_set_reply( req, in_buff, in_size );
225 if (!(status = wine_server_call( req )))
227 code = reply->code;
228 ioctl = reply->next;
229 device = reply->user_ptr;
230 in_size = reply->in_size;
231 out_size = reply->out_size;
233 else
235 ioctl = 0; /* no previous ioctl */
236 out_size = 0;
237 in_size = reply->in_size;
240 SERVER_END_REQ;
242 switch(status)
244 case STATUS_SUCCESS:
245 HeapFree( GetProcessHeap(), 0, out_buff );
246 if (out_size) out_buff = HeapAlloc( GetProcessHeap(), 0, out_size );
247 else out_buff = NULL;
248 status = process_ioctl( device, code, in_buff, in_size, out_buff, &out_size );
249 break;
250 case STATUS_BUFFER_OVERFLOW:
251 HeapFree( GetProcessHeap(), 0, in_buff );
252 in_buff = HeapAlloc( GetProcessHeap(), 0, in_size );
253 /* restart with larger buffer */
254 break;
255 case STATUS_PENDING:
256 if (WaitForMultipleObjects( 2, handles, FALSE, INFINITE ) == WAIT_OBJECT_0)
257 return STATUS_SUCCESS;
258 break;
264 /***********************************************************************
265 * IoAllocateDriverObjectExtension (NTOSKRNL.EXE.@)
267 NTSTATUS WINAPI IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject,
268 PVOID ClientIdentificationAddress,
269 ULONG DriverObjectExtensionSize,
270 PVOID *DriverObjectExtension )
272 FIXME( "%p, %p, %u, %p\n", DriverObject, ClientIdentificationAddress,
273 DriverObjectExtensionSize, DriverObjectExtension );
274 return STATUS_NOT_IMPLEMENTED;
278 /***********************************************************************
279 * IoGetDriverObjectExtension (NTOSKRNL.EXE.@)
281 PVOID WINAPI IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject,
282 PVOID ClientIdentificationAddress )
284 FIXME( "%p, %p\n", DriverObject, ClientIdentificationAddress );
285 return NULL;
289 /***********************************************************************
290 * IoInitializeIrp (NTOSKRNL.EXE.@)
292 void WINAPI IoInitializeIrp( IRP *irp, USHORT size, CCHAR stack_size )
294 TRACE( "%p, %u, %d\n", irp, size, stack_size );
296 RtlZeroMemory( irp, size );
298 irp->Type = IO_TYPE_IRP;
299 irp->Size = size;
300 InitializeListHead( &irp->ThreadListEntry );
301 irp->StackCount = stack_size;
302 irp->CurrentLocation = stack_size + 1;
303 irp->Tail.Overlay.s.u.CurrentStackLocation =
304 (PIO_STACK_LOCATION)(irp + 1) + stack_size;
308 /***********************************************************************
309 * IoAllocateIrp (NTOSKRNL.EXE.@)
311 PIRP WINAPI IoAllocateIrp( CCHAR stack_size, BOOLEAN charge_quota )
313 SIZE_T size;
314 PIRP irp;
316 TRACE( "%d, %d\n", stack_size, charge_quota );
318 size = sizeof(IRP) + stack_size * sizeof(IO_STACK_LOCATION);
319 irp = ExAllocatePool( NonPagedPool, size );
320 if (irp == NULL)
321 return NULL;
322 IoInitializeIrp( irp, size, stack_size );
323 irp->AllocationFlags = IRP_ALLOCATED_FIXED_SIZE;
324 if (charge_quota)
325 irp->AllocationFlags |= IRP_LOOKASIDE_ALLOCATION;
326 return irp;
330 /***********************************************************************
331 * IoFreeIrp (NTOSKRNL.EXE.@)
333 void WINAPI IoFreeIrp( IRP *irp )
335 TRACE( "%p\n", irp );
337 ExFreePool( irp );
341 /***********************************************************************
342 * IoAllocateMdl (NTOSKRNL.EXE.@)
344 PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN SecondaryBuffer, BOOLEAN ChargeQuota, PIRP Irp )
346 FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer, ChargeQuota, Irp );
347 return NULL;
351 /***********************************************************************
352 * IoAllocateWorkItem (NTOSKRNL.EXE.@)
354 PIO_WORKITEM WINAPI IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject )
356 FIXME( "stub: %p\n", DeviceObject );
357 return NULL;
361 /***********************************************************************
362 * IoAttachDeviceToDeviceStack (NTOSKRNL.EXE.@)
364 PDEVICE_OBJECT WINAPI IoAttachDeviceToDeviceStack( DEVICE_OBJECT *source,
365 DEVICE_OBJECT *target )
367 TRACE( "%p, %p\n", source, target );
368 target->AttachedDevice = source;
369 source->StackSize = target->StackSize + 1;
370 return target;
374 /***********************************************************************
375 * IoBuildDeviceIoControlRequest (NTOSKRNL.EXE.@)
377 PIRP WINAPI IoBuildDeviceIoControlRequest( ULONG IoControlCode,
378 PDEVICE_OBJECT DeviceObject,
379 PVOID InputBuffer,
380 ULONG InputBufferLength,
381 PVOID OutputBuffer,
382 ULONG OutputBufferLength,
383 BOOLEAN InternalDeviceIoControl,
384 PKEVENT Event,
385 PIO_STATUS_BLOCK IoStatusBlock )
387 PIRP irp;
388 PIO_STACK_LOCATION irpsp;
389 struct IrpInstance *instance;
391 TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
392 IoControlCode, DeviceObject, InputBuffer, InputBufferLength,
393 OutputBuffer, OutputBufferLength, InternalDeviceIoControl,
394 Event, IoStatusBlock );
396 if (DeviceObject == NULL)
397 return NULL;
399 irp = IoAllocateIrp( DeviceObject->StackSize, FALSE );
400 if (irp == NULL)
401 return NULL;
403 instance = HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance) );
404 if (instance == NULL)
406 IoFreeIrp( irp );
407 return NULL;
409 instance->irp = irp;
410 list_add_tail( &Irps, &instance->entry );
412 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation - 1;
413 irpsp->MajorFunction = InternalDeviceIoControl ?
414 IRP_MJ_INTERNAL_DEVICE_CONTROL : IRP_MJ_DEVICE_CONTROL;
415 irpsp->Parameters.DeviceIoControl.IoControlCode = IoControlCode;
416 irp->UserIosb = IoStatusBlock;
417 irp->UserEvent = Event;
419 return irp;
423 /***********************************************************************
424 * IoCreateDriver (NTOSKRNL.EXE.@)
426 NTSTATUS WINAPI IoCreateDriver( UNICODE_STRING *name, PDRIVER_INITIALIZE init )
428 DRIVER_OBJECT *driver;
429 DRIVER_EXTENSION *extension;
430 NTSTATUS status;
432 if (!(driver = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
433 sizeof(*driver) + sizeof(*extension) )))
434 return STATUS_NO_MEMORY;
436 if ((status = RtlDuplicateUnicodeString( 1, name, &driver->DriverName )))
438 RtlFreeHeap( GetProcessHeap(), 0, driver );
439 return status;
442 extension = (DRIVER_EXTENSION *)(driver + 1);
443 driver->Size = sizeof(*driver);
444 driver->DriverInit = init;
445 driver->DriverExtension = extension;
446 extension->DriverObject = driver;
447 extension->ServiceKeyName = driver->DriverName;
449 status = driver->DriverInit( driver, name );
451 if (status)
453 RtlFreeUnicodeString( &driver->DriverName );
454 RtlFreeHeap( GetProcessHeap(), 0, driver );
456 return status;
460 /***********************************************************************
461 * IoDeleteDriver (NTOSKRNL.EXE.@)
463 void WINAPI IoDeleteDriver( DRIVER_OBJECT *driver )
465 RtlFreeUnicodeString( &driver->DriverName );
466 RtlFreeHeap( GetProcessHeap(), 0, driver );
470 /***********************************************************************
471 * IoCreateDevice (NTOSKRNL.EXE.@)
473 NTSTATUS WINAPI IoCreateDevice( DRIVER_OBJECT *driver, ULONG ext_size,
474 UNICODE_STRING *name, DEVICE_TYPE type,
475 ULONG characteristics, BOOLEAN exclusive,
476 DEVICE_OBJECT **ret_device )
478 NTSTATUS status;
479 DEVICE_OBJECT *device;
480 HANDLE handle = 0;
481 HANDLE manager = get_device_manager();
483 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
484 driver, ext_size, debugstr_us(name), type, characteristics, exclusive, ret_device );
486 if (!(device = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*device) + ext_size )))
487 return STATUS_NO_MEMORY;
489 SERVER_START_REQ( create_device )
491 req->access = 0;
492 req->attributes = 0;
493 req->rootdir = 0;
494 req->manager = wine_server_obj_handle( manager );
495 req->user_ptr = device;
496 if (name) wine_server_add_data( req, name->Buffer, name->Length );
497 if (!(status = wine_server_call( req ))) handle = wine_server_ptr_handle( reply->handle );
499 SERVER_END_REQ;
501 if (status == STATUS_SUCCESS)
503 device->DriverObject = driver;
504 device->DeviceExtension = device + 1;
505 device->DeviceType = type;
506 device->StackSize = 1;
507 device->Reserved = handle;
509 device->NextDevice = driver->DeviceObject;
510 driver->DeviceObject = device;
512 *ret_device = device;
514 else HeapFree( GetProcessHeap(), 0, device );
516 return status;
520 /***********************************************************************
521 * IoDeleteDevice (NTOSKRNL.EXE.@)
523 void WINAPI IoDeleteDevice( DEVICE_OBJECT *device )
525 NTSTATUS status;
527 TRACE( "%p\n", device );
529 SERVER_START_REQ( delete_device )
531 req->handle = wine_server_obj_handle( device->Reserved );
532 status = wine_server_call( req );
534 SERVER_END_REQ;
536 if (status == STATUS_SUCCESS)
538 DEVICE_OBJECT **prev = &device->DriverObject->DeviceObject;
539 while (*prev && *prev != device) prev = &(*prev)->NextDevice;
540 if (*prev) *prev = (*prev)->NextDevice;
541 NtClose( device->Reserved );
542 HeapFree( GetProcessHeap(), 0, device );
547 /***********************************************************************
548 * IoCreateSymbolicLink (NTOSKRNL.EXE.@)
550 NTSTATUS WINAPI IoCreateSymbolicLink( UNICODE_STRING *name, UNICODE_STRING *target )
552 HANDLE handle;
553 OBJECT_ATTRIBUTES attr;
555 attr.Length = sizeof(attr);
556 attr.RootDirectory = 0;
557 attr.ObjectName = name;
558 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF;
559 attr.SecurityDescriptor = NULL;
560 attr.SecurityQualityOfService = NULL;
562 TRACE( "%s -> %s\n", debugstr_us(name), debugstr_us(target) );
563 /* FIXME: store handle somewhere */
564 return NtCreateSymbolicLinkObject( &handle, SYMBOLIC_LINK_ALL_ACCESS, &attr, target );
568 /***********************************************************************
569 * IoDeleteSymbolicLink (NTOSKRNL.EXE.@)
571 NTSTATUS WINAPI IoDeleteSymbolicLink( UNICODE_STRING *name )
573 HANDLE handle;
574 OBJECT_ATTRIBUTES attr;
575 NTSTATUS status;
577 attr.Length = sizeof(attr);
578 attr.RootDirectory = 0;
579 attr.ObjectName = name;
580 attr.Attributes = OBJ_CASE_INSENSITIVE;
581 attr.SecurityDescriptor = NULL;
582 attr.SecurityQualityOfService = NULL;
584 if (!(status = NtOpenSymbolicLinkObject( &handle, 0, &attr )))
586 SERVER_START_REQ( unlink_object )
588 req->handle = wine_server_obj_handle( handle );
589 status = wine_server_call( req );
591 SERVER_END_REQ;
592 NtClose( handle );
594 return status;
598 /***********************************************************************
599 * IoGetDeviceObjectPointer (NTOSKRNL.EXE.@)
601 NTSTATUS WINAPI IoGetDeviceObjectPointer( UNICODE_STRING *name, ACCESS_MASK access, PFILE_OBJECT *file, PDEVICE_OBJECT *device )
603 FIXME( "stub: %s %x %p %p\n", debugstr_us(name), access, file, device );
604 return STATUS_NOT_IMPLEMENTED;
608 /***********************************************************************
609 * IofCallDriver (NTOSKRNL.EXE.@)
611 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
612 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver )
613 NTSTATUS WINAPI __regs_IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
614 #else
615 NTSTATUS WINAPI IofCallDriver( DEVICE_OBJECT *device, IRP *irp )
616 #endif
618 PDRIVER_DISPATCH dispatch;
619 IO_STACK_LOCATION *irpsp;
620 NTSTATUS status;
622 TRACE( "%p %p\n", device, irp );
624 --irp->CurrentLocation;
625 irpsp = --irp->Tail.Overlay.s.u.CurrentStackLocation;
626 dispatch = device->DriverObject->MajorFunction[irpsp->MajorFunction];
627 status = dispatch( device, irp );
629 return status;
633 /***********************************************************************
634 * IoGetRelatedDeviceObject (NTOSKRNL.EXE.@)
636 PDEVICE_OBJECT WINAPI IoGetRelatedDeviceObject( PFILE_OBJECT obj )
638 FIXME( "stub: %p\n", obj );
639 return NULL;
642 static CONFIGURATION_INFORMATION configuration_information;
644 /***********************************************************************
645 * IoGetConfigurationInformation (NTOSKRNL.EXE.@)
647 PCONFIGURATION_INFORMATION WINAPI IoGetConfigurationInformation(void)
649 FIXME( "partial stub\n" );
650 /* FIXME: return actual devices on system */
651 return &configuration_information;
655 /***********************************************************************
656 * IoRegisterDriverReinitialization (NTOSKRNL.EXE.@)
658 void WINAPI IoRegisterDriverReinitialization( PDRIVER_OBJECT obj, PDRIVER_REINITIALIZE reinit, PVOID context )
660 FIXME( "stub: %p %p %p\n", obj, reinit, context );
664 /***********************************************************************
665 * IoRegisterShutdownNotification (NTOSKRNL.EXE.@)
667 NTSTATUS WINAPI IoRegisterShutdownNotification( PDEVICE_OBJECT obj )
669 FIXME( "stub: %p\n", obj );
670 return STATUS_SUCCESS;
674 /***********************************************************************
675 * IofCompleteRequest (NTOSKRNL.EXE.@)
677 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
678 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest )
679 void WINAPI __regs_IofCompleteRequest( IRP *irp, UCHAR priority_boost )
680 #else
681 void WINAPI IofCompleteRequest( IRP *irp, UCHAR priority_boost )
682 #endif
684 IO_STACK_LOCATION *irpsp;
685 PIO_COMPLETION_ROUTINE routine;
686 IO_STATUS_BLOCK *iosb;
687 struct IrpInstance *instance;
688 NTSTATUS status, stat;
689 int call_flag = 0;
691 TRACE( "%p %u\n", irp, priority_boost );
693 iosb = irp->UserIosb;
694 status = irp->IoStatus.u.Status;
695 while (irp->CurrentLocation <= irp->StackCount)
697 irpsp = irp->Tail.Overlay.s.u.CurrentStackLocation;
698 routine = irpsp->CompletionRoutine;
699 call_flag = 0;
700 /* FIXME: add SL_INVOKE_ON_CANCEL support */
701 if (routine)
703 if ((irpsp->Control & SL_INVOKE_ON_SUCCESS) && STATUS_SUCCESS == status)
704 call_flag = 1;
705 if ((irpsp->Control & SL_INVOKE_ON_ERROR) && STATUS_SUCCESS != status)
706 call_flag = 1;
708 ++irp->CurrentLocation;
709 ++irp->Tail.Overlay.s.u.CurrentStackLocation;
710 if (call_flag)
712 TRACE( "calling %p( %p, %p, %p )\n", routine,
713 irpsp->DeviceObject, irp, irpsp->Context );
714 stat = routine( irpsp->DeviceObject, irp, irpsp->Context );
715 TRACE( "CompletionRoutine returned %x\n", stat );
716 if (STATUS_MORE_PROCESSING_REQUIRED == stat)
717 return;
720 if (iosb && STATUS_SUCCESS == status)
722 iosb->u.Status = irp->IoStatus.u.Status;
723 iosb->Information = irp->IoStatus.Information;
725 LIST_FOR_EACH_ENTRY( instance, &Irps, struct IrpInstance, entry )
727 if (instance->irp == irp)
729 list_remove( &instance->entry );
730 HeapFree( GetProcessHeap(), 0, instance );
731 IoFreeIrp( irp );
732 break;
738 /***********************************************************************
739 * InterlockedCompareExchange (NTOSKRNL.EXE.@)
741 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
742 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange )
743 LONG WINAPI __regs_NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
744 #else
745 LONG WINAPI NTOSKRNL_InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
746 #endif
748 return InterlockedCompareExchange( dest, xchg, compare );
752 /***********************************************************************
753 * InterlockedDecrement (NTOSKRNL.EXE.@)
755 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
756 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement )
757 LONG WINAPI __regs_NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
758 #else
759 LONG WINAPI NTOSKRNL_InterlockedDecrement( LONG volatile *dest )
760 #endif
762 return InterlockedDecrement( dest );
766 /***********************************************************************
767 * InterlockedExchange (NTOSKRNL.EXE.@)
769 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
770 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange )
771 LONG WINAPI __regs_NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
772 #else
773 LONG WINAPI NTOSKRNL_InterlockedExchange( LONG volatile *dest, LONG val )
774 #endif
776 return InterlockedExchange( dest, val );
780 /***********************************************************************
781 * InterlockedExchangeAdd (NTOSKRNL.EXE.@)
783 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
784 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd )
785 LONG WINAPI __regs_NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
786 #else
787 LONG WINAPI NTOSKRNL_InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
788 #endif
790 return InterlockedExchangeAdd( dest, incr );
794 /***********************************************************************
795 * InterlockedIncrement (NTOSKRNL.EXE.@)
797 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
798 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement )
799 LONG WINAPI __regs_NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
800 #else
801 LONG WINAPI NTOSKRNL_InterlockedIncrement( LONG volatile *dest )
802 #endif
804 return InterlockedIncrement( dest );
808 /***********************************************************************
809 * ExAllocatePool (NTOSKRNL.EXE.@)
811 PVOID WINAPI ExAllocatePool( POOL_TYPE type, SIZE_T size )
813 return ExAllocatePoolWithTag( type, size, 0 );
817 /***********************************************************************
818 * ExAllocatePoolWithQuota (NTOSKRNL.EXE.@)
820 PVOID WINAPI ExAllocatePoolWithQuota( POOL_TYPE type, SIZE_T size )
822 return ExAllocatePoolWithTag( type, size, 0 );
826 /***********************************************************************
827 * ExAllocatePoolWithTag (NTOSKRNL.EXE.@)
829 PVOID WINAPI ExAllocatePoolWithTag( POOL_TYPE type, SIZE_T size, ULONG tag )
831 /* FIXME: handle page alignment constraints */
832 void *ret = HeapAlloc( GetProcessHeap(), 0, size );
833 TRACE( "%lu pool %u -> %p\n", size, type, ret );
834 return ret;
838 /***********************************************************************
839 * ExAllocatePoolWithQuotaTag (NTOSKRNL.EXE.@)
841 PVOID WINAPI ExAllocatePoolWithQuotaTag( POOL_TYPE type, SIZE_T size, ULONG tag )
843 return ExAllocatePoolWithTag( type, size, tag );
847 /***********************************************************************
848 * ExFreePool (NTOSKRNL.EXE.@)
850 void WINAPI ExFreePool( void *ptr )
852 ExFreePoolWithTag( ptr, 0 );
856 /***********************************************************************
857 * ExFreePoolWithTag (NTOSKRNL.EXE.@)
859 void WINAPI ExFreePoolWithTag( void *ptr, ULONG tag )
861 TRACE( "%p\n", ptr );
862 HeapFree( GetProcessHeap(), 0, ptr );
866 /***********************************************************************
867 * KeInitializeSpinLock (NTOSKRNL.EXE.@)
869 void WINAPI KeInitializeSpinLock( PKSPIN_LOCK SpinLock )
871 FIXME("%p\n", SpinLock);
875 /***********************************************************************
876 * KeInitializeTimerEx (NTOSKRNL.EXE.@)
878 void WINAPI KeInitializeTimerEx( PKTIMER Timer, TIMER_TYPE Type )
880 FIXME("%p %d\n", Timer, Type);
884 /***********************************************************************
885 * KeInitializeTimer (NTOSKRNL.EXE.@)
887 void WINAPI KeInitializeTimer( PKTIMER Timer )
889 KeInitializeTimerEx(Timer, NotificationTimer);
893 /**********************************************************************
894 * KeQueryActiveProcessors (NTOSKRNL.EXE.@)
896 * Return the active Processors as bitmask
898 * RETURNS
899 * active Processors as bitmask
902 KAFFINITY WINAPI KeQueryActiveProcessors( void )
904 DWORD_PTR AffinityMask;
906 GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask, NULL);
907 return AffinityMask;
911 /**********************************************************************
912 * KeQueryInterruptTime (NTOSKRNL.EXE.@)
914 * Return the interrupt time count
917 ULONGLONG WINAPI KeQueryInterruptTime( void )
919 LARGE_INTEGER totaltime;
921 KeQueryTickCount(&totaltime);
922 return totaltime.QuadPart;
926 /***********************************************************************
927 * KeQuerySystemTime (NTOSKRNL.EXE.@)
929 void WINAPI KeQuerySystemTime( LARGE_INTEGER *time )
931 NtQuerySystemTime( time );
935 /***********************************************************************
936 * KeQueryTickCount (NTOSKRNL.EXE.@)
938 void WINAPI KeQueryTickCount( LARGE_INTEGER *count )
940 count->QuadPart = NtGetTickCount();
941 /* update the global variable too */
942 KeTickCount.LowPart = count->u.LowPart;
943 KeTickCount.High1Time = count->u.HighPart;
944 KeTickCount.High2Time = count->u.HighPart;
948 /***********************************************************************
949 * KeQueryTimeIncrement (NTOSKRNL.EXE.@)
951 ULONG WINAPI KeQueryTimeIncrement(void)
953 return 10000;
957 /***********************************************************************
958 * MmAllocateNonCachedMemory (NTOSKRNL.EXE.@)
960 PVOID WINAPI MmAllocateNonCachedMemory( SIZE_T size )
962 TRACE( "%lu\n", size );
963 return VirtualAlloc( NULL, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE|PAGE_NOCACHE );
967 /***********************************************************************
968 * MmFreeNonCachedMemory (NTOSKRNL.EXE.@)
970 void WINAPI MmFreeNonCachedMemory( void *addr, SIZE_T size )
972 TRACE( "%p %lu\n", addr, size );
973 VirtualFree( addr, 0, MEM_RELEASE );
976 /***********************************************************************
977 * MmIsAddressValid (NTOSKRNL.EXE.@)
979 * Check if the process can access the virtual address without a pagefault
981 * PARAMS
982 * VirtualAddress [I] Address to check
984 * RETURNS
985 * Failure: FALSE
986 * Success: TRUE (Accessing the Address works without a Pagefault)
989 BOOLEAN WINAPI MmIsAddressValid(PVOID VirtualAddress)
991 TRACE("(%p)\n", VirtualAddress);
992 return !IsBadWritePtr(VirtualAddress, 1);
995 /***********************************************************************
996 * MmPageEntireDriver (NTOSKRNL.EXE.@)
998 PVOID WINAPI MmPageEntireDriver(PVOID AddrInSection)
1000 TRACE("%p\n", AddrInSection);
1001 return AddrInSection;
1004 /***********************************************************************
1005 * MmResetDriverPaging (NTOSKRNL.EXE.@)
1007 void WINAPI MmResetDriverPaging(PVOID AddrInSection)
1009 TRACE("%p\n", AddrInSection);
1013 /***********************************************************************
1014 * ObReferenceObjectByHandle (NTOSKRNL.EXE.@)
1016 NTSTATUS WINAPI ObReferenceObjectByHandle( HANDLE obj, ACCESS_MASK access,
1017 POBJECT_TYPE type,
1018 KPROCESSOR_MODE mode, PVOID* ptr,
1019 POBJECT_HANDLE_INFORMATION info)
1021 FIXME( "stub: %p %x %p %d %p %p\n", obj, access, type, mode, ptr, info);
1022 return STATUS_NOT_IMPLEMENTED;
1026 /***********************************************************************
1027 * ObfDereferenceObject (NTOSKRNL.EXE.@)
1029 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1030 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject )
1031 void WINAPI __regs_ObfDereferenceObject( VOID *obj )
1032 #else
1033 void WINAPI ObfDereferenceObject( VOID *obj )
1034 #endif
1036 FIXME( "stub: %p\n", obj );
1040 /***********************************************************************
1041 * PsCreateSystemThread (NTOSKRNL.EXE.@)
1043 NTSTATUS WINAPI PsCreateSystemThread(PHANDLE ThreadHandle, ULONG DesiredAccess,
1044 POBJECT_ATTRIBUTES ObjectAttributes,
1045 HANDLE ProcessHandle, PCLIENT_ID ClientId,
1046 PKSTART_ROUTINE StartRoutine, PVOID StartContext)
1048 if (!ProcessHandle) ProcessHandle = GetCurrentProcess();
1049 return RtlCreateUserThread(ProcessHandle, 0, FALSE, 0, 0,
1050 0, StartRoutine, StartContext,
1051 ThreadHandle, ClientId);
1054 /***********************************************************************
1055 * PsGetCurrentProcessId (NTOSKRNL.EXE.@)
1057 HANDLE WINAPI PsGetCurrentProcessId(void)
1059 return (HANDLE)GetCurrentProcessId(); /* FIXME: not quite right... */
1063 /***********************************************************************
1064 * PsGetCurrentThreadId (NTOSKRNL.EXE.@)
1066 HANDLE WINAPI PsGetCurrentThreadId(void)
1068 return (HANDLE)GetCurrentThreadId(); /* FIXME: not quite right... */
1072 /***********************************************************************
1073 * PsGetVersion (NTOSKRNL.EXE.@)
1075 BOOLEAN WINAPI PsGetVersion(ULONG *major, ULONG *minor, ULONG *build, UNICODE_STRING *version )
1077 RTL_OSVERSIONINFOEXW info;
1079 RtlGetVersion( &info );
1080 if (major) *major = info.dwMajorVersion;
1081 if (minor) *minor = info.dwMinorVersion;
1082 if (build) *build = info.dwBuildNumber;
1084 if (version)
1086 #if 0 /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1087 size_t len = min( strlenW(info.szCSDVersion)*sizeof(WCHAR), version->MaximumLength );
1088 memcpy( version->Buffer, info.szCSDVersion, len );
1089 if (len < version->MaximumLength) version->Buffer[len / sizeof(WCHAR)] = 0;
1090 version->Length = len;
1091 #endif
1093 return TRUE;
1097 /***********************************************************************
1098 * PsSetCreateProcessNotifyRoutine (NTOSKRNL.EXE.@)
1100 NTSTATUS WINAPI PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback, BOOLEAN remove )
1102 FIXME( "stub: %p %d\n", callback, remove );
1103 return STATUS_SUCCESS;
1106 /***********************************************************************
1107 * MmGetSystemRoutineAddress (NTOSKRNL.EXE.@)
1109 PVOID WINAPI MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName)
1111 HMODULE hMod;
1112 STRING routineNameA;
1113 PVOID pFunc = NULL;
1115 static const WCHAR ntoskrnlW[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1116 static const WCHAR halW[] = {'h','a','l','.','d','l','l',0};
1118 if (!SystemRoutineName) return NULL;
1120 if (RtlUnicodeStringToAnsiString( &routineNameA, SystemRoutineName, TRUE ) == STATUS_SUCCESS)
1122 /* We only support functions exported from ntoskrnl.exe or hal.dll */
1123 hMod = GetModuleHandleW( ntoskrnlW );
1124 pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1125 if (!pFunc)
1127 hMod = GetModuleHandleW( halW );
1128 if (hMod) pFunc = GetProcAddress( hMod, routineNameA.Buffer );
1130 RtlFreeAnsiString( &routineNameA );
1133 TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName), pFunc );
1134 return pFunc;
1137 /*****************************************************
1138 * DllMain
1140 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
1142 LARGE_INTEGER count;
1144 switch(reason)
1146 case DLL_PROCESS_ATTACH:
1147 DisableThreadLibraryCalls( inst );
1148 RtlAddVectoredExceptionHandler( TRUE, vectored_handler );
1149 KeQueryTickCount( &count ); /* initialize the global KeTickCount */
1150 break;
1152 return TRUE;