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