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
)
142 f
= fopen("/proc/self/status", "r");
145 while (fgets(line
, sizeof(line
), f
))
147 if (sscanf(line
, "VmPeak: %lu", &value
))
148 pvmi
->PeakVirtualSize
= (ULONG64
)value
* 1024;
149 else if (sscanf(line
, "VmSize: %lu", &value
))
150 pvmi
->VirtualSize
= (ULONG64
)value
* 1024;
151 else if (sscanf(line
, "VmHWM: %lu", &value
))
152 pvmi
->PeakWorkingSetSize
= (ULONG64
)value
* 1024;
153 else if (sscanf(line
, "VmRSS: %lu", &value
))
154 pvmi
->WorkingSetSize
= (ULONG64
)value
* 1024;
155 else if (sscanf(line
, "RssAnon: %lu", &value
))
156 pvmi
->PagefileUsage
+= (ULONG64
)value
* 1024;
157 else if (sscanf(line
, "VmSwap: %lu", &value
))
158 pvmi
->PagefileUsage
+= (ULONG64
)value
* 1024;
160 pvmi
->PeakPagefileUsage
= pvmi
->PagefileUsage
;
167 static void fill_VM_COUNTERS(VM_COUNTERS
* pvmi
)
169 /* FIXME : real data */
174 /******************************************************************************
175 * NtQueryInformationProcess [NTDLL.@]
176 * ZwQueryInformationProcess [NTDLL.@]
179 NTSTATUS WINAPI
NtQueryInformationProcess(
180 IN HANDLE ProcessHandle
,
181 IN PROCESSINFOCLASS ProcessInformationClass
,
182 OUT PVOID ProcessInformation
,
183 IN ULONG ProcessInformationLength
,
184 OUT PULONG ReturnLength
)
186 NTSTATUS ret
= STATUS_SUCCESS
;
189 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
190 ProcessHandle
,ProcessInformationClass
,
191 ProcessInformation
,ProcessInformationLength
,
194 switch (ProcessInformationClass
)
196 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits
);
197 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority
);
198 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority
);
199 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort
);
200 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken
);
201 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation
);
202 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize
);
203 UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers
);
204 UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits
);
205 UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch
);
206 UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL
);
207 UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup
);
208 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass
);
209 UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information
);
210 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost
);
211 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap
);
212 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation
);
213 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation
);
214 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled
);
215 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination
);
216 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing
);
218 case ProcessBasicInformation
:
220 PROCESS_BASIC_INFORMATION pbi
;
221 const ULONG_PTR affinity_mask
= get_system_affinity_mask();
223 if (ProcessInformationLength
>= sizeof(PROCESS_BASIC_INFORMATION
))
225 if (!ProcessInformation
)
226 ret
= STATUS_ACCESS_VIOLATION
;
227 else if (!ProcessHandle
)
228 ret
= STATUS_INVALID_HANDLE
;
231 SERVER_START_REQ(get_process_info
)
233 req
->handle
= wine_server_obj_handle( ProcessHandle
);
234 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
236 pbi
.ExitStatus
= reply
->exit_code
;
237 pbi
.PebBaseAddress
= wine_server_get_ptr( reply
->peb
);
238 pbi
.AffinityMask
= reply
->affinity
& affinity_mask
;
239 pbi
.BasePriority
= reply
->priority
;
240 pbi
.UniqueProcessId
= reply
->pid
;
241 pbi
.InheritedFromUniqueProcessId
= reply
->ppid
;
246 memcpy(ProcessInformation
, &pbi
, sizeof(PROCESS_BASIC_INFORMATION
));
248 len
= sizeof(PROCESS_BASIC_INFORMATION
);
251 if (ProcessInformationLength
> sizeof(PROCESS_BASIC_INFORMATION
))
252 ret
= STATUS_INFO_LENGTH_MISMATCH
;
256 len
= sizeof(PROCESS_BASIC_INFORMATION
);
257 ret
= STATUS_INFO_LENGTH_MISMATCH
;
261 case ProcessIoCounters
:
265 if (ProcessInformationLength
>= sizeof(IO_COUNTERS
))
267 if (!ProcessInformation
)
268 ret
= STATUS_ACCESS_VIOLATION
;
269 else if (!ProcessHandle
)
270 ret
= STATUS_INVALID_HANDLE
;
273 /* FIXME : real data */
274 memset(&pii
, 0 , sizeof(IO_COUNTERS
));
276 memcpy(ProcessInformation
, &pii
, sizeof(IO_COUNTERS
));
278 len
= sizeof(IO_COUNTERS
);
281 if (ProcessInformationLength
> sizeof(IO_COUNTERS
))
282 ret
= STATUS_INFO_LENGTH_MISMATCH
;
286 len
= sizeof(IO_COUNTERS
);
287 ret
= STATUS_INFO_LENGTH_MISMATCH
;
291 case ProcessVmCounters
:
295 /* older Windows versions don't have the PrivatePageCount field */
296 if (ProcessInformationLength
>= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
))
298 if (!ProcessInformation
)
299 ret
= STATUS_ACCESS_VIOLATION
;
302 memset(&pvmi
, 0 , sizeof(VM_COUNTERS
));
303 if (ProcessHandle
== GetCurrentProcess())
304 fill_VM_COUNTERS(&pvmi
);
307 SERVER_START_REQ(get_process_vm_counters
)
309 req
->handle
= wine_server_obj_handle( ProcessHandle
);
310 if (!(ret
= wine_server_call( req
)))
312 pvmi
.PeakVirtualSize
= reply
->peak_virtual_size
;
313 pvmi
.VirtualSize
= reply
->virtual_size
;
314 pvmi
.PeakWorkingSetSize
= reply
->peak_working_set_size
;
315 pvmi
.WorkingSetSize
= reply
->working_set_size
;
316 pvmi
.PagefileUsage
= reply
->pagefile_usage
;
317 pvmi
.PeakPagefileUsage
= reply
->peak_pagefile_usage
;
324 len
= ProcessInformationLength
;
325 if (len
!= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
)) len
= sizeof(VM_COUNTERS
);
327 memcpy(ProcessInformation
, &pvmi
, min(ProcessInformationLength
,sizeof(VM_COUNTERS
)));
330 if (ProcessInformationLength
!= FIELD_OFFSET(VM_COUNTERS
,PrivatePageCount
) &&
331 ProcessInformationLength
!= sizeof(VM_COUNTERS
))
332 ret
= STATUS_INFO_LENGTH_MISMATCH
;
337 ret
= STATUS_INFO_LENGTH_MISMATCH
;
343 KERNEL_USER_TIMES pti
;
345 if (ProcessInformationLength
>= sizeof(KERNEL_USER_TIMES
))
347 if (!ProcessInformation
)
348 ret
= STATUS_ACCESS_VIOLATION
;
349 else if (!ProcessHandle
)
350 ret
= STATUS_INVALID_HANDLE
;
353 /* FIXME : User- and KernelTime have to be implemented */
354 memset(&pti
, 0, sizeof(KERNEL_USER_TIMES
));
356 SERVER_START_REQ(get_process_info
)
358 req
->handle
= wine_server_obj_handle( ProcessHandle
);
359 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
361 pti
.CreateTime
.QuadPart
= reply
->start_time
;
362 pti
.ExitTime
.QuadPart
= reply
->end_time
;
367 memcpy(ProcessInformation
, &pti
, sizeof(KERNEL_USER_TIMES
));
368 len
= sizeof(KERNEL_USER_TIMES
);
371 if (ProcessInformationLength
> sizeof(KERNEL_USER_TIMES
))
372 ret
= STATUS_INFO_LENGTH_MISMATCH
;
376 len
= sizeof(KERNEL_USER_TIMES
);
377 ret
= STATUS_INFO_LENGTH_MISMATCH
;
381 case ProcessDebugPort
:
382 len
= sizeof(DWORD_PTR
);
383 if (ProcessInformationLength
== len
)
385 if (!ProcessInformation
)
386 ret
= STATUS_ACCESS_VIOLATION
;
387 else if (!ProcessHandle
)
388 ret
= STATUS_INVALID_HANDLE
;
391 SERVER_START_REQ(get_process_info
)
393 req
->handle
= wine_server_obj_handle( ProcessHandle
);
394 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
396 *(DWORD_PTR
*)ProcessInformation
= reply
->debugger_present
? ~(DWORD_PTR
)0 : 0;
403 ret
= STATUS_INFO_LENGTH_MISMATCH
;
405 case ProcessDebugFlags
:
407 if (ProcessInformationLength
== len
)
409 if (!ProcessInformation
)
410 ret
= STATUS_ACCESS_VIOLATION
;
411 else if (!ProcessHandle
)
412 ret
= STATUS_INVALID_HANDLE
;
415 SERVER_START_REQ(get_process_info
)
417 req
->handle
= wine_server_obj_handle( ProcessHandle
);
418 if ((ret
= wine_server_call( req
)) == STATUS_SUCCESS
)
420 *(DWORD
*)ProcessInformation
= reply
->debug_children
;
427 ret
= STATUS_INFO_LENGTH_MISMATCH
;
429 case ProcessDefaultHardErrorMode
:
430 len
= sizeof(process_error_mode
);
431 if (ProcessInformationLength
== len
)
432 memcpy(ProcessInformation
, &process_error_mode
, len
);
434 ret
= STATUS_INFO_LENGTH_MISMATCH
;
436 case ProcessDebugObjectHandle
:
437 /* "These are not the debuggers you are looking for." *
438 * set it to 0 aka "no debugger" to satisfy copy protections */
439 len
= sizeof(HANDLE
);
440 if (ProcessInformationLength
== len
)
442 if (!ProcessInformation
)
443 ret
= STATUS_ACCESS_VIOLATION
;
444 else if (!ProcessHandle
)
445 ret
= STATUS_INVALID_HANDLE
;
448 memset(ProcessInformation
, 0, ProcessInformationLength
);
449 ret
= STATUS_PORT_NOT_SET
;
453 ret
= STATUS_INFO_LENGTH_MISMATCH
;
455 case ProcessHandleCount
:
456 if (ProcessInformationLength
>= 4)
458 if (!ProcessInformation
)
459 ret
= STATUS_ACCESS_VIOLATION
;
460 else if (!ProcessHandle
)
461 ret
= STATUS_INVALID_HANDLE
;
464 memset(ProcessInformation
, 0, 4);
468 if (ProcessInformationLength
> 4)
469 ret
= STATUS_INFO_LENGTH_MISMATCH
;
474 ret
= STATUS_INFO_LENGTH_MISMATCH
;
478 case ProcessAffinityMask
:
479 len
= sizeof(ULONG_PTR
);
480 if (ProcessInformationLength
== len
)
482 const ULONG_PTR system_mask
= get_system_affinity_mask();
484 SERVER_START_REQ(get_process_info
)
486 req
->handle
= wine_server_obj_handle( ProcessHandle
);
487 if (!(ret
= wine_server_call( req
)))
488 *(ULONG_PTR
*)ProcessInformation
= reply
->affinity
& system_mask
;
492 else ret
= STATUS_INFO_LENGTH_MISMATCH
;
495 case ProcessWow64Information
:
496 len
= sizeof(ULONG_PTR
);
497 if (ProcessInformationLength
!= len
) ret
= STATUS_INFO_LENGTH_MISMATCH
;
498 else if (!ProcessInformation
) ret
= STATUS_ACCESS_VIOLATION
;
499 else if(!ProcessHandle
) ret
= STATUS_INVALID_HANDLE
;
504 if (ProcessHandle
== GetCurrentProcess()) val
= is_wow64
;
505 else if (server_cpus
& ((1 << CPU_x86_64
) | (1 << CPU_ARM64
)))
507 SERVER_START_REQ( get_process_info
)
509 req
->handle
= wine_server_obj_handle( ProcessHandle
);
510 if (!(ret
= wine_server_call( req
)))
511 val
= (reply
->cpu
!= CPU_x86_64
&& reply
->cpu
!= CPU_ARM64
);
515 *(ULONG_PTR
*)ProcessInformation
= val
;
518 case ProcessImageFileName
:
519 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
520 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
521 SERVER_START_REQ(get_dll_info
)
523 UNICODE_STRING
*image_file_name_str
= ProcessInformation
;
525 req
->handle
= wine_server_obj_handle( ProcessHandle
);
526 req
->base_address
= 0; /* main module */
527 wine_server_set_reply( req
, image_file_name_str
? image_file_name_str
+ 1 : NULL
,
528 ProcessInformationLength
> sizeof(UNICODE_STRING
) ? ProcessInformationLength
- sizeof(UNICODE_STRING
) : 0 );
529 ret
= wine_server_call( req
);
530 if (ret
== STATUS_BUFFER_TOO_SMALL
) ret
= STATUS_INFO_LENGTH_MISMATCH
;
532 len
= sizeof(UNICODE_STRING
) + reply
->filename_len
;
533 if (ret
== STATUS_SUCCESS
)
535 image_file_name_str
->MaximumLength
= image_file_name_str
->Length
= reply
->filename_len
;
536 image_file_name_str
->Buffer
= (PWSTR
)(image_file_name_str
+ 1);
541 case ProcessExecuteFlags
:
543 if (ProcessInformationLength
== len
)
544 *(ULONG
*)ProcessInformation
= execute_flags
;
546 ret
= STATUS_INFO_LENGTH_MISMATCH
;
549 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
550 ProcessHandle
,ProcessInformationClass
,
551 ProcessInformation
,ProcessInformationLength
,
553 ret
= STATUS_INVALID_INFO_CLASS
;
557 if (ReturnLength
) *ReturnLength
= len
;
562 /******************************************************************************
563 * NtSetInformationProcess [NTDLL.@]
564 * ZwSetInformationProcess [NTDLL.@]
566 NTSTATUS WINAPI
NtSetInformationProcess(
567 IN HANDLE ProcessHandle
,
568 IN PROCESSINFOCLASS ProcessInformationClass
,
569 IN PVOID ProcessInformation
,
570 IN ULONG ProcessInformationLength
)
572 NTSTATUS ret
= STATUS_SUCCESS
;
574 switch (ProcessInformationClass
)
576 case ProcessDefaultHardErrorMode
:
577 if (ProcessInformationLength
!= sizeof(UINT
)) return STATUS_INVALID_PARAMETER
;
578 process_error_mode
= *(UINT
*)ProcessInformation
;
580 case ProcessAffinityMask
:
582 const ULONG_PTR system_mask
= get_system_affinity_mask();
584 if (ProcessInformationLength
!= sizeof(DWORD_PTR
)) return STATUS_INVALID_PARAMETER
;
585 if (*(PDWORD_PTR
)ProcessInformation
& ~system_mask
)
586 return STATUS_INVALID_PARAMETER
;
587 if (!*(PDWORD_PTR
)ProcessInformation
)
588 return STATUS_INVALID_PARAMETER
;
589 SERVER_START_REQ( set_process_info
)
591 req
->handle
= wine_server_obj_handle( ProcessHandle
);
592 req
->affinity
= *(PDWORD_PTR
)ProcessInformation
;
593 req
->mask
= SET_PROCESS_INFO_AFFINITY
;
594 ret
= wine_server_call( req
);
599 case ProcessPriorityClass
:
600 if (ProcessInformationLength
!= sizeof(PROCESS_PRIORITY_CLASS
))
601 return STATUS_INVALID_PARAMETER
;
604 PROCESS_PRIORITY_CLASS
* ppc
= ProcessInformation
;
606 SERVER_START_REQ( set_process_info
)
608 req
->handle
= wine_server_obj_handle( ProcessHandle
);
609 /* FIXME Foreground isn't used */
610 req
->priority
= ppc
->PriorityClass
;
611 req
->mask
= SET_PROCESS_INFO_PRIORITY
;
612 ret
= wine_server_call( req
);
618 case ProcessExecuteFlags
:
619 if (ProcessInformationLength
!= sizeof(ULONG
))
620 return STATUS_INVALID_PARAMETER
;
621 else if (execute_flags
& MEM_EXECUTE_OPTION_PERMANENT
)
622 return STATUS_ACCESS_DENIED
;
626 switch (*(ULONG
*)ProcessInformation
& (MEM_EXECUTE_OPTION_ENABLE
|MEM_EXECUTE_OPTION_DISABLE
))
628 case MEM_EXECUTE_OPTION_ENABLE
:
631 case MEM_EXECUTE_OPTION_DISABLE
:
635 return STATUS_INVALID_PARAMETER
;
637 execute_flags
= *(ULONG
*)ProcessInformation
;
638 VIRTUAL_SetForceExec( enable
);
643 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
644 ProcessHandle
,ProcessInformationClass
,ProcessInformation
,
645 ProcessInformationLength
);
646 ret
= STATUS_NOT_IMPLEMENTED
;
652 /******************************************************************************
653 * NtFlushInstructionCache [NTDLL.@]
654 * ZwFlushInstructionCache [NTDLL.@]
656 NTSTATUS WINAPI
NtFlushInstructionCache(
657 IN HANDLE ProcessHandle
,
658 IN LPCVOID BaseAddress
,
664 #if defined(__x86_64__) || defined(__i386__)
665 TRACE("%p %p %ld - no-op on x86 and x86_64\n", ProcessHandle
, BaseAddress
, Size
);
667 FIXME("%p %p %ld\n", ProcessHandle
, BaseAddress
, Size
);
670 return STATUS_SUCCESS
;
673 /******************************************************************
674 * NtOpenProcess [NTDLL.@]
675 * ZwOpenProcess [NTDLL.@]
677 NTSTATUS WINAPI
NtOpenProcess(PHANDLE handle
, ACCESS_MASK access
,
678 const OBJECT_ATTRIBUTES
* attr
, const CLIENT_ID
* cid
)
682 SERVER_START_REQ( open_process
)
684 req
->pid
= HandleToULong(cid
->UniqueProcess
);
685 req
->access
= access
;
686 req
->attributes
= attr
? attr
->Attributes
: 0;
687 status
= wine_server_call( req
);
688 if (!status
) *handle
= wine_server_ptr_handle( reply
->handle
);
694 /******************************************************************************
698 NTSTATUS WINAPI
NtResumeProcess( HANDLE handle
)
700 FIXME("stub: %p\n", handle
);
701 return STATUS_NOT_IMPLEMENTED
;
704 /******************************************************************************
708 NTSTATUS WINAPI
NtSuspendProcess( HANDLE handle
)
710 FIXME("stub: %p\n", handle
);
711 return STATUS_NOT_IMPLEMENTED
;