d3d10: Make get_property_info() static.
[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;
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                 {
1053                     user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
1054                     cached_sci.FeatureSet |= CPU_FEATURE_3DNOW;
1055                 }
1056                 /* This will also catch sse2, but we have sse itself
1057                  * if we have sse2, so no problem */
1058                 if (strstr(value, "sse"))
1059                 {
1060                     user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
1061                     cached_sci.FeatureSet |= CPU_FEATURE_SSE;
1062                 }
1063                 if (strstr(value, "sse2"))
1064                 {
1065                     user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
1066                     cached_sci.FeatureSet |= CPU_FEATURE_SSE2;
1067                 }
1068                 if (strstr(value, "pni"))
1069                     user_shared_data->ProcessorFeatures[PF_SSE3_INSTRUCTIONS_AVAILABLE] = TRUE;
1070                 if (strstr(value, "pae"))
1071                     user_shared_data->ProcessorFeatures[PF_PAE_ENABLED] = TRUE;
1072                 if (strstr(value, "ht"))
1073                     cached_sci.FeatureSet |= CPU_FEATURE_HTT;
1074                 continue;
1075             }
1076         }
1077         fclose(f);
1078     }
1079 #elif defined (__NetBSD__)
1080     {
1081         int mib[2];
1082         int value;
1083         size_t val_len;
1084         char model[256];
1085         char *cpuclass;
1086         FILE *f = fopen("/var/run/dmesg.boot", "r");
1087
1088         /* first deduce as much as possible from the sysctls */
1089         mib[0] = CTL_MACHDEP;
1090 #ifdef CPU_FPU_PRESENT
1091         mib[1] = CPU_FPU_PRESENT;
1092         val_len = sizeof(value);
1093         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1094             user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = !value;
1095 #endif
1096 #ifdef CPU_SSE
1097         mib[1] = CPU_SSE;   /* this should imply MMX */
1098         val_len = sizeof(value);
1099         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1100             if (value) user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1101 #endif
1102 #ifdef CPU_SSE2
1103         mib[1] = CPU_SSE2;  /* this should imply MMX */
1104         val_len = sizeof(value);
1105         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1106             if (value) user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1107 #endif
1108         mib[0] = CTL_HW;
1109         mib[1] = HW_NCPU;
1110         val_len = sizeof(value);
1111         if (sysctl(mib, 2, &value, &val_len, NULL, 0) >= 0)
1112             if (value > NtCurrentTeb()->Peb->NumberOfProcessors)
1113                 NtCurrentTeb()->Peb->NumberOfProcessors = value;
1114         mib[1] = HW_MODEL;
1115         val_len = sizeof(model)-1;
1116         if (sysctl(mib, 2, model, &val_len, NULL, 0) >= 0)
1117         {
1118             model[val_len] = '\0'; /* just in case */
1119             cpuclass = strstr(model, "-class");
1120             if (cpuclass != NULL) {
1121                 while(cpuclass > model && cpuclass[0] != '(') cpuclass--;
1122                 if (!strncmp(cpuclass+1, "386", 3))
1123                 {
1124                     cached_sci.Level= 3;
1125                 }
1126                 if (!strncmp(cpuclass+1, "486", 3))
1127                 {
1128                     cached_sci.Level= 4;
1129                 }
1130                 if (!strncmp(cpuclass+1, "586", 3))
1131                 {
1132                     cached_sci.Level= 5;
1133                 }
1134                 if (!strncmp(cpuclass+1, "686", 3))
1135                 {
1136                     cached_sci.Level= 6;
1137                     /* this should imply MMX */
1138                     user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1139                 }
1140             }
1141         }
1142
1143         /* it may be worth reading from /var/run/dmesg.boot for
1144            additional information such as CX8, MMX and TSC
1145            (however this information should be considered less
1146            reliable than that from the sysctl calls) */
1147         if (f != NULL)
1148         {
1149             while (fgets(model, 255, f) != NULL)
1150             {
1151                 int cpu, features;
1152                 if (sscanf(model, "cpu%d: features %x<", &cpu, &features) == 2)
1153                 {
1154                     /* we could scan the string but it is easier
1155                        to test the bits directly */
1156                     if (features & 0x1)
1157                         user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = TRUE;
1158                     if (features & 0x10)
1159                         user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
1160                     if (features & 0x100)
1161                         user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
1162                     if (features & 0x800000)
1163                         user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1164
1165                     break;
1166                 }
1167             }
1168             fclose(f);
1169         }
1170     }
1171 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
1172     {
1173         int ret, num;
1174         size_t len;
1175
1176         get_cpuinfo( &cached_sci );
1177
1178         /* Check for OS support of SSE -- Is this used, and should it be sse1 or sse2? */
1179         /*len = sizeof(num);
1180           ret = sysctlbyname("hw.instruction_sse", &num, &len, NULL, 0);
1181           if (!ret)
1182           user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = num;*/
1183
1184         len = sizeof(num);
1185         ret = sysctlbyname("hw.ncpu", &num, &len, NULL, 0);
1186         if (!ret)
1187             NtCurrentTeb()->Peb->NumberOfProcessors = num;
1188
1189         len = sizeof(num);
1190         if (!sysctlbyname("hw.clockrate", &num, &len, NULL, 0))
1191             cpuHz = num * 1000 * 1000;
1192     }
1193 #elif defined(__sun)
1194     {
1195         int num = sysconf( _SC_NPROCESSORS_ONLN );
1196
1197         if (num == -1) num = 1;
1198         get_cpuinfo( &cached_sci );
1199         NtCurrentTeb()->Peb->NumberOfProcessors = num;
1200     }
1201 #elif defined (__OpenBSD__)
1202     {
1203         int mib[2], num, ret;
1204         size_t len;
1205
1206         mib[0] = CTL_HW;
1207         mib[1] = HW_NCPU;
1208         len = sizeof(num);
1209
1210         ret = sysctl(mib, 2, &num, &len, NULL, 0);
1211         if (!ret)
1212             NtCurrentTeb()->Peb->NumberOfProcessors = num;
1213     }
1214 #elif defined (__APPLE__)
1215     {
1216         size_t valSize;
1217         unsigned long long longVal;
1218         int value;
1219         int cputype;
1220         char buffer[1024];
1221
1222         valSize = sizeof(int);
1223         if (sysctlbyname ("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
1224         {
1225             if (value)
1226                 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = FALSE;
1227             else
1228                 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = TRUE;
1229         }
1230         valSize = sizeof(int);
1231         if (sysctlbyname ("hw.ncpu", &value, &valSize, NULL, 0) == 0)
1232             NtCurrentTeb()->Peb->NumberOfProcessors = value;
1233
1234         /* FIXME: we don't use the "hw.activecpu" value... but the cached one */
1235
1236         valSize = sizeof(int);
1237         if (sysctlbyname ("hw.cputype", &cputype, &valSize, NULL, 0) == 0)
1238         {
1239             switch (cputype)
1240             {
1241             case CPU_TYPE_POWERPC:
1242                 cached_sci.Architecture = PROCESSOR_ARCHITECTURE_PPC;
1243                 valSize = sizeof(int);
1244                 if (sysctlbyname ("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
1245                 {
1246                     switch (value)
1247                     {
1248                     case CPU_SUBTYPE_POWERPC_601:
1249                     case CPU_SUBTYPE_POWERPC_602:       cached_sci.Level = 1;   break;
1250                     case CPU_SUBTYPE_POWERPC_603:       cached_sci.Level = 3;   break;
1251                     case CPU_SUBTYPE_POWERPC_603e:
1252                     case CPU_SUBTYPE_POWERPC_603ev:     cached_sci.Level = 6;   break;
1253                     case CPU_SUBTYPE_POWERPC_604:       cached_sci.Level = 4;   break;
1254                     case CPU_SUBTYPE_POWERPC_604e:      cached_sci.Level = 9;   break;
1255                     case CPU_SUBTYPE_POWERPC_620:       cached_sci.Level = 20;  break;
1256                     case CPU_SUBTYPE_POWERPC_750:       /* G3/G4 derive from 603 so ... */
1257                     case CPU_SUBTYPE_POWERPC_7400:
1258                     case CPU_SUBTYPE_POWERPC_7450:      cached_sci.Level = 6;   break;
1259                     case CPU_SUBTYPE_POWERPC_970:       cached_sci.Level = 9;
1260                         /* :o) user_shared_data->ProcessorFeatures[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
1261                         break;
1262                     default: break;
1263                     }
1264                 }
1265                 break; /* CPU_TYPE_POWERPC */
1266             case CPU_TYPE_I386:
1267                 cached_sci.Architecture = PROCESSOR_ARCHITECTURE_INTEL;
1268                 valSize = sizeof(int);
1269                 if (sysctlbyname ("machdep.cpu.family", &value, &valSize, NULL, 0) == 0)
1270                 {
1271                     cached_sci.Level = value;
1272                 }
1273                 valSize = sizeof(int);
1274                 if (sysctlbyname ("machdep.cpu.model", &value, &valSize, NULL, 0) == 0)
1275                     cached_sci.Revision = (value << 8);
1276                 valSize = sizeof(int);
1277                 if (sysctlbyname ("machdep.cpu.stepping", &value, &valSize, NULL, 0) == 0)
1278                     cached_sci.Revision |= value;
1279                 valSize = sizeof(buffer);
1280                 if (sysctlbyname ("machdep.cpu.features", buffer, &valSize, NULL, 0) == 0)
1281                 {
1282                     if (!valSize) FIXME("Buffer not large enough, please increase\n");
1283                     if (strstr(buffer, "CX8"))   user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = TRUE;
1284                     if (strstr(buffer, "CX16"))  user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE128] = TRUE;
1285                     if (strstr(buffer, "MMX"))   user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = TRUE;
1286                     if (strstr(buffer, "TSC"))   user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = TRUE;
1287                     if (strstr(buffer, "3DNOW")) user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = TRUE;
1288                     if (strstr(buffer, "SSE"))   user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = TRUE;
1289                     if (strstr(buffer, "SSE2"))  user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = TRUE;
1290                     if (strstr(buffer, "SSE3"))  user_shared_data->ProcessorFeatures[PF_SSE3_INSTRUCTIONS_AVAILABLE] = TRUE;
1291                     if (strstr(buffer, "PAE"))   user_shared_data->ProcessorFeatures[PF_PAE_ENABLED] = TRUE;
1292                     if (strstr(buffer, "HTT"))   cached_sci.FeatureSet |= CPU_FEATURE_HTT;
1293                 }
1294                 break; /* CPU_TYPE_I386 */
1295             default: break;
1296             } /* switch (cputype) */
1297         }
1298         valSize = sizeof(longVal);
1299         if (!sysctlbyname("hw.cpufrequency", &longVal, &valSize, NULL, 0))
1300             cpuHz = longVal;
1301     }
1302 #else
1303     FIXME("not yet supported on this system\n");
1304 #endif
1305     TRACE("<- CPU arch %d, level %d, rev %d, features 0x%x\n",
1306           cached_sci.Architecture, cached_sci.Level, cached_sci.Revision, cached_sci.FeatureSet);
1307 }
1308
1309 /******************************************************************************
1310  * NtQuerySystemInformation [NTDLL.@]
1311  * ZwQuerySystemInformation [NTDLL.@]
1312  *
1313  * ARGUMENTS:
1314  *  SystemInformationClass      Index to a certain information structure
1315  *      SystemTimeAdjustmentInformation SYSTEM_TIME_ADJUSTMENT
1316  *      SystemCacheInformation          SYSTEM_CACHE_INFORMATION
1317  *      SystemConfigurationInformation  CONFIGURATION_INFORMATION
1318  *      observed (class/len):
1319  *              0x0/0x2c
1320  *              0x12/0x18
1321  *              0x2/0x138
1322  *              0x8/0x600
1323  *              0x25/0xc
1324  *  SystemInformation   caller supplies storage for the information structure
1325  *  Length              size of the structure
1326  *  ResultLength        Data written
1327  */
1328 NTSTATUS WINAPI NtQuerySystemInformation(
1329         IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
1330         OUT PVOID SystemInformation,
1331         IN ULONG Length,
1332         OUT PULONG ResultLength)
1333 {
1334     NTSTATUS    ret = STATUS_SUCCESS;
1335     ULONG       len = 0;
1336
1337     TRACE("(0x%08x,%p,0x%08x,%p)\n",
1338           SystemInformationClass,SystemInformation,Length,ResultLength);
1339
1340     switch (SystemInformationClass)
1341     {
1342     case SystemBasicInformation:
1343         {
1344             SYSTEM_BASIC_INFORMATION sbi;
1345
1346             virtual_get_system_info( &sbi );
1347             len = sizeof(sbi);
1348
1349             if ( Length == len)
1350             {
1351                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1352                 else memcpy( SystemInformation, &sbi, len);
1353             }
1354             else ret = STATUS_INFO_LENGTH_MISMATCH;
1355         }
1356         break;
1357     case SystemCpuInformation:
1358         if (Length >= (len = sizeof(cached_sci)))
1359         {
1360             if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1361             else memcpy(SystemInformation, &cached_sci, len);
1362         }
1363         else ret = STATUS_INFO_LENGTH_MISMATCH;
1364         break;
1365     case SystemPerformanceInformation:
1366         {
1367             SYSTEM_PERFORMANCE_INFORMATION spi;
1368             static BOOL fixme_written = FALSE;
1369             FILE *fp;
1370
1371             memset(&spi, 0 , sizeof(spi));
1372             len = sizeof(spi);
1373
1374             spi.Reserved3 = 0x7fffffff; /* Available paged pool memory? */
1375
1376             if ((fp = fopen("/proc/uptime", "r")))
1377             {
1378                 double uptime, idle_time;
1379
1380                 fscanf(fp, "%lf %lf", &uptime, &idle_time);
1381                 fclose(fp);
1382                 spi.IdleTime.QuadPart = 10000000 * idle_time;
1383             }
1384             else
1385             {
1386                 static ULONGLONG idle;
1387                 /* many programs expect IdleTime to change so fake change */
1388                 spi.IdleTime.QuadPart = ++idle;
1389             }
1390
1391             if (Length >= len)
1392             {
1393                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1394                 else memcpy( SystemInformation, &spi, len);
1395             }
1396             else ret = STATUS_INFO_LENGTH_MISMATCH;
1397             if(!fixme_written) {
1398                 FIXME("info_class SYSTEM_PERFORMANCE_INFORMATION\n");
1399                 fixme_written = TRUE;
1400             }
1401         }
1402         break;
1403     case SystemTimeOfDayInformation:
1404         {
1405             SYSTEM_TIMEOFDAY_INFORMATION sti;
1406
1407             memset(&sti, 0 , sizeof(sti));
1408
1409             /* liKeSystemTime, liExpTimeZoneBias, uCurrentTimeZoneId */
1410             sti.liKeBootTime.QuadPart = server_start_time;
1411
1412             if (Length <= sizeof(sti))
1413             {
1414                 len = Length;
1415                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1416                 else memcpy( SystemInformation, &sti, Length);
1417             }
1418             else ret = STATUS_INFO_LENGTH_MISMATCH;
1419         }
1420         break;
1421     case SystemProcessInformation:
1422         {
1423             SYSTEM_PROCESS_INFORMATION* spi = SystemInformation;
1424             SYSTEM_PROCESS_INFORMATION* last = NULL;
1425             HANDLE hSnap = 0;
1426             WCHAR procname[1024];
1427             WCHAR* exename;
1428             DWORD wlen = 0;
1429             DWORD procstructlen = 0;
1430
1431             SERVER_START_REQ( create_snapshot )
1432             {
1433                 req->flags      = SNAP_PROCESS | SNAP_THREAD;
1434                 req->attributes = 0;
1435                 if (!(ret = wine_server_call( req )))
1436                     hSnap = wine_server_ptr_handle( reply->handle );
1437             }
1438             SERVER_END_REQ;
1439             len = 0;
1440             while (ret == STATUS_SUCCESS)
1441             {
1442                 SERVER_START_REQ( next_process )
1443                 {
1444                     req->handle = wine_server_obj_handle( hSnap );
1445                     req->reset = (len == 0);
1446                     wine_server_set_reply( req, procname, sizeof(procname)-sizeof(WCHAR) );
1447                     if (!(ret = wine_server_call( req )))
1448                     {
1449                         /* Make sure procname is 0 terminated */
1450                         procname[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
1451
1452                         /* Get only the executable name, not the path */
1453                         if ((exename = strrchrW(procname, '\\')) != NULL) exename++;
1454                         else exename = procname;
1455
1456                         wlen = (strlenW(exename) + 1) * sizeof(WCHAR);
1457
1458                         procstructlen = sizeof(*spi) + wlen + ((reply->threads - 1) * sizeof(SYSTEM_THREAD_INFORMATION));
1459
1460                         if (Length >= len + procstructlen)
1461                         {
1462                             /* ftCreationTime, ftUserTime, ftKernelTime;
1463                              * vmCounters, ioCounters
1464                              */
1465  
1466                             memset(spi, 0, sizeof(*spi));
1467
1468                             spi->NextEntryOffset = procstructlen - wlen;
1469                             spi->dwThreadCount = reply->threads;
1470
1471                             /* spi->pszProcessName will be set later on */
1472
1473                             spi->dwBasePriority = reply->priority;
1474                             spi->UniqueProcessId = UlongToHandle(reply->pid);
1475                             spi->ParentProcessId = UlongToHandle(reply->ppid);
1476                             spi->HandleCount = reply->handles;
1477
1478                             /* spi->ti will be set later on */
1479
1480                             len += procstructlen;
1481                         }
1482                         else ret = STATUS_INFO_LENGTH_MISMATCH;
1483                     }
1484                 }
1485                 SERVER_END_REQ;
1486  
1487                 if (ret != STATUS_SUCCESS)
1488                 {
1489                     if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
1490                     break;
1491                 }
1492                 else /* Length is already checked for */
1493                 {
1494                     int     i, j;
1495
1496                     /* set thread info */
1497                     i = j = 0;
1498                     while (ret == STATUS_SUCCESS)
1499                     {
1500                         SERVER_START_REQ( next_thread )
1501                         {
1502                             req->handle = wine_server_obj_handle( hSnap );
1503                             req->reset = (j == 0);
1504                             if (!(ret = wine_server_call( req )))
1505                             {
1506                                 j++;
1507                                 if (UlongToHandle(reply->pid) == spi->UniqueProcessId)
1508                                 {
1509                                     /* ftKernelTime, ftUserTime, ftCreateTime;
1510                                      * dwTickCount, dwStartAddress
1511                                      */
1512
1513                                     memset(&spi->ti[i], 0, sizeof(spi->ti));
1514
1515                                     spi->ti[i].CreateTime.QuadPart = 0xdeadbeef;
1516                                     spi->ti[i].ClientId.UniqueProcess = UlongToHandle(reply->pid);
1517                                     spi->ti[i].ClientId.UniqueThread  = UlongToHandle(reply->tid);
1518                                     spi->ti[i].dwCurrentPriority = reply->base_pri + reply->delta_pri;
1519                                     spi->ti[i].dwBasePriority = reply->base_pri;
1520                                     i++;
1521                                 }
1522                             }
1523                         }
1524                         SERVER_END_REQ;
1525                     }
1526                     if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
1527
1528                     /* now append process name */
1529                     spi->ProcessName.Buffer = (WCHAR*)((char*)spi + spi->NextEntryOffset);
1530                     spi->ProcessName.Length = wlen - sizeof(WCHAR);
1531                     spi->ProcessName.MaximumLength = wlen;
1532                     memcpy( spi->ProcessName.Buffer, exename, wlen );
1533                     spi->NextEntryOffset += wlen;
1534
1535                     last = spi;
1536                     spi = (SYSTEM_PROCESS_INFORMATION*)((char*)spi + spi->NextEntryOffset);
1537                 }
1538             }
1539             if (ret == STATUS_SUCCESS && last) last->NextEntryOffset = 0;
1540             if (hSnap) NtClose(hSnap);
1541         }
1542         break;
1543     case SystemProcessorPerformanceInformation:
1544         {
1545             SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi = NULL;
1546             unsigned int cpus = 0;
1547             int out_cpus = Length / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
1548
1549             if (out_cpus == 0)
1550             {
1551                 len = 0;
1552                 ret = STATUS_INFO_LENGTH_MISMATCH;
1553                 break;
1554             }
1555             else
1556 #ifdef __APPLE__
1557             {
1558                 processor_cpu_load_info_data_t *pinfo;
1559                 mach_msg_type_number_t info_count;
1560
1561                 if (host_processor_info (mach_host_self (),
1562                                          PROCESSOR_CPU_LOAD_INFO,
1563                                          &cpus,
1564                                          (processor_info_array_t*)&pinfo,
1565                                          &info_count) == 0)
1566                 {
1567                     int i;
1568                     cpus = min(cpus,out_cpus);
1569                     len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
1570                     sppi = RtlAllocateHeap(GetProcessHeap(), 0,len);
1571                     for (i = 0; i < cpus; i++)
1572                     {
1573                         sppi[i].IdleTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_IDLE];
1574                         sppi[i].KernelTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_SYSTEM];
1575                         sppi[i].UserTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_USER];
1576                     }
1577                     vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count * sizeof(natural_t));
1578                 }
1579             }
1580 #else
1581             {
1582                 FILE *cpuinfo = fopen("/proc/stat", "r");
1583                 if (cpuinfo)
1584                 {
1585                     unsigned long clk_tck = sysconf(_SC_CLK_TCK);
1586                     unsigned long usr,nice,sys,idle,remainder[8];
1587                     int i, count;
1588                     char name[32];
1589                     char line[255];
1590
1591                     /* first line is combined usage */
1592                     while (fgets(line,255,cpuinfo))
1593                     {
1594                         count = sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
1595                                        name, &usr, &nice, &sys, &idle,
1596                                        &remainder[0], &remainder[1], &remainder[2], &remainder[3],
1597                                        &remainder[4], &remainder[5], &remainder[6], &remainder[7]);
1598
1599                         if (count < 5 || strncmp( name, "cpu", 3 )) break;
1600                         for (i = 0; i + 5 < count; ++i) sys += remainder[i];
1601                         sys += idle;
1602                         usr += nice;
1603                         cpus = atoi( name + 3 ) + 1;
1604                         if (cpus > out_cpus) break;
1605                         len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
1606                         if (sppi)
1607                             sppi = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sppi, len );
1608                         else
1609                             sppi = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, len );
1610
1611                         sppi[cpus-1].IdleTime.QuadPart   = (ULONGLONG)idle * 10000000 / clk_tck;
1612                         sppi[cpus-1].KernelTime.QuadPart = (ULONGLONG)sys * 10000000 / clk_tck;
1613                         sppi[cpus-1].UserTime.QuadPart   = (ULONGLONG)usr * 10000000 / clk_tck;
1614                     }
1615                     fclose(cpuinfo);
1616                 }
1617             }
1618 #endif
1619
1620             if (cpus == 0)
1621             {
1622                 static int i = 1;
1623                 int n;
1624                 cpus = min(NtCurrentTeb()->Peb->NumberOfProcessors, out_cpus);
1625                 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
1626                 sppi = RtlAllocateHeap(GetProcessHeap(), 0, len);
1627                 FIXME("stub info_class SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION\n");
1628                 /* many programs expect these values to change so fake change */
1629                 for (n = 0; n < cpus; n++)
1630                 {
1631                     sppi[n].KernelTime.QuadPart = 1 * i;
1632                     sppi[n].UserTime.QuadPart   = 2 * i;
1633                     sppi[n].IdleTime.QuadPart   = 3 * i;
1634                 }
1635                 i++;
1636             }
1637
1638             if (Length >= len)
1639             {
1640                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1641                 else memcpy( SystemInformation, sppi, len);
1642             }
1643             else ret = STATUS_INFO_LENGTH_MISMATCH;
1644
1645             RtlFreeHeap(GetProcessHeap(),0,sppi);
1646         }
1647         break;
1648     case SystemModuleInformation:
1649         /* FIXME: should be system-wide */
1650         if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1651         else ret = LdrQueryProcessModuleInformation( SystemInformation, Length, &len );
1652         break;
1653     case SystemHandleInformation:
1654         {
1655             SYSTEM_HANDLE_INFORMATION shi;
1656
1657             memset(&shi, 0, sizeof(shi));
1658             len = sizeof(shi);
1659
1660             if ( Length >= len)
1661             {
1662                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1663                 else memcpy( SystemInformation, &shi, len);
1664             }
1665             else ret = STATUS_INFO_LENGTH_MISMATCH;
1666             FIXME("info_class SYSTEM_HANDLE_INFORMATION\n");
1667         }
1668         break;
1669     case SystemCacheInformation:
1670         {
1671             SYSTEM_CACHE_INFORMATION sci;
1672
1673             memset(&sci, 0, sizeof(sci)); /* FIXME */
1674             len = sizeof(sci);
1675
1676             if ( Length >= len)
1677             {
1678                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1679                 else memcpy( SystemInformation, &sci, len);
1680             }
1681             else ret = STATUS_INFO_LENGTH_MISMATCH;
1682             FIXME("info_class SYSTEM_CACHE_INFORMATION\n");
1683         }
1684         break;
1685     case SystemInterruptInformation:
1686         {
1687             SYSTEM_INTERRUPT_INFORMATION sii;
1688
1689             memset(&sii, 0, sizeof(sii));
1690             len = sizeof(sii);
1691
1692             if ( Length >= len)
1693             {
1694                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1695                 else memcpy( SystemInformation, &sii, len);
1696             }
1697             else ret = STATUS_INFO_LENGTH_MISMATCH;
1698             FIXME("info_class SYSTEM_INTERRUPT_INFORMATION\n");
1699         }
1700         break;
1701     case SystemKernelDebuggerInformation:
1702         {
1703             SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
1704
1705             skdi.DebuggerEnabled = FALSE;
1706             skdi.DebuggerNotPresent = TRUE;
1707             len = sizeof(skdi);
1708
1709             if ( Length >= len)
1710             {
1711                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1712                 else memcpy( SystemInformation, &skdi, len);
1713             }
1714             else ret = STATUS_INFO_LENGTH_MISMATCH;
1715         }
1716         break;
1717     case SystemRegistryQuotaInformation:
1718         {
1719             /* Something to do with the size of the registry             *
1720              * Since we don't have a size limitation, fake it            *
1721              * This is almost certainly wrong.                           *
1722              * This sets each of the three words in the struct to 32 MB, *
1723              * which is enough to make the IE 5 installer happy.         */
1724             SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
1725
1726             srqi.RegistryQuotaAllowed = 0x2000000;
1727             srqi.RegistryQuotaUsed = 0x200000;
1728             srqi.Reserved1 = (void*)0x200000;
1729             len = sizeof(srqi);
1730
1731             if ( Length >= len)
1732             {
1733                 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1734                 else
1735                 {
1736                     FIXME("SystemRegistryQuotaInformation: faking max registry size of 32 MB\n");
1737                     memcpy( SystemInformation, &srqi, len);
1738                 }
1739             }
1740             else ret = STATUS_INFO_LENGTH_MISMATCH;
1741         }
1742         break;
1743     default:
1744         FIXME("(0x%08x,%p,0x%08x,%p) stub\n",
1745               SystemInformationClass,SystemInformation,Length,ResultLength);
1746
1747         /* Several Information Classes are not implemented on Windows and return 2 different values 
1748          * STATUS_NOT_IMPLEMENTED or STATUS_INVALID_INFO_CLASS
1749          * in 95% of the cases it's STATUS_INVALID_INFO_CLASS, so use this as the default
1750         */
1751         ret = STATUS_INVALID_INFO_CLASS;
1752     }
1753
1754     if (ResultLength) *ResultLength = len;
1755
1756     return ret;
1757 }
1758
1759 /******************************************************************************
1760  * NtSetSystemInformation [NTDLL.@]
1761  * ZwSetSystemInformation [NTDLL.@]
1762  */
1763 NTSTATUS WINAPI NtSetSystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG Length)
1764 {
1765     FIXME("(0x%08x,%p,0x%08x) stub\n",SystemInformationClass,SystemInformation,Length);
1766     return STATUS_SUCCESS;
1767 }
1768
1769 /******************************************************************************
1770  *  NtCreatePagingFile          [NTDLL.@]
1771  *  ZwCreatePagingFile          [NTDLL.@]
1772  */
1773 NTSTATUS WINAPI NtCreatePagingFile(
1774         PUNICODE_STRING PageFileName,
1775         PLARGE_INTEGER MinimumSize,
1776         PLARGE_INTEGER MaximumSize,
1777         PLARGE_INTEGER ActualSize)
1778 {
1779     FIXME("(%p %p %p %p) stub\n", PageFileName, MinimumSize, MaximumSize, ActualSize);
1780     return STATUS_SUCCESS;
1781 }
1782
1783 /******************************************************************************
1784  *  NtDisplayString                             [NTDLL.@]
1785  *
1786  * writes a string to the nt-textmode screen eg. during startup
1787  */
1788 NTSTATUS WINAPI NtDisplayString ( PUNICODE_STRING string )
1789 {
1790     STRING stringA;
1791     NTSTATUS ret;
1792
1793     if (!(ret = RtlUnicodeStringToAnsiString( &stringA, string, TRUE )))
1794     {
1795         MESSAGE( "%.*s", stringA.Length, stringA.Buffer );
1796         RtlFreeAnsiString( &stringA );
1797     }
1798     return ret;
1799 }
1800
1801 /******************************************************************************
1802  *  NtInitiatePowerAction                       [NTDLL.@]
1803  *
1804  */
1805 NTSTATUS WINAPI NtInitiatePowerAction(
1806         IN POWER_ACTION SystemAction,
1807         IN SYSTEM_POWER_STATE MinSystemState,
1808         IN ULONG Flags,
1809         IN BOOLEAN Asynchronous)
1810 {
1811         FIXME("(%d,%d,0x%08x,%d),stub\n",
1812                 SystemAction,MinSystemState,Flags,Asynchronous);
1813         return STATUS_NOT_IMPLEMENTED;
1814 }
1815         
1816
1817 /******************************************************************************
1818  *  NtPowerInformation                          [NTDLL.@]
1819  *
1820  */
1821 NTSTATUS WINAPI NtPowerInformation(
1822         IN POWER_INFORMATION_LEVEL InformationLevel,
1823         IN PVOID lpInputBuffer,
1824         IN ULONG nInputBufferSize,
1825         IN PVOID lpOutputBuffer,
1826         IN ULONG nOutputBufferSize)
1827 {
1828         TRACE("(%d,%p,%d,%p,%d)\n",
1829                 InformationLevel,lpInputBuffer,nInputBufferSize,lpOutputBuffer,nOutputBufferSize);
1830         switch(InformationLevel) {
1831                 case SystemPowerCapabilities: {
1832                         PSYSTEM_POWER_CAPABILITIES PowerCaps = lpOutputBuffer;
1833                         FIXME("semi-stub: SystemPowerCapabilities\n");
1834                         if (nOutputBufferSize < sizeof(SYSTEM_POWER_CAPABILITIES))
1835                                 return STATUS_BUFFER_TOO_SMALL;
1836                         /* FIXME: These values are based off a native XP desktop, should probably use APM/ACPI to get the 'real' values */
1837                         PowerCaps->PowerButtonPresent = TRUE;
1838                         PowerCaps->SleepButtonPresent = FALSE;
1839                         PowerCaps->LidPresent = FALSE;
1840                         PowerCaps->SystemS1 = TRUE;
1841                         PowerCaps->SystemS2 = FALSE;
1842                         PowerCaps->SystemS3 = FALSE;
1843                         PowerCaps->SystemS4 = TRUE;
1844                         PowerCaps->SystemS5 = TRUE;
1845                         PowerCaps->HiberFilePresent = TRUE;
1846                         PowerCaps->FullWake = TRUE;
1847                         PowerCaps->VideoDimPresent = FALSE;
1848                         PowerCaps->ApmPresent = FALSE;
1849                         PowerCaps->UpsPresent = FALSE;
1850                         PowerCaps->ThermalControl = FALSE;
1851                         PowerCaps->ProcessorThrottle = FALSE;
1852                         PowerCaps->ProcessorMinThrottle = 100;
1853                         PowerCaps->ProcessorMaxThrottle = 100;
1854                         PowerCaps->DiskSpinDown = TRUE;
1855                         PowerCaps->SystemBatteriesPresent = FALSE;
1856                         PowerCaps->BatteriesAreShortTerm = FALSE;
1857                         PowerCaps->BatteryScale[0].Granularity = 0;
1858                         PowerCaps->BatteryScale[0].Capacity = 0;
1859                         PowerCaps->BatteryScale[1].Granularity = 0;
1860                         PowerCaps->BatteryScale[1].Capacity = 0;
1861                         PowerCaps->BatteryScale[2].Granularity = 0;
1862                         PowerCaps->BatteryScale[2].Capacity = 0;
1863                         PowerCaps->AcOnLineWake = PowerSystemUnspecified;
1864                         PowerCaps->SoftLidWake = PowerSystemUnspecified;
1865                         PowerCaps->RtcWake = PowerSystemSleeping1;
1866                         PowerCaps->MinDeviceWakeState = PowerSystemUnspecified;
1867                         PowerCaps->DefaultLowLatencyWake = PowerSystemUnspecified;
1868                         return STATUS_SUCCESS;
1869                 }
1870                 case SystemExecutionState: {
1871                         PULONG ExecutionState = lpOutputBuffer;
1872                         WARN("semi-stub: SystemExecutionState\n"); /* Needed for .NET Framework, but using a FIXME is really noisy. */
1873                         if (lpInputBuffer != NULL)
1874                                 return STATUS_INVALID_PARAMETER;
1875                         /* FIXME: The actual state should be the value set by SetThreadExecutionState which is not currently implemented. */
1876                         *ExecutionState = ES_USER_PRESENT;
1877                         return STATUS_SUCCESS;
1878                 }
1879                 case ProcessorInformation: {
1880                         PPROCESSOR_POWER_INFORMATION cpu_power = lpOutputBuffer;
1881
1882                         WARN("semi-stub: ProcessorInformation\n");
1883                         if (nOutputBufferSize < sizeof(PROCESSOR_POWER_INFORMATION))
1884                                 return STATUS_BUFFER_TOO_SMALL;
1885                         cpu_power->Number = NtCurrentTeb()->Peb->NumberOfProcessors;
1886                         cpu_power->MaxMhz = cpuHz / 1000000;
1887                         cpu_power->CurrentMhz = cpuHz / 1000000;
1888                         cpu_power->MhzLimit = cpuHz / 1000000;
1889                         cpu_power->MaxIdleState = 0; /* FIXME */
1890                         cpu_power->CurrentIdleState = 0; /* FIXME */
1891                         return STATUS_SUCCESS;
1892                 }
1893                 default:
1894                         /* FIXME: Needed by .NET Framework */
1895                         WARN("Unimplemented NtPowerInformation action: %d\n", InformationLevel);
1896                         return STATUS_NOT_IMPLEMENTED;
1897         }
1898 }
1899
1900 /******************************************************************************
1901  *  NtShutdownSystem                            [NTDLL.@]
1902  *
1903  */
1904 NTSTATUS WINAPI NtShutdownSystem(SHUTDOWN_ACTION Action)
1905 {
1906     FIXME("%d\n",Action);
1907     return STATUS_SUCCESS;
1908 }
1909
1910 /******************************************************************************
1911  *  NtAllocateLocallyUniqueId (NTDLL.@)
1912  */
1913 NTSTATUS WINAPI NtAllocateLocallyUniqueId(PLUID Luid)
1914 {
1915     NTSTATUS status;
1916
1917     TRACE("%p\n", Luid);
1918
1919     if (!Luid)
1920         return STATUS_ACCESS_VIOLATION;
1921
1922     SERVER_START_REQ( allocate_locally_unique_id )
1923     {
1924         status = wine_server_call( req );
1925         if (!status)
1926         {
1927             Luid->LowPart = reply->luid.low_part;
1928             Luid->HighPart = reply->luid.high_part;
1929         }
1930     }
1931     SERVER_END_REQ;
1932
1933     return status;
1934 }
1935
1936 /******************************************************************************
1937  *        VerSetConditionMask   (NTDLL.@)
1938  */
1939 ULONGLONG WINAPI VerSetConditionMask( ULONGLONG dwlConditionMask, DWORD dwTypeBitMask,
1940                                       BYTE dwConditionMask)
1941 {
1942     if(dwTypeBitMask == 0)
1943         return dwlConditionMask;
1944     dwConditionMask &= 0x07;
1945     if(dwConditionMask == 0)
1946         return dwlConditionMask;
1947
1948     if(dwTypeBitMask & VER_PRODUCT_TYPE)
1949         dwlConditionMask |= dwConditionMask << 7*3;
1950     else if (dwTypeBitMask & VER_SUITENAME)
1951         dwlConditionMask |= dwConditionMask << 6*3;
1952     else if (dwTypeBitMask & VER_SERVICEPACKMAJOR)
1953         dwlConditionMask |= dwConditionMask << 5*3;
1954     else if (dwTypeBitMask & VER_SERVICEPACKMINOR)
1955         dwlConditionMask |= dwConditionMask << 4*3;
1956     else if (dwTypeBitMask & VER_PLATFORMID)
1957         dwlConditionMask |= dwConditionMask << 3*3;
1958     else if (dwTypeBitMask & VER_BUILDNUMBER)
1959         dwlConditionMask |= dwConditionMask << 2*3;
1960     else if (dwTypeBitMask & VER_MAJORVERSION)
1961         dwlConditionMask |= dwConditionMask << 1*3;
1962     else if (dwTypeBitMask & VER_MINORVERSION)
1963         dwlConditionMask |= dwConditionMask << 0*3;
1964     return dwlConditionMask;
1965 }
1966
1967 /******************************************************************************
1968  *  NtAccessCheckAndAuditAlarm   (NTDLL.@)
1969  *  ZwAccessCheckAndAuditAlarm   (NTDLL.@)
1970  */
1971 NTSTATUS WINAPI NtAccessCheckAndAuditAlarm(PUNICODE_STRING SubsystemName, HANDLE HandleId, PUNICODE_STRING ObjectTypeName,
1972                                            PUNICODE_STRING ObjectName, PSECURITY_DESCRIPTOR SecurityDescriptor,
1973                                            ACCESS_MASK DesiredAccess, PGENERIC_MAPPING GenericMapping, BOOLEAN ObjectCreation,
1974                                            PACCESS_MASK GrantedAccess, PBOOLEAN AccessStatus, PBOOLEAN GenerateOnClose)
1975 {
1976     FIXME("(%s, %p, %s, %p, 0x%08x, %p, %d, %p, %p, %p), stub\n", debugstr_us(SubsystemName), HandleId,
1977           debugstr_us(ObjectTypeName), SecurityDescriptor, DesiredAccess, GenericMapping, ObjectCreation,
1978           GrantedAccess, AccessStatus, GenerateOnClose);
1979
1980     return STATUS_NOT_IMPLEMENTED;
1981 }
1982
1983 /******************************************************************************
1984  *  NtSystemDebugControl   (NTDLL.@)
1985  *  ZwSystemDebugControl   (NTDLL.@)
1986  */
1987 NTSTATUS WINAPI NtSystemDebugControl(SYSDBG_COMMAND command, PVOID inbuffer, ULONG inbuflength, PVOID outbuffer,
1988                                      ULONG outbuflength, PULONG retlength)
1989 {
1990     FIXME("(%d, %p, %d, %p, %d, %p), stub\n", command, inbuffer, inbuflength, outbuffer, outbuflength, retlength);
1991
1992     return STATUS_NOT_IMPLEMENTED;
1993 }