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 );
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 /******************************************************************************
102 * NtQueryInformationProcess [NTDLL.@]
103 * ZwQueryInformationProcess [NTDLL.@]
106 NTSTATUS WINAPI NtQueryInformationProcess(
107 IN HANDLE ProcessHandle,
108 IN PROCESSINFOCLASS ProcessInformationClass,
109 OUT PVOID ProcessInformation,
110 IN ULONG ProcessInformationLength,
111 OUT PULONG ReturnLength)
113 NTSTATUS ret = STATUS_SUCCESS;
116 TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
117 ProcessHandle,ProcessInformationClass,
118 ProcessInformation,ProcessInformationLength,
121 switch (ProcessInformationClass)
123 UNIMPLEMENTED_INFO_CLASS(ProcessQuotaLimits);
124 UNIMPLEMENTED_INFO_CLASS(ProcessBasePriority);
125 UNIMPLEMENTED_INFO_CLASS(ProcessRaisePriority);
126 UNIMPLEMENTED_INFO_CLASS(ProcessExceptionPort);
127 UNIMPLEMENTED_INFO_CLASS(ProcessAccessToken);
128 UNIMPLEMENTED_INFO_CLASS(ProcessLdtInformation);
129 UNIMPLEMENTED_INFO_CLASS(ProcessLdtSize);
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(ProcessHandleTracing);
146 case ProcessBasicInformation:
148 PROCESS_BASIC_INFORMATION pbi;
149 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
151 if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
153 if (!ProcessInformation)
154 ret = STATUS_ACCESS_VIOLATION;
155 else if (!ProcessHandle)
156 ret = STATUS_INVALID_HANDLE;
159 SERVER_START_REQ(get_process_info)
161 req->handle = wine_server_obj_handle( ProcessHandle );
162 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
164 pbi.ExitStatus = reply->exit_code;
165 pbi.PebBaseAddress = wine_server_get_ptr( reply->peb );
166 pbi.AffinityMask = reply->affinity & affinity_mask;
167 pbi.BasePriority = reply->priority;
168 pbi.UniqueProcessId = reply->pid;
169 pbi.InheritedFromUniqueProcessId = reply->ppid;
174 memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
176 len = sizeof(PROCESS_BASIC_INFORMATION);
179 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
180 ret = STATUS_INFO_LENGTH_MISMATCH;
184 len = sizeof(PROCESS_BASIC_INFORMATION);
185 ret = STATUS_INFO_LENGTH_MISMATCH;
189 case ProcessIoCounters:
193 if (ProcessInformationLength >= sizeof(IO_COUNTERS))
195 if (!ProcessInformation)
196 ret = STATUS_ACCESS_VIOLATION;
197 else if (!ProcessHandle)
198 ret = STATUS_INVALID_HANDLE;
201 /* FIXME : real data */
202 memset(&pii, 0 , sizeof(IO_COUNTERS));
204 memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
206 len = sizeof(IO_COUNTERS);
209 if (ProcessInformationLength > sizeof(IO_COUNTERS))
210 ret = STATUS_INFO_LENGTH_MISMATCH;
214 len = sizeof(IO_COUNTERS);
215 ret = STATUS_INFO_LENGTH_MISMATCH;
219 case ProcessVmCounters:
223 /* older Windows versions don't have the PrivatePageCount field */
224 if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
226 if (!ProcessInformation)
227 ret = STATUS_ACCESS_VIOLATION;
228 else if (!ProcessHandle)
229 ret = STATUS_INVALID_HANDLE;
232 /* FIXME : real data */
233 memset(&pvmi, 0 , sizeof(VM_COUNTERS));
235 len = ProcessInformationLength;
236 if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
238 memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
241 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
242 ProcessInformationLength != sizeof(VM_COUNTERS))
243 ret = STATUS_INFO_LENGTH_MISMATCH;
248 ret = STATUS_INFO_LENGTH_MISMATCH;
254 KERNEL_USER_TIMES pti;
256 if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
258 if (!ProcessInformation)
259 ret = STATUS_ACCESS_VIOLATION;
260 else if (!ProcessHandle)
261 ret = STATUS_INVALID_HANDLE;
264 /* FIXME : User- and KernelTime have to be implemented */
265 memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
267 SERVER_START_REQ(get_process_info)
269 req->handle = wine_server_obj_handle( ProcessHandle );
270 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
272 pti.CreateTime.QuadPart = reply->start_time;
273 pti.ExitTime.QuadPart = reply->end_time;
278 memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
279 len = sizeof(KERNEL_USER_TIMES);
282 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
283 ret = STATUS_INFO_LENGTH_MISMATCH;
287 len = sizeof(KERNEL_USER_TIMES);
288 ret = STATUS_INFO_LENGTH_MISMATCH;
292 case ProcessDebugPort:
293 len = sizeof(DWORD_PTR);
294 if (ProcessInformationLength == len)
296 if (!ProcessInformation)
297 ret = STATUS_ACCESS_VIOLATION;
298 else if (!ProcessHandle)
299 ret = STATUS_INVALID_HANDLE;
302 SERVER_START_REQ(get_process_info)
304 req->handle = wine_server_obj_handle( ProcessHandle );
305 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
307 *(DWORD_PTR *)ProcessInformation = reply->debugger_present ? ~(DWORD_PTR)0 : 0;
314 ret = STATUS_INFO_LENGTH_MISMATCH;
316 case ProcessDebugFlags:
318 if (ProcessInformationLength == len)
320 if (!ProcessInformation)
321 ret = STATUS_ACCESS_VIOLATION;
322 else if (!ProcessHandle)
323 ret = STATUS_INVALID_HANDLE;
326 SERVER_START_REQ(get_process_info)
328 req->handle = wine_server_obj_handle( ProcessHandle );
329 if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
331 *(DWORD *)ProcessInformation = !reply->debugger_present;
338 ret = STATUS_INFO_LENGTH_MISMATCH;
340 case ProcessDefaultHardErrorMode:
341 len = sizeof(process_error_mode);
342 if (ProcessInformationLength == len)
343 memcpy(ProcessInformation, &process_error_mode, len);
345 ret = STATUS_INFO_LENGTH_MISMATCH;
347 case ProcessDebugObjectHandle:
348 /* "These are not the debuggers you are looking for." *
349 * set it to 0 aka "no debugger" to satisfy copy protections */
350 len = sizeof(HANDLE);
351 if (ProcessInformationLength == len)
353 if (!ProcessInformation)
354 ret = STATUS_ACCESS_VIOLATION;
355 else if (!ProcessHandle)
356 ret = STATUS_INVALID_HANDLE;
359 memset(ProcessInformation, 0, ProcessInformationLength);
360 ret = STATUS_PORT_NOT_SET;
364 ret = STATUS_INFO_LENGTH_MISMATCH;
366 case ProcessHandleCount:
367 if (ProcessInformationLength >= 4)
369 if (!ProcessInformation)
370 ret = STATUS_ACCESS_VIOLATION;
371 else if (!ProcessHandle)
372 ret = STATUS_INVALID_HANDLE;
375 memset(ProcessInformation, 0, 4);
379 if (ProcessInformationLength > 4)
380 ret = STATUS_INFO_LENGTH_MISMATCH;
385 ret = STATUS_INFO_LENGTH_MISMATCH;
388 case ProcessWow64Information:
390 if (ProcessInformationLength == len)
394 if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
395 else if (server_cpus & (1 << CPU_x86_64))
397 SERVER_START_REQ( get_process_info )
399 req->handle = wine_server_obj_handle( ProcessHandle );
400 if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
404 *(DWORD *)ProcessInformation = val;
406 else ret = STATUS_INFO_LENGTH_MISMATCH;
408 case ProcessImageFileName:
409 /* FIXME: this will return a DOS path. Windows returns an NT path. Changing this would require also changing kernel32.QueryFullProcessImageName.
410 * The latter may be harder because of the lack of RtlNtPathNameToDosPathName. */
411 SERVER_START_REQ(get_dll_info)
413 UNICODE_STRING *image_file_name_str = ProcessInformation;
415 req->handle = wine_server_obj_handle( ProcessHandle );
416 req->base_address = 0; /* main module */
417 wine_server_set_reply( req, image_file_name_str ? image_file_name_str + 1 : NULL,
418 ProcessInformationLength > sizeof(UNICODE_STRING) ? ProcessInformationLength - sizeof(UNICODE_STRING) : 0 );
419 ret = wine_server_call( req );
420 if (ret == STATUS_BUFFER_TOO_SMALL) ret = STATUS_INFO_LENGTH_MISMATCH;
422 len = sizeof(UNICODE_STRING) + reply->filename_len;
423 if (ret == STATUS_SUCCESS)
425 image_file_name_str->MaximumLength = image_file_name_str->Length = reply->filename_len;
426 image_file_name_str->Buffer = (PWSTR)(image_file_name_str + 1);
431 case ProcessExecuteFlags:
433 if (ProcessInformationLength == len)
434 *(ULONG *)ProcessInformation = execute_flags;
436 ret = STATUS_INFO_LENGTH_MISMATCH;
439 FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
440 ProcessHandle,ProcessInformationClass,
441 ProcessInformation,ProcessInformationLength,
443 ret = STATUS_INVALID_INFO_CLASS;
447 if (ReturnLength) *ReturnLength = len;
452 /******************************************************************************
453 * NtSetInformationProcess [NTDLL.@]
454 * ZwSetInformationProcess [NTDLL.@]
456 NTSTATUS WINAPI NtSetInformationProcess(
457 IN HANDLE ProcessHandle,
458 IN PROCESSINFOCLASS ProcessInformationClass,
459 IN PVOID ProcessInformation,
460 IN ULONG ProcessInformationLength)
462 NTSTATUS ret = STATUS_SUCCESS;
464 switch (ProcessInformationClass)
466 case ProcessDefaultHardErrorMode:
467 if (ProcessInformationLength != sizeof(UINT)) return STATUS_INVALID_PARAMETER;
468 process_error_mode = *(UINT *)ProcessInformation;
470 case ProcessAffinityMask:
471 if (ProcessInformationLength != sizeof(DWORD_PTR)) return STATUS_INVALID_PARAMETER;
472 if (*(PDWORD_PTR)ProcessInformation & ~(((DWORD_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1))
473 return STATUS_INVALID_PARAMETER;
474 if (!*(PDWORD_PTR)ProcessInformation)
475 return STATUS_INVALID_PARAMETER;
476 SERVER_START_REQ( set_process_info )
478 req->handle = wine_server_obj_handle( ProcessHandle );
479 req->affinity = *(PDWORD_PTR)ProcessInformation;
480 req->mask = SET_PROCESS_INFO_AFFINITY;
481 ret = wine_server_call( req );
485 case ProcessPriorityClass:
486 if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
487 return STATUS_INVALID_PARAMETER;
490 PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
492 SERVER_START_REQ( set_process_info )
494 req->handle = wine_server_obj_handle( ProcessHandle );
495 /* FIXME Foreground isn't used */
496 req->priority = ppc->PriorityClass;
497 req->mask = SET_PROCESS_INFO_PRIORITY;
498 ret = wine_server_call( req );
504 case ProcessExecuteFlags:
505 if (ProcessInformationLength != sizeof(ULONG))
506 return STATUS_INVALID_PARAMETER;
507 else if (execute_flags & MEM_EXECUTE_OPTION_PERMANENT)
508 return STATUS_ACCESS_DENIED;
512 switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
514 case MEM_EXECUTE_OPTION_ENABLE:
517 case MEM_EXECUTE_OPTION_DISABLE:
521 return STATUS_INVALID_PARAMETER;
523 execute_flags = *(ULONG *)ProcessInformation;
524 VIRTUAL_SetForceExec( enable );
529 FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
530 ProcessHandle,ProcessInformationClass,ProcessInformation,
531 ProcessInformationLength);
532 ret = STATUS_NOT_IMPLEMENTED;
538 /******************************************************************************
539 * NtFlushInstructionCache [NTDLL.@]
540 * ZwFlushInstructionCache [NTDLL.@]
542 NTSTATUS WINAPI NtFlushInstructionCache(
543 IN HANDLE ProcessHandle,
544 IN LPCVOID BaseAddress,
548 TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
550 FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
552 return STATUS_SUCCESS;
555 /******************************************************************
556 * NtOpenProcess [NTDLL.@]
557 * ZwOpenProcess [NTDLL.@]
559 NTSTATUS WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
560 const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
564 SERVER_START_REQ( open_process )
566 req->pid = HandleToULong(cid->UniqueProcess);
567 req->access = access;
568 req->attributes = attr ? attr->Attributes : 0;
569 status = wine_server_call( req );
570 if (!status) *handle = wine_server_ptr_handle( reply->handle );