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