ntdll: Define correct address space limits for x86_64.
[wine] / dlls / ntdll / process.c
1 /*
2  * NT basis DLL
3  *
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)
6  *
7  * Copyright 1996-1998 Marcus Meissner
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "wine/debug.h"
33 #include "windef.h"
34 #include "winternl.h"
35 #include "ntdll_misc.h"
36 #include "wine/server.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
39
40 static ULONG execute_flags = MEM_EXECUTE_OPTION_ENABLE;
41
42 /*
43  *      Process object
44  */
45
46 /******************************************************************************
47  *  NtTerminateProcess                  [NTDLL.@]
48  *
49  *  Native applications must kill themselves when done
50  */
51 NTSTATUS WINAPI NtTerminateProcess( HANDLE handle, LONG exit_code )
52 {
53     NTSTATUS ret;
54     BOOL self;
55     SERVER_START_REQ( terminate_process )
56     {
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;
61     }
62     SERVER_END_REQ;
63     if (self) exit( exit_code );
64     return ret;
65 }
66
67 /******************************************************************************
68  *  RtlGetCurrentPeb  [NTDLL.@]
69  *
70  */
71 PEB * WINAPI RtlGetCurrentPeb(void)
72 {
73     return NtCurrentTeb()->Peb;
74 }
75
76 /***********************************************************************
77  *           __wine_make_process_system   (NTDLL.@)
78  *
79  * Mark the current process as a system process.
80  * Returns the event that is signaled when all non-system processes have exited.
81  */
82 HANDLE CDECL __wine_make_process_system(void)
83 {
84     HANDLE ret = 0;
85     SERVER_START_REQ( make_process_system )
86     {
87         if (!wine_server_call( req )) ret = wine_server_ptr_handle( reply->event );
88     }
89     SERVER_END_REQ;
90     return ret;
91 }
92
93
94 #define UNIMPLEMENTED_INFO_CLASS(c) \
95     case c: \
96         FIXME("(process=%p) Unimplemented information class: " #c "\n", ProcessHandle); \
97         ret = STATUS_INVALID_INFO_CLASS; \
98         break
99
100 /******************************************************************************
101 *  NtQueryInformationProcess            [NTDLL.@]
102 *  ZwQueryInformationProcess            [NTDLL.@]
103 *
104 */
105 NTSTATUS WINAPI NtQueryInformationProcess(
106         IN HANDLE ProcessHandle,
107         IN PROCESSINFOCLASS ProcessInformationClass,
108         OUT PVOID ProcessInformation,
109         IN ULONG ProcessInformationLength,
110         OUT PULONG ReturnLength)
111 {
112     NTSTATUS ret = STATUS_SUCCESS;
113     ULONG len = 0;
114
115     TRACE("(%p,0x%08x,%p,0x%08x,%p)\n",
116           ProcessHandle,ProcessInformationClass,
117           ProcessInformation,ProcessInformationLength,
118           ReturnLength);
119
120     switch (ProcessInformationClass) 
121     {
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);
147
148     case ProcessBasicInformation:
149         {
150             PROCESS_BASIC_INFORMATION pbi;
151             const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
152
153             if (ProcessInformationLength >= sizeof(PROCESS_BASIC_INFORMATION))
154             {
155                 if (!ProcessInformation)
156                     ret = STATUS_ACCESS_VIOLATION;
157                 else if (!ProcessHandle)
158                     ret = STATUS_INVALID_HANDLE;
159                 else
160                 {
161                     SERVER_START_REQ(get_process_info)
162                     {
163                         req->handle = wine_server_obj_handle( ProcessHandle );
164                         if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
165                         {
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;
172                         }
173                     }
174                     SERVER_END_REQ;
175
176                     memcpy(ProcessInformation, &pbi, sizeof(PROCESS_BASIC_INFORMATION));
177
178                     len = sizeof(PROCESS_BASIC_INFORMATION);
179                 }
180
181                 if (ProcessInformationLength > sizeof(PROCESS_BASIC_INFORMATION))
182                     ret = STATUS_INFO_LENGTH_MISMATCH;
183             }
184             else
185             {
186                 len = sizeof(PROCESS_BASIC_INFORMATION);
187                 ret = STATUS_INFO_LENGTH_MISMATCH;
188             }
189         }
190         break;
191     case ProcessIoCounters:
192         {
193             IO_COUNTERS pii;
194
195             if (ProcessInformationLength >= sizeof(IO_COUNTERS))
196             {
197                 if (!ProcessInformation)
198                     ret = STATUS_ACCESS_VIOLATION;
199                 else if (!ProcessHandle)
200                     ret = STATUS_INVALID_HANDLE;
201                 else
202                 {
203                     /* FIXME : real data */
204                     memset(&pii, 0 , sizeof(IO_COUNTERS));
205
206                     memcpy(ProcessInformation, &pii, sizeof(IO_COUNTERS));
207
208                     len = sizeof(IO_COUNTERS);
209                 }
210
211                 if (ProcessInformationLength > sizeof(IO_COUNTERS))
212                     ret = STATUS_INFO_LENGTH_MISMATCH;
213             }
214             else
215             {
216                 len = sizeof(IO_COUNTERS);
217                 ret = STATUS_INFO_LENGTH_MISMATCH;
218             }
219         }
220         break;
221     case ProcessVmCounters:
222         {
223             VM_COUNTERS pvmi;
224
225             /* older Windows versions don't have the PrivatePageCount field */
226             if (ProcessInformationLength >= FIELD_OFFSET(VM_COUNTERS,PrivatePageCount))
227             {
228                 if (!ProcessInformation)
229                     ret = STATUS_ACCESS_VIOLATION;
230                 else if (!ProcessHandle)
231                     ret = STATUS_INVALID_HANDLE;
232                 else
233                 {
234                     /* FIXME : real data */
235                     memset(&pvmi, 0 , sizeof(VM_COUNTERS));
236
237                     len = ProcessInformationLength;
238                     if (len != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount)) len = sizeof(VM_COUNTERS);
239
240                     memcpy(ProcessInformation, &pvmi, min(ProcessInformationLength,sizeof(VM_COUNTERS)));
241                 }
242
243                 if (ProcessInformationLength != FIELD_OFFSET(VM_COUNTERS,PrivatePageCount) &&
244                     ProcessInformationLength != sizeof(VM_COUNTERS))
245                     ret = STATUS_INFO_LENGTH_MISMATCH;
246             }
247             else
248             {
249                 len = sizeof(pvmi);
250                 ret = STATUS_INFO_LENGTH_MISMATCH;
251             }
252         }
253         break;
254     case ProcessTimes:
255         {
256             KERNEL_USER_TIMES pti;
257
258             if (ProcessInformationLength >= sizeof(KERNEL_USER_TIMES))
259             {
260                 if (!ProcessInformation)
261                     ret = STATUS_ACCESS_VIOLATION;
262                 else if (!ProcessHandle)
263                     ret = STATUS_INVALID_HANDLE;
264                 else
265                 {
266                     /* FIXME : User- and KernelTime have to be implemented */
267                     memset(&pti, 0, sizeof(KERNEL_USER_TIMES));
268
269                     SERVER_START_REQ(get_process_info)
270                     {
271                       req->handle = wine_server_obj_handle( ProcessHandle );
272                       if ((ret = wine_server_call( req )) == STATUS_SUCCESS)
273                       {
274                           pti.CreateTime.QuadPart = reply->start_time;
275                           pti.ExitTime.QuadPart = reply->end_time;
276                       }
277                     }
278                     SERVER_END_REQ;
279
280                     memcpy(ProcessInformation, &pti, sizeof(KERNEL_USER_TIMES));
281                     len = sizeof(KERNEL_USER_TIMES);
282                 }
283
284                 if (ProcessInformationLength > sizeof(KERNEL_USER_TIMES))
285                     ret = STATUS_INFO_LENGTH_MISMATCH;
286             }
287             else
288             {
289                 len = sizeof(KERNEL_USER_TIMES);
290                 ret = STATUS_INFO_LENGTH_MISMATCH;
291             }
292         }
293         break;
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 */
297         len = 4;
298         if (ProcessInformationLength == len)
299             memset(ProcessInformation, 0, ProcessInformationLength);
300         else
301             ret = STATUS_INFO_LENGTH_MISMATCH;
302         break;
303     case ProcessHandleCount:
304         if (ProcessInformationLength >= 4)
305         {
306             if (!ProcessInformation)
307                 ret = STATUS_ACCESS_VIOLATION;
308             else if (!ProcessHandle)
309                 ret = STATUS_INVALID_HANDLE;
310             else
311             {
312                 memset(ProcessInformation, 0, 4);
313                 len = 4;
314             }
315
316             if (ProcessInformationLength > 4)
317                 ret = STATUS_INFO_LENGTH_MISMATCH;
318         }
319         else
320         {
321             len = 4;
322             ret = STATUS_INFO_LENGTH_MISMATCH;
323         }
324         break;
325     case ProcessWow64Information:
326         len = sizeof(DWORD);
327         if (ProcessInformationLength == len)
328         {
329             DWORD val = 0;
330
331             if (ProcessHandle == GetCurrentProcess()) val = is_wow64;
332             else if (server_cpus & (1 << CPU_x86_64))
333             {
334                 SERVER_START_REQ( get_process_info )
335                 {
336                     req->handle = wine_server_obj_handle( ProcessHandle );
337                     if (!(ret = wine_server_call( req ))) val = (reply->cpu != CPU_x86_64);
338                 }
339                 SERVER_END_REQ;
340             }
341             *(DWORD *)ProcessInformation = val;
342         }
343         else ret = STATUS_INFO_LENGTH_MISMATCH;
344         break;
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)
349         {
350             UNICODE_STRING *image_file_name_str = ProcessInformation;
351
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;
358
359             len = sizeof(UNICODE_STRING) + reply->filename_len;
360             if (ret == STATUS_SUCCESS)
361             {
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);
364             }
365         }
366         SERVER_END_REQ;
367         break;
368     case ProcessExecuteFlags:
369         len = sizeof(ULONG);
370         if (ProcessInformationLength == len)
371             *(ULONG *)ProcessInformation = execute_flags;
372         else
373             ret = STATUS_INFO_LENGTH_MISMATCH;
374         break;
375     default:
376         FIXME("(%p,info_class=%d,%p,0x%08x,%p) Unknown information class\n",
377               ProcessHandle,ProcessInformationClass,
378               ProcessInformation,ProcessInformationLength,
379               ReturnLength);
380         ret = STATUS_INVALID_INFO_CLASS;
381         break;
382     }
383
384     if (ReturnLength) *ReturnLength = len;
385     
386     return ret;
387 }
388
389 /******************************************************************************
390  * NtSetInformationProcess [NTDLL.@]
391  * ZwSetInformationProcess [NTDLL.@]
392  */
393 NTSTATUS WINAPI NtSetInformationProcess(
394         IN HANDLE ProcessHandle,
395         IN PROCESSINFOCLASS ProcessInformationClass,
396         IN PVOID ProcessInformation,
397         IN ULONG ProcessInformationLength)
398 {
399     NTSTATUS ret = STATUS_SUCCESS;
400
401     switch (ProcessInformationClass)
402     {
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 )
408         {
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 );
413         }
414         SERVER_END_REQ;
415         break;
416     case ProcessPriorityClass:
417         if (ProcessInformationLength != sizeof(PROCESS_PRIORITY_CLASS))
418             return STATUS_INVALID_PARAMETER;
419         else
420         {
421             PROCESS_PRIORITY_CLASS* ppc = ProcessInformation;
422
423             SERVER_START_REQ( set_process_info )
424             {
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 );
430             }
431             SERVER_END_REQ;
432         }
433         break;
434
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;
440         else
441         {
442             BOOL enable;
443             switch (*(ULONG *)ProcessInformation & (MEM_EXECUTE_OPTION_ENABLE|MEM_EXECUTE_OPTION_DISABLE))
444             {
445             case MEM_EXECUTE_OPTION_ENABLE:
446                 enable = TRUE;
447                 break;
448             case MEM_EXECUTE_OPTION_DISABLE:
449                 enable = FALSE;
450                 break;
451             default:
452                 return STATUS_INVALID_PARAMETER;
453             }
454             execute_flags = *(ULONG *)ProcessInformation;
455             VIRTUAL_SetForceExec( enable );
456         }
457         break;
458
459     default:
460         FIXME("(%p,0x%08x,%p,0x%08x) stub\n",
461               ProcessHandle,ProcessInformationClass,ProcessInformation,
462               ProcessInformationLength);
463         ret = STATUS_NOT_IMPLEMENTED;
464         break;
465     }
466     return ret;
467 }
468
469 /******************************************************************************
470  * NtFlushInstructionCache [NTDLL.@]
471  * ZwFlushInstructionCache [NTDLL.@]
472  */
473 NTSTATUS WINAPI NtFlushInstructionCache(
474         IN HANDLE ProcessHandle,
475         IN LPCVOID BaseAddress,
476         IN SIZE_T Size)
477 {
478 #ifdef __i386__
479     TRACE("%p %p %ld - no-op on x86\n", ProcessHandle, BaseAddress, Size );
480 #else
481     FIXME("%p %p %ld\n", ProcessHandle, BaseAddress, Size );
482 #endif
483     return STATUS_SUCCESS;
484 }
485
486 /******************************************************************
487  *              NtOpenProcess [NTDLL.@]
488  *              ZwOpenProcess [NTDLL.@]
489  */
490 NTSTATUS  WINAPI NtOpenProcess(PHANDLE handle, ACCESS_MASK access,
491                                const OBJECT_ATTRIBUTES* attr, const CLIENT_ID* cid)
492 {
493     NTSTATUS    status;
494
495     SERVER_START_REQ( open_process )
496     {
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 );
502     }
503     SERVER_END_REQ;
504     return status;
505 }