winspool.drv: Set the printer name in AddJobW.
[wine] / dlls / ntdll / nt.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 "config.h"
25 #include "wine/port.h"
26
27 #ifdef HAVE_SYS_PARAM_H
28 # include <sys/param.h>
29 #endif
30 #ifdef HAVE_SYS_SYSCTL_H
31 # include <sys/sysctl.h>
32 #endif
33 #ifdef HAVE_MACHINE_CPU_H
34 # include <machine/cpu.h>
35 #endif
36 #ifdef HAVE_MACH_MACHINE_H
37 # include <mach/machine.h>
38 #endif
39
40 #include <ctype.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 #endif
48 #include <time.h>
49
50 #define NONAMELESSUNION
51 #include "ntstatus.h"
52 #define WIN32_NO_STATUS
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
55 #include "windef.h"
56 #include "winternl.h"
57 #include "ntdll_misc.h"
58 #include "wine/server.h"
59 #include "ddk/wdm.h"
60
61 #ifdef __APPLE__
62 #include <mach/mach_init.h>
63 #include <mach/mach_host.h>
64 #include <mach/vm_map.h>
65 #endif
66
67 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
68
69 /*
70  *      Token
71  */
72
73 /******************************************************************************
74  *  NtDuplicateToken            [NTDLL.@]
75  *  ZwDuplicateToken            [NTDLL.@]
76  */
77 NTSTATUS WINAPI NtDuplicateToken(
78         IN HANDLE ExistingToken,
79         IN ACCESS_MASK DesiredAccess,
80         IN POBJECT_ATTRIBUTES ObjectAttributes,
81         IN SECURITY_IMPERSONATION_LEVEL ImpersonationLevel,
82         IN TOKEN_TYPE TokenType,
83         OUT PHANDLE NewToken)
84 {
85     NTSTATUS status;
86
87     TRACE("(%p,0x%08x,%s,0x%08x,0x%08x,%p)\n",
88           ExistingToken, DesiredAccess, debugstr_ObjectAttributes(ObjectAttributes),
89           ImpersonationLevel, TokenType, NewToken);
90
91     if (ObjectAttributes && ObjectAttributes->SecurityQualityOfService)
92     {
93         SECURITY_QUALITY_OF_SERVICE *SecurityQOS = ObjectAttributes->SecurityQualityOfService;
94         TRACE("ObjectAttributes->SecurityQualityOfService = {%d, %d, %d, %s}\n",
95             SecurityQOS->Length, SecurityQOS->ImpersonationLevel,
96             SecurityQOS->ContextTrackingMode,
97             SecurityQOS->EffectiveOnly ? "TRUE" : "FALSE");
98         ImpersonationLevel = SecurityQOS->ImpersonationLevel;
99     }
100
101     SERVER_START_REQ( duplicate_token )
102     {
103         req->handle              = wine_server_obj_handle( ExistingToken );
104         req->access              = DesiredAccess;
105         req->attributes          = ObjectAttributes ? ObjectAttributes->Attributes : 0;
106         req->primary             = (TokenType == TokenPrimary);
107         req->impersonation_level = ImpersonationLevel;
108         status = wine_server_call( req );
109         if (!status) *NewToken = wine_server_ptr_handle( reply->new_handle );
110     }
111     SERVER_END_REQ;
112
113     return status;
114 }
115
116 /******************************************************************************
117  *  NtOpenProcessToken          [NTDLL.@]
118  *  ZwOpenProcessToken          [NTDLL.@]
119  */
120 NTSTATUS WINAPI NtOpenProcessToken(
121         HANDLE ProcessHandle,
122         DWORD DesiredAccess,
123         HANDLE *TokenHandle)
124 {
125     return NtOpenProcessTokenEx( ProcessHandle, DesiredAccess, 0, TokenHandle );
126 }
127
128 /******************************************************************************
129  *  NtOpenProcessTokenEx   [NTDLL.@]
130  *  ZwOpenProcessTokenEx   [NTDLL.@]
131  */
132 NTSTATUS WINAPI NtOpenProcessTokenEx( HANDLE process, DWORD access, DWORD attributes,
133                                       HANDLE *handle )
134 {
135     NTSTATUS ret;
136
137     TRACE("(%p,0x%08x,0x%08x,%p)\n", process, access, attributes, handle);
138
139     SERVER_START_REQ( open_token )
140     {
141         req->handle     = wine_server_obj_handle( process );
142         req->access     = access;
143         req->attributes = attributes;
144         req->flags      = 0;
145         ret = wine_server_call( req );
146         if (!ret) *handle = wine_server_ptr_handle( reply->token );
147     }
148     SERVER_END_REQ;
149     return ret;
150 }
151
152 /******************************************************************************
153  *  NtOpenThreadToken           [NTDLL.@]
154  *  ZwOpenThreadToken           [NTDLL.@]
155  */
156 NTSTATUS WINAPI NtOpenThreadToken(
157         HANDLE ThreadHandle,
158         DWORD DesiredAccess,
159         BOOLEAN OpenAsSelf,
160         HANDLE *TokenHandle)
161 {
162     return NtOpenThreadTokenEx( ThreadHandle, DesiredAccess, OpenAsSelf, 0, TokenHandle );
163 }
164
165 /******************************************************************************
166  *  NtOpenThreadTokenEx   [NTDLL.@]
167  *  ZwOpenThreadTokenEx   [NTDLL.@]
168  */
169 NTSTATUS WINAPI NtOpenThreadTokenEx( HANDLE thread, DWORD access, BOOLEAN as_self, DWORD attributes,
170                                      HANDLE *handle )
171 {
172     NTSTATUS ret;
173
174     TRACE("(%p,0x%08x,%u,0x%08x,%p)\n", thread, access, as_self, attributes, handle );
175
176     SERVER_START_REQ( open_token )
177     {
178         req->handle     = wine_server_obj_handle( thread );
179         req->access     = access;
180         req->attributes = attributes;
181         req->flags      = OPEN_TOKEN_THREAD;
182         if (as_self) req->flags |= OPEN_TOKEN_AS_SELF;
183         ret = wine_server_call( req );
184         if (!ret) *handle = wine_server_ptr_handle( reply->token );
185     }
186     SERVER_END_REQ;
187
188     return ret;
189 }
190
191 /******************************************************************************
192  *  NtAdjustPrivilegesToken             [NTDLL.@]
193  *  ZwAdjustPrivilegesToken             [NTDLL.@]
194  *
195  * FIXME: parameters unsafe
196  */
197 NTSTATUS WINAPI NtAdjustPrivilegesToken(
198         IN HANDLE TokenHandle,
199         IN BOOLEAN DisableAllPrivileges,
200         IN PTOKEN_PRIVILEGES NewState,
201         IN DWORD BufferLength,
202         OUT PTOKEN_PRIVILEGES PreviousState,
203         OUT PDWORD ReturnLength)
204 {
205     NTSTATUS ret;
206
207     TRACE("(%p,0x%08x,%p,0x%08x,%p,%p)\n",
208         TokenHandle, DisableAllPrivileges, NewState, BufferLength, PreviousState, ReturnLength);
209
210     SERVER_START_REQ( adjust_token_privileges )
211     {
212         req->handle = wine_server_obj_handle( TokenHandle );
213         req->disable_all = DisableAllPrivileges;
214         req->get_modified_state = (PreviousState != NULL);
215         if (!DisableAllPrivileges)
216         {
217             wine_server_add_data( req, NewState->Privileges,
218                                   NewState->PrivilegeCount * sizeof(NewState->Privileges[0]) );
219         }
220         if (PreviousState && BufferLength >= FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ))
221             wine_server_set_reply( req, PreviousState->Privileges,
222                                    BufferLength - FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) );
223         ret = wine_server_call( req );
224         if (PreviousState)
225         {
226             *ReturnLength = reply->len + FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges );
227             PreviousState->PrivilegeCount = reply->len / sizeof(LUID_AND_ATTRIBUTES);
228         }
229     }
230     SERVER_END_REQ;
231
232     return ret;
233 }
234
235 /******************************************************************************
236 *  NtQueryInformationToken              [NTDLL.@]
237 *  ZwQueryInformationToken              [NTDLL.@]
238 *
239 * NOTES
240 *  Buffer for TokenUser:
241 *   0x00 TOKEN_USER the PSID field points to the SID
242 *   0x08 SID
243 *
244 */
245 NTSTATUS WINAPI NtQueryInformationToken(
246         HANDLE token,
247         TOKEN_INFORMATION_CLASS tokeninfoclass,
248         PVOID tokeninfo,
249         ULONG tokeninfolength,
250         PULONG retlen )
251 {
252     static const ULONG info_len [] =
253     {
254         0,
255         0,    /* TokenUser */
256         0,    /* TokenGroups */
257         0,    /* TokenPrivileges */
258         0,    /* TokenOwner */
259         0,    /* TokenPrimaryGroup */
260         0,    /* TokenDefaultDacl */
261         sizeof(TOKEN_SOURCE), /* TokenSource */
262         sizeof(TOKEN_TYPE),  /* TokenType */
263         sizeof(SECURITY_IMPERSONATION_LEVEL), /* TokenImpersonationLevel */
264         sizeof(TOKEN_STATISTICS), /* TokenStatistics */
265         0,    /* TokenRestrictedSids */
266         0,    /* TokenSessionId */
267         0,    /* TokenGroupsAndPrivileges */
268         0,    /* TokenSessionReference */
269         0,    /* TokenSandBoxInert */
270         0,    /* TokenAuditPolicy */
271         0,    /* TokenOrigin */
272         0,    /* TokenElevationType */
273         0,    /* TokenLinkedToken */
274         sizeof(TOKEN_ELEVATION), /* TokenElevation */
275         0,    /* TokenHasRestrictions */
276         0,    /* TokenAccessInformation */
277         0,    /* TokenVirtualizationAllowed */
278         0,    /* TokenVirtualizationEnabled */
279         0,    /* TokenIntegrityLevel */
280         0,    /* TokenUIAccess */
281         0,    /* TokenMandatoryPolicy */
282         0     /* TokenLogonSid */
283     };
284
285     ULONG len = 0;
286     NTSTATUS status = STATUS_SUCCESS;
287
288     TRACE("(%p,%d,%p,%d,%p)\n",
289           token,tokeninfoclass,tokeninfo,tokeninfolength,retlen);
290
291     if (tokeninfoclass < MaxTokenInfoClass)
292         len = info_len[tokeninfoclass];
293
294     if (retlen) *retlen = len;
295
296     if (tokeninfolength < len)
297         return STATUS_BUFFER_TOO_SMALL;
298
299     switch (tokeninfoclass)
300     {
301     case TokenUser:
302         SERVER_START_REQ( get_token_sid )
303         {
304             TOKEN_USER * tuser = tokeninfo;
305             PSID sid = tuser + 1;
306             DWORD sid_len = tokeninfolength < sizeof(TOKEN_USER) ? 0 : tokeninfolength - sizeof(TOKEN_USER);
307
308             req->handle = wine_server_obj_handle( token );
309             req->which_sid = tokeninfoclass;
310             wine_server_set_reply( req, sid, sid_len );
311             status = wine_server_call( req );
312             if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_USER);
313             if (status == STATUS_SUCCESS)
314             {
315                 tuser->User.Sid = sid;
316                 tuser->User.Attributes = 0;
317             }
318         }
319         SERVER_END_REQ;
320         break;
321     case TokenGroups:
322     {
323         char stack_buffer[256];
324         unsigned int server_buf_len = sizeof(stack_buffer);
325         void *buffer = stack_buffer;
326         BOOLEAN need_more_memory;
327
328         /* we cannot work out the size of the server buffer required for the
329          * input size, since there are two factors affecting how much can be
330          * stored in the buffer - number of groups and lengths of sids */
331         do
332         {
333             need_more_memory = FALSE;
334
335             SERVER_START_REQ( get_token_groups )
336             {
337                 TOKEN_GROUPS *groups = tokeninfo;
338
339                 req->handle = wine_server_obj_handle( token );
340                 wine_server_set_reply( req, buffer, server_buf_len );
341                 status = wine_server_call( req );
342                 if (status == STATUS_BUFFER_TOO_SMALL)
343                 {
344                     if (buffer == stack_buffer)
345                         buffer = RtlAllocateHeap(GetProcessHeap(), 0, reply->user_len);
346                     else
347                         buffer = RtlReAllocateHeap(GetProcessHeap(), 0, buffer, reply->user_len);
348                     if (!buffer) return STATUS_NO_MEMORY;
349
350                     server_buf_len = reply->user_len;
351                     need_more_memory = TRUE;
352                 }
353                 else if (status == STATUS_SUCCESS)
354                 {
355                     struct token_groups *tg = buffer;
356                     unsigned int *attr = (unsigned int *)(tg + 1);
357                     ULONG i;
358                     const int non_sid_portion = (sizeof(struct token_groups) + tg->count * sizeof(unsigned int));
359                     SID *sids = (SID *)((char *)tokeninfo + FIELD_OFFSET( TOKEN_GROUPS, Groups[tg->count] ));
360                     ULONG needed_bytes = FIELD_OFFSET( TOKEN_GROUPS, Groups[tg->count] ) +
361                         reply->user_len - non_sid_portion;
362
363                     if (retlen) *retlen = needed_bytes;
364
365                     if (needed_bytes <= tokeninfolength)
366                     {
367                         groups->GroupCount = tg->count;
368                         memcpy( sids, (char *)buffer + non_sid_portion,
369                                 reply->user_len - non_sid_portion );
370
371                         for (i = 0; i < tg->count; i++)
372                         {
373                             groups->Groups[i].Attributes = attr[i];
374                             groups->Groups[i].Sid = sids;
375                             sids = (SID *)((char *)sids + RtlLengthSid(sids));
376                         }
377                     }
378                     else status = STATUS_BUFFER_TOO_SMALL;
379                 }
380                 else if (retlen) *retlen = 0;
381             }
382             SERVER_END_REQ;
383         } while (need_more_memory);
384         if (buffer != stack_buffer) RtlFreeHeap(GetProcessHeap(), 0, buffer);
385         break;
386     }
387     case TokenPrimaryGroup:
388         SERVER_START_REQ( get_token_sid )
389         {
390             TOKEN_PRIMARY_GROUP *tgroup = tokeninfo;
391             PSID sid = tgroup + 1;
392             DWORD sid_len = tokeninfolength < sizeof(TOKEN_PRIMARY_GROUP) ? 0 : tokeninfolength - sizeof(TOKEN_PRIMARY_GROUP);
393
394             req->handle = wine_server_obj_handle( token );
395             req->which_sid = tokeninfoclass;
396             wine_server_set_reply( req, sid, sid_len );
397             status = wine_server_call( req );
398             if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_PRIMARY_GROUP);
399             if (status == STATUS_SUCCESS)
400                 tgroup->PrimaryGroup = sid;
401         }
402         SERVER_END_REQ;
403         break;
404     case TokenPrivileges:
405         SERVER_START_REQ( get_token_privileges )
406         {
407             TOKEN_PRIVILEGES *tpriv = tokeninfo;
408             req->handle = wine_server_obj_handle( token );
409             if (tpriv && tokeninfolength > FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ))
410                 wine_server_set_reply( req, tpriv->Privileges, tokeninfolength - FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) );
411             status = wine_server_call( req );
412             if (retlen) *retlen = FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) + reply->len;
413             if (tpriv) tpriv->PrivilegeCount = reply->len / sizeof(LUID_AND_ATTRIBUTES);
414         }
415         SERVER_END_REQ;
416         break;
417     case TokenOwner:
418         SERVER_START_REQ( get_token_sid )
419         {
420             TOKEN_OWNER *towner = tokeninfo;
421             PSID sid = towner + 1;
422             DWORD sid_len = tokeninfolength < sizeof(TOKEN_OWNER) ? 0 : tokeninfolength - sizeof(TOKEN_OWNER);
423
424             req->handle = wine_server_obj_handle( token );
425             req->which_sid = tokeninfoclass;
426             wine_server_set_reply( req, sid, sid_len );
427             status = wine_server_call( req );
428             if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_OWNER);
429             if (status == STATUS_SUCCESS)
430                 towner->Owner = sid;
431         }
432         SERVER_END_REQ;
433         break;
434     case TokenImpersonationLevel:
435         SERVER_START_REQ( get_token_impersonation_level )
436         {
437             SECURITY_IMPERSONATION_LEVEL *impersonation_level = tokeninfo;
438             req->handle = wine_server_obj_handle( token );
439             status = wine_server_call( req );
440             if (status == STATUS_SUCCESS)
441                 *impersonation_level = reply->impersonation_level;
442         }
443         SERVER_END_REQ;
444         break;
445     case TokenStatistics:
446         SERVER_START_REQ( get_token_statistics )
447         {
448             TOKEN_STATISTICS *statistics = tokeninfo;
449             req->handle = wine_server_obj_handle( token );
450             status = wine_server_call( req );
451             if (status == STATUS_SUCCESS)
452             {
453                 statistics->TokenId.LowPart  = reply->token_id.low_part;
454                 statistics->TokenId.HighPart = reply->token_id.high_part;
455                 statistics->AuthenticationId.LowPart  = 0; /* FIXME */
456                 statistics->AuthenticationId.HighPart = 0; /* FIXME */
457                 statistics->ExpirationTime.u.HighPart = 0x7fffffff;
458                 statistics->ExpirationTime.u.LowPart  = 0xffffffff;
459                 statistics->TokenType = reply->primary ? TokenPrimary : TokenImpersonation;
460                 statistics->ImpersonationLevel = reply->impersonation_level;
461
462                 /* kernel information not relevant to us */
463                 statistics->DynamicCharged = 0;
464                 statistics->DynamicAvailable = 0;
465
466                 statistics->GroupCount = reply->group_count;
467                 statistics->PrivilegeCount = reply->privilege_count;
468                 statistics->ModifiedId.LowPart  = reply->modified_id.low_part;
469                 statistics->ModifiedId.HighPart = reply->modified_id.high_part;
470             }
471         }
472         SERVER_END_REQ;
473         break;
474     case TokenType:
475         SERVER_START_REQ( get_token_statistics )
476         {
477             TOKEN_TYPE *token_type = tokeninfo;
478             req->handle = wine_server_obj_handle( token );
479             status = wine_server_call( req );
480             if (status == STATUS_SUCCESS)
481                 *token_type = reply->primary ? TokenPrimary : TokenImpersonation;
482         }
483         SERVER_END_REQ;
484         break;
485     case TokenDefaultDacl:
486         SERVER_START_REQ( get_token_default_dacl )
487         {
488             TOKEN_DEFAULT_DACL *default_dacl = tokeninfo;
489             ACL *acl = (ACL *)(default_dacl + 1);
490             DWORD acl_len;
491
492             if (tokeninfolength < sizeof(TOKEN_DEFAULT_DACL)) acl_len = 0;
493             else acl_len = tokeninfolength - sizeof(TOKEN_DEFAULT_DACL);
494
495             req->handle = wine_server_obj_handle( token );
496             wine_server_set_reply( req, acl, acl_len );
497             status = wine_server_call( req );
498
499             if (retlen) *retlen = reply->acl_len + sizeof(TOKEN_DEFAULT_DACL);
500             if (status == STATUS_SUCCESS)
501             {
502                 if (reply->acl_len)
503                     default_dacl->DefaultDacl = acl;
504                 else
505                     default_dacl->DefaultDacl = NULL;
506             }
507         }
508         SERVER_END_REQ;
509         break;
510     case TokenElevation:
511         {
512             TOKEN_ELEVATION *elevation = tokeninfo;
513             FIXME("QueryInformationToken( ..., TokenElevation, ...) semi-stub\n");
514             elevation->TokenIsElevated = TRUE;
515         }
516         break;
517     default:
518         {
519             ERR("Unhandled Token Information class %d!\n", tokeninfoclass);
520             return STATUS_NOT_IMPLEMENTED;
521         }
522     }
523     return status;
524 }
525
526 /******************************************************************************
527 *  NtSetInformationToken                [NTDLL.@]
528 *  ZwSetInformationToken                [NTDLL.@]
529 */
530 NTSTATUS WINAPI NtSetInformationToken(
531         HANDLE TokenHandle,
532         TOKEN_INFORMATION_CLASS TokenInformationClass,
533         PVOID TokenInformation,
534         ULONG TokenInformationLength)
535 {
536     NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
537
538     TRACE("%p %d %p %u\n", TokenHandle, TokenInformationClass,
539            TokenInformation, TokenInformationLength);
540
541     switch (TokenInformationClass)
542     {
543     case TokenDefaultDacl:
544         if (TokenInformationLength < sizeof(TOKEN_DEFAULT_DACL))
545         {
546             ret = STATUS_INFO_LENGTH_MISMATCH;
547             break;
548         }
549         if (!TokenInformation)
550         {
551             ret = STATUS_ACCESS_VIOLATION;
552             break;
553         }
554         SERVER_START_REQ( set_token_default_dacl )
555         {
556             ACL *acl = ((TOKEN_DEFAULT_DACL *)TokenInformation)->DefaultDacl;
557             WORD size;
558
559             if (acl) size = acl->AclSize;
560             else size = 0;
561
562             req->handle = wine_server_obj_handle( TokenHandle );
563             wine_server_add_data( req, acl, size );
564             ret = wine_server_call( req );
565         }
566         SERVER_END_REQ;
567         break;
568     default:
569         FIXME("unimplemented class %u\n", TokenInformationClass);
570         break;
571     }
572
573     return ret;
574 }
575
576 /******************************************************************************
577 *  NtAdjustGroupsToken          [NTDLL.@]
578 *  ZwAdjustGroupsToken          [NTDLL.@]
579 */
580 NTSTATUS WINAPI NtAdjustGroupsToken(
581         HANDLE TokenHandle,
582         BOOLEAN ResetToDefault,
583         PTOKEN_GROUPS NewState,
584         ULONG BufferLength,
585         PTOKEN_GROUPS PreviousState,
586         PULONG ReturnLength)
587 {
588     FIXME("%p %d %p %u %p %p\n", TokenHandle, ResetToDefault,
589           NewState, BufferLength, PreviousState, ReturnLength);
590     return STATUS_NOT_IMPLEMENTED;
591 }
592
593 /******************************************************************************
594 *  NtPrivilegeCheck             [NTDLL.@]
595 *  ZwPrivilegeCheck             [NTDLL.@]
596 */
597 NTSTATUS WINAPI NtPrivilegeCheck(
598     HANDLE ClientToken,
599     PPRIVILEGE_SET RequiredPrivileges,
600     PBOOLEAN Result)
601 {
602     NTSTATUS status;
603     SERVER_START_REQ( check_token_privileges )
604     {
605         req->handle = wine_server_obj_handle( ClientToken );
606         req->all_required = ((RequiredPrivileges->Control & PRIVILEGE_SET_ALL_NECESSARY) ? TRUE : FALSE);
607         wine_server_add_data( req, RequiredPrivileges->Privilege,
608             RequiredPrivileges->PrivilegeCount * sizeof(RequiredPrivileges->Privilege[0]) );
609         wine_server_set_reply( req, RequiredPrivileges->Privilege,
610             RequiredPrivileges->PrivilegeCount * sizeof(RequiredPrivileges->Privilege[0]) );
611
612         status = wine_server_call( req );
613
614         if (status == STATUS_SUCCESS)
615             *Result = (reply->has_privileges ? TRUE : FALSE);
616     }
617     SERVER_END_REQ;
618     return status;
619 }
620
621 /*
622  *      Section
623  */
624
625 /******************************************************************************
626  *  NtQuerySection      [NTDLL.@]
627  */
628 NTSTATUS WINAPI NtQuerySection(
629         IN HANDLE SectionHandle,
630         IN SECTION_INFORMATION_CLASS SectionInformationClass,
631         OUT PVOID SectionInformation,
632         IN ULONG Length,
633         OUT PULONG ResultLength)
634 {
635         FIXME("(%p,%d,%p,0x%08x,%p) stub!\n",
636         SectionHandle,SectionInformationClass,SectionInformation,Length,ResultLength);
637         return 0;
638 }
639
640 /*
641  *      ports
642  */
643
644 /******************************************************************************
645  *  NtCreatePort                [NTDLL.@]
646  *  ZwCreatePort                [NTDLL.@]
647  */
648 NTSTATUS WINAPI NtCreatePort(PHANDLE PortHandle,POBJECT_ATTRIBUTES ObjectAttributes,
649                              ULONG MaxConnectInfoLength,ULONG MaxDataLength,PULONG reserved)
650 {
651   FIXME("(%p,%p,%u,%u,%p),stub!\n",PortHandle,ObjectAttributes,
652         MaxConnectInfoLength,MaxDataLength,reserved);
653   return STATUS_NOT_IMPLEMENTED;
654 }
655
656 /******************************************************************************
657  *  NtConnectPort               [NTDLL.@]
658  *  ZwConnectPort               [NTDLL.@]
659  */
660 NTSTATUS WINAPI NtConnectPort(
661         PHANDLE PortHandle,
662         PUNICODE_STRING PortName,
663         PSECURITY_QUALITY_OF_SERVICE SecurityQos,
664         PLPC_SECTION_WRITE WriteSection,
665         PLPC_SECTION_READ ReadSection,
666         PULONG MaximumMessageLength,
667         PVOID ConnectInfo,
668         PULONG pConnectInfoLength)
669 {
670     FIXME("(%p,%s,%p,%p,%p,%p,%p,%p),stub!\n",
671           PortHandle,debugstr_w(PortName->Buffer),SecurityQos,
672           WriteSection,ReadSection,MaximumMessageLength,ConnectInfo,
673           pConnectInfoLength);
674     if (ConnectInfo && pConnectInfoLength)
675         TRACE("\tMessage = %s\n",debugstr_an(ConnectInfo,*pConnectInfoLength));
676     return STATUS_NOT_IMPLEMENTED;
677 }
678
679 /******************************************************************************
680  *  NtSecureConnectPort                (NTDLL.@)
681  *  ZwSecureConnectPort                (NTDLL.@)
682  */
683 NTSTATUS WINAPI NtSecureConnectPort(
684         PHANDLE PortHandle,
685         PUNICODE_STRING PortName,
686         PSECURITY_QUALITY_OF_SERVICE SecurityQos,
687         PLPC_SECTION_WRITE WriteSection,
688         PSID pSid,
689         PLPC_SECTION_READ ReadSection,
690         PULONG MaximumMessageLength,
691         PVOID ConnectInfo,
692         PULONG pConnectInfoLength)
693 {
694     FIXME("(%p,%s,%p,%p,%p,%p,%p,%p,%p),stub!\n",
695           PortHandle,debugstr_w(PortName->Buffer),SecurityQos,
696           WriteSection,pSid,ReadSection,MaximumMessageLength,ConnectInfo,
697           pConnectInfoLength);
698     return STATUS_NOT_IMPLEMENTED;
699 }
700
701 /******************************************************************************
702  *  NtListenPort                [NTDLL.@]
703  *  ZwListenPort                [NTDLL.@]
704  */
705 NTSTATUS WINAPI NtListenPort(HANDLE PortHandle,PLPC_MESSAGE pLpcMessage)
706 {
707   FIXME("(%p,%p),stub!\n",PortHandle,pLpcMessage);
708   return STATUS_NOT_IMPLEMENTED;
709 }
710
711 /******************************************************************************
712  *  NtAcceptConnectPort [NTDLL.@]
713  *  ZwAcceptConnectPort [NTDLL.@]
714  */
715 NTSTATUS WINAPI NtAcceptConnectPort(
716         PHANDLE PortHandle,
717         ULONG PortIdentifier,
718         PLPC_MESSAGE pLpcMessage,
719         BOOLEAN Accept,
720         PLPC_SECTION_WRITE WriteSection,
721         PLPC_SECTION_READ ReadSection)
722 {
723   FIXME("(%p,%u,%p,%d,%p,%p),stub!\n",
724         PortHandle,PortIdentifier,pLpcMessage,Accept,WriteSection,ReadSection);
725   return STATUS_NOT_IMPLEMENTED;
726 }
727
728 /******************************************************************************
729  *  NtCompleteConnectPort       [NTDLL.@]
730  *  ZwCompleteConnectPort       [NTDLL.@]
731  */
732 NTSTATUS WINAPI NtCompleteConnectPort(HANDLE PortHandle)
733 {
734   FIXME("(%p),stub!\n",PortHandle);
735   return STATUS_NOT_IMPLEMENTED;
736 }
737
738 /******************************************************************************
739  *  NtRegisterThreadTerminatePort       [NTDLL.@]
740  *  ZwRegisterThreadTerminatePort       [NTDLL.@]
741  */
742 NTSTATUS WINAPI NtRegisterThreadTerminatePort(HANDLE PortHandle)
743 {
744   FIXME("(%p),stub!\n",PortHandle);
745   return STATUS_NOT_IMPLEMENTED;
746 }
747
748 /******************************************************************************
749  *  NtRequestWaitReplyPort              [NTDLL.@]
750  *  ZwRequestWaitReplyPort              [NTDLL.@]
751  */
752 NTSTATUS WINAPI NtRequestWaitReplyPort(
753         HANDLE PortHandle,
754         PLPC_MESSAGE pLpcMessageIn,
755         PLPC_MESSAGE pLpcMessageOut)
756 {
757   FIXME("(%p,%p,%p),stub!\n",PortHandle,pLpcMessageIn,pLpcMessageOut);
758   if(pLpcMessageIn)
759   {
760     TRACE("Message to send:\n");
761     TRACE("\tDataSize            = %u\n",pLpcMessageIn->DataSize);
762     TRACE("\tMessageSize         = %u\n",pLpcMessageIn->MessageSize);
763     TRACE("\tMessageType         = %u\n",pLpcMessageIn->MessageType);
764     TRACE("\tVirtualRangesOffset = %u\n",pLpcMessageIn->VirtualRangesOffset);
765     TRACE("\tClientId.UniqueProcess = %p\n",pLpcMessageIn->ClientId.UniqueProcess);
766     TRACE("\tClientId.UniqueThread  = %p\n",pLpcMessageIn->ClientId.UniqueThread);
767     TRACE("\tMessageId           = %lu\n",pLpcMessageIn->MessageId);
768     TRACE("\tSectionSize         = %lu\n",pLpcMessageIn->SectionSize);
769     TRACE("\tData                = %s\n",
770       debugstr_an((const char*)pLpcMessageIn->Data,pLpcMessageIn->DataSize));
771   }
772   return STATUS_NOT_IMPLEMENTED;
773 }
774
775 /******************************************************************************
776  *  NtReplyWaitReceivePort      [NTDLL.@]
777  *  ZwReplyWaitReceivePort      [NTDLL.@]
778  */
779 NTSTATUS WINAPI NtReplyWaitReceivePort(
780         HANDLE PortHandle,
781         PULONG PortIdentifier,
782         PLPC_MESSAGE ReplyMessage,
783         PLPC_MESSAGE Message)
784 {
785   FIXME("(%p,%p,%p,%p),stub!\n",PortHandle,PortIdentifier,ReplyMessage,Message);
786   return STATUS_NOT_IMPLEMENTED;
787 }
788
789 /*
790  *      Misc
791  */
792
793  /******************************************************************************
794  *  NtSetIntervalProfile        [NTDLL.@]
795  *  ZwSetIntervalProfile        [NTDLL.@]
796  */
797 NTSTATUS WINAPI NtSetIntervalProfile(
798         ULONG Interval,
799         KPROFILE_SOURCE Source)
800 {
801     FIXME("%u,%d\n", Interval, Source);
802     return STATUS_SUCCESS;
803 }
804
805 static  SYSTEM_CPU_INFORMATION cached_sci;
806 static  ULONGLONG cpuHz = 1000000000; /* default to a 1GHz */
807
808 #define AUTH    0x68747541      /* "Auth" */
809 #define ENTI    0x69746e65      /* "enti" */
810 #define CAMD    0x444d4163      /* "cAMD" */
811
812 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
813  * We are compiled with -fPIC, so we can't clobber ebx.
814  */
815 static inline void do_cpuid(unsigned int ax, unsigned int *p)
816 {
817 #ifdef __i386__
818         __asm__("pushl %%ebx\n\t"
819                 "cpuid\n\t"
820                 "movl %%ebx, %%esi\n\t"
821                 "popl %%ebx"
822                 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
823                 :  "0" (ax));
824 #endif
825 }
826
827 /* From xf86info havecpuid.c 1.11 */
828 static inline int have_cpuid(void)
829 {
830 #ifdef __i386__
831         unsigned int f1, f2;
832         __asm__("pushfl\n\t"
833                 "pushfl\n\t"
834                 "popl %0\n\t"
835                 "movl %0,%1\n\t"
836                 "xorl %2,%0\n\t"
837                 "pushl %0\n\t"
838                 "popfl\n\t"
839                 "pushfl\n\t"
840                 "popl %0\n\t"
841                 "popfl"
842                 : "=&r" (f1), "=&r" (f2)
843                 : "ir" (0x00200000));
844         return ((f1^f2) & 0x00200000) != 0;
845 #else
846         return 0;
847 #endif
848 }
849
850 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
851 {
852     unsigned int regs[4], regs2[4];
853
854     if (!have_cpuid()) return;
855
856     do_cpuid(0x00000000, regs);  /* get standard cpuid level and vendor name */
857     if (regs[0]>=0x00000001)   /* Check for supported cpuid version */
858     {
859         do_cpuid(0x00000001, regs2); /* get cpu features */
860         switch ((regs2[0] >> 8) & 0xf)  /* cpu family */
861         {
862         case 3: info->Level = 3;        break;
863         case 4: info->Level = 4;        break;
864         case 5: info->Level = 5;        break;
865         case 15: /* PPro/2/3/4 has same info as P1 */
866         case 6: info->Level = 6;         break;
867         default:
868             FIXME("unknown cpu family %d, please report! (-> setting to 386)\n",
869                   (regs2[0] >> 8)&0xf);
870             info->Level = 3;
871             break;
872         }
873         user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED]       = !(regs2[3] & 1);
874         user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE]   = (regs2[3] & (1 << 4 )) >> 4;
875         user_shared_data->ProcessorFeatures[PF_PAE_ENABLED]                   = (regs2[3] & (1 << 6 )) >> 6;
876         user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE]       = (regs2[3] & (1 << 8 )) >> 8;
877         user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE]    = (regs2[3] & (1 << 23)) >> 23;
878         user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE]   = (regs2[3] & (1 << 25)) >> 25;
879         user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 26)) >> 26;
880
881         if (regs[1] == AUTH && regs[3] == ENTI && regs[2] == CAMD)
882         {
883             do_cpuid(0x80000000, regs);  /* get vendor cpuid level */
884             if (regs[0] >= 0x80000001)
885             {
886                 do_cpuid(0x80000001, regs2);  /* get vendor features */
887                 user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = (regs2[3] & (1 << 31 )) >> 31;
888             }
889         }
890     }
891 }
892
893 /******************************************************************
894  *              fill_cpu_info
895  *
896  * inits a couple of places with CPU related information:
897  * - cached_sci & cpuHZ in this file
898  * - Peb->NumberOfProcessors
899  * - SharedUserData->ProcessFeatures[] array
900  *
901  * It creates a registry subhierarchy, looking like:
902  * "\HARDWARE\DESCRIPTION\System\CentralProcessor\<processornumber>\Identifier (CPU x86)".
903  * Note that there is a hierarchy for every processor installed, so this
904  * supports multiprocessor systems. This is done like Win95 does it, I think.
905  *
906  * It creates some registry entries in the environment part:
907  * "\HKLM\System\CurrentControlSet\Control\Session Manager\Environment". These are
908  * always present. When deleted, Windows will add them again.
909  */
910 void fill_cpu_info(void)
911 {
912     memset(&cached_sci, 0, sizeof(cached_sci));
913     /* choose sensible defaults ...
914      * FIXME: perhaps overridable with precompiler flags?
915      */
916 #ifdef __i386__
917     cached_sci.Architecture     = PROCESSOR_ARCHITECTURE_INTEL;
918     cached_sci.Level            = 5; /* 586 */
919 #elif defined(__x86_64__)
920     cached_sci.Architecture     = PROCESSOR_ARCHITECTURE_AMD64;
921 #elif defined(__powerpc__)
922     cached_sci.Architecture     = PROCESSOR_ARCHITECTURE_PPC;
923 #elif defined(__ALPHA__)
924     cached_sci.Architecture     = PROCESSOR_ARCHITECTURE_ALPHA;
925 #elif defined(__sparc__)
926     cached_sci.Architecture     = PROCESSOR_ARCHITECTURE_SPARC;
927 #else
928 #error Unknown CPU
929 #endif
930     cached_sci.Revision         = 0;
931     cached_sci.Reserved         = 0;
932     cached_sci.FeatureSet       = 0x1fff; /* FIXME: set some sensible defaults out of ProcessFeatures[] */
933
934     NtCurrentTeb()->Peb->NumberOfProcessors = 1;
935
936     /* Hmm, reasonable processor feature defaults? */
937
938 #ifdef linux
939     {
940         char line[200];
941         FILE *f = fopen ("/proc/cpuinfo", "r");
942
943         if (!f)
944                 return;
945         while (fgets(line,200,f) != NULL)
946         {
947             char        *s,*value;
948
949             /* NOTE: the ':' is the only character we can rely on */
950             if (!(value = strchr(line,':')))
951                 continue;
952
953             /* terminate the valuename */
954             s = value - 1;
955             while ((s >= line) && ((*s == ' ') || (*s == '\t'))) s--;
956             *(s + 1) = '\0';
957
958             /* and strip leading spaces from value */
959             value += 1;
960             while (*value==' ') value++;
961             if ((s = strchr(value,'\n')))
962                 *s='\0';
963
964             if (!strcasecmp(line, "processor"))
965             {
966                 /* processor number counts up... */
967                 unsigned int x;
968
969                 if (sscanf(value, "%d",&x))
970                     if (x + 1 > NtCurrentTeb()->Peb->NumberOfProcessors)
971                         NtCurrentTeb()->Peb->NumberOfProcessors = x + 1;
972
973                 continue;
974             }
975             if (!strcasecmp(line, "model"))
976             {
977                 /* First part of Revision */
978                 int     x;
979
980                 if (sscanf(value, "%d",&x))
981                     cached_sci.Revision = cached_sci.Revision | (x << 8);
982
983                 continue;
984             }
985
986             /* 2.1 method */
987             if (!strcasecmp(line, "cpu family"))
988             {
989                 if (isdigit(value[0]))
990                 {
991                     cached_sci.Level = atoi(value);
992                 }
993                 continue;
994             }
995             /* old 2.0 method */
996             if (!strcasecmp(line, "cpu"))
997             {
998                 if (isdigit(value[0]) && value[1] == '8' && value[2] == '6' && value[3] == 0)
999                 {
1000                     switch (cached_sci.Level = value[0] - '0')
1001                     {
1002                     case 3:
1003                     case 4:
1004                     case 5:
1005                     case 6:
1006                         break;
1007                     default:
1008                         FIXME("unknown Linux 2.0 cpu family '%s', please report ! (-> setting to 386)\n", value);
1009                         cached_sci.Level = 3;
1010                         break;
1011                     }
1012                 }
1013                 continue;
1014             }
1015             if (!strcasecmp(line, "stepping"))
1016             {
1017                 /* Second part of Revision */
1018                 int     x;
1019
1020                 if (sscanf(value, "%d",&x))
1021                     cached_sci.Revision = cached_sci.Revision | x;
1022                 continue;
1023             }
1024             if (!strcasecmp(line, "cpu MHz"))
1025             {
1026                 double cmz;
1027                 if (sscanf( value, "%lf", &cmz ) == 1)
1028                 {
1029                     /* SYSTEMINFO doesn't have a slot for cpu speed, so store in a global */
1030                     cpuHz = cmz * 1000 * 1000;
1031                 }
1032                 continue;
1033             }
1034             if (!strcasecmp(line, "fdiv_bug"))
1035             {
1036                 if (!strncasecmp(value, "yes",3))
1037                     user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_PRECISION_ERRATA] = TRUE;
1038                 continue;
1039             }
1040             if (!strcasecmp(line, "fpu"))
1041             {
1042                 if (!strncasecmp(value, "no",2))
1043                     user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = TRUE;
1044                 continue;
1045             }
1046             if (!strcasecmp(line, "flags") || !strcasecmp(line, "features"))
1047             {
1048                 if (strstr(value, "cx8"))
1049                     user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
1050                 if (strstr(value, "mmx"))
1051                     user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1052                 if (strstr(value, "tsc"))
1053                     user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
1054                 if (strstr(value, "3dnow"))
1055                     user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
1056                 /* This will also catch sse2, but we have sse itself
1057                  * if we have sse2, so no problem */
1058                 if (strstr(value, "sse"))
1059                     user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
1060                 if (strstr(value, "sse2"))
1061                     user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
1062                 if (strstr(value, "pae"))
1063                     user_shared_data->ProcessorFeatures[PF_PAE_ENABLED] = TRUE;
1064
1065                 continue;
1066             }
1067         }
1068         fclose(f);
1069     }
1070 #elif defined (__NetBSD__)
1071     {
1072         int mib[2];
1073         int value;
1074         size_t val_len;
1075         char model[256];
1076         char *cpuclass;
1077         FILE *f = fopen("/var/run/dmesg.boot", "r");
1078
1079         /* first deduce as much as possible from the sysctls */
1080         mib[0] = CTL_MACHDEP;
1081 #ifdef CPU_FPU_PRESENT
1082         mib[1] = CPU_FPU_PRESENT;
1083         val_len = sizeof(value);
1084         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1085             user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = !value;
1086 #endif
1087 #ifdef CPU_SSE
1088         mib[1] = CPU_SSE;   /* this should imply MMX */
1089         val_len = sizeof(value);
1090         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1091             if (value) user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1092 #endif
1093 #ifdef CPU_SSE2
1094         mib[1] = CPU_SSE2;  /* this should imply MMX */
1095         val_len = sizeof(value);
1096         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1097             if (value) user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1098 #endif
1099         mib[0] = CTL_HW;
1100         mib[1] = HW_NCPU;
1101         val_len = sizeof(value);
1102         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1103             if (value > NtCurrentTeb()->Peb->NumberOfProcessors)
1104                 NtCurrentTeb()->Peb->NumberOfProcessors = value;
1105         mib[1] = HW_MODEL;
1106         val_len = sizeof(model)-1;
1107         if (sysctl(mib, 2, model, &val_len, NULL, 0) >= 0)
1108         {
1109             model[val_len] = '\0'; /* just in case */
1110             cpuclass = strstr(model, "-class");
1111             if (cpuclass != NULL) {
1112                 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
1113                 if (!strncmp(cpuclass+1, "386", 3))
1114                 {
1115                     cached_sci.Level= 3;
1116                 }
1117                 if (!strncmp(cpuclass+1, "486", 3))
1118                 {
1119                     cached_sci.Level= 4;
1120                 }
1121                 if (!strncmp(cpuclass+1, "586", 3))
1122                 {
1123                     cached_sci.Level= 5;
1124                 }
1125                 if (!strncmp(cpuclass+1, "686", 3))
1126                 {
1127                     cached_sci.Level= 6;
1128                     /* this should imply MMX */
1129                     user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1130                 }
1131             }
1132         }
1133
1134         /* it may be worth reading from /var/run/dmesg.boot for
1135            additional information such as CX8, MMX and TSC
1136            (however this information should be considered less
1137            reliable than that from the sysctl calls) */
1138         if (f != NULL)
1139         {
1140             while (fgets(model, 255, f) != NULL)
1141             {
1142                 int cpu, features;
1143                 if (sscanf(model, "cpu%d: features %x<", &cpu, &features) == 2)
1144                 {
1145                     /* we could scan the string but it is easier
1146                        to test the bits directly */
1147                     if (features & 0x1)
1148                         user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = TRUE;
1149                     if (features & 0x10)
1150                         user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
1151                     if (features & 0x100)
1152                         user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
1153                     if (features & 0x800000)
1154                         user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1155
1156                     break;
1157                 }
1158             }
1159             fclose(f);
1160         }
1161     }
1162 #elif defined(__FreeBSD__)
1163     {
1164         int ret, num;
1165         size_t len;
1166
1167         get_cpuinfo( &cached_sci );
1168
1169         /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
1170         /*len = sizeof(num);
1171           ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
1172           if (!ret)
1173           user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
1174
1175         len = sizeof(num);
1176         ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
1177         if (!ret)
1178             NtCurrentTeb()->Peb->NumberOfProcessors = num;
1179
1180         len = sizeof(num);
1181         if (!sysctlbyname("dev.cpu.0.freq", &num, &len, NULL, 0))
1182             cpuHz = num * 1000 * 1000;
1183     }
1184 #elif defined(__sun)
1185     {
1186         int num = sysconf( _SC_NPROCESSORS_ONLN );
1187
1188         if (num == -1) num = 1;
1189         get_cpuinfo( &cached_sci );
1190         NtCurrentTeb()->Peb->NumberOfProcessors = num;
1191     }
1192 #elif defined (__OpenBSD__)
1193     {
1194         int mib[2], num;
1195         size_t len;
1196
1197         mib[0] = CTL_HW;
1198         mib[1] = HW_NCPU;
1199         len = sizeof(num);
1200
1201         num = sysctl(mib, 2, &num, &len, NULL, 0);
1202         NtCurrentTeb()->Peb->NumberOfProcessors = num;
1203     }
1204 #elif defined (__APPLE__)
1205     {
1206         size_t valSize;
1207         unsigned long long longVal;
1208         int value;
1209         int cputype;
1210         char buffer[256];
1211
1212         valSize = sizeof(int);
1213         if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
1214         {
1215             if (value)
1216                 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = FALSE;
1217             else
1218                 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = TRUE;
1219         }
1220         valSize = sizeof(int);
1221         if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
1222             NtCurrentTeb()->Peb->NumberOfProcessors = value;
1223
1224         /* FIXME: we don't use the "hw.activecpu" value... but the cached one */
1225
1226         valSize = sizeof(int);
1227         if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
1228         {
1229             switch (cputype)
1230             {
1231             case CPU_TYPE_POWERPC:
1232                 cached_sci.Architecture = PROCESSOR_ARCHITECTURE_PPC;
1233                 valSize = sizeof(int);
1234                 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
1235                 {
1236                     switch (value)
1237                     {
1238                     case CPU_SUBTYPE_POWERPC_601:
1239                     case CPU_SUBTYPE_POWERPC_602:       cached_sci.Level = 1;   break;
1240                     case CPU_SUBTYPE_POWERPC_603:       cached_sci.Level = 3;   break;
1241                     case CPU_SUBTYPE_POWERPC_603e:
1242                     case CPU_SUBTYPE_POWERPC_603ev:     cached_sci.Level = 6;   break;
1243                     case CPU_SUBTYPE_POWERPC_604:       cached_sci.Level = 4;   break;
1244                     case CPU_SUBTYPE_POWERPC_604e:      cached_sci.Level = 9;   break;
1245                     case CPU_SUBTYPE_POWERPC_620:       cached_sci.Level = 20;  break;
1246                     case CPU_SUBTYPE_POWERPC_750:       /* G3/G4 derive from 603 so ... */
1247                     case CPU_SUBTYPE_POWERPC_7400:
1248                     case CPU_SUBTYPE_POWERPC_7450:      cached_sci.Level = 6;   break;
1249                     case CPU_SUBTYPE_POWERPC_970:       cached_sci.Level = 9;
1250                         /* :o) user_shared_data->ProcessorFeatures[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
1251                         break;
1252                     default: break;
1253                     }
1254                 }
1255                 break; /* CPU_TYPE_POWERPC */
1256             case CPU_TYPE_I386:
1257                 cached_sci.Architecture = PROCESSOR_ARCHITECTURE_INTEL;
1258                 valSize = sizeof(int);
1259                 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
1260                 {
1261                     cached_sci.Level = value;
1262                 }
1263                 valSize = sizeof(int);
1264                 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
1265                     cached_sci.Revision = (value << 8);
1266                 valSize = sizeof(int);
1267                 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
1268                     cached_sci.Revision |= value;
1269                 valSize = sizeof(buffer);
1270                 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
1271                 {
1272                     cached_sci.Revision |= value;
1273                     if (strstr(buffer, "CX8"))   user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
1274                     if (strstr(buffer, "MMX"))   user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1275                     if (strstr(buffer, "TSC"))   user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
1276                     if (strstr(buffer, "3DNOW")) user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
1277                     if (strstr(buffer, "SSE"))   user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
1278                     if (strstr(buffer, "SSE2"))  user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
1279                     if (strstr(buffer, "PAE"))   user_shared_data->ProcessorFeatures[PF_PAE_ENABLED] = TRUE;
1280                 }
1281                 break; /* CPU_TYPE_I386 */
1282             default: break;
1283             } /* switch (cputype) */
1284         }
1285         valSize = sizeof(longVal);
1286         if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
1287             cpuHz = longVal;
1288     }
1289 #else
1290     FIXME("not yet supported on this system\n");
1291 #endif
1292     TRACE("<- CPU arch %d, level %d, rev %d, features 0x%x\n",
1293           cached_sci.Architecture, cached_sci.Level, cached_sci.Revision, cached_sci.FeatureSet);
1294 }
1295
1296 /******************************************************************************
1297  * NtQuerySystemInformation [NTDLL.@]
1298  * ZwQuerySystemInformation [NTDLL.@]
1299  *
1300  * ARGUMENTS:
1301  *  SystemInformationClass      Index to a certain information structure
1302  *      SystemTimeAdjustmentInformation SYSTEM_TIME_ADJUSTMENT
1303  *      SystemCacheInformation          SYSTEM_CACHE_INFORMATION
1304  *      SystemConfigurationInformation  CONFIGURATION_INFORMATION
1305  *      observed (class/len):
1306  *              0x0/0x2c
1307  *              0x12/0x18
1308  *              0x2/0x138
1309  *              0x8/0x600
1310  *              0x25/0xc
1311  *  SystemInformation   caller supplies storage for the information structure
1312  *  Length              size of the structure
1313  *  ResultLength        Data written
1314  */
1315 NTSTATUS WINAPI NtQuerySystemInformation(
1316         IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
1317         OUT PVOID SystemInformation,
1318         IN ULONG Length,
1319         OUT PULONG ResultLength)
1320 {
1321     NTSTATUS    ret = STATUS_SUCCESS;
1322     ULONG       len = 0;
1323
1324     TRACE("(0x%08x,%p,0x%08x,%p)\n",
1325           SystemInformationClass,SystemInformation,Length,ResultLength);
1326
1327     switch (SystemInformationClass)
1328     {
1329     case SystemBasicInformation:
1330         {
1331             SYSTEM_BASIC_INFORMATION sbi;
1332
1333             virtual_get_system_info( &sbi );
1334             len = sizeof(sbi);
1335
1336             if ( Length == len)
1337             {
1338                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1339                 else memcpy( SystemInformation, &sbi, len);
1340             }
1341             else ret = STATUS_INFO_LENGTH_MISMATCH;
1342         }
1343         break;
1344     case SystemCpuInformation:
1345         if (Length >= (len = sizeof(cached_sci)))
1346         {
1347             if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1348             else memcpy(SystemInformation, &cached_sci, len);
1349         }
1350         else ret = STATUS_INFO_LENGTH_MISMATCH;
1351         break;
1352     case SystemPerformanceInformation:
1353         {
1354             SYSTEM_PERFORMANCE_INFORMATION spi;
1355             static BOOL fixme_written = FALSE;
1356
1357             memset(&spi, 0 , sizeof(spi));
1358             len = sizeof(spi);
1359
1360             spi.Reserved3 = 0x7fffffff; /* Available paged pool memory? */
1361
1362             if (Length >= len)
1363             {
1364                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1365                 else memcpy( SystemInformation, &spi, len);
1366             }
1367             else ret = STATUS_INFO_LENGTH_MISMATCH;
1368             if(!fixme_written) {
1369                 FIXME("info_class SYSTEM_PERFORMANCE_INFORMATION\n");
1370                 fixme_written = TRUE;
1371             }
1372         }
1373         break;
1374     case SystemTimeOfDayInformation:
1375         {
1376             SYSTEM_TIMEOFDAY_INFORMATION sti;
1377
1378             memset(&sti, 0 , sizeof(sti));
1379
1380             /* liKeSystemTime, liExpTimeZoneBias, uCurrentTimeZoneId */
1381             sti.liKeBootTime.QuadPart = server_start_time;
1382
1383             if (Length <= sizeof(sti))
1384             {
1385                 len = Length;
1386                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1387                 else memcpy( SystemInformation, &sti, Length);
1388             }
1389             else ret = STATUS_INFO_LENGTH_MISMATCH;
1390         }
1391         break;
1392     case SystemProcessInformation:
1393         {
1394             SYSTEM_PROCESS_INFORMATION* spi = SystemInformation;
1395             SYSTEM_PROCESS_INFORMATION* last = NULL;
1396             HANDLE hSnap = 0;
1397             WCHAR procname[1024];
1398             WCHAR* exename;
1399             DWORD wlen = 0;
1400             DWORD procstructlen = 0;
1401
1402             SERVER_START_REQ( create_snapshot )
1403             {
1404                 req->flags      = SNAP_PROCESS | SNAP_THREAD;
1405                 req->attributes = 0;
1406                 if (!(ret = wine_server_call( req )))
1407                     hSnap = wine_server_ptr_handle( reply->handle );
1408             }
1409             SERVER_END_REQ;
1410             len = 0;
1411             while (ret == STATUS_SUCCESS)
1412             {
1413                 SERVER_START_REQ( next_process )
1414                 {
1415                     req->handle = wine_server_obj_handle( hSnap );
1416                     req->reset = (len == 0);
1417                     wine_server_set_reply( req, procname, sizeof(procname)-sizeof(WCHAR) );
1418                     if (!(ret = wine_server_call( req )))
1419                     {
1420                         /* Make sure procname is 0 terminated */
1421                         procname[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
1422
1423                         /* Get only the executable name, not the path */
1424                         if ((exename = strrchrW(procname, '\\')) != NULL) exename++;
1425                         else exename = procname;
1426
1427                         wlen = (strlenW(exename) + 1) * sizeof(WCHAR);
1428
1429                         procstructlen = sizeof(*spi) + wlen + ((reply->threads - 1) * sizeof(SYSTEM_THREAD_INFORMATION));
1430
1431                         if (Length >= len + procstructlen)
1432                         {
1433                             /* ftCreationTime, ftUserTime, ftKernelTime;
1434                              * vmCounters, ioCounters
1435                              */
1436  
1437                             memset(spi, 0, sizeof(*spi));
1438
1439                             spi->NextEntryOffset = procstructlen - wlen;
1440                             spi->dwThreadCount = reply->threads;
1441
1442                             /* spi->pszProcessName will be set later on */
1443
1444                             spi->dwBasePriority = reply->priority;
1445                             spi->UniqueProcessId = UlongToHandle(reply->pid);
1446                             spi->ParentProcessId = UlongToHandle(reply->ppid);
1447                             spi->HandleCount = reply->handles;
1448
1449                             /* spi->ti will be set later on */
1450
1451                             len += procstructlen;
1452                         }
1453                         else ret = STATUS_INFO_LENGTH_MISMATCH;
1454                     }
1455                 }
1456                 SERVER_END_REQ;
1457  
1458                 if (ret != STATUS_SUCCESS)
1459                 {
1460                     if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
1461                     break;
1462                 }
1463                 else /* Length is already checked for */
1464                 {
1465                     int     i, j;
1466
1467                     /* set thread info */
1468                     i = j = 0;
1469                     while (ret == STATUS_SUCCESS)
1470                     {
1471                         SERVER_START_REQ( next_thread )
1472                         {
1473                             req->handle = wine_server_obj_handle( hSnap );
1474                             req->reset = (j == 0);
1475                             if (!(ret = wine_server_call( req )))
1476                             {
1477                                 j++;
1478                                 if (UlongToHandle(reply->pid) == spi->UniqueProcessId)
1479                                 {
1480                                     /* ftKernelTime, ftUserTime, ftCreateTime;
1481                                      * dwTickCount, dwStartAddress
1482                                      */
1483
1484                                     memset(&spi->ti[i], 0, sizeof(spi->ti));
1485
1486                                     spi->ti[i].CreateTime.QuadPart = 0xdeadbeef;
1487                                     spi->ti[i].ClientId.UniqueProcess = UlongToHandle(reply->pid);
1488                                     spi->ti[i].ClientId.UniqueThread  = UlongToHandle(reply->tid);
1489                                     spi->ti[i].dwCurrentPriority = reply->base_pri + reply->delta_pri;
1490                                     spi->ti[i].dwBasePriority = reply->base_pri;
1491                                     i++;
1492                                 }
1493                             }
1494                         }
1495                         SERVER_END_REQ;
1496                     }
1497                     if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
1498
1499                     /* now append process name */
1500                     spi->ProcessName.Buffer = (WCHAR*)((char*)spi + spi->NextEntryOffset);
1501                     spi->ProcessName.Length = wlen - sizeof(WCHAR);
1502                     spi->ProcessName.MaximumLength = wlen;
1503                     memcpy( spi->ProcessName.Buffer, exename, wlen );
1504                     spi->NextEntryOffset += wlen;
1505
1506                     last = spi;
1507                     spi = (SYSTEM_PROCESS_INFORMATION*)((char*)spi + spi->NextEntryOffset);
1508                 }
1509             }
1510             if (ret == STATUS_SUCCESS && last) last->NextEntryOffset = 0;
1511             if (hSnap) NtClose(hSnap);
1512         }
1513         break;
1514     case SystemProcessorPerformanceInformation:
1515         {
1516             SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi = NULL;
1517             unsigned int cpus = 0;
1518             int out_cpus = Length / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
1519
1520             if (out_cpus == 0)
1521             {
1522                 len = 0;
1523                 ret = STATUS_INFO_LENGTH_MISMATCH;
1524                 break;
1525             }
1526             else
1527 #ifdef __APPLE__
1528             {
1529                 processor_cpu_load_info_data_t *pinfo;
1530                 mach_msg_type_number_t info_count;
1531
1532                 if (host_processor_info (mach_host_self (),
1533                                          PROCESSOR_CPU_LOAD_INFO,
1534                                          &cpus,
1535                                          (processor_info_array_t*)&pinfo,
1536                                          &info_count) == 0)
1537                 {
1538                     int i;
1539                     cpus = min(cpus,out_cpus);
1540                     len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
1541                     sppi = RtlAllocateHeap(GetProcessHeap(), 0,len);
1542                     for (i = 0; i < cpus; i++)
1543                     {
1544                         sppi[i].IdleTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_IDLE];
1545                         sppi[i].KernelTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_SYSTEM];
1546                         sppi[i].UserTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_USER];
1547                     }
1548                     vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count * sizeof(natural_t));
1549                 }
1550             }
1551 #else
1552             {
1553                 FILE *cpuinfo = fopen("/proc/stat", "r");
1554                 if (cpuinfo)
1555                 {
1556                     unsigned usr,nice,sys;
1557                     unsigned long idle;
1558                     int count;
1559                     char name[10];
1560                     char line[255];
1561
1562                     /* first line is combined usage */
1563                     if (fgets(line,255,cpuinfo))
1564                         count = sscanf(line, "%s %u %u %u %lu", name, &usr, &nice,
1565                                        &sys, &idle);
1566                     else
1567                         count = 0;
1568                     /* we set this up in the for older non-smp enabled kernels */
1569                     if (count == 5 && strcmp(name, "cpu") == 0)
1570                     {
1571                         sppi = RtlAllocateHeap(GetProcessHeap(), 0,
1572                                                sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
1573                         sppi->IdleTime.QuadPart = idle;
1574                         sppi->KernelTime.QuadPart = sys;
1575                         sppi->UserTime.QuadPart = usr;
1576                         cpus = 1;
1577                         len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
1578                     }
1579
1580                     do
1581                     {
1582                         if (fgets(line, 255, cpuinfo))
1583                             count = sscanf(line, "%s %u %u %u %lu", name, &usr,
1584                                            &nice, &sys, &idle);
1585                         else
1586                             count = 0;
1587                         if (count == 5 && strncmp(name, "cpu", 3)==0)
1588                         {
1589                             out_cpus --;
1590                             if (name[3]=='0') /* first cpu */
1591                             {
1592                                 sppi->IdleTime.QuadPart = idle;
1593                                 sppi->KernelTime.QuadPart = sys;
1594                                 sppi->UserTime.QuadPart = usr;
1595                             }
1596                             else /* new cpu */
1597                             {
1598                                 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * (cpus+1);
1599                                 sppi = RtlReAllocateHeap(GetProcessHeap(), 0, sppi, len);
1600                                 sppi[cpus].IdleTime.QuadPart = idle;
1601                                 sppi[cpus].KernelTime.QuadPart = sys;
1602                                 sppi[cpus].UserTime.QuadPart = usr;
1603                                 cpus++;
1604                             }
1605                         }
1606                         else
1607                             break;
1608                     } while (out_cpus > 0);
1609                     fclose(cpuinfo);
1610                 }
1611             }
1612 #endif
1613
1614             if (cpus == 0)
1615             {
1616                 static int i = 1;
1617
1618                 sppi = RtlAllocateHeap(GetProcessHeap(),0,sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
1619
1620                 memset(sppi, 0 , sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION));
1621                 FIXME("stub info_class SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION\n");
1622
1623                 /* many programs expect these values to change so fake change */
1624                 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
1625                 sppi->KernelTime.QuadPart = 1 * i;
1626                 sppi->UserTime.QuadPart = 2 * i;
1627                 sppi->IdleTime.QuadPart = 3 * i;
1628                 i++;
1629             }
1630
1631             if (Length >= len)
1632             {
1633                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1634                 else memcpy( SystemInformation, sppi, len);
1635             }
1636             else ret = STATUS_INFO_LENGTH_MISMATCH;
1637
1638             RtlFreeHeap(GetProcessHeap(),0,sppi);
1639         }
1640         break;
1641     case SystemModuleInformation:
1642         /* FIXME: should be system-wide */
1643         if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1644         else ret = LdrQueryProcessModuleInformation( SystemInformation, Length, &len );
1645         break;
1646     case SystemHandleInformation:
1647         {
1648             SYSTEM_HANDLE_INFORMATION shi;
1649
1650             memset(&shi, 0, sizeof(shi));
1651             len = sizeof(shi);
1652
1653             if ( Length >= len)
1654             {
1655                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1656                 else memcpy( SystemInformation, &shi, len);
1657             }
1658             else ret = STATUS_INFO_LENGTH_MISMATCH;
1659             FIXME("info_class SYSTEM_HANDLE_INFORMATION\n");
1660         }
1661         break;
1662     case SystemCacheInformation:
1663         {
1664             SYSTEM_CACHE_INFORMATION sci;
1665
1666             memset(&sci, 0, sizeof(sci)); /* FIXME */
1667             len = sizeof(sci);
1668
1669             if ( Length >= len)
1670             {
1671                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1672                 else memcpy( SystemInformation, &sci, len);
1673             }
1674             else ret = STATUS_INFO_LENGTH_MISMATCH;
1675             FIXME("info_class SYSTEM_CACHE_INFORMATION\n");
1676         }
1677         break;
1678     case SystemInterruptInformation:
1679         {
1680             SYSTEM_INTERRUPT_INFORMATION sii;
1681
1682             memset(&sii, 0, sizeof(sii));
1683             len = sizeof(sii);
1684
1685             if ( Length >= len)
1686             {
1687                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1688                 else memcpy( SystemInformation, &sii, len);
1689             }
1690             else ret = STATUS_INFO_LENGTH_MISMATCH;
1691             FIXME("info_class SYSTEM_INTERRUPT_INFORMATION\n");
1692         }
1693         break;
1694     case SystemKernelDebuggerInformation:
1695         {
1696             SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
1697
1698             skdi.DebuggerEnabled = FALSE;
1699             skdi.DebuggerNotPresent = TRUE;
1700             len = sizeof(skdi);
1701
1702             if ( Length >= len)
1703             {
1704                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1705                 else memcpy( SystemInformation, &skdi, len);
1706             }
1707             else ret = STATUS_INFO_LENGTH_MISMATCH;
1708         }
1709         break;
1710     case SystemRegistryQuotaInformation:
1711         {
1712             /* Something to do with the size of the registry             *
1713              * Since we don't have a size limitation, fake it            *
1714              * This is almost certainly wrong.                           *
1715              * This sets each of the three words in the struct to 32 MB, *
1716              * which is enough to make the IE 5 installer happy.         */
1717             SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
1718
1719             srqi.RegistryQuotaAllowed = 0x2000000;
1720             srqi.RegistryQuotaUsed = 0x200000;
1721             srqi.Reserved1 = (void*)0x200000;
1722             len = sizeof(srqi);
1723
1724             if ( Length >= len)
1725             {
1726                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1727                 else
1728                 {
1729                     FIXME("SystemRegistryQuotaInformation: faking max registry size of 32 MB\n");
1730                     memcpy( SystemInformation, &srqi, len);
1731                 }
1732             }
1733             else ret = STATUS_INFO_LENGTH_MISMATCH;
1734         }
1735         break;
1736     default:
1737         FIXME("(0x%08x,%p,0x%08x,%p) stub\n",
1738               SystemInformationClass,SystemInformation,Length,ResultLength);
1739
1740         /* Several Information Classes are not implemented on Windows and return 2 different values 
1741          * STATUS_NOT_IMPLEMENTED or STATUS_INVALID_INFO_CLASS
1742          * in 95% of the cases it's STATUS_INVALID_INFO_CLASS, so use this as the default
1743         */
1744         ret = STATUS_INVALID_INFO_CLASS;
1745     }
1746
1747     if (ResultLength) *ResultLength = len;
1748
1749     return ret;
1750 }
1751
1752 /******************************************************************************
1753  * NtSetSystemInformation [NTDLL.@]
1754  * ZwSetSystemInformation [NTDLL.@]
1755  */
1756 NTSTATUS WINAPI NtSetSystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG Length)
1757 {
1758     FIXME("(0x%08x,%p,0x%08x) stub\n",SystemInformationClass,SystemInformation,Length);
1759     return STATUS_SUCCESS;
1760 }
1761
1762 /******************************************************************************
1763  *  NtCreatePagingFile          [NTDLL.@]
1764  *  ZwCreatePagingFile          [NTDLL.@]
1765  */
1766 NTSTATUS WINAPI NtCreatePagingFile(
1767         PUNICODE_STRING PageFileName,
1768         PLARGE_INTEGER MinimumSize,
1769         PLARGE_INTEGER MaximumSize,
1770         PLARGE_INTEGER ActualSize)
1771 {
1772     FIXME("(%p %p %p %p) stub\n", PageFileName, MinimumSize, MaximumSize, ActualSize);
1773     return STATUS_SUCCESS;
1774 }
1775
1776 /******************************************************************************
1777  *  NtDisplayString                             [NTDLL.@]
1778  *
1779  * writes a string to the nt-textmode screen eg. during startup
1780  */
1781 NTSTATUS WINAPI NtDisplayString ( PUNICODE_STRING string )
1782 {
1783     STRING stringA;
1784     NTSTATUS ret;
1785
1786     if (!(ret = RtlUnicodeStringToAnsiString( &stringA, string, TRUE )))
1787     {
1788         MESSAGE( "%.*s", stringA.Length, stringA.Buffer );
1789         RtlFreeAnsiString( &stringA );
1790     }
1791     return ret;
1792 }
1793
1794 /******************************************************************************
1795  *  NtInitiatePowerAction                       [NTDLL.@]
1796  *
1797  */
1798 NTSTATUS WINAPI NtInitiatePowerAction(
1799         IN POWER_ACTION SystemAction,
1800         IN SYSTEM_POWER_STATE MinSystemState,
1801         IN ULONG Flags,
1802         IN BOOLEAN Asynchronous)
1803 {
1804         FIXME("(%d,%d,0x%08x,%d),stub\n",
1805                 SystemAction,MinSystemState,Flags,Asynchronous);
1806         return STATUS_NOT_IMPLEMENTED;
1807 }
1808         
1809
1810 /******************************************************************************
1811  *  NtPowerInformation                          [NTDLL.@]
1812  *
1813  */
1814 NTSTATUS WINAPI NtPowerInformation(
1815         IN POWER_INFORMATION_LEVEL InformationLevel,
1816         IN PVOID lpInputBuffer,
1817         IN ULONG nInputBufferSize,
1818         IN PVOID lpOutputBuffer,
1819         IN ULONG nOutputBufferSize)
1820 {
1821         TRACE("(%d,%p,%d,%p,%d)\n",
1822                 InformationLevel,lpInputBuffer,nInputBufferSize,lpOutputBuffer,nOutputBufferSize);
1823         switch(InformationLevel) {
1824                 case SystemPowerCapabilities: {
1825                         PSYSTEM_POWER_CAPABILITIES PowerCaps = lpOutputBuffer;
1826                         FIXME("semi-stub: SystemPowerCapabilities\n");
1827                         if (nOutputBufferSize < sizeof(SYSTEM_POWER_CAPABILITIES))
1828                                 return STATUS_BUFFER_TOO_SMALL;
1829                         /* FIXME: These values are based off a native XP desktop, should probably use APM/ACPI to get the 'real' values */
1830                         PowerCaps->PowerButtonPresent = TRUE;
1831                         PowerCaps->SleepButtonPresent = FALSE;
1832                         PowerCaps->LidPresent = FALSE;
1833                         PowerCaps->SystemS1 = TRUE;
1834                         PowerCaps->SystemS2 = FALSE;
1835                         PowerCaps->SystemS3 = FALSE;
1836                         PowerCaps->SystemS4 = TRUE;
1837                         PowerCaps->SystemS5 = TRUE;
1838                         PowerCaps->HiberFilePresent = TRUE;
1839                         PowerCaps->FullWake = TRUE;
1840                         PowerCaps->VideoDimPresent = FALSE;
1841                         PowerCaps->ApmPresent = FALSE;
1842                         PowerCaps->UpsPresent = FALSE;
1843                         PowerCaps->ThermalControl = FALSE;
1844                         PowerCaps->ProcessorThrottle = FALSE;
1845                         PowerCaps->ProcessorMinThrottle = 100;
1846                         PowerCaps->ProcessorMaxThrottle = 100;
1847                         PowerCaps->DiskSpinDown = TRUE;
1848                         PowerCaps->SystemBatteriesPresent = FALSE;
1849                         PowerCaps->BatteriesAreShortTerm = FALSE;
1850                         PowerCaps->BatteryScale[0].Granularity = 0;
1851                         PowerCaps->BatteryScale[0].Capacity = 0;
1852                         PowerCaps->BatteryScale[1].Granularity = 0;
1853                         PowerCaps->BatteryScale[1].Capacity = 0;
1854                         PowerCaps->BatteryScale[2].Granularity = 0;
1855                         PowerCaps->BatteryScale[2].Capacity = 0;
1856                         PowerCaps->AcOnLineWake = PowerSystemUnspecified;
1857                         PowerCaps->SoftLidWake = PowerSystemUnspecified;
1858                         PowerCaps->RtcWake = PowerSystemSleeping1;
1859                         PowerCaps->MinDeviceWakeState = PowerSystemUnspecified;
1860                         PowerCaps->DefaultLowLatencyWake = PowerSystemUnspecified;
1861                         return STATUS_SUCCESS;
1862                 }
1863                 case SystemExecutionState: {
1864                         PULONG ExecutionState = lpOutputBuffer;
1865                         WARN("semi-stub: SystemExecutionState\n"); /* Needed for .NET Framework, but using a FIXME is really noisy. */
1866                         if (lpInputBuffer != NULL)
1867                                 return STATUS_INVALID_PARAMETER;
1868                         /* FIXME: The actual state should be the value set by SetThreadExecutionState which is not currently implemented. */
1869                         *ExecutionState = ES_USER_PRESENT;
1870                         return STATUS_SUCCESS;
1871                 }
1872                 case ProcessorInformation: {
1873                         PPROCESSOR_POWER_INFORMATION cpu_power = lpOutputBuffer;
1874
1875                         WARN("semi-stub: ProcessorInformation\n");
1876                         if (nOutputBufferSize < sizeof(PROCESSOR_POWER_INFORMATION))
1877                                 return STATUS_BUFFER_TOO_SMALL;
1878                         cpu_power->Number = NtCurrentTeb()->Peb->NumberOfProcessors;
1879                         cpu_power->MaxMhz = cpuHz / 1000000;
1880                         cpu_power->CurrentMhz = cpuHz / 1000000;
1881                         cpu_power->MhzLimit = cpuHz / 1000000;
1882                         cpu_power->MaxIdleState = 0; /* FIXME */
1883                         cpu_power->CurrentIdleState = 0; /* FIXME */
1884                         return STATUS_SUCCESS;
1885                 }
1886                 default:
1887                         /* FIXME: Needed by .NET Framework */
1888                         WARN("Unimplemented NtPowerInformation action: %d\n", InformationLevel);
1889                         return STATUS_NOT_IMPLEMENTED;
1890         }
1891 }
1892
1893 /******************************************************************************
1894  *  NtShutdownSystem                            [NTDLL.@]
1895  *
1896  */
1897 NTSTATUS WINAPI NtShutdownSystem(SHUTDOWN_ACTION Action)
1898 {
1899     FIXME("%d\n",Action);
1900     return STATUS_SUCCESS;
1901 }
1902
1903 /******************************************************************************
1904  *  NtAllocateLocallyUniqueId (NTDLL.@)
1905  */
1906 NTSTATUS WINAPI NtAllocateLocallyUniqueId(PLUID Luid)
1907 {
1908     NTSTATUS status;
1909
1910     TRACE("%p\n", Luid);
1911
1912     if (!Luid)
1913         return STATUS_ACCESS_VIOLATION;
1914
1915     SERVER_START_REQ( allocate_locally_unique_id )
1916     {
1917         status = wine_server_call( req );
1918         if (!status)
1919         {
1920             Luid->LowPart = reply->luid.low_part;
1921             Luid->HighPart = reply->luid.high_part;
1922         }
1923     }
1924     SERVER_END_REQ;
1925
1926     return status;
1927 }
1928
1929 /******************************************************************************
1930  *        VerSetConditionMask   (NTDLL.@)
1931  */
1932 ULONGLONG WINAPI VerSetConditionMask( ULONGLONG dwlConditionMask, DWORD dwTypeBitMask,
1933                                       BYTE dwConditionMask)
1934 {
1935     if(dwTypeBitMask == 0)
1936         return dwlConditionMask;
1937     dwConditionMask &= 0x07;
1938     if(dwConditionMask == 0)
1939         return dwlConditionMask;
1940
1941     if(dwTypeBitMask & VER_PRODUCT_TYPE)
1942         dwlConditionMask |= dwConditionMask << 7*3;
1943     else if (dwTypeBitMask & VER_SUITENAME)
1944         dwlConditionMask |= dwConditionMask << 6*3;
1945     else if (dwTypeBitMask & VER_SERVICEPACKMAJOR)
1946         dwlConditionMask |= dwConditionMask << 5*3;
1947     else if (dwTypeBitMask & VER_SERVICEPACKMINOR)
1948         dwlConditionMask |= dwConditionMask << 4*3;
1949     else if (dwTypeBitMask & VER_PLATFORMID)
1950         dwlConditionMask |= dwConditionMask << 3*3;
1951     else if (dwTypeBitMask & VER_BUILDNUMBER)
1952         dwlConditionMask |= dwConditionMask << 2*3;
1953     else if (dwTypeBitMask & VER_MAJORVERSION)
1954         dwlConditionMask |= dwConditionMask << 1*3;
1955     else if (dwTypeBitMask & VER_MINORVERSION)
1956         dwlConditionMask |= dwConditionMask << 0*3;
1957     return dwlConditionMask;
1958 }
1959
1960 /******************************************************************************
1961  *  NtAccessCheckAndAuditAlarm   (NTDLL.@)
1962  *  ZwAccessCheckAndAuditAlarm   (NTDLL.@)
1963  */
1964 NTSTATUS WINAPI NtAccessCheckAndAuditAlarm(PUNICODE_STRING SubsystemName, HANDLE HandleId, PUNICODE_STRING ObjectTypeName,
1965                                            PUNICODE_STRING ObjectName, PSECURITY_DESCRIPTOR SecurityDescriptor,
1966                                            ACCESS_MASK DesiredAccess, PGENERIC_MAPPING GenericMapping, BOOLEAN ObjectCreation,
1967                                            PACCESS_MASK GrantedAccess, PBOOLEAN AccessStatus, PBOOLEAN GenerateOnClose)
1968 {
1969     FIXME("(%s, %p, %s, %p, 0x%08x, %p, %d, %p, %p, %p), stub\n", debugstr_us(SubsystemName), HandleId,
1970           debugstr_us(ObjectTypeName), SecurityDescriptor, DesiredAccess, GenericMapping, ObjectCreation,
1971           GrantedAccess, AccessStatus, GenerateOnClose);
1972
1973     return STATUS_NOT_IMPLEMENTED;
1974 }
1975
1976 /******************************************************************************
1977  *  NtSystemDebugControl   (NTDLL.@)
1978  *  ZwSystemDebugControl   (NTDLL.@)
1979  */
1980 NTSTATUS WINAPI NtSystemDebugControl(SYSDBG_COMMAND command, PVOID inbuffer, ULONG inbuflength, PVOID outbuffer,
1981                                      ULONG outbuflength, PULONG retlength)
1982 {
1983     FIXME("(%d, %p, %d, %p, %d, %p), stub\n", command, inbuffer, inbuflength, outbuffer, outbuflength, retlength);
1984
1985     return STATUS_NOT_IMPLEMENTED;
1986 }