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) exit( exit_code );
67 /******************************************************************************
68 * RtlGetCurrentPeb [NTDLL.@]
71 PEB * WINAPI RtlGetCurrentPeb(void)
73 return NtCurrentTeb()->Peb;
76 /******************************************************************************
77 * RtlGetNtGlobalFlags [NTDLL.@]
80 ULONG WINAPI RtlGetNtGlobalFlags(void)
82 return NtCurrentTeb()->Peb->NtGlobalFlag;
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 );
103 #define UNIMPLEMENTED_INFO_CLASS(c) \
105 FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
106 ret = STATUS_INVALID_INFO_CLASS; \
109 /******************************************************************************
110 * NtQueryInformationProcess [NTDLL.@]
111 * ZwQueryInformationProcess [NTDLL.@]
114 NTSTATUS WINAPI NtQueryInformationProcess(
115 IN HANDLE ProcessHandle,
116 IN PROCESSINFOCLASS ProcessInformationClass,
117 OUT PVOID ProcessInformation,
118 IN ULONG ProcessInformationLength,
119 OUT PULONG ReturnLength)
121 NTSTATUS ret = STATUS_SUCCESS;
124 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
125 ProcessHandle,ProcessInformationClass,
126 ProcessInformation,ProcessInformationLength,
129 switch (ProcessInformationClass)
131 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
132 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
133 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
134 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
135 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
136 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
137 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
138 UNIMPLEMENTED_INFO_CLASS(ProcessDefaultHardErrorMode);
139 UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
140 UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
141 UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
142 UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
143 UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
144 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
145 UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
146 UNIMPLEMENTED_INFO_CLASS(ProcessAffinityMask);
147 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
148 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
149 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
150 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
151 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
152 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
153 UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
154 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
156 case ProcessBasicInformation:
158 PROCESS_BASIC_INFORMATION pbi;
159 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
161 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
163 if (!ProcessInformation)
164 ret = STATUS_ACCESS_VIOLATION;
165 else if (!ProcessHandle)
166 ret = STATUS_INVALID_HANDLE;
169 SERVER_START_REQ(get_process_info)
171 req->handle = wine_server_obj_handle( ProcessHandle );
172 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
174 pbi.ExitStatus = reply->exit_code;
175 pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
176 pbi.AffinityMask = reply->affinity & affinity_mask;
177 pbi.BasePriority = reply->priority;
178 pbi.UniqueProcessId = reply->pid;
179 pbi.InheritedFromUniqueProcessId = reply->ppid;
184 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
186 len = sizeof(PROCESS_BASIC_INFORMATION);
189 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
190 ret = STATUS_INFO_LENGTH_MISMATCH;
194 len = sizeof(PROCESS_BASIC_INFORMATION);
195 ret = STATUS_INFO_LENGTH_MISMATCH;
199 case ProcessIoCounters:
203 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
205 if (!ProcessInformation)
206 ret = STATUS_ACCESS_VIOLATION;
207 else if (!ProcessHandle)
208 ret = STATUS_INVALID_HANDLE;
211 /* FIXME : real data */
212 memset(&pii, 0 , sizeof(IO_COUNTERS));
214 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
216 len = sizeof(IO_COUNTERS);
219 if (ProcessInformationLength > sizeof(IO_COUNTERS))
220 ret = STATUS_INFO_LENGTH_MISMATCH;
224 len = sizeof(IO_COUNTERS);
225 ret = STATUS_INFO_LENGTH_MISMATCH;
229 case ProcessVmCounters:
233 /* older Windows versions don't have the PrivatePageCount field */
234 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
236 if (!ProcessInformation)
237 ret = STATUS_ACCESS_VIOLATION;
238 else if (!ProcessHandle)
239 ret = STATUS_INVALID_HANDLE;
242 /* FIXME : real data */
243 memset(&pvmi, 0 , sizeof(VM_COUNTERS));
245 len = ProcessInformationLength;
246 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
248 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
251 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
252 ProcessInformationLength != sizeof(VM_COUNTERS))
253 ret = STATUS_INFO_LENGTH_MISMATCH;
258 ret = STATUS_INFO_LENGTH_MISMATCH;
264 KERNEL_USER_TIMES pti;
266 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
268 if (!ProcessInformation)
269 ret = STATUS_ACCESS_VIOLATION;
270 else if (!ProcessHandle)
271 ret = STATUS_INVALID_HANDLE;
274 /* FIXME : User- and KernelTime have to be implemented */
275 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
277 SERVER_START_REQ(get_process_info)
279 req->handle = wine_server_obj_handle( ProcessHandle );
280 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
282 pti.CreateTime.QuadPart = reply->start_time;
283 pti.ExitTime.QuadPart = reply->end_time;
288 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
289 len = sizeof(KERNEL_USER_TIMES);
292 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
293 ret = STATUS_INFO_LENGTH_MISMATCH;
297 len = sizeof(KERNEL_USER_TIMES);
298 ret = STATUS_INFO_LENGTH_MISMATCH;
302 case ProcessDebugPort:
303 /* "These are not the debuggers you are looking for." *
304 * set it to 0 aka "no debugger" to satisfy copy protections */
306 if (ProcessInformationLength == len)
307 memset(ProcessInformation, 0, ProcessInformationLength);
309 ret = STATUS_INFO_LENGTH_MISMATCH;
311 case ProcessDebugObjectHandle:
312 /* "These are not the debuggers you are looking for." *
313 * set it to 0 aka "no debugger" to satisfy copy protections */
314 len = sizeof(HANDLE);
315 if (ProcessInformationLength == len)
317 if (!ProcessInformation)
318 ret = STATUS_ACCESS_VIOLATION;
319 else if (!ProcessHandle)
320 ret = STATUS_INVALID_HANDLE;
322 memset(ProcessInformation, 0, ProcessInformationLength);
325 ret = STATUS_INFO_LENGTH_MISMATCH;
327 case ProcessHandleCount:
328 if (ProcessInformationLength >= 4)
330 if (!ProcessInformation)
331 ret = STATUS_ACCESS_VIOLATION;
332 else if (!ProcessHandle)
333 ret = STATUS_INVALID_HANDLE;
336 memset(ProcessInformation, 0, 4);
340 if (ProcessInformationLength > 4)
341 ret = STATUS_INFO_LENGTH_MISMATCH;
346 ret = STATUS_INFO_LENGTH_MISMATCH;
349 case ProcessWow64Information:
351 if (ProcessInformationLength == len)
355 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
356 else if (server_cpus & (1 << CPU_x86_64))
358 SERVER_START_REQ( get_process_info )
360 req->handle = wine_server_obj_handle( ProcessHandle );
361 if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
365 *(DWORD *)ProcessInformation = val;
367 else ret = STATUS_INFO_LENGTH_MISMATCH;
369 case ProcessImageFileName:
370 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
371 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
372 SERVER_START_REQ(get_dll_info)
374 UNICODE_STRING *image_file_name_str = ProcessInformation;
376 req->handle = wine_server_obj_handle( ProcessHandle );
377 req->base_address = 0; /* main module */
378 wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
379 ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
380 ret = wine_server_call( req );
381 if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
383 len = sizeof(UNICODE_STRING) + reply->filename_len;
384 if (ret == STATUS_SUCCESS)
386 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
387 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
392 case ProcessExecuteFlags:
394 if (ProcessInformationLength == len)
395 *(ULONG *)ProcessInformation = execute_flags;
397 ret = STATUS_INFO_LENGTH_MISMATCH;
400 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
401 ProcessHandle,ProcessInformationClass,
402 ProcessInformation,ProcessInformationLength,
404 ret = STATUS_INVALID_INFO_CLASS;
408 if (ReturnLength) *ReturnLength = len;
413 /******************************************************************************
414 * NtSetInformationProcess [NTDLL.@]
415 * ZwSetInformationProcess [NTDLL.@]
417 NTSTATUS WINAPI NtSetInformationProcess(
418 IN HANDLE ProcessHandle,
419 IN PROCESSINFOCLASS ProcessInformationClass,
420 IN PVOID ProcessInformation,
421 IN ULONG ProcessInformationLength)
423 NTSTATUS ret = STATUS_SUCCESS;
425 switch (ProcessInformationClass)
427 case ProcessAffinityMask:
428 if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
429 if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
430 return STATUS_INVALID_PARAMETER;
431 if (!*(PDWORD_PTR)ProcessInformation)
432 return STATUS_INVALID_PARAMETER;
433 SERVER_START_REQ( set_process_info )
435 req->handle = wine_server_obj_handle( ProcessHandle );
436 req->affinity = *(PDWORD_PTR)ProcessInformation;
437 req->mask = SET_PROCESS_INFO_AFFINITY;
438 ret = wine_server_call( req );
442 case ProcessPriorityClass:
443 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
444 return STATUS_INVALID_PARAMETER;
447 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
449 SERVER_START_REQ( set_process_info )
451 req->handle = wine_server_obj_handle( ProcessHandle );
452 /* FIXME Foreground isn't used */
453 req->priority = ppc->PriorityClass;
454 req->mask = SET_PROCESS_INFO_PRIORITY;
455 ret = wine_server_call( req );
461 case ProcessExecuteFlags:
462 if (ProcessInformationLength != sizeof(ULONG))
463 return STATUS_INVALID_PARAMETER;
464 else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
465 return STATUS_ACCESS_DENIED;
469 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
471 case MEM_EXECUTE_OPTION_ENABLE:
474 case MEM_EXECUTE_OPTION_DISABLE:
478 return STATUS_INVALID_PARAMETER;
480 execute_flags = *(ULONG *)ProcessInformation;
481 VIRTUAL_SetForceExec( enable );
486 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
487 ProcessHandle,ProcessInformationClass,ProcessInformation,
488 ProcessInformationLength);
489 ret = STATUS_NOT_IMPLEMENTED;
495 /******************************************************************************
496 * NtFlushInstructionCache [NTDLL.@]
497 * ZwFlushInstructionCache [NTDLL.@]
499 NTSTATUS WINAPI NtFlushInstructionCache(
500 IN HANDLE ProcessHandle,
501 IN LPCVOID BaseAddress,
505 TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
507 FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
509 return STATUS_SUCCESS;
512 /******************************************************************
513 * NtOpenProcess [NTDLL.@]
514 * ZwOpenProcess [NTDLL.@]
516 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
517 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
521 SERVER_START_REQ( open_process )
523 req->pid = HandleToULong(cid->UniqueProcess);
524 req->access = access;
525 req->attributes = attr ? attr->Attributes : 0;
526 status = wine_server_call( req );
527 if (!status) *handle = wine_server_ptr_handle( reply->handle );