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 * __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 );
94 #define UNIMPLEMENTED_INFO_CLASS(c) \
96 FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
97 ret = STATUS_INVALID_INFO_CLASS; \
100 /******************************************************************************
101 * NtQueryInformationProcess [NTDLL.@]
102 * ZwQueryInformationProcess [NTDLL.@]
105 NTSTATUS WINAPI NtQueryInformationProcess(
106 IN HANDLE ProcessHandle,
107 IN PROCESSINFOCLASS ProcessInformationClass,
108 OUT PVOID ProcessInformation,
109 IN ULONG ProcessInformationLength,
110 OUT PULONG ReturnLength)
112 NTSTATUS ret = STATUS_SUCCESS;
115 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
116 ProcessHandle,ProcessInformationClass,
117 ProcessInformation,ProcessInformationLength,
120 switch (ProcessInformationClass)
122 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
123 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
124 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
125 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
126 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
127 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
128 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
129 UNIMPLEMENTED_INFO_CLASS(ProcessDefaultHardErrorMode);
130 UNIMPLEMENTED_INFO_CLASS(ProcessIoPortHandlers);
131 UNIMPLEMENTED_INFO_CLASS(ProcessPooledUsageAndLimits);
132 UNIMPLEMENTED_INFO_CLASS(ProcessWorkingSetWatch);
133 UNIMPLEMENTED_INFO_CLASS(ProcessUserModeIOPL);
134 UNIMPLEMENTED_INFO_CLASS(ProcessEnableAlignmentFaultFixup);
135 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityClass);
136 UNIMPLEMENTED_INFO_CLASS(ProcessWx86Information);
137 UNIMPLEMENTED_INFO_CLASS(ProcessAffinityMask);
138 UNIMPLEMENTED_INFO_CLASS(ProcessPriorityBoost);
139 UNIMPLEMENTED_INFO_CLASS(ProcessDeviceMap);
140 UNIMPLEMENTED_INFO_CLASS(ProcessSessionInformation);
141 UNIMPLEMENTED_INFO_CLASS(ProcessForegroundInformation);
142 UNIMPLEMENTED_INFO_CLASS(ProcessLUIDDeviceMapsEnabled);
143 UNIMPLEMENTED_INFO_CLASS(ProcessBreakOnTermination);
144 UNIMPLEMENTED_INFO_CLASS(ProcessDebugObjectHandle);
145 UNIMPLEMENTED_INFO_CLASS(ProcessDebugFlags);
146 UNIMPLEMENTED_INFO_CLASS(ProcessHandleTracing);
148 case ProcessBasicInformation:
150 PROCESS_BASIC_INFORMATION pbi;
151 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
153 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
155 if (!ProcessInformation)
156 ret = STATUS_ACCESS_VIOLATION;
157 else if (!ProcessHandle)
158 ret = STATUS_INVALID_HANDLE;
161 SERVER_START_REQ(get_process_info)
163 req->handle = wine_server_obj_handle( ProcessHandle );
164 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
166 pbi.ExitStatus = reply->exit_code;
167 pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
168 pbi.AffinityMask = reply->affinity & affinity_mask;
169 pbi.BasePriority = reply->priority;
170 pbi.UniqueProcessId = reply->pid;
171 pbi.InheritedFromUniqueProcessId = reply->ppid;
176 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
178 len = sizeof(PROCESS_BASIC_INFORMATION);
181 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
182 ret = STATUS_INFO_LENGTH_MISMATCH;
186 len = sizeof(PROCESS_BASIC_INFORMATION);
187 ret = STATUS_INFO_LENGTH_MISMATCH;
191 case ProcessIoCounters:
195 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
197 if (!ProcessInformation)
198 ret = STATUS_ACCESS_VIOLATION;
199 else if (!ProcessHandle)
200 ret = STATUS_INVALID_HANDLE;
203 /* FIXME : real data */
204 memset(&pii, 0 , sizeof(IO_COUNTERS));
206 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
208 len = sizeof(IO_COUNTERS);
211 if (ProcessInformationLength > sizeof(IO_COUNTERS))
212 ret = STATUS_INFO_LENGTH_MISMATCH;
216 len = sizeof(IO_COUNTERS);
217 ret = STATUS_INFO_LENGTH_MISMATCH;
221 case ProcessVmCounters:
225 /* older Windows versions don't have the PrivatePageCount field */
226 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
228 if (!ProcessInformation)
229 ret = STATUS_ACCESS_VIOLATION;
230 else if (!ProcessHandle)
231 ret = STATUS_INVALID_HANDLE;
234 /* FIXME : real data */
235 memset(&pvmi, 0 , sizeof(VM_COUNTERS));
237 len = ProcessInformationLength;
238 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
240 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
243 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
244 ProcessInformationLength != sizeof(VM_COUNTERS))
245 ret = STATUS_INFO_LENGTH_MISMATCH;
250 ret = STATUS_INFO_LENGTH_MISMATCH;
256 KERNEL_USER_TIMES pti;
258 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
260 if (!ProcessInformation)
261 ret = STATUS_ACCESS_VIOLATION;
262 else if (!ProcessHandle)
263 ret = STATUS_INVALID_HANDLE;
266 /* FIXME : User- and KernelTime have to be implemented */
267 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
269 SERVER_START_REQ(get_process_info)
271 req->handle = wine_server_obj_handle( ProcessHandle );
272 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
274 pti.CreateTime.QuadPart = reply->start_time;
275 pti.ExitTime.QuadPart = reply->end_time;
280 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
281 len = sizeof(KERNEL_USER_TIMES);
284 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
285 ret = STATUS_INFO_LENGTH_MISMATCH;
289 len = sizeof(KERNEL_USER_TIMES);
290 ret = STATUS_INFO_LENGTH_MISMATCH;
294 case ProcessDebugPort:
295 /* "These are not the debuggers you are looking for." *
296 * set it to 0 aka "no debugger" to satisfy copy protections */
298 if (ProcessInformationLength == len)
299 memset(ProcessInformation, 0, ProcessInformationLength);
301 ret = STATUS_INFO_LENGTH_MISMATCH;
303 case ProcessHandleCount:
304 if (ProcessInformationLength >= 4)
306 if (!ProcessInformation)
307 ret = STATUS_ACCESS_VIOLATION;
308 else if (!ProcessHandle)
309 ret = STATUS_INVALID_HANDLE;
312 memset(ProcessInformation, 0, 4);
316 if (ProcessInformationLength > 4)
317 ret = STATUS_INFO_LENGTH_MISMATCH;
322 ret = STATUS_INFO_LENGTH_MISMATCH;
325 case ProcessWow64Information:
327 if (ProcessInformationLength == len)
331 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
332 else if (server_cpus & (1 << CPU_x86_64))
334 SERVER_START_REQ( get_process_info )
336 req->handle = wine_server_obj_handle( ProcessHandle );
337 if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
341 *(DWORD *)ProcessInformation = val;
343 else ret = STATUS_INFO_LENGTH_MISMATCH;
345 case ProcessImageFileName:
346 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
347 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
348 SERVER_START_REQ(get_dll_info)
350 UNICODE_STRING *image_file_name_str = ProcessInformation;
352 req->handle = wine_server_obj_handle( ProcessHandle );
353 req->base_address = 0; /* main module */
354 wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
355 ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
356 ret = wine_server_call( req );
357 if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
359 len = sizeof(UNICODE_STRING) + reply->filename_len;
360 if (ret == STATUS_SUCCESS)
362 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
363 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
368 case ProcessExecuteFlags:
370 if (ProcessInformationLength == len)
371 *(ULONG *)ProcessInformation = execute_flags;
373 ret = STATUS_INFO_LENGTH_MISMATCH;
376 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
377 ProcessHandle,ProcessInformationClass,
378 ProcessInformation,ProcessInformationLength,
380 ret = STATUS_INVALID_INFO_CLASS;
384 if (ReturnLength) *ReturnLength = len;
389 /******************************************************************************
390 * NtSetInformationProcess [NTDLL.@]
391 * ZwSetInformationProcess [NTDLL.@]
393 NTSTATUS WINAPI NtSetInformationProcess(
394 IN HANDLE ProcessHandle,
395 IN PROCESSINFOCLASS ProcessInformationClass,
396 IN PVOID ProcessInformation,
397 IN ULONG ProcessInformationLength)
399 NTSTATUS ret = STATUS_SUCCESS;
401 switch (ProcessInformationClass)
403 case ProcessAffinityMask:
404 if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
405 if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
406 return STATUS_INVALID_PARAMETER;
407 SERVER_START_REQ( set_process_info )
409 req->handle = wine_server_obj_handle( ProcessHandle );
410 req->affinity = *(PDWORD_PTR)ProcessInformation;
411 req->mask = SET_PROCESS_INFO_AFFINITY;
412 ret = wine_server_call( req );
416 case ProcessPriorityClass:
417 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
418 return STATUS_INVALID_PARAMETER;
421 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
423 SERVER_START_REQ( set_process_info )
425 req->handle = wine_server_obj_handle( ProcessHandle );
426 /* FIXME Foreground isn't used */
427 req->priority = ppc->PriorityClass;
428 req->mask = SET_PROCESS_INFO_PRIORITY;
429 ret = wine_server_call( req );
435 case ProcessExecuteFlags:
436 if (ProcessInformationLength != sizeof(ULONG))
437 return STATUS_INVALID_PARAMETER;
438 else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
439 return STATUS_ACCESS_DENIED;
443 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
445 case MEM_EXECUTE_OPTION_ENABLE:
448 case MEM_EXECUTE_OPTION_DISABLE:
452 return STATUS_INVALID_PARAMETER;
454 execute_flags = *(ULONG *)ProcessInformation;
455 VIRTUAL_SetForceExec( enable );
460 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
461 ProcessHandle,ProcessInformationClass,ProcessInformation,
462 ProcessInformationLength);
463 ret = STATUS_NOT_IMPLEMENTED;
469 /******************************************************************************
470 * NtFlushInstructionCache [NTDLL.@]
471 * ZwFlushInstructionCache [NTDLL.@]
473 NTSTATUS WINAPI NtFlushInstructionCache(
474 IN HANDLE ProcessHandle,
475 IN LPCVOID BaseAddress,
479 TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
481 FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
483 return STATUS_SUCCESS;
486 /******************************************************************
487 * NtOpenProcess [NTDLL.@]
488 * ZwOpenProcess [NTDLL.@]
490 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
491 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
495 SERVER_START_REQ( open_process )
497 req->pid = HandleToULong(cid->UniqueProcess);
498 req->access = access;
499 req->attributes = attr ? attr->Attributes : 0;
500 status = wine_server_call( req );
501 if (!status) *handle = wine_server_ptr_handle( reply->handle );