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
36 #define WIN32_NO_STATUS
37 #include "wine/debug.h"
40 #include "ntdll_misc.h"
41 #include "wine/server.h"
43 #ifdef HAVE_MACH_MACH_H
44 #include <mach/mach.h>
47 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
49 static ULONG execute_flags
= MEM_EXECUTE_OPTION_DISABLE
;
55 /******************************************************************************
56 * NtTerminateProcess [NTDLL.@]
58 * Native applications must kill themselves when done
60 NTSTATUS WINAPI
NtTerminateProcess( HANDLE handle
, LONG exit_code
)
64 SERVER_START_REQ( terminate_process
)
66 req
->handle
= wine_server_obj_handle( handle
);
67 req
->exit_code
= exit_code
;
68 ret
= wine_server_call( req
);
69 self
= !ret
&& reply
->self
;
72 if (self
&& handle
) _exit( exit_code
);
76 /******************************************************************************
77 * RtlGetCurrentPeb [NTDLL.@]
80 PEB
* WINAPI
RtlGetCurrentPeb(void)
82 return NtCurrentTeb()->Peb
;
85 /***********************************************************************
86 * __wine_make_process_system (NTDLL.@)
88 * Mark the current process as a system process.
89 * Returns the event that is signaled when all non-system processes have exited.
91 HANDLE CDECL
__wine_make_process_system(void)
94 SERVER_START_REQ( make_process_system
)
96 if (!wine_server_call( req
)) ret
= wine_server_ptr_handle( reply
->event
);
102 static UINT process_error_mode
;
104 #define UNIMPLEMENTED_INFO_CLASS(c) \
106 FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
107 ret = STATUS_INVALID_INFO_CLASS; \
110 ULONG_PTR
get_system_affinity_mask(void)
112 ULONG num_cpus
= NtCurrentTeb()->Peb
->NumberOfProcessors
;
113 if (num_cpus
>= sizeof(ULONG_PTR
) * 8) return ~(ULONG_PTR
)0;
114 return ((ULONG_PTR
)1 << num_cpus
) - 1;
117 #if defined(HAVE_MACH_MACH_H)
119 static void fill_VM_COUNTERS(VM_COUNTERS
* pvmi
)
121 #if defined(MACH_TASK_BASIC_INFO)
122 struct mach_task_basic_info info
;
123 mach_msg_type_number_t infoCount
= MACH_TASK_BASIC_INFO_COUNT
;
124 if(task_info(mach_task_self(), MACH_TASK_BASIC_INFO
, (task_info_t
)&info
, &infoCount
) == KERN_SUCCESS
)
126 pvmi
->VirtualSize
= info
.resident_size
+ info
.virtual_size
;
127 pvmi
->PagefileUsage
= info
.virtual_size
;
128 pvmi
->WorkingSetSize
= info
.resident_size
;
129 pvmi
->PeakWorkingSetSize
= info
.resident_size_max
;
136 static void fill_VM_COUNTERS(VM_COUNTERS
* pvmi
)
138 /* FIXME : real data */
143 /******************************************************************************
144 * NtQueryInformationProcess [NTDLL.@]
145 * ZwQueryInformationProcess [NTDLL.@]
148 NTSTATUS WINAPI
NtQueryInformationProcess(
149 IN HANDLE ProcessHandle
,
150 IN PROCESSINFOCLASS ProcessInformationClass
,
151 OUT PVOID ProcessInformation
,
152 IN ULONG ProcessInformationLength
,
153 OUT PULONG ReturnLength
)
155 NTSTATUS ret
= STATUS_SUCCESS
;
158 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
159 ProcessHandle
,ProcessInformationClass
,
160 ProcessInformation
,ProcessInformationLength
,
163 switch (ProcessInformationClass
)
165 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits
);
166 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority
);
167 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority
);
168 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort
);
169 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken
);
170 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation
);
171 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize
);
172 UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers
);
173 UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits
);
174 UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch
);
175 UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL
);
176 UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup
);
177 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass
);
178 UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information
);
179 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost
);
180 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap
);
181 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation
);
182 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation
);
183 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled
);
184 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination
);
185 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing
);
187 case ProcessBasicInformation
:
189 PROCESS_BASIC_INFORMATION pbi
;
190 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
192 if (ProcessInformationLength
>= sizeof(PROCESS_BASIC_INFORMATION
))
194 if (!ProcessInformation
)
195 ret
= STATUS_ACCESS_VIOLATION
;
196 else if (!ProcessHandle
)
197 ret
= STATUS_INVALID_HANDLE
;
200 SERVER_START_REQ(get_process_info
)
202 req
->handle
= wine_server_obj_handle( ProcessHandle
);
203 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
205 pbi
.ExitStatus
= reply
->exit_code
;
206 pbi
.PebBaseAddress
= wine_server_get_ptr( reply
->peb
);
207 pbi
.AffinityMask
= reply
->affinity
& affinity_mask
;
208 pbi
.BasePriority
= reply
->priority
;
209 pbi
.UniqueProcessId
= reply
->pid
;
210 pbi
.InheritedFromUniqueProcessId
= reply
->ppid
;
215 memcpy(ProcessInformation
, &pbi
, sizeof(PROCESS_BASIC_INFORMATION
));
217 len
= sizeof(PROCESS_BASIC_INFORMATION
);
220 if (ProcessInformationLength
> sizeof(PROCESS_BASIC_INFORMATION
))
221 ret
= STATUS_INFO_LENGTH_MISMATCH
;
225 len
= sizeof(PROCESS_BASIC_INFORMATION
);
226 ret
= STATUS_INFO_LENGTH_MISMATCH
;
230 case ProcessIoCounters
:
234 if (ProcessInformationLength
>= sizeof(IO_COUNTERS
))
236 if (!ProcessInformation
)
237 ret
= STATUS_ACCESS_VIOLATION
;
238 else if (!ProcessHandle
)
239 ret
= STATUS_INVALID_HANDLE
;
242 /* FIXME : real data */
243 memset(&pii
, 0 , sizeof(IO_COUNTERS
));
245 memcpy(ProcessInformation
, &pii
, sizeof(IO_COUNTERS
));
247 len
= sizeof(IO_COUNTERS
);
250 if (ProcessInformationLength
> sizeof(IO_COUNTERS
))
251 ret
= STATUS_INFO_LENGTH_MISMATCH
;
255 len
= sizeof(IO_COUNTERS
);
256 ret
= STATUS_INFO_LENGTH_MISMATCH
;
260 case ProcessVmCounters
:
264 /* older Windows versions don't have the PrivatePageCount field */
265 if (ProcessInformationLength
>= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
))
267 if (!ProcessInformation
)
268 ret
= STATUS_ACCESS_VIOLATION
;
269 else if (!ProcessHandle
)
270 ret
= STATUS_INVALID_HANDLE
;
273 memset(&pvmi
, 0 , sizeof(VM_COUNTERS
));
274 if (ProcessHandle
== GetCurrentProcess())
275 fill_VM_COUNTERS(&pvmi
);
277 FIXME("Need wineserver call to get VM counters for another process\n");
279 len
= ProcessInformationLength
;
280 if (len
!= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
)) len
= sizeof(VM_COUNTERS
);
282 memcpy(ProcessInformation
, &pvmi
, min(ProcessInformationLength
,sizeof(VM_COUNTERS
)));
285 if (ProcessInformationLength
!= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
) &&
286 ProcessInformationLength
!= sizeof(VM_COUNTERS
))
287 ret
= STATUS_INFO_LENGTH_MISMATCH
;
292 ret
= STATUS_INFO_LENGTH_MISMATCH
;
298 KERNEL_USER_TIMES pti
;
300 if (ProcessInformationLength
>= sizeof(KERNEL_USER_TIMES
))
302 if (!ProcessInformation
)
303 ret
= STATUS_ACCESS_VIOLATION
;
304 else if (!ProcessHandle
)
305 ret
= STATUS_INVALID_HANDLE
;
308 /* FIXME : User- and KernelTime have to be implemented */
309 memset(&pti
, 0, sizeof(KERNEL_USER_TIMES
));
311 SERVER_START_REQ(get_process_info
)
313 req
->handle
= wine_server_obj_handle( ProcessHandle
);
314 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
316 pti
.CreateTime
.QuadPart
= reply
->start_time
;
317 pti
.ExitTime
.QuadPart
= reply
->end_time
;
322 memcpy(ProcessInformation
, &pti
, sizeof(KERNEL_USER_TIMES
));
323 len
= sizeof(KERNEL_USER_TIMES
);
326 if (ProcessInformationLength
> sizeof(KERNEL_USER_TIMES
))
327 ret
= STATUS_INFO_LENGTH_MISMATCH
;
331 len
= sizeof(KERNEL_USER_TIMES
);
332 ret
= STATUS_INFO_LENGTH_MISMATCH
;
336 case ProcessDebugPort
:
337 len
= sizeof(DWORD_PTR
);
338 if (ProcessInformationLength
== len
)
340 if (!ProcessInformation
)
341 ret
= STATUS_ACCESS_VIOLATION
;
342 else if (!ProcessHandle
)
343 ret
= STATUS_INVALID_HANDLE
;
346 SERVER_START_REQ(get_process_info
)
348 req
->handle
= wine_server_obj_handle( ProcessHandle
);
349 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
351 *(DWORD_PTR
*)ProcessInformation
= reply
->debugger_present
? ~(DWORD_PTR
)0 : 0;
358 ret
= STATUS_INFO_LENGTH_MISMATCH
;
360 case ProcessDebugFlags
:
362 if (ProcessInformationLength
== len
)
364 if (!ProcessInformation
)
365 ret
= STATUS_ACCESS_VIOLATION
;
366 else if (!ProcessHandle
)
367 ret
= STATUS_INVALID_HANDLE
;
370 SERVER_START_REQ(get_process_info
)
372 req
->handle
= wine_server_obj_handle( ProcessHandle
);
373 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
375 *(DWORD
*)ProcessInformation
= reply
->debug_children
;
382 ret
= STATUS_INFO_LENGTH_MISMATCH
;
384 case ProcessDefaultHardErrorMode
:
385 len
= sizeof(process_error_mode
);
386 if (ProcessInformationLength
== len
)
387 memcpy(ProcessInformation
, &process_error_mode
, len
);
389 ret
= STATUS_INFO_LENGTH_MISMATCH
;
391 case ProcessDebugObjectHandle
:
392 /* "These are not the debuggers you are looking for." *
393 * set it to 0 aka "no debugger" to satisfy copy protections */
394 len
= sizeof(HANDLE
);
395 if (ProcessInformationLength
== len
)
397 if (!ProcessInformation
)
398 ret
= STATUS_ACCESS_VIOLATION
;
399 else if (!ProcessHandle
)
400 ret
= STATUS_INVALID_HANDLE
;
403 memset(ProcessInformation
, 0, ProcessInformationLength
);
404 ret
= STATUS_PORT_NOT_SET
;
408 ret
= STATUS_INFO_LENGTH_MISMATCH
;
410 case ProcessHandleCount
:
411 if (ProcessInformationLength
>= 4)
413 if (!ProcessInformation
)
414 ret
= STATUS_ACCESS_VIOLATION
;
415 else if (!ProcessHandle
)
416 ret
= STATUS_INVALID_HANDLE
;
419 memset(ProcessInformation
, 0, 4);
423 if (ProcessInformationLength
> 4)
424 ret
= STATUS_INFO_LENGTH_MISMATCH
;
429 ret
= STATUS_INFO_LENGTH_MISMATCH
;
433 case ProcessAffinityMask
:
434 len
= sizeof(ULONG_PTR
);
435 if (ProcessInformationLength
== len
)
437 const ULONG_PTR system_mask
= get_system_affinity_mask();
439 SERVER_START_REQ(get_process_info
)
441 req
->handle
= wine_server_obj_handle( ProcessHandle
);
442 if (!(ret
= wine_server_call( req
)))
443 *(ULONG_PTR
*)ProcessInformation
= reply
->affinity
& system_mask
;
447 else ret
= STATUS_INFO_LENGTH_MISMATCH
;
450 case ProcessWow64Information
:
451 len
= sizeof(ULONG_PTR
);
452 if (ProcessInformationLength
!= len
) ret
= STATUS_INFO_LENGTH_MISMATCH
;
453 else if (!ProcessInformation
) ret
= STATUS_ACCESS_VIOLATION
;
454 else if(!ProcessHandle
) ret
= STATUS_INVALID_HANDLE
;
459 if (ProcessHandle
== GetCurrentProcess()) val
= is_wow64
;
460 else if (server_cpus
& ((1 << CPU_x86_64
) | (1 << CPU_ARM64
)))
462 SERVER_START_REQ( get_process_info
)
464 req
->handle
= wine_server_obj_handle( ProcessHandle
);
465 if (!(ret
= wine_server_call( req
)))
466 val
= (reply
->cpu
!= CPU_x86_64
&& reply
->cpu
!= CPU_ARM64
);
470 *(ULONG_PTR
*)ProcessInformation
= val
;
473 case ProcessImageFileName
:
474 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
475 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
476 SERVER_START_REQ(get_dll_info
)
478 UNICODE_STRING
*image_file_name_str
= ProcessInformation
;
480 req
->handle
= wine_server_obj_handle( ProcessHandle
);
481 req
->base_address
= 0; /* main module */
482 wine_server_set_reply( req
, image_file_name_str
? image_file_name_str
+ 1 : NULL
,
483 ProcessInformationLength
> sizeof(UNICODE_STRING
) ? ProcessInformationLength
- sizeof(UNICODE_STRING
) : 0 );
484 ret
= wine_server_call( req
);
485 if (ret
== STATUS_BUFFER_TOO_SMALL
) ret
= STATUS_INFO_LENGTH_MISMATCH
;
487 len
= sizeof(UNICODE_STRING
) + reply
->filename_len
;
488 if (ret
== STATUS_SUCCESS
)
490 image_file_name_str
->MaximumLength
= image_file_name_str
->Length
= reply
->filename_len
;
491 image_file_name_str
->Buffer
= (PWSTR
)(image_file_name_str
+ 1);
496 case ProcessExecuteFlags
:
498 if (ProcessInformationLength
== len
)
499 *(ULONG
*)ProcessInformation
= execute_flags
;
501 ret
= STATUS_INFO_LENGTH_MISMATCH
;
504 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
505 ProcessHandle
,ProcessInformationClass
,
506 ProcessInformation
,ProcessInformationLength
,
508 ret
= STATUS_INVALID_INFO_CLASS
;
512 if (ReturnLength
) *ReturnLength
= len
;
517 /******************************************************************************
518 * NtSetInformationProcess [NTDLL.@]
519 * ZwSetInformationProcess [NTDLL.@]
521 NTSTATUS WINAPI
NtSetInformationProcess(
522 IN HANDLE ProcessHandle
,
523 IN PROCESSINFOCLASS ProcessInformationClass
,
524 IN PVOID ProcessInformation
,
525 IN ULONG ProcessInformationLength
)
527 NTSTATUS ret
= STATUS_SUCCESS
;
529 switch (ProcessInformationClass
)
531 case ProcessDefaultHardErrorMode
:
532 if (ProcessInformationLength
!= sizeof(UINT
)) return STATUS_INVALID_PARAMETER
;
533 process_error_mode
= *(UINT
*)ProcessInformation
;
535 case ProcessAffinityMask
:
537 const ULONG_PTR system_mask
= get_system_affinity_mask();
539 if (ProcessInformationLength
!= sizeof(DWORD_PTR
)) return STATUS_INVALID_PARAMETER
;
540 if (*(PDWORD_PTR
)ProcessInformation
& ~system_mask
)
541 return STATUS_INVALID_PARAMETER
;
542 if (!*(PDWORD_PTR
)ProcessInformation
)
543 return STATUS_INVALID_PARAMETER
;
544 SERVER_START_REQ( set_process_info
)
546 req
->handle
= wine_server_obj_handle( ProcessHandle
);
547 req
->affinity
= *(PDWORD_PTR
)ProcessInformation
;
548 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
549 ret
= wine_server_call( req
);
554 case ProcessPriorityClass
:
555 if (ProcessInformationLength
!= sizeof(PROCESS_PRIORITY_CLASS
))
556 return STATUS_INVALID_PARAMETER
;
559 PROCESS_PRIORITY_CLASS
* ppc
= ProcessInformation
;
561 SERVER_START_REQ( set_process_info
)
563 req
->handle
= wine_server_obj_handle( ProcessHandle
);
564 /* FIXME Foreground isn't used */
565 req
->priority
= ppc
->PriorityClass
;
566 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
567 ret
= wine_server_call( req
);
573 case ProcessExecuteFlags
:
574 if (ProcessInformationLength
!= sizeof(ULONG
))
575 return STATUS_INVALID_PARAMETER
;
576 else if (execute_flags
& MEM_EXECUTE_OPTION_PERMANENT
)
577 return STATUS_ACCESS_DENIED
;
581 switch (*(ULONG
*)ProcessInformation
& (MEM_EXECUTE_OPTION_ENABLE
|MEM_EXECUTE_OPTION_DISABLE
))
583 case MEM_EXECUTE_OPTION_ENABLE
:
586 case MEM_EXECUTE_OPTION_DISABLE
:
590 return STATUS_INVALID_PARAMETER
;
592 execute_flags
= *(ULONG
*)ProcessInformation
;
593 VIRTUAL_SetForceExec( enable
);
598 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
599 ProcessHandle
,ProcessInformationClass
,ProcessInformation
,
600 ProcessInformationLength
);
601 ret
= STATUS_NOT_IMPLEMENTED
;
607 /******************************************************************************
608 * NtFlushInstructionCache [NTDLL.@]
609 * ZwFlushInstructionCache [NTDLL.@]
611 NTSTATUS WINAPI
NtFlushInstructionCache(
612 IN HANDLE ProcessHandle
,
613 IN LPCVOID BaseAddress
,
619 #if defined(__x86_64__) || defined(__i386__)
620 TRACE("%p %p %ld - no-op on x86 and x86_64\n", ProcessHandle
, BaseAddress
, Size
);
622 FIXME("%p %p %ld\n", ProcessHandle
, BaseAddress
, Size
);
625 return STATUS_SUCCESS
;
628 /******************************************************************
629 * NtOpenProcess [NTDLL.@]
630 * ZwOpenProcess [NTDLL.@]
632 NTSTATUS WINAPI
NtOpenProcess(PHANDLE handle
, ACCESS_MASK access
,
633 const OBJECT_ATTRIBUTES
* attr
, const CLIENT_ID
* cid
)
637 SERVER_START_REQ( open_process
)
639 req
->pid
= HandleToULong(cid
->UniqueProcess
);
640 req
->access
= access
;
641 req
->attributes
= attr
? attr
->Attributes
: 0;
642 status
= wine_server_call( req
);
643 if (!status
) *handle
= wine_server_ptr_handle( reply
->handle
);
649 /******************************************************************************
653 NTSTATUS WINAPI
NtResumeProcess( HANDLE handle
)
655 FIXME("stub: %p\n", handle
);
656 return STATUS_NOT_IMPLEMENTED
;
659 /******************************************************************************
663 NTSTATUS WINAPI
NtSuspendProcess( HANDLE handle
)
665 FIXME("stub: %p\n", handle
);
666 return STATUS_NOT_IMPLEMENTED
;