4 * This file contains the Nt* API functions of NTDLL.DLL.
5 * In the original ntdll.dll they all seem to just call int 0x2e (down to the NTOSKRNL)
7 * Copyright 1996-1998 Marcus Meissner
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #define WIN32_NO_STATUS
32 #include "wine/debug.h"
35 #include "ntdll_misc.h"
36 #include "wine/server.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
40 static ULONG execute_flags
= MEM_EXECUTE_OPTION_DISABLE
;
46 /******************************************************************************
47 * NtTerminateProcess [NTDLL.@]
49 * Native applications must kill themselves when done
51 NTSTATUS WINAPI
NtTerminateProcess( HANDLE handle
, LONG exit_code
)
55 SERVER_START_REQ( terminate_process
)
57 req
->handle
= wine_server_obj_handle( handle
);
58 req
->exit_code
= exit_code
;
59 ret
= wine_server_call( req
);
60 self
= !ret
&& reply
->self
;
63 if (self
&& handle
) exit( exit_code
);
67 /******************************************************************************
68 * RtlGetCurrentPeb [NTDLL.@]
71 PEB
* WINAPI
RtlGetCurrentPeb(void)
73 return NtCurrentTeb()->Peb
;
76 /***********************************************************************
77 * __wine_make_process_system (NTDLL.@)
79 * Mark the current process as a system process.
80 * Returns the event that is signaled when all non-system processes have exited.
82 HANDLE CDECL
__wine_make_process_system(void)
85 SERVER_START_REQ( make_process_system
)
87 if (!wine_server_call( req
)) ret
= wine_server_ptr_handle( reply
->event
);
93 static UINT process_error_mode
;
95 #define UNIMPLEMENTED_INFO_CLASS(c) \
97 FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
98 ret = STATUS_INVALID_INFO_CLASS; \
101 ULONG_PTR
get_system_affinity_mask(void)
103 ULONG num_cpus
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
104 if (num_cpus
>= sizeof(ULONG_PTR
) * 8) return ~(ULONG_PTR
)0;
105 return ((ULONG_PTR
)1 << num_cpus
) - 1;
108 /******************************************************************************
109 * NtQueryInformationProcess [NTDLL.@]
110 * ZwQueryInformationProcess [NTDLL.@]
113 NTSTATUS WINAPI
NtQueryInformationProcess(
114 IN HANDLE ProcessHandle
,
115 IN PROCESSINFOCLASS ProcessInformationClass
,
116 OUT PVOID ProcessInformation
,
117 IN ULONG ProcessInformationLength
,
118 OUT PULONG ReturnLength
)
120 NTSTATUS ret
= STATUS_SUCCESS
;
123 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
124 ProcessHandle
,ProcessInformationClass
,
125 ProcessInformation
,ProcessInformationLength
,
128 switch (ProcessInformationClass
)
130 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits
);
131 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority
);
132 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority
);
133 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort
);
134 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken
);
135 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation
);
136 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize
);
137 UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers
);
138 UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits
);
139 UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch
);
140 UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL
);
141 UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup
);
142 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass
);
143 UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information
);
144 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost
);
145 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap
);
146 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation
);
147 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation
);
148 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled
);
149 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination
);
150 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing
);
152 case ProcessBasicInformation
:
154 PROCESS_BASIC_INFORMATION pbi
;
155 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
157 if (ProcessInformationLength
>= sizeof(PROCESS_BASIC_INFORMATION
))
159 if (!ProcessInformation
)
160 ret
= STATUS_ACCESS_VIOLATION
;
161 else if (!ProcessHandle
)
162 ret
= STATUS_INVALID_HANDLE
;
165 SERVER_START_REQ(get_process_info
)
167 req
->handle
= wine_server_obj_handle( ProcessHandle
);
168 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
170 pbi
.ExitStatus
= reply
->exit_code
;
171 pbi
.PebBaseAddress
= wine_server_get_ptr( reply
->peb
);
172 pbi
.AffinityMask
= reply
->affinity
& affinity_mask
;
173 pbi
.BasePriority
= reply
->priority
;
174 pbi
.UniqueProcessId
= reply
->pid
;
175 pbi
.InheritedFromUniqueProcessId
= reply
->ppid
;
180 memcpy(ProcessInformation
, &pbi
, sizeof(PROCESS_BASIC_INFORMATION
));
182 len
= sizeof(PROCESS_BASIC_INFORMATION
);
185 if (ProcessInformationLength
> sizeof(PROCESS_BASIC_INFORMATION
))
186 ret
= STATUS_INFO_LENGTH_MISMATCH
;
190 len
= sizeof(PROCESS_BASIC_INFORMATION
);
191 ret
= STATUS_INFO_LENGTH_MISMATCH
;
195 case ProcessIoCounters
:
199 if (ProcessInformationLength
>= sizeof(IO_COUNTERS
))
201 if (!ProcessInformation
)
202 ret
= STATUS_ACCESS_VIOLATION
;
203 else if (!ProcessHandle
)
204 ret
= STATUS_INVALID_HANDLE
;
207 /* FIXME : real data */
208 memset(&pii
, 0 , sizeof(IO_COUNTERS
));
210 memcpy(ProcessInformation
, &pii
, sizeof(IO_COUNTERS
));
212 len
= sizeof(IO_COUNTERS
);
215 if (ProcessInformationLength
> sizeof(IO_COUNTERS
))
216 ret
= STATUS_INFO_LENGTH_MISMATCH
;
220 len
= sizeof(IO_COUNTERS
);
221 ret
= STATUS_INFO_LENGTH_MISMATCH
;
225 case ProcessVmCounters
:
229 /* older Windows versions don't have the PrivatePageCount field */
230 if (ProcessInformationLength
>= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
))
232 if (!ProcessInformation
)
233 ret
= STATUS_ACCESS_VIOLATION
;
234 else if (!ProcessHandle
)
235 ret
= STATUS_INVALID_HANDLE
;
238 /* FIXME : real data */
239 memset(&pvmi
, 0 , sizeof(VM_COUNTERS
));
241 len
= ProcessInformationLength
;
242 if (len
!= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
)) len
= sizeof(VM_COUNTERS
);
244 memcpy(ProcessInformation
, &pvmi
, min(ProcessInformationLength
,sizeof(VM_COUNTERS
)));
247 if (ProcessInformationLength
!= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
) &&
248 ProcessInformationLength
!= sizeof(VM_COUNTERS
))
249 ret
= STATUS_INFO_LENGTH_MISMATCH
;
254 ret
= STATUS_INFO_LENGTH_MISMATCH
;
260 KERNEL_USER_TIMES pti
;
262 if (ProcessInformationLength
>= sizeof(KERNEL_USER_TIMES
))
264 if (!ProcessInformation
)
265 ret
= STATUS_ACCESS_VIOLATION
;
266 else if (!ProcessHandle
)
267 ret
= STATUS_INVALID_HANDLE
;
270 /* FIXME : User- and KernelTime have to be implemented */
271 memset(&pti
, 0, sizeof(KERNEL_USER_TIMES
));
273 SERVER_START_REQ(get_process_info
)
275 req
->handle
= wine_server_obj_handle( ProcessHandle
);
276 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
278 pti
.CreateTime
.QuadPart
= reply
->start_time
;
279 pti
.ExitTime
.QuadPart
= reply
->end_time
;
284 memcpy(ProcessInformation
, &pti
, sizeof(KERNEL_USER_TIMES
));
285 len
= sizeof(KERNEL_USER_TIMES
);
288 if (ProcessInformationLength
> sizeof(KERNEL_USER_TIMES
))
289 ret
= STATUS_INFO_LENGTH_MISMATCH
;
293 len
= sizeof(KERNEL_USER_TIMES
);
294 ret
= STATUS_INFO_LENGTH_MISMATCH
;
298 case ProcessDebugPort
:
299 len
= sizeof(DWORD_PTR
);
300 if (ProcessInformationLength
== len
)
302 if (!ProcessInformation
)
303 ret
= STATUS_ACCESS_VIOLATION
;
304 else if (!ProcessHandle
)
305 ret
= STATUS_INVALID_HANDLE
;
308 SERVER_START_REQ(get_process_info
)
310 req
->handle
= wine_server_obj_handle( ProcessHandle
);
311 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
313 *(DWORD_PTR
*)ProcessInformation
= reply
->debugger_present
? ~(DWORD_PTR
)0 : 0;
320 ret
= STATUS_INFO_LENGTH_MISMATCH
;
322 case ProcessDebugFlags
:
324 if (ProcessInformationLength
== len
)
326 if (!ProcessInformation
)
327 ret
= STATUS_ACCESS_VIOLATION
;
328 else if (!ProcessHandle
)
329 ret
= STATUS_INVALID_HANDLE
;
332 SERVER_START_REQ(get_process_info
)
334 req
->handle
= wine_server_obj_handle( ProcessHandle
);
335 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
337 *(DWORD
*)ProcessInformation
= !reply
->debugger_present
;
344 ret
= STATUS_INFO_LENGTH_MISMATCH
;
346 case ProcessDefaultHardErrorMode
:
347 len
= sizeof(process_error_mode
);
348 if (ProcessInformationLength
== len
)
349 memcpy(ProcessInformation
, &process_error_mode
, len
);
351 ret
= STATUS_INFO_LENGTH_MISMATCH
;
353 case ProcessDebugObjectHandle
:
354 /* "These are not the debuggers you are looking for." *
355 * set it to 0 aka "no debugger" to satisfy copy protections */
356 len
= sizeof(HANDLE
);
357 if (ProcessInformationLength
== len
)
359 if (!ProcessInformation
)
360 ret
= STATUS_ACCESS_VIOLATION
;
361 else if (!ProcessHandle
)
362 ret
= STATUS_INVALID_HANDLE
;
365 memset(ProcessInformation
, 0, ProcessInformationLength
);
366 ret
= STATUS_PORT_NOT_SET
;
370 ret
= STATUS_INFO_LENGTH_MISMATCH
;
372 case ProcessHandleCount
:
373 if (ProcessInformationLength
>= 4)
375 if (!ProcessInformation
)
376 ret
= STATUS_ACCESS_VIOLATION
;
377 else if (!ProcessHandle
)
378 ret
= STATUS_INVALID_HANDLE
;
381 memset(ProcessInformation
, 0, 4);
385 if (ProcessInformationLength
> 4)
386 ret
= STATUS_INFO_LENGTH_MISMATCH
;
391 ret
= STATUS_INFO_LENGTH_MISMATCH
;
395 case ProcessAffinityMask
:
396 len
= sizeof(ULONG_PTR
);
397 if (ProcessInformationLength
== len
)
399 const ULONG_PTR system_mask
= get_system_affinity_mask();
401 SERVER_START_REQ(get_process_info
)
403 req
->handle
= wine_server_obj_handle( ProcessHandle
);
404 if (!(ret
= wine_server_call( req
)))
405 *(ULONG_PTR
*)ProcessInformation
= reply
->affinity
& system_mask
;
409 else ret
= STATUS_INFO_LENGTH_MISMATCH
;
412 case ProcessWow64Information
:
413 len
= sizeof(ULONG_PTR
);
414 if (ProcessInformationLength
!= len
) ret
= STATUS_INFO_LENGTH_MISMATCH
;
415 else if (!ProcessInformation
) ret
= STATUS_ACCESS_VIOLATION
;
416 else if(!ProcessHandle
) ret
= STATUS_INVALID_HANDLE
;
421 if (ProcessHandle
== GetCurrentProcess()) val
= is_wow64
;
422 else if (server_cpus
& (1 << CPU_x86_64
))
424 SERVER_START_REQ( get_process_info
)
426 req
->handle
= wine_server_obj_handle( ProcessHandle
);
427 if (!(ret
= wine_server_call( req
))) val
= (reply
->cpu
!= CPU_x86_64
);
431 *(ULONG_PTR
*)ProcessInformation
= val
;
434 case ProcessImageFileName
:
435 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
436 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
437 SERVER_START_REQ(get_dll_info
)
439 UNICODE_STRING
*image_file_name_str
= ProcessInformation
;
441 req
->handle
= wine_server_obj_handle( ProcessHandle
);
442 req
->base_address
= 0; /* main module */
443 wine_server_set_reply( req
, image_file_name_str
? image_file_name_str
+ 1 : NULL
,
444 ProcessInformationLength
> sizeof(UNICODE_STRING
) ? ProcessInformationLength
- sizeof(UNICODE_STRING
) : 0 );
445 ret
= wine_server_call( req
);
446 if (ret
== STATUS_BUFFER_TOO_SMALL
) ret
= STATUS_INFO_LENGTH_MISMATCH
;
448 len
= sizeof(UNICODE_STRING
) + reply
->filename_len
;
449 if (ret
== STATUS_SUCCESS
)
451 image_file_name_str
->MaximumLength
= image_file_name_str
->Length
= reply
->filename_len
;
452 image_file_name_str
->Buffer
= (PWSTR
)(image_file_name_str
+ 1);
457 case ProcessExecuteFlags
:
459 if (ProcessInformationLength
== len
)
460 *(ULONG
*)ProcessInformation
= execute_flags
;
462 ret
= STATUS_INFO_LENGTH_MISMATCH
;
465 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
466 ProcessHandle
,ProcessInformationClass
,
467 ProcessInformation
,ProcessInformationLength
,
469 ret
= STATUS_INVALID_INFO_CLASS
;
473 if (ReturnLength
) *ReturnLength
= len
;
478 /******************************************************************************
479 * NtSetInformationProcess [NTDLL.@]
480 * ZwSetInformationProcess [NTDLL.@]
482 NTSTATUS WINAPI
NtSetInformationProcess(
483 IN HANDLE ProcessHandle
,
484 IN PROCESSINFOCLASS ProcessInformationClass
,
485 IN PVOID ProcessInformation
,
486 IN ULONG ProcessInformationLength
)
488 NTSTATUS ret
= STATUS_SUCCESS
;
490 switch (ProcessInformationClass
)
492 case ProcessDefaultHardErrorMode
:
493 if (ProcessInformationLength
!= sizeof(UINT
)) return STATUS_INVALID_PARAMETER
;
494 process_error_mode
= *(UINT
*)ProcessInformation
;
496 case ProcessAffinityMask
:
498 const ULONG_PTR system_mask
= get_system_affinity_mask();
500 if (ProcessInformationLength
!= sizeof(DWORD_PTR
)) return STATUS_INVALID_PARAMETER
;
501 if (*(PDWORD_PTR
)ProcessInformation
& ~system_mask
)
502 return STATUS_INVALID_PARAMETER
;
503 if (!*(PDWORD_PTR
)ProcessInformation
)
504 return STATUS_INVALID_PARAMETER
;
505 SERVER_START_REQ( set_process_info
)
507 req
->handle
= wine_server_obj_handle( ProcessHandle
);
508 req
->affinity
= *(PDWORD_PTR
)ProcessInformation
;
509 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
510 ret
= wine_server_call( req
);
515 case ProcessPriorityClass
:
516 if (ProcessInformationLength
!= sizeof(PROCESS_PRIORITY_CLASS
))
517 return STATUS_INVALID_PARAMETER
;
520 PROCESS_PRIORITY_CLASS
* ppc
= ProcessInformation
;
522 SERVER_START_REQ( set_process_info
)
524 req
->handle
= wine_server_obj_handle( ProcessHandle
);
525 /* FIXME Foreground isn't used */
526 req
->priority
= ppc
->PriorityClass
;
527 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
528 ret
= wine_server_call( req
);
534 case ProcessExecuteFlags
:
535 if (ProcessInformationLength
!= sizeof(ULONG
))
536 return STATUS_INVALID_PARAMETER
;
537 else if (execute_flags
& MEM_EXECUTE_OPTION_PERMANENT
)
538 return STATUS_ACCESS_DENIED
;
542 switch (*(ULONG
*)ProcessInformation
& (MEM_EXECUTE_OPTION_ENABLE
|MEM_EXECUTE_OPTION_DISABLE
))
544 case MEM_EXECUTE_OPTION_ENABLE
:
547 case MEM_EXECUTE_OPTION_DISABLE
:
551 return STATUS_INVALID_PARAMETER
;
553 execute_flags
= *(ULONG
*)ProcessInformation
;
554 VIRTUAL_SetForceExec( enable
);
559 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
560 ProcessHandle
,ProcessInformationClass
,ProcessInformation
,
561 ProcessInformationLength
);
562 ret
= STATUS_NOT_IMPLEMENTED
;
568 /******************************************************************************
569 * NtFlushInstructionCache [NTDLL.@]
570 * ZwFlushInstructionCache [NTDLL.@]
572 NTSTATUS WINAPI
NtFlushInstructionCache(
573 IN HANDLE ProcessHandle
,
574 IN LPCVOID BaseAddress
,
580 #if defined(__x86_64__) || defined(__i386__)
581 TRACE("%p %p %ld - no-op on x86 and x86_64\n", ProcessHandle
, BaseAddress
, Size
);
583 FIXME("%p %p %ld\n", ProcessHandle
, BaseAddress
, Size
);
586 return STATUS_SUCCESS
;
589 /******************************************************************
590 * NtOpenProcess [NTDLL.@]
591 * ZwOpenProcess [NTDLL.@]
593 NTSTATUS WINAPI
NtOpenProcess(PHANDLE handle
, ACCESS_MASK access
,
594 const OBJECT_ATTRIBUTES
* attr
, const CLIENT_ID
* cid
)
598 SERVER_START_REQ( open_process
)
600 req
->pid
= HandleToULong(cid
->UniqueProcess
);
601 req
->access
= access
;
602 req
->attributes
= attr
? attr
->Attributes
: 0;
603 status
= wine_server_call( req
);
604 if (!status
) *handle
= wine_server_ptr_handle( reply
->handle
);