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
22 #include "wine/port.h"
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
30 #define WIN32_NO_STATUS
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
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
);
57 typedef void (WINAPI
*PCREATE_THREAD_NOTIFY_ROUTINE
)(HANDLE
,HANDLE
,BOOLEAN
);
59 static struct list Irps
= LIST_INIT(Irps
);
68 #define DEFINE_FASTCALL1_ENTRYPOINT( name ) \
69 __ASM_STDCALL_FUNC( name, 4, \
73 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(4))
74 #define DEFINE_FASTCALL2_ENTRYPOINT( name ) \
75 __ASM_STDCALL_FUNC( name, 8, \
80 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(8))
81 #define DEFINE_FASTCALL3_ENTRYPOINT( name ) \
82 __ASM_STDCALL_FUNC( name, 12, \
87 "jmp " __ASM_NAME("__regs_") #name __ASM_STDCALL(12))
90 static inline LPCSTR
debugstr_us( const UNICODE_STRING
*us
)
92 if (!us
) return "<null>";
93 return debugstr_wn( us
->Buffer
, us
->Length
/ sizeof(WCHAR
) );
96 static HANDLE
get_device_manager(void)
98 static HANDLE device_manager
;
99 HANDLE handle
= 0, ret
= device_manager
;
103 SERVER_START_REQ( create_device_manager
)
105 req
->access
= SYNCHRONIZE
;
107 if (!wine_server_call( req
)) handle
= wine_server_ptr_handle( reply
->handle
);
113 ERR( "failed to create the device manager\n" );
116 if (!(ret
= InterlockedCompareExchangePointer( &device_manager
, handle
, 0 )))
119 NtClose( handle
); /* somebody beat us to it */
124 /* exception handler for emulation of privileged instructions */
125 static LONG CALLBACK
vectored_handler( EXCEPTION_POINTERS
*ptrs
)
127 EXCEPTION_RECORD
*record
= ptrs
->ExceptionRecord
;
129 if (record
->ExceptionCode
== EXCEPTION_ACCESS_VIOLATION
||
130 record
->ExceptionCode
== EXCEPTION_PRIV_INSTRUCTION
)
133 CONTEXT
*context
= ptrs
->ContextRecord
;
134 extern DWORD
__wine_emulate_instruction( EXCEPTION_RECORD
*rec
, CONTEXT
*context
);
136 if (__wine_emulate_instruction( record
, context
) == ExceptionContinueExecution
)
137 return EXCEPTION_CONTINUE_EXECUTION
;
139 FIXME( "Privileged instruction emulation not implemented on this CPU\n" );
142 return EXCEPTION_CONTINUE_SEARCH
;
145 /* process an ioctl request for a given device */
146 static NTSTATUS
process_ioctl( DEVICE_OBJECT
*device
, ULONG code
, void *in_buff
, ULONG in_size
,
147 void *out_buff
, ULONG
*out_size
)
151 IO_STACK_LOCATION irpsp
;
152 PDRIVER_DISPATCH dispatch
= device
->DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
];
156 TRACE( "ioctl %x device %p in_size %u out_size %u\n", code
, device
, in_size
, *out_size
);
158 /* so we can spot things that we should initialize */
159 memset( &irp
, 0x55, sizeof(irp
) );
160 memset( &irpsp
, 0x66, sizeof(irpsp
) );
161 memset( &mdl
, 0x77, sizeof(mdl
) );
163 irp
.RequestorMode
= UserMode
;
164 irp
.AssociatedIrp
.SystemBuffer
= in_buff
;
165 irp
.UserBuffer
= out_buff
;
166 irp
.MdlAddress
= &mdl
;
167 irp
.Tail
.Overlay
.s
.u
.CurrentStackLocation
= &irpsp
;
170 irpsp
.MajorFunction
= IRP_MJ_DEVICE_CONTROL
;
171 irpsp
.Parameters
.DeviceIoControl
.OutputBufferLength
= *out_size
;
172 irpsp
.Parameters
.DeviceIoControl
.InputBufferLength
= in_size
;
173 irpsp
.Parameters
.DeviceIoControl
.IoControlCode
= code
;
174 irpsp
.Parameters
.DeviceIoControl
.Type3InputBuffer
= in_buff
;
175 irpsp
.DeviceObject
= device
;
176 irpsp
.CompletionRoutine
= NULL
;
180 mdl
.StartVa
= out_buff
;
181 mdl
.ByteCount
= *out_size
;
184 device
->CurrentIrp
= &irp
;
186 KeQueryTickCount( &count
); /* update the global KeTickCount */
189 DPRINTF( "%04x:Call driver dispatch %p (device=%p,irp=%p)\n",
190 GetCurrentThreadId(), dispatch
, device
, &irp
);
192 status
= dispatch( device
, &irp
);
195 DPRINTF( "%04x:Ret driver dispatch %p (device=%p,irp=%p) retval=%08x\n",
196 GetCurrentThreadId(), dispatch
, device
, &irp
, status
);
198 *out_size
= (irp
.IoStatus
.u
.Status
>= 0) ? irp
.IoStatus
.Information
: 0;
199 return irp
.IoStatus
.u
.Status
;
203 /***********************************************************************
204 * wine_ntoskrnl_main_loop (Not a Windows API)
206 NTSTATUS CDECL
wine_ntoskrnl_main_loop( HANDLE stop_event
)
208 HANDLE manager
= get_device_manager();
209 obj_handle_t ioctl
= 0;
210 NTSTATUS status
= STATUS_SUCCESS
;
212 void *in_buff
, *out_buff
= NULL
;
213 DEVICE_OBJECT
*device
= NULL
;
214 ULONG in_size
= 4096, out_size
= 0;
217 if (!(in_buff
= HeapAlloc( GetProcessHeap(), 0, in_size
)))
219 ERR( "failed to allocate buffer\n" );
220 return STATUS_NO_MEMORY
;
223 handles
[0] = stop_event
;
224 handles
[1] = manager
;
228 SERVER_START_REQ( get_next_device_request
)
230 req
->manager
= wine_server_obj_handle( manager
);
232 req
->status
= status
;
233 wine_server_add_data( req
, out_buff
, out_size
);
234 wine_server_set_reply( req
, in_buff
, in_size
);
235 if (!(status
= wine_server_call( req
)))
239 device
= wine_server_get_ptr( reply
->user_ptr
);
240 in_size
= reply
->in_size
;
241 out_size
= reply
->out_size
;
245 ioctl
= 0; /* no previous ioctl */
247 in_size
= reply
->in_size
;
255 HeapFree( GetProcessHeap(), 0, out_buff
);
256 if (out_size
) out_buff
= HeapAlloc( GetProcessHeap(), 0, out_size
);
257 else out_buff
= NULL
;
258 status
= process_ioctl( device
, code
, in_buff
, in_size
, out_buff
, &out_size
);
260 case STATUS_BUFFER_OVERFLOW
:
261 HeapFree( GetProcessHeap(), 0, in_buff
);
262 in_buff
= HeapAlloc( GetProcessHeap(), 0, in_size
);
263 /* restart with larger buffer */
266 if (WaitForMultipleObjects( 2, handles
, FALSE
, INFINITE
) == WAIT_OBJECT_0
)
268 HeapFree( GetProcessHeap(), 0, in_buff
);
269 HeapFree( GetProcessHeap(), 0, out_buff
);
270 return STATUS_SUCCESS
;
278 /***********************************************************************
279 * IoAllocateDriverObjectExtension (NTOSKRNL.EXE.@)
281 NTSTATUS WINAPI
IoAllocateDriverObjectExtension( PDRIVER_OBJECT DriverObject
,
282 PVOID ClientIdentificationAddress
,
283 ULONG DriverObjectExtensionSize
,
284 PVOID
*DriverObjectExtension
)
286 FIXME( "stub: %p, %p, %u, %p\n", DriverObject
, ClientIdentificationAddress
,
287 DriverObjectExtensionSize
, DriverObjectExtension
);
288 return STATUS_NOT_IMPLEMENTED
;
292 /***********************************************************************
293 * IoGetDriverObjectExtension (NTOSKRNL.EXE.@)
295 PVOID WINAPI
IoGetDriverObjectExtension( PDRIVER_OBJECT DriverObject
,
296 PVOID ClientIdentificationAddress
)
298 FIXME( "stub: %p, %p\n", DriverObject
, ClientIdentificationAddress
);
303 /***********************************************************************
304 * IoInitializeIrp (NTOSKRNL.EXE.@)
306 void WINAPI
IoInitializeIrp( IRP
*irp
, USHORT size
, CCHAR stack_size
)
308 TRACE( "%p, %u, %d\n", irp
, size
, stack_size
);
310 RtlZeroMemory( irp
, size
);
312 irp
->Type
= IO_TYPE_IRP
;
314 InitializeListHead( &irp
->ThreadListEntry
);
315 irp
->StackCount
= stack_size
;
316 irp
->CurrentLocation
= stack_size
+ 1;
317 irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
=
318 (PIO_STACK_LOCATION
)(irp
+ 1) + stack_size
;
322 /***********************************************************************
323 * IoAllocateIrp (NTOSKRNL.EXE.@)
325 PIRP WINAPI
IoAllocateIrp( CCHAR stack_size
, BOOLEAN charge_quota
)
330 TRACE( "%d, %d\n", stack_size
, charge_quota
);
332 size
= sizeof(IRP
) + stack_size
* sizeof(IO_STACK_LOCATION
);
333 irp
= ExAllocatePool( NonPagedPool
, size
);
336 IoInitializeIrp( irp
, size
, stack_size
);
337 irp
->AllocationFlags
= IRP_ALLOCATED_FIXED_SIZE
;
339 irp
->AllocationFlags
|= IRP_LOOKASIDE_ALLOCATION
;
344 /***********************************************************************
345 * IoFreeIrp (NTOSKRNL.EXE.@)
347 void WINAPI
IoFreeIrp( IRP
*irp
)
349 TRACE( "%p\n", irp
);
355 /***********************************************************************
356 * IoAllocateMdl (NTOSKRNL.EXE.@)
358 PMDL WINAPI
IoAllocateMdl( PVOID VirtualAddress
, ULONG Length
, BOOLEAN SecondaryBuffer
, BOOLEAN ChargeQuota
, PIRP Irp
)
360 FIXME( "stub: %p, %u, %i, %i, %p\n", VirtualAddress
, Length
, SecondaryBuffer
, ChargeQuota
, Irp
);
365 /***********************************************************************
366 * IoAllocateWorkItem (NTOSKRNL.EXE.@)
368 PIO_WORKITEM WINAPI
IoAllocateWorkItem( PDEVICE_OBJECT DeviceObject
)
370 FIXME( "stub: %p\n", DeviceObject
);
375 /***********************************************************************
376 * IoAttachDeviceToDeviceStack (NTOSKRNL.EXE.@)
378 PDEVICE_OBJECT WINAPI
IoAttachDeviceToDeviceStack( DEVICE_OBJECT
*source
,
379 DEVICE_OBJECT
*target
)
381 TRACE( "%p, %p\n", source
, target
);
382 target
->AttachedDevice
= source
;
383 source
->StackSize
= target
->StackSize
+ 1;
388 /***********************************************************************
389 * IoBuildDeviceIoControlRequest (NTOSKRNL.EXE.@)
391 PIRP WINAPI
IoBuildDeviceIoControlRequest( ULONG IoControlCode
,
392 PDEVICE_OBJECT DeviceObject
,
394 ULONG InputBufferLength
,
396 ULONG OutputBufferLength
,
397 BOOLEAN InternalDeviceIoControl
,
399 PIO_STATUS_BLOCK IoStatusBlock
)
402 PIO_STACK_LOCATION irpsp
;
403 struct IrpInstance
*instance
;
405 TRACE( "%x, %p, %p, %u, %p, %u, %u, %p, %p\n",
406 IoControlCode
, DeviceObject
, InputBuffer
, InputBufferLength
,
407 OutputBuffer
, OutputBufferLength
, InternalDeviceIoControl
,
408 Event
, IoStatusBlock
);
410 if (DeviceObject
== NULL
)
413 irp
= IoAllocateIrp( DeviceObject
->StackSize
, FALSE
);
417 instance
= HeapAlloc( GetProcessHeap(), 0, sizeof(struct IrpInstance
) );
418 if (instance
== NULL
)
424 list_add_tail( &Irps
, &instance
->entry
);
426 irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
- 1;
427 irpsp
->MajorFunction
= InternalDeviceIoControl
?
428 IRP_MJ_INTERNAL_DEVICE_CONTROL
: IRP_MJ_DEVICE_CONTROL
;
429 irpsp
->Parameters
.DeviceIoControl
.IoControlCode
= IoControlCode
;
430 irp
->UserIosb
= IoStatusBlock
;
431 irp
->UserEvent
= Event
;
437 /***********************************************************************
438 * IoCreateDriver (NTOSKRNL.EXE.@)
440 NTSTATUS WINAPI
IoCreateDriver( UNICODE_STRING
*name
, PDRIVER_INITIALIZE init
)
442 DRIVER_OBJECT
*driver
;
443 DRIVER_EXTENSION
*extension
;
446 if (!(driver
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
,
447 sizeof(*driver
) + sizeof(*extension
) )))
448 return STATUS_NO_MEMORY
;
450 if ((status
= RtlDuplicateUnicodeString( 1, name
, &driver
->DriverName
)))
452 RtlFreeHeap( GetProcessHeap(), 0, driver
);
456 extension
= (DRIVER_EXTENSION
*)(driver
+ 1);
457 driver
->Size
= sizeof(*driver
);
458 driver
->DriverInit
= init
;
459 driver
->DriverExtension
= extension
;
460 extension
->DriverObject
= driver
;
461 extension
->ServiceKeyName
= driver
->DriverName
;
463 status
= driver
->DriverInit( driver
, name
);
467 RtlFreeUnicodeString( &driver
->DriverName
);
468 RtlFreeHeap( GetProcessHeap(), 0, driver
);
474 /***********************************************************************
475 * IoDeleteDriver (NTOSKRNL.EXE.@)
477 void WINAPI
IoDeleteDriver( DRIVER_OBJECT
*driver
)
479 RtlFreeUnicodeString( &driver
->DriverName
);
480 RtlFreeHeap( GetProcessHeap(), 0, driver
);
484 /***********************************************************************
485 * IoCreateDevice (NTOSKRNL.EXE.@)
487 NTSTATUS WINAPI
IoCreateDevice( DRIVER_OBJECT
*driver
, ULONG ext_size
,
488 UNICODE_STRING
*name
, DEVICE_TYPE type
,
489 ULONG characteristics
, BOOLEAN exclusive
,
490 DEVICE_OBJECT
**ret_device
)
493 DEVICE_OBJECT
*device
;
495 HANDLE manager
= get_device_manager();
497 TRACE( "(%p, %u, %s, %u, %x, %u, %p)\n",
498 driver
, ext_size
, debugstr_us(name
), type
, characteristics
, exclusive
, ret_device
);
500 if (!(device
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*device
) + ext_size
)))
501 return STATUS_NO_MEMORY
;
503 SERVER_START_REQ( create_device
)
508 req
->manager
= wine_server_obj_handle( manager
);
509 req
->user_ptr
= wine_server_client_ptr( device
);
510 if (name
) wine_server_add_data( req
, name
->Buffer
, name
->Length
);
511 if (!(status
= wine_server_call( req
))) handle
= wine_server_ptr_handle( reply
->handle
);
515 if (status
== STATUS_SUCCESS
)
517 device
->DriverObject
= driver
;
518 device
->DeviceExtension
= device
+ 1;
519 device
->DeviceType
= type
;
520 device
->StackSize
= 1;
521 device
->Reserved
= handle
;
523 device
->NextDevice
= driver
->DeviceObject
;
524 driver
->DeviceObject
= device
;
526 *ret_device
= device
;
528 else HeapFree( GetProcessHeap(), 0, device
);
534 /***********************************************************************
535 * IoDeleteDevice (NTOSKRNL.EXE.@)
537 void WINAPI
IoDeleteDevice( DEVICE_OBJECT
*device
)
541 TRACE( "%p\n", device
);
543 SERVER_START_REQ( delete_device
)
545 req
->handle
= wine_server_obj_handle( device
->Reserved
);
546 status
= wine_server_call( req
);
550 if (status
== STATUS_SUCCESS
)
552 DEVICE_OBJECT
**prev
= &device
->DriverObject
->DeviceObject
;
553 while (*prev
&& *prev
!= device
) prev
= &(*prev
)->NextDevice
;
554 if (*prev
) *prev
= (*prev
)->NextDevice
;
555 NtClose( device
->Reserved
);
556 HeapFree( GetProcessHeap(), 0, device
);
561 /***********************************************************************
562 * IoCreateSymbolicLink (NTOSKRNL.EXE.@)
564 NTSTATUS WINAPI
IoCreateSymbolicLink( UNICODE_STRING
*name
, UNICODE_STRING
*target
)
567 OBJECT_ATTRIBUTES attr
;
569 attr
.Length
= sizeof(attr
);
570 attr
.RootDirectory
= 0;
571 attr
.ObjectName
= name
;
572 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
;
573 attr
.SecurityDescriptor
= NULL
;
574 attr
.SecurityQualityOfService
= NULL
;
576 TRACE( "%s -> %s\n", debugstr_us(name
), debugstr_us(target
) );
577 /* FIXME: store handle somewhere */
578 return NtCreateSymbolicLinkObject( &handle
, SYMBOLIC_LINK_ALL_ACCESS
, &attr
, target
);
582 /***********************************************************************
583 * IoDeleteSymbolicLink (NTOSKRNL.EXE.@)
585 NTSTATUS WINAPI
IoDeleteSymbolicLink( UNICODE_STRING
*name
)
588 OBJECT_ATTRIBUTES attr
;
591 attr
.Length
= sizeof(attr
);
592 attr
.RootDirectory
= 0;
593 attr
.ObjectName
= name
;
594 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
595 attr
.SecurityDescriptor
= NULL
;
596 attr
.SecurityQualityOfService
= NULL
;
598 if (!(status
= NtOpenSymbolicLinkObject( &handle
, 0, &attr
)))
600 SERVER_START_REQ( unlink_object
)
602 req
->handle
= wine_server_obj_handle( handle
);
603 status
= wine_server_call( req
);
612 /***********************************************************************
613 * IoGetDeviceObjectPointer (NTOSKRNL.EXE.@)
615 NTSTATUS WINAPI
IoGetDeviceObjectPointer( UNICODE_STRING
*name
, ACCESS_MASK access
, PFILE_OBJECT
*file
, PDEVICE_OBJECT
*device
)
617 FIXME( "stub: %s %x %p %p\n", debugstr_us(name
), access
, file
, device
);
618 return STATUS_NOT_IMPLEMENTED
;
622 /***********************************************************************
623 * IofCallDriver (NTOSKRNL.EXE.@)
625 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
626 DEFINE_FASTCALL2_ENTRYPOINT( IofCallDriver
)
627 NTSTATUS WINAPI
__regs_IofCallDriver( DEVICE_OBJECT
*device
, IRP
*irp
)
629 NTSTATUS WINAPI
IofCallDriver( DEVICE_OBJECT
*device
, IRP
*irp
)
632 PDRIVER_DISPATCH dispatch
;
633 IO_STACK_LOCATION
*irpsp
;
636 TRACE( "%p %p\n", device
, irp
);
638 --irp
->CurrentLocation
;
639 irpsp
= --irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
640 dispatch
= device
->DriverObject
->MajorFunction
[irpsp
->MajorFunction
];
641 status
= dispatch( device
, irp
);
647 /***********************************************************************
648 * IoGetRelatedDeviceObject (NTOSKRNL.EXE.@)
650 PDEVICE_OBJECT WINAPI
IoGetRelatedDeviceObject( PFILE_OBJECT obj
)
652 FIXME( "stub: %p\n", obj
);
656 static CONFIGURATION_INFORMATION configuration_information
;
658 /***********************************************************************
659 * IoGetConfigurationInformation (NTOSKRNL.EXE.@)
661 PCONFIGURATION_INFORMATION WINAPI
IoGetConfigurationInformation(void)
663 FIXME( "partial stub\n" );
664 /* FIXME: return actual devices on system */
665 return &configuration_information
;
669 /***********************************************************************
670 * IoRegisterDriverReinitialization (NTOSKRNL.EXE.@)
672 void WINAPI
IoRegisterDriverReinitialization( PDRIVER_OBJECT obj
, PDRIVER_REINITIALIZE reinit
, PVOID context
)
674 FIXME( "stub: %p %p %p\n", obj
, reinit
, context
);
678 /***********************************************************************
679 * IoRegisterShutdownNotification (NTOSKRNL.EXE.@)
681 NTSTATUS WINAPI
IoRegisterShutdownNotification( PDEVICE_OBJECT obj
)
683 FIXME( "stub: %p\n", obj
);
684 return STATUS_SUCCESS
;
688 /***********************************************************************
689 * IofCompleteRequest (NTOSKRNL.EXE.@)
691 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
692 DEFINE_FASTCALL2_ENTRYPOINT( IofCompleteRequest
)
693 void WINAPI
__regs_IofCompleteRequest( IRP
*irp
, UCHAR priority_boost
)
695 void WINAPI
IofCompleteRequest( IRP
*irp
, UCHAR priority_boost
)
698 IO_STACK_LOCATION
*irpsp
;
699 PIO_COMPLETION_ROUTINE routine
;
700 IO_STATUS_BLOCK
*iosb
;
701 struct IrpInstance
*instance
;
702 NTSTATUS status
, stat
;
705 TRACE( "%p %u\n", irp
, priority_boost
);
707 iosb
= irp
->UserIosb
;
708 status
= irp
->IoStatus
.u
.Status
;
709 while (irp
->CurrentLocation
<= irp
->StackCount
)
711 irpsp
= irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
712 routine
= irpsp
->CompletionRoutine
;
714 /* FIXME: add SL_INVOKE_ON_CANCEL support */
717 if ((irpsp
->Control
& SL_INVOKE_ON_SUCCESS
) && STATUS_SUCCESS
== status
)
719 if ((irpsp
->Control
& SL_INVOKE_ON_ERROR
) && STATUS_SUCCESS
!= status
)
722 ++irp
->CurrentLocation
;
723 ++irp
->Tail
.Overlay
.s
.u
.CurrentStackLocation
;
726 TRACE( "calling %p( %p, %p, %p )\n", routine
,
727 irpsp
->DeviceObject
, irp
, irpsp
->Context
);
728 stat
= routine( irpsp
->DeviceObject
, irp
, irpsp
->Context
);
729 TRACE( "CompletionRoutine returned %x\n", stat
);
730 if (STATUS_MORE_PROCESSING_REQUIRED
== stat
)
734 if (iosb
&& STATUS_SUCCESS
== status
)
736 iosb
->u
.Status
= irp
->IoStatus
.u
.Status
;
737 iosb
->Information
= irp
->IoStatus
.Information
;
739 LIST_FOR_EACH_ENTRY( instance
, &Irps
, struct IrpInstance
, entry
)
741 if (instance
->irp
== irp
)
743 list_remove( &instance
->entry
);
744 HeapFree( GetProcessHeap(), 0, instance
);
752 /***********************************************************************
753 * InterlockedCompareExchange (NTOSKRNL.EXE.@)
755 #ifdef DEFINE_FASTCALL3_ENTRYPOINT
756 DEFINE_FASTCALL3_ENTRYPOINT( NTOSKRNL_InterlockedCompareExchange
)
757 LONG WINAPI
__regs_NTOSKRNL_InterlockedCompareExchange( LONG
volatile *dest
, LONG xchg
, LONG compare
)
759 LONG WINAPI
NTOSKRNL_InterlockedCompareExchange( LONG
volatile *dest
, LONG xchg
, LONG compare
)
762 return InterlockedCompareExchange( dest
, xchg
, compare
);
766 /***********************************************************************
767 * InterlockedDecrement (NTOSKRNL.EXE.@)
769 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
770 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedDecrement
)
771 LONG WINAPI
__regs_NTOSKRNL_InterlockedDecrement( LONG
volatile *dest
)
773 LONG WINAPI
NTOSKRNL_InterlockedDecrement( LONG
volatile *dest
)
776 return InterlockedDecrement( dest
);
780 /***********************************************************************
781 * InterlockedExchange (NTOSKRNL.EXE.@)
783 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
784 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchange
)
785 LONG WINAPI
__regs_NTOSKRNL_InterlockedExchange( LONG
volatile *dest
, LONG val
)
787 LONG WINAPI
NTOSKRNL_InterlockedExchange( LONG
volatile *dest
, LONG val
)
790 return InterlockedExchange( dest
, val
);
794 /***********************************************************************
795 * InterlockedExchangeAdd (NTOSKRNL.EXE.@)
797 #ifdef DEFINE_FASTCALL2_ENTRYPOINT
798 DEFINE_FASTCALL2_ENTRYPOINT( NTOSKRNL_InterlockedExchangeAdd
)
799 LONG WINAPI
__regs_NTOSKRNL_InterlockedExchangeAdd( LONG
volatile *dest
, LONG incr
)
801 LONG WINAPI
NTOSKRNL_InterlockedExchangeAdd( LONG
volatile *dest
, LONG incr
)
804 return InterlockedExchangeAdd( dest
, incr
);
808 /***********************************************************************
809 * InterlockedIncrement (NTOSKRNL.EXE.@)
811 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
812 DEFINE_FASTCALL1_ENTRYPOINT( NTOSKRNL_InterlockedIncrement
)
813 LONG WINAPI
__regs_NTOSKRNL_InterlockedIncrement( LONG
volatile *dest
)
815 LONG WINAPI
NTOSKRNL_InterlockedIncrement( LONG
volatile *dest
)
818 return InterlockedIncrement( dest
);
822 /***********************************************************************
823 * ExAllocatePool (NTOSKRNL.EXE.@)
825 PVOID WINAPI
ExAllocatePool( POOL_TYPE type
, SIZE_T size
)
827 return ExAllocatePoolWithTag( type
, size
, 0 );
831 /***********************************************************************
832 * ExAllocatePoolWithQuota (NTOSKRNL.EXE.@)
834 PVOID WINAPI
ExAllocatePoolWithQuota( POOL_TYPE type
, SIZE_T size
)
836 return ExAllocatePoolWithTag( type
, size
, 0 );
840 /***********************************************************************
841 * ExAllocatePoolWithTag (NTOSKRNL.EXE.@)
843 PVOID WINAPI
ExAllocatePoolWithTag( POOL_TYPE type
, SIZE_T size
, ULONG tag
)
845 /* FIXME: handle page alignment constraints */
846 void *ret
= HeapAlloc( GetProcessHeap(), 0, size
);
847 TRACE( "%lu pool %u -> %p\n", size
, type
, ret
);
852 /***********************************************************************
853 * ExAllocatePoolWithQuotaTag (NTOSKRNL.EXE.@)
855 PVOID WINAPI
ExAllocatePoolWithQuotaTag( POOL_TYPE type
, SIZE_T size
, ULONG tag
)
857 return ExAllocatePoolWithTag( type
, size
, tag
);
861 /***********************************************************************
862 * ExFreePool (NTOSKRNL.EXE.@)
864 void WINAPI
ExFreePool( void *ptr
)
866 ExFreePoolWithTag( ptr
, 0 );
870 /***********************************************************************
871 * ExFreePoolWithTag (NTOSKRNL.EXE.@)
873 void WINAPI
ExFreePoolWithTag( void *ptr
, ULONG tag
)
875 TRACE( "%p\n", ptr
);
876 HeapFree( GetProcessHeap(), 0, ptr
);
880 /***********************************************************************
881 * KeInitializeEvent (NTOSKRNL.EXE.@)
883 void WINAPI
KeInitializeEvent( PRKEVENT Event
, EVENT_TYPE Type
, BOOLEAN State
)
885 FIXME( "stub: %p %d %d\n", Event
, Type
, State
);
889 /***********************************************************************
890 * KeInitializeMutex (NTOSKRNL.EXE.@)
892 void WINAPI
KeInitializeMutex(PRKMUTEX Mutex
, ULONG Level
)
894 FIXME( "stub: %p, %u\n", Mutex
, Level
);
898 /***********************************************************************
899 * KeInitializeSpinLock (NTOSKRNL.EXE.@)
901 void WINAPI
KeInitializeSpinLock( PKSPIN_LOCK SpinLock
)
903 FIXME( "stub: %p\n", SpinLock
);
907 /***********************************************************************
908 * KeInitializeTimerEx (NTOSKRNL.EXE.@)
910 void WINAPI
KeInitializeTimerEx( PKTIMER Timer
, TIMER_TYPE Type
)
912 FIXME( "stub: %p %d\n", Timer
, Type
);
916 /***********************************************************************
917 * KeInitializeTimer (NTOSKRNL.EXE.@)
919 void WINAPI
KeInitializeTimer( PKTIMER Timer
)
921 KeInitializeTimerEx(Timer
, NotificationTimer
);
925 /**********************************************************************
926 * KeQueryActiveProcessors (NTOSKRNL.EXE.@)
928 * Return the active Processors as bitmask
931 * active Processors as bitmask
934 KAFFINITY WINAPI
KeQueryActiveProcessors( void )
936 DWORD_PTR AffinityMask
;
938 GetProcessAffinityMask( GetCurrentProcess(), &AffinityMask
, NULL
);
943 /**********************************************************************
944 * KeQueryInterruptTime (NTOSKRNL.EXE.@)
946 * Return the interrupt time count
949 ULONGLONG WINAPI
KeQueryInterruptTime( void )
951 LARGE_INTEGER totaltime
;
953 KeQueryTickCount(&totaltime
);
954 return totaltime
.QuadPart
;
958 /***********************************************************************
959 * KeQuerySystemTime (NTOSKRNL.EXE.@)
961 void WINAPI
KeQuerySystemTime( LARGE_INTEGER
*time
)
963 NtQuerySystemTime( time
);
967 /***********************************************************************
968 * KeQueryTickCount (NTOSKRNL.EXE.@)
970 void WINAPI
KeQueryTickCount( LARGE_INTEGER
*count
)
972 count
->QuadPart
= NtGetTickCount();
973 /* update the global variable too */
974 KeTickCount
.LowPart
= count
->u
.LowPart
;
975 KeTickCount
.High1Time
= count
->u
.HighPart
;
976 KeTickCount
.High2Time
= count
->u
.HighPart
;
980 /***********************************************************************
981 * KeQueryTimeIncrement (NTOSKRNL.EXE.@)
983 ULONG WINAPI
KeQueryTimeIncrement(void)
989 /***********************************************************************
990 * MmAllocateNonCachedMemory (NTOSKRNL.EXE.@)
992 PVOID WINAPI
MmAllocateNonCachedMemory( SIZE_T size
)
994 TRACE( "%lu\n", size
);
995 return VirtualAlloc( NULL
, size
, MEM_RESERVE
|MEM_COMMIT
, PAGE_READWRITE
|PAGE_NOCACHE
);
999 /***********************************************************************
1000 * MmFreeNonCachedMemory (NTOSKRNL.EXE.@)
1002 void WINAPI
MmFreeNonCachedMemory( void *addr
, SIZE_T size
)
1004 TRACE( "%p %lu\n", addr
, size
);
1005 VirtualFree( addr
, 0, MEM_RELEASE
);
1008 /***********************************************************************
1009 * MmIsAddressValid (NTOSKRNL.EXE.@)
1011 * Check if the process can access the virtual address without a pagefault
1014 * VirtualAddress [I] Address to check
1018 * Success: TRUE (Accessing the Address works without a Pagefault)
1021 BOOLEAN WINAPI
MmIsAddressValid(PVOID VirtualAddress
)
1023 TRACE("(%p)\n", VirtualAddress
);
1024 return !IsBadWritePtr(VirtualAddress
, 1);
1027 /***********************************************************************
1028 * MmPageEntireDriver (NTOSKRNL.EXE.@)
1030 PVOID WINAPI
MmPageEntireDriver(PVOID AddrInSection
)
1032 TRACE("%p\n", AddrInSection
);
1033 return AddrInSection
;
1036 /***********************************************************************
1037 * MmResetDriverPaging (NTOSKRNL.EXE.@)
1039 void WINAPI
MmResetDriverPaging(PVOID AddrInSection
)
1041 TRACE("%p\n", AddrInSection
);
1045 /***********************************************************************
1046 * ObReferenceObjectByHandle (NTOSKRNL.EXE.@)
1048 NTSTATUS WINAPI
ObReferenceObjectByHandle( HANDLE obj
, ACCESS_MASK access
,
1050 KPROCESSOR_MODE mode
, PVOID
* ptr
,
1051 POBJECT_HANDLE_INFORMATION info
)
1053 FIXME( "stub: %p %x %p %d %p %p\n", obj
, access
, type
, mode
, ptr
, info
);
1054 return STATUS_NOT_IMPLEMENTED
;
1058 /***********************************************************************
1059 * ObfDereferenceObject (NTOSKRNL.EXE.@)
1061 #ifdef DEFINE_FASTCALL1_ENTRYPOINT
1062 DEFINE_FASTCALL1_ENTRYPOINT( ObfDereferenceObject
)
1063 void WINAPI
__regs_ObfDereferenceObject( VOID
*obj
)
1065 void WINAPI
ObfDereferenceObject( VOID
*obj
)
1068 FIXME( "stub: %p\n", obj
);
1072 /***********************************************************************
1073 * PsCreateSystemThread (NTOSKRNL.EXE.@)
1075 NTSTATUS WINAPI
PsCreateSystemThread(PHANDLE ThreadHandle
, ULONG DesiredAccess
,
1076 POBJECT_ATTRIBUTES ObjectAttributes
,
1077 HANDLE ProcessHandle
, PCLIENT_ID ClientId
,
1078 PKSTART_ROUTINE StartRoutine
, PVOID StartContext
)
1080 if (!ProcessHandle
) ProcessHandle
= GetCurrentProcess();
1081 return RtlCreateUserThread(ProcessHandle
, 0, FALSE
, 0, 0,
1082 0, StartRoutine
, StartContext
,
1083 ThreadHandle
, ClientId
);
1086 /***********************************************************************
1087 * PsGetCurrentProcessId (NTOSKRNL.EXE.@)
1089 HANDLE WINAPI
PsGetCurrentProcessId(void)
1091 return UlongToHandle(GetCurrentProcessId()); /* FIXME: not quite right... */
1095 /***********************************************************************
1096 * PsGetCurrentThreadId (NTOSKRNL.EXE.@)
1098 HANDLE WINAPI
PsGetCurrentThreadId(void)
1100 return UlongToHandle(GetCurrentThreadId()); /* FIXME: not quite right... */
1104 /***********************************************************************
1105 * PsGetVersion (NTOSKRNL.EXE.@)
1107 BOOLEAN WINAPI
PsGetVersion(ULONG
*major
, ULONG
*minor
, ULONG
*build
, UNICODE_STRING
*version
)
1109 RTL_OSVERSIONINFOEXW info
;
1111 RtlGetVersion( &info
);
1112 if (major
) *major
= info
.dwMajorVersion
;
1113 if (minor
) *minor
= info
.dwMinorVersion
;
1114 if (build
) *build
= info
.dwBuildNumber
;
1118 #if 0 /* FIXME: GameGuard passes an uninitialized pointer in version->Buffer */
1119 size_t len
= min( strlenW(info
.szCSDVersion
)*sizeof(WCHAR
), version
->MaximumLength
);
1120 memcpy( version
->Buffer
, info
.szCSDVersion
, len
);
1121 if (len
< version
->MaximumLength
) version
->Buffer
[len
/ sizeof(WCHAR
)] = 0;
1122 version
->Length
= len
;
1129 /***********************************************************************
1130 * PsSetCreateProcessNotifyRoutine (NTOSKRNL.EXE.@)
1132 NTSTATUS WINAPI
PsSetCreateProcessNotifyRoutine( PCREATE_PROCESS_NOTIFY_ROUTINE callback
, BOOLEAN remove
)
1134 FIXME( "stub: %p %d\n", callback
, remove
);
1135 return STATUS_SUCCESS
;
1139 /***********************************************************************
1140 * PsSetCreateThreadNotifyRoutine (NTOSKRNL.EXE.@)
1142 NTSTATUS WINAPI
PsSetCreateThreadNotifyRoutine( PCREATE_THREAD_NOTIFY_ROUTINE NotifyRoutine
)
1144 FIXME( "stub: %p\n", NotifyRoutine
);
1145 return STATUS_SUCCESS
;
1149 /***********************************************************************
1150 * MmGetSystemRoutineAddress (NTOSKRNL.EXE.@)
1152 PVOID WINAPI
MmGetSystemRoutineAddress(PUNICODE_STRING SystemRoutineName
)
1155 STRING routineNameA
;
1158 static const WCHAR ntoskrnlW
[] = {'n','t','o','s','k','r','n','l','.','e','x','e',0};
1159 static const WCHAR halW
[] = {'h','a','l','.','d','l','l',0};
1161 if (!SystemRoutineName
) return NULL
;
1163 if (RtlUnicodeStringToAnsiString( &routineNameA
, SystemRoutineName
, TRUE
) == STATUS_SUCCESS
)
1165 /* We only support functions exported from ntoskrnl.exe or hal.dll */
1166 hMod
= GetModuleHandleW( ntoskrnlW
);
1167 pFunc
= GetProcAddress( hMod
, routineNameA
.Buffer
);
1170 hMod
= GetModuleHandleW( halW
);
1171 if (hMod
) pFunc
= GetProcAddress( hMod
, routineNameA
.Buffer
);
1173 RtlFreeAnsiString( &routineNameA
);
1176 TRACE( "%s -> %p\n", debugstr_us(SystemRoutineName
), pFunc
);
1181 /***********************************************************************
1182 * MmQuerySystemSize (NTOSKRNL.EXE.@)
1184 MM_SYSTEMSIZE WINAPI
MmQuerySystemSize(void)
1187 return MmLargeSystem
;
1191 /*****************************************************
1194 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
1196 static void *handler
;
1197 LARGE_INTEGER count
;
1201 case DLL_PROCESS_ATTACH
:
1202 DisableThreadLibraryCalls( inst
);
1203 handler
= RtlAddVectoredExceptionHandler( TRUE
, vectored_handler
);
1204 KeQueryTickCount( &count
); /* initialize the global KeTickCount */
1206 case DLL_PROCESS_DETACH
:
1207 RtlRemoveVectoredExceptionHandler( handler
);