user32/tests: Remove an unused macro.
[wine.git] / dlls / ntdll / nt.c
blob3f5ae2cf23260f6dd1acbe519832cf1840c90cfb
1 /*
2 * NT basis DLL
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)
7 * Copyright 1996-1998 Marcus Meissner
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.
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.
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
24 #include "config.h"
25 #include "wine/port.h"
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
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>
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"
61 #ifdef __APPLE__
62 #include <mach/mach_init.h>
63 #include <mach/mach_host.h>
64 #include <mach/vm_map.h>
65 #endif
67 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
70 * Token
73 /******************************************************************************
74 * NtDuplicateToken [NTDLL.@]
75 * ZwDuplicateToken [NTDLL.@]
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)
85 NTSTATUS status;
87 TRACE("(%p,0x%08x,%s,0x%08x,0x%08x,%p)\n",
88 ExistingToken, DesiredAccess, debugstr_ObjectAttributes(ObjectAttributes),
89 ImpersonationLevel, TokenType, NewToken);
91 if (ObjectAttributes && ObjectAttributes->SecurityQualityOfService)
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;
101 SERVER_START_REQ( duplicate_token )
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 );
111 SERVER_END_REQ;
113 return status;
116 /******************************************************************************
117 * NtOpenProcessToken [NTDLL.@]
118 * ZwOpenProcessToken [NTDLL.@]
120 NTSTATUS WINAPI NtOpenProcessToken(
121 HANDLE ProcessHandle,
122 DWORD DesiredAccess,
123 HANDLE *TokenHandle)
125 return NtOpenProcessTokenEx( ProcessHandle, DesiredAccess, 0, TokenHandle );
128 /******************************************************************************
129 * NtOpenProcessTokenEx [NTDLL.@]
130 * ZwOpenProcessTokenEx [NTDLL.@]
132 NTSTATUS WINAPI NtOpenProcessTokenEx( HANDLE process, DWORD access, DWORD attributes,
133 HANDLE *handle )
135 NTSTATUS ret;
137 TRACE("(%p,0x%08x,0x%08x,%p)\n", process, access, attributes, handle);
139 SERVER_START_REQ( open_token )
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 );
148 SERVER_END_REQ;
149 return ret;
152 /******************************************************************************
153 * NtOpenThreadToken [NTDLL.@]
154 * ZwOpenThreadToken [NTDLL.@]
156 NTSTATUS WINAPI NtOpenThreadToken(
157 HANDLE ThreadHandle,
158 DWORD DesiredAccess,
159 BOOLEAN OpenAsSelf,
160 HANDLE *TokenHandle)
162 return NtOpenThreadTokenEx( ThreadHandle, DesiredAccess, OpenAsSelf, 0, TokenHandle );
165 /******************************************************************************
166 * NtOpenThreadTokenEx [NTDLL.@]
167 * ZwOpenThreadTokenEx [NTDLL.@]
169 NTSTATUS WINAPI NtOpenThreadTokenEx( HANDLE thread, DWORD access, BOOLEAN as_self, DWORD attributes,
170 HANDLE *handle )
172 NTSTATUS ret;
174 TRACE("(%p,0x%08x,%u,0x%08x,%p)\n", thread, access, as_self, attributes, handle );
176 SERVER_START_REQ( open_token )
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 );
186 SERVER_END_REQ;
188 return ret;
191 /******************************************************************************
192 * NtAdjustPrivilegesToken [NTDLL.@]
193 * ZwAdjustPrivilegesToken [NTDLL.@]
195 * FIXME: parameters unsafe
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)
205 NTSTATUS ret;
207 TRACE("(%p,0x%08x,%p,0x%08x,%p,%p)\n",
208 TokenHandle, DisableAllPrivileges, NewState, BufferLength, PreviousState, ReturnLength);
210 SERVER_START_REQ( adjust_token_privileges )
212 req->handle = wine_server_obj_handle( TokenHandle );
213 req->disable_all = DisableAllPrivileges;
214 req->get_modified_state = (PreviousState != NULL);
215 if (!DisableAllPrivileges)
217 wine_server_add_data( req, NewState->Privileges,
218 NewState->PrivilegeCount * sizeof(NewState->Privileges[0]) );
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)
226 if (ReturnLength) *ReturnLength = reply->len + FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges );
227 PreviousState->PrivilegeCount = reply->len / sizeof(LUID_AND_ATTRIBUTES);
230 SERVER_END_REQ;
232 return ret;
235 /******************************************************************************
236 * NtQueryInformationToken [NTDLL.@]
237 * ZwQueryInformationToken [NTDLL.@]
239 * NOTES
240 * Buffer for TokenUser:
241 * 0x00 TOKEN_USER the PSID field points to the SID
242 * 0x08 SID
245 NTSTATUS WINAPI NtQueryInformationToken(
246 HANDLE token,
247 TOKEN_INFORMATION_CLASS tokeninfoclass,
248 PVOID tokeninfo,
249 ULONG tokeninfolength,
250 PULONG retlen )
252 static const ULONG info_len [] =
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 0, /* TokenIsAppContainer */
284 0, /* TokenCapabilities */
285 sizeof(TOKEN_APPCONTAINER_INFORMATION) + sizeof(SID), /* TokenAppContainerSid */
286 0, /* TokenAppContainerNumber */
287 0, /* TokenUserClaimAttributes*/
288 0, /* TokenDeviceClaimAttributes */
289 0, /* TokenRestrictedUserClaimAttributes */
290 0, /* TokenRestrictedDeviceClaimAttributes */
291 0, /* TokenDeviceGroups */
292 0, /* TokenRestrictedDeviceGroups */
293 0, /* TokenSecurityAttributes */
294 0, /* TokenIsRestricted */
295 0 /* TokenProcessTrustLevel */
298 ULONG len = 0;
299 NTSTATUS status = STATUS_SUCCESS;
301 TRACE("(%p,%d,%p,%d,%p)\n",
302 token,tokeninfoclass,tokeninfo,tokeninfolength,retlen);
304 if (tokeninfoclass < MaxTokenInfoClass)
305 len = info_len[tokeninfoclass];
307 if (retlen) *retlen = len;
309 if (tokeninfolength < len)
310 return STATUS_BUFFER_TOO_SMALL;
312 switch (tokeninfoclass)
314 case TokenUser:
315 SERVER_START_REQ( get_token_sid )
317 TOKEN_USER * tuser = tokeninfo;
318 PSID sid = tuser + 1;
319 DWORD sid_len = tokeninfolength < sizeof(TOKEN_USER) ? 0 : tokeninfolength - sizeof(TOKEN_USER);
321 req->handle = wine_server_obj_handle( token );
322 req->which_sid = tokeninfoclass;
323 wine_server_set_reply( req, sid, sid_len );
324 status = wine_server_call( req );
325 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_USER);
326 if (status == STATUS_SUCCESS)
328 tuser->User.Sid = sid;
329 tuser->User.Attributes = 0;
332 SERVER_END_REQ;
333 break;
334 case TokenGroups:
336 void *buffer;
338 /* reply buffer is always shorter than output one */
339 buffer = tokeninfolength ? RtlAllocateHeap(GetProcessHeap(), 0, tokeninfolength) : NULL;
341 SERVER_START_REQ( get_token_groups )
343 TOKEN_GROUPS *groups = tokeninfo;
345 req->handle = wine_server_obj_handle( token );
346 wine_server_set_reply( req, buffer, tokeninfolength );
347 status = wine_server_call( req );
348 if (status == STATUS_BUFFER_TOO_SMALL)
350 if (retlen) *retlen = reply->user_len;
352 else if (status == STATUS_SUCCESS)
354 struct token_groups *tg = buffer;
355 unsigned int *attr = (unsigned int *)(tg + 1);
356 ULONG i;
357 const int non_sid_portion = (sizeof(struct token_groups) + tg->count * sizeof(unsigned int));
358 SID *sids = (SID *)((char *)tokeninfo + FIELD_OFFSET( TOKEN_GROUPS, Groups[tg->count] ));
360 if (retlen) *retlen = reply->user_len;
362 groups->GroupCount = tg->count;
363 memcpy( sids, (char *)buffer + non_sid_portion,
364 reply->user_len - FIELD_OFFSET( TOKEN_GROUPS, Groups[tg->count] ));
366 for (i = 0; i < tg->count; i++)
368 groups->Groups[i].Attributes = attr[i];
369 groups->Groups[i].Sid = sids;
370 sids = (SID *)((char *)sids + RtlLengthSid(sids));
373 else if (retlen) *retlen = 0;
375 SERVER_END_REQ;
377 RtlFreeHeap(GetProcessHeap(), 0, buffer);
378 break;
380 case TokenPrimaryGroup:
381 SERVER_START_REQ( get_token_sid )
383 TOKEN_PRIMARY_GROUP *tgroup = tokeninfo;
384 PSID sid = tgroup + 1;
385 DWORD sid_len = tokeninfolength < sizeof(TOKEN_PRIMARY_GROUP) ? 0 : tokeninfolength - sizeof(TOKEN_PRIMARY_GROUP);
387 req->handle = wine_server_obj_handle( token );
388 req->which_sid = tokeninfoclass;
389 wine_server_set_reply( req, sid, sid_len );
390 status = wine_server_call( req );
391 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_PRIMARY_GROUP);
392 if (status == STATUS_SUCCESS)
393 tgroup->PrimaryGroup = sid;
395 SERVER_END_REQ;
396 break;
397 case TokenPrivileges:
398 SERVER_START_REQ( get_token_privileges )
400 TOKEN_PRIVILEGES *tpriv = tokeninfo;
401 req->handle = wine_server_obj_handle( token );
402 if (tpriv && tokeninfolength > FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ))
403 wine_server_set_reply( req, tpriv->Privileges, tokeninfolength - FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) );
404 status = wine_server_call( req );
405 if (retlen) *retlen = FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) + reply->len;
406 if (tpriv) tpriv->PrivilegeCount = reply->len / sizeof(LUID_AND_ATTRIBUTES);
408 SERVER_END_REQ;
409 break;
410 case TokenOwner:
411 SERVER_START_REQ( get_token_sid )
413 TOKEN_OWNER *towner = tokeninfo;
414 PSID sid = towner + 1;
415 DWORD sid_len = tokeninfolength < sizeof(TOKEN_OWNER) ? 0 : tokeninfolength - sizeof(TOKEN_OWNER);
417 req->handle = wine_server_obj_handle( token );
418 req->which_sid = tokeninfoclass;
419 wine_server_set_reply( req, sid, sid_len );
420 status = wine_server_call( req );
421 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_OWNER);
422 if (status == STATUS_SUCCESS)
423 towner->Owner = sid;
425 SERVER_END_REQ;
426 break;
427 case TokenImpersonationLevel:
428 SERVER_START_REQ( get_token_impersonation_level )
430 SECURITY_IMPERSONATION_LEVEL *impersonation_level = tokeninfo;
431 req->handle = wine_server_obj_handle( token );
432 status = wine_server_call( req );
433 if (status == STATUS_SUCCESS)
434 *impersonation_level = reply->impersonation_level;
436 SERVER_END_REQ;
437 break;
438 case TokenStatistics:
439 SERVER_START_REQ( get_token_statistics )
441 TOKEN_STATISTICS *statistics = tokeninfo;
442 req->handle = wine_server_obj_handle( token );
443 status = wine_server_call( req );
444 if (status == STATUS_SUCCESS)
446 statistics->TokenId.LowPart = reply->token_id.low_part;
447 statistics->TokenId.HighPart = reply->token_id.high_part;
448 statistics->AuthenticationId.LowPart = 0; /* FIXME */
449 statistics->AuthenticationId.HighPart = 0; /* FIXME */
450 statistics->ExpirationTime.u.HighPart = 0x7fffffff;
451 statistics->ExpirationTime.u.LowPart = 0xffffffff;
452 statistics->TokenType = reply->primary ? TokenPrimary : TokenImpersonation;
453 statistics->ImpersonationLevel = reply->impersonation_level;
455 /* kernel information not relevant to us */
456 statistics->DynamicCharged = 0;
457 statistics->DynamicAvailable = 0;
459 statistics->GroupCount = reply->group_count;
460 statistics->PrivilegeCount = reply->privilege_count;
461 statistics->ModifiedId.LowPart = reply->modified_id.low_part;
462 statistics->ModifiedId.HighPart = reply->modified_id.high_part;
465 SERVER_END_REQ;
466 break;
467 case TokenType:
468 SERVER_START_REQ( get_token_statistics )
470 TOKEN_TYPE *token_type = tokeninfo;
471 req->handle = wine_server_obj_handle( token );
472 status = wine_server_call( req );
473 if (status == STATUS_SUCCESS)
474 *token_type = reply->primary ? TokenPrimary : TokenImpersonation;
476 SERVER_END_REQ;
477 break;
478 case TokenDefaultDacl:
479 SERVER_START_REQ( get_token_default_dacl )
481 TOKEN_DEFAULT_DACL *default_dacl = tokeninfo;
482 ACL *acl = (ACL *)(default_dacl + 1);
483 DWORD acl_len;
485 if (tokeninfolength < sizeof(TOKEN_DEFAULT_DACL)) acl_len = 0;
486 else acl_len = tokeninfolength - sizeof(TOKEN_DEFAULT_DACL);
488 req->handle = wine_server_obj_handle( token );
489 wine_server_set_reply( req, acl, acl_len );
490 status = wine_server_call( req );
492 if (retlen) *retlen = reply->acl_len + sizeof(TOKEN_DEFAULT_DACL);
493 if (status == STATUS_SUCCESS)
495 if (reply->acl_len)
496 default_dacl->DefaultDacl = acl;
497 else
498 default_dacl->DefaultDacl = NULL;
501 SERVER_END_REQ;
502 break;
503 case TokenElevationType:
505 TOKEN_ELEVATION_TYPE *elevation_type = tokeninfo;
506 FIXME("QueryInformationToken( ..., TokenElevationType, ...) semi-stub\n");
507 *elevation_type = TokenElevationTypeFull;
509 break;
510 case TokenElevation:
512 TOKEN_ELEVATION *elevation = tokeninfo;
513 FIXME("QueryInformationToken( ..., TokenElevation, ...) semi-stub\n");
514 elevation->TokenIsElevated = TRUE;
516 break;
517 case TokenSessionId:
519 *((DWORD*)tokeninfo) = 0;
520 FIXME("QueryInformationToken( ..., TokenSessionId, ...) semi-stub\n");
522 break;
523 case TokenIntegrityLevel:
525 /* report always "S-1-16-12288" (high mandatory level) for now */
526 static const SID high_level = {SID_REVISION, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY},
527 {SECURITY_MANDATORY_HIGH_RID}};
529 TOKEN_MANDATORY_LABEL *tml = tokeninfo;
530 PSID psid = tml + 1;
532 tml->Label.Sid = psid;
533 tml->Label.Attributes = SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED;
534 memcpy(psid, &high_level, sizeof(SID));
536 break;
537 case TokenAppContainerSid:
539 TOKEN_APPCONTAINER_INFORMATION *container = tokeninfo;
540 FIXME("QueryInformationToken( ..., TokenAppContainerSid, ...) semi-stub\n");
541 container->TokenAppContainer = NULL;
543 break;
544 default:
546 ERR("Unhandled Token Information class %d!\n", tokeninfoclass);
547 return STATUS_NOT_IMPLEMENTED;
550 return status;
553 /******************************************************************************
554 * NtSetInformationToken [NTDLL.@]
555 * ZwSetInformationToken [NTDLL.@]
557 NTSTATUS WINAPI NtSetInformationToken(
558 HANDLE TokenHandle,
559 TOKEN_INFORMATION_CLASS TokenInformationClass,
560 PVOID TokenInformation,
561 ULONG TokenInformationLength)
563 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
565 TRACE("%p %d %p %u\n", TokenHandle, TokenInformationClass,
566 TokenInformation, TokenInformationLength);
568 switch (TokenInformationClass)
570 case TokenDefaultDacl:
571 if (TokenInformationLength < sizeof(TOKEN_DEFAULT_DACL))
573 ret = STATUS_INFO_LENGTH_MISMATCH;
574 break;
576 if (!TokenInformation)
578 ret = STATUS_ACCESS_VIOLATION;
579 break;
581 SERVER_START_REQ( set_token_default_dacl )
583 ACL *acl = ((TOKEN_DEFAULT_DACL *)TokenInformation)->DefaultDacl;
584 WORD size;
586 if (acl) size = acl->AclSize;
587 else size = 0;
589 req->handle = wine_server_obj_handle( TokenHandle );
590 wine_server_add_data( req, acl, size );
591 ret = wine_server_call( req );
593 SERVER_END_REQ;
594 break;
595 default:
596 FIXME("unimplemented class %u\n", TokenInformationClass);
597 break;
600 return ret;
603 /******************************************************************************
604 * NtAdjustGroupsToken [NTDLL.@]
605 * ZwAdjustGroupsToken [NTDLL.@]
607 NTSTATUS WINAPI NtAdjustGroupsToken(
608 HANDLE TokenHandle,
609 BOOLEAN ResetToDefault,
610 PTOKEN_GROUPS NewState,
611 ULONG BufferLength,
612 PTOKEN_GROUPS PreviousState,
613 PULONG ReturnLength)
615 FIXME("%p %d %p %u %p %p\n", TokenHandle, ResetToDefault,
616 NewState, BufferLength, PreviousState, ReturnLength);
617 return STATUS_NOT_IMPLEMENTED;
620 /******************************************************************************
621 * NtPrivilegeCheck [NTDLL.@]
622 * ZwPrivilegeCheck [NTDLL.@]
624 NTSTATUS WINAPI NtPrivilegeCheck(
625 HANDLE ClientToken,
626 PPRIVILEGE_SET RequiredPrivileges,
627 PBOOLEAN Result)
629 NTSTATUS status;
630 SERVER_START_REQ( check_token_privileges )
632 req->handle = wine_server_obj_handle( ClientToken );
633 req->all_required = (RequiredPrivileges->Control & PRIVILEGE_SET_ALL_NECESSARY) != 0;
634 wine_server_add_data( req, RequiredPrivileges->Privilege,
635 RequiredPrivileges->PrivilegeCount * sizeof(RequiredPrivileges->Privilege[0]) );
636 wine_server_set_reply( req, RequiredPrivileges->Privilege,
637 RequiredPrivileges->PrivilegeCount * sizeof(RequiredPrivileges->Privilege[0]) );
639 status = wine_server_call( req );
641 if (status == STATUS_SUCCESS)
642 *Result = reply->has_privileges != 0;
644 SERVER_END_REQ;
645 return status;
649 * Section
652 /******************************************************************************
653 * NtQuerySection [NTDLL.@]
655 NTSTATUS WINAPI NtQuerySection(
656 IN HANDLE SectionHandle,
657 IN SECTION_INFORMATION_CLASS SectionInformationClass,
658 OUT PVOID SectionInformation,
659 IN ULONG Length,
660 OUT PULONG ResultLength)
662 FIXME("(%p,%d,%p,0x%08x,%p) stub!\n",
663 SectionHandle,SectionInformationClass,SectionInformation,Length,ResultLength);
664 return 0;
668 * ports
671 /******************************************************************************
672 * NtCreatePort [NTDLL.@]
673 * ZwCreatePort [NTDLL.@]
675 NTSTATUS WINAPI NtCreatePort(PHANDLE PortHandle,POBJECT_ATTRIBUTES ObjectAttributes,
676 ULONG MaxConnectInfoLength,ULONG MaxDataLength,PULONG reserved)
678 FIXME("(%p,%p,%u,%u,%p),stub!\n",PortHandle,ObjectAttributes,
679 MaxConnectInfoLength,MaxDataLength,reserved);
680 return STATUS_NOT_IMPLEMENTED;
683 /******************************************************************************
684 * NtConnectPort [NTDLL.@]
685 * ZwConnectPort [NTDLL.@]
687 NTSTATUS WINAPI NtConnectPort(
688 PHANDLE PortHandle,
689 PUNICODE_STRING PortName,
690 PSECURITY_QUALITY_OF_SERVICE SecurityQos,
691 PLPC_SECTION_WRITE WriteSection,
692 PLPC_SECTION_READ ReadSection,
693 PULONG MaximumMessageLength,
694 PVOID ConnectInfo,
695 PULONG pConnectInfoLength)
697 FIXME("(%p,%s,%p,%p,%p,%p,%p,%p),stub!\n",
698 PortHandle,debugstr_w(PortName->Buffer),SecurityQos,
699 WriteSection,ReadSection,MaximumMessageLength,ConnectInfo,
700 pConnectInfoLength);
701 if (ConnectInfo && pConnectInfoLength)
702 TRACE("\tMessage = %s\n",debugstr_an(ConnectInfo,*pConnectInfoLength));
703 return STATUS_NOT_IMPLEMENTED;
706 /******************************************************************************
707 * NtSecureConnectPort (NTDLL.@)
708 * ZwSecureConnectPort (NTDLL.@)
710 NTSTATUS WINAPI NtSecureConnectPort(
711 PHANDLE PortHandle,
712 PUNICODE_STRING PortName,
713 PSECURITY_QUALITY_OF_SERVICE SecurityQos,
714 PLPC_SECTION_WRITE WriteSection,
715 PSID pSid,
716 PLPC_SECTION_READ ReadSection,
717 PULONG MaximumMessageLength,
718 PVOID ConnectInfo,
719 PULONG pConnectInfoLength)
721 FIXME("(%p,%s,%p,%p,%p,%p,%p,%p,%p),stub!\n",
722 PortHandle,debugstr_w(PortName->Buffer),SecurityQos,
723 WriteSection,pSid,ReadSection,MaximumMessageLength,ConnectInfo,
724 pConnectInfoLength);
725 return STATUS_NOT_IMPLEMENTED;
728 /******************************************************************************
729 * NtListenPort [NTDLL.@]
730 * ZwListenPort [NTDLL.@]
732 NTSTATUS WINAPI NtListenPort(HANDLE PortHandle,PLPC_MESSAGE pLpcMessage)
734 FIXME("(%p,%p),stub!\n",PortHandle,pLpcMessage);
735 return STATUS_NOT_IMPLEMENTED;
738 /******************************************************************************
739 * NtAcceptConnectPort [NTDLL.@]
740 * ZwAcceptConnectPort [NTDLL.@]
742 NTSTATUS WINAPI NtAcceptConnectPort(
743 PHANDLE PortHandle,
744 ULONG PortIdentifier,
745 PLPC_MESSAGE pLpcMessage,
746 BOOLEAN Accept,
747 PLPC_SECTION_WRITE WriteSection,
748 PLPC_SECTION_READ ReadSection)
750 FIXME("(%p,%u,%p,%d,%p,%p),stub!\n",
751 PortHandle,PortIdentifier,pLpcMessage,Accept,WriteSection,ReadSection);
752 return STATUS_NOT_IMPLEMENTED;
755 /******************************************************************************
756 * NtCompleteConnectPort [NTDLL.@]
757 * ZwCompleteConnectPort [NTDLL.@]
759 NTSTATUS WINAPI NtCompleteConnectPort(HANDLE PortHandle)
761 FIXME("(%p),stub!\n",PortHandle);
762 return STATUS_NOT_IMPLEMENTED;
765 /******************************************************************************
766 * NtRegisterThreadTerminatePort [NTDLL.@]
767 * ZwRegisterThreadTerminatePort [NTDLL.@]
769 NTSTATUS WINAPI NtRegisterThreadTerminatePort(HANDLE PortHandle)
771 FIXME("(%p),stub!\n",PortHandle);
772 return STATUS_NOT_IMPLEMENTED;
775 /******************************************************************************
776 * NtRequestWaitReplyPort [NTDLL.@]
777 * ZwRequestWaitReplyPort [NTDLL.@]
779 NTSTATUS WINAPI NtRequestWaitReplyPort(
780 HANDLE PortHandle,
781 PLPC_MESSAGE pLpcMessageIn,
782 PLPC_MESSAGE pLpcMessageOut)
784 FIXME("(%p,%p,%p),stub!\n",PortHandle,pLpcMessageIn,pLpcMessageOut);
785 if(pLpcMessageIn)
787 TRACE("Message to send:\n");
788 TRACE("\tDataSize = %u\n",pLpcMessageIn->DataSize);
789 TRACE("\tMessageSize = %u\n",pLpcMessageIn->MessageSize);
790 TRACE("\tMessageType = %u\n",pLpcMessageIn->MessageType);
791 TRACE("\tVirtualRangesOffset = %u\n",pLpcMessageIn->VirtualRangesOffset);
792 TRACE("\tClientId.UniqueProcess = %p\n",pLpcMessageIn->ClientId.UniqueProcess);
793 TRACE("\tClientId.UniqueThread = %p\n",pLpcMessageIn->ClientId.UniqueThread);
794 TRACE("\tMessageId = %lu\n",pLpcMessageIn->MessageId);
795 TRACE("\tSectionSize = %lu\n",pLpcMessageIn->SectionSize);
796 TRACE("\tData = %s\n",
797 debugstr_an((const char*)pLpcMessageIn->Data,pLpcMessageIn->DataSize));
799 return STATUS_NOT_IMPLEMENTED;
802 /******************************************************************************
803 * NtReplyWaitReceivePort [NTDLL.@]
804 * ZwReplyWaitReceivePort [NTDLL.@]
806 NTSTATUS WINAPI NtReplyWaitReceivePort(
807 HANDLE PortHandle,
808 PULONG PortIdentifier,
809 PLPC_MESSAGE ReplyMessage,
810 PLPC_MESSAGE Message)
812 FIXME("(%p,%p,%p,%p),stub!\n",PortHandle,PortIdentifier,ReplyMessage,Message);
813 return STATUS_NOT_IMPLEMENTED;
817 * Misc
820 /******************************************************************************
821 * NtSetIntervalProfile [NTDLL.@]
822 * ZwSetIntervalProfile [NTDLL.@]
824 NTSTATUS WINAPI NtSetIntervalProfile(
825 ULONG Interval,
826 KPROFILE_SOURCE Source)
828 FIXME("%u,%d\n", Interval, Source);
829 return STATUS_SUCCESS;
832 static SYSTEM_CPU_INFORMATION cached_sci;
834 /*******************************************************************************
835 * Architecture specific feature detection for CPUs
837 * This a set of mutually exclusive #if define()s each providing its own get_cpuinfo() to be called
838 * from fill_cpu_info();
840 #if defined(__i386__) || defined(__x86_64__)
842 #define AUTH 0x68747541 /* "Auth" */
843 #define ENTI 0x69746e65 /* "enti" */
844 #define CAMD 0x444d4163 /* "cAMD" */
846 #define GENU 0x756e6547 /* "Genu" */
847 #define INEI 0x49656e69 /* "ineI" */
848 #define NTEL 0x6c65746e /* "ntel" */
850 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
851 * We are compiled with -fPIC, so we can't clobber ebx.
853 static inline void do_cpuid(unsigned int ax, unsigned int *p)
855 #ifdef __i386__
856 __asm__("pushl %%ebx\n\t"
857 "cpuid\n\t"
858 "movl %%ebx, %%esi\n\t"
859 "popl %%ebx"
860 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
861 : "0" (ax));
862 #elif defined(__x86_64__)
863 __asm__("push %%rbx\n\t"
864 "cpuid\n\t"
865 "movq %%rbx, %%rsi\n\t"
866 "pop %%rbx"
867 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
868 : "0" (ax));
869 #endif
872 /* From xf86info havecpuid.c 1.11 */
873 static inline BOOL have_cpuid(void)
875 #ifdef __i386__
876 unsigned int f1, f2;
877 __asm__("pushfl\n\t"
878 "pushfl\n\t"
879 "popl %0\n\t"
880 "movl %0,%1\n\t"
881 "xorl %2,%0\n\t"
882 "pushl %0\n\t"
883 "popfl\n\t"
884 "pushfl\n\t"
885 "popl %0\n\t"
886 "popfl"
887 : "=&r" (f1), "=&r" (f2)
888 : "ir" (0x00200000));
889 return ((f1^f2) & 0x00200000) != 0;
890 #elif defined(__x86_64__)
891 return TRUE;
892 #else
893 return FALSE;
894 #endif
897 /* Detect if a SSE2 processor is capable of Denormals Are Zero (DAZ) mode.
899 * This function assumes you have already checked for SSE2/FXSAVE support. */
900 static inline BOOL have_sse_daz_mode(void)
902 #ifdef __i386__
903 typedef struct DECLSPEC_ALIGN(16) _M128A {
904 ULONGLONG Low;
905 LONGLONG High;
906 } M128A;
908 typedef struct _XMM_SAVE_AREA32 {
909 WORD ControlWord;
910 WORD StatusWord;
911 BYTE TagWord;
912 BYTE Reserved1;
913 WORD ErrorOpcode;
914 DWORD ErrorOffset;
915 WORD ErrorSelector;
916 WORD Reserved2;
917 DWORD DataOffset;
918 WORD DataSelector;
919 WORD Reserved3;
920 DWORD MxCsr;
921 DWORD MxCsr_Mask;
922 M128A FloatRegisters[8];
923 M128A XmmRegisters[16];
924 BYTE Reserved4[96];
925 } XMM_SAVE_AREA32;
927 /* Intel says we need a zeroed 16-byte aligned buffer */
928 char buffer[512 + 16];
929 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
930 memset(buffer, 0, sizeof(buffer));
932 __asm__ __volatile__( "fxsave %0" : "=m" (*state) : "m" (*state) );
934 return (state->MxCsr_Mask & (1 << 6)) >> 6;
935 #else /* all x86_64 processors include SSE2 with DAZ mode */
936 return TRUE;
937 #endif
940 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
942 unsigned int regs[4], regs2[4];
944 #if defined(__i386__)
945 info->Architecture = PROCESSOR_ARCHITECTURE_INTEL;
946 #elif defined(__x86_64__)
947 info->Architecture = PROCESSOR_ARCHITECTURE_AMD64;
948 #endif
950 /* We're at least a 386 */
951 info->FeatureSet = CPU_FEATURE_VME | CPU_FEATURE_X86 | CPU_FEATURE_PGE;
952 info->Level = 3;
954 if (!have_cpuid()) return;
956 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
957 if (regs[0]>=0x00000001) /* Check for supported cpuid version */
959 do_cpuid(0x00000001, regs2); /* get cpu features */
961 if(regs2[3] & (1 << 3 )) info->FeatureSet |= CPU_FEATURE_PSE;
962 if(regs2[3] & (1 << 4 )) info->FeatureSet |= CPU_FEATURE_TSC;
963 if(regs2[3] & (1 << 8 )) info->FeatureSet |= CPU_FEATURE_CX8;
964 if(regs2[3] & (1 << 11)) info->FeatureSet |= CPU_FEATURE_SEP;
965 if(regs2[3] & (1 << 12)) info->FeatureSet |= CPU_FEATURE_MTRR;
966 if(regs2[3] & (1 << 15)) info->FeatureSet |= CPU_FEATURE_CMOV;
967 if(regs2[3] & (1 << 16)) info->FeatureSet |= CPU_FEATURE_PAT;
968 if(regs2[3] & (1 << 23)) info->FeatureSet |= CPU_FEATURE_MMX;
969 if(regs2[3] & (1 << 24)) info->FeatureSet |= CPU_FEATURE_FXSR;
970 if(regs2[3] & (1 << 25)) info->FeatureSet |= CPU_FEATURE_SSE;
971 if(regs2[3] & (1 << 26)) info->FeatureSet |= CPU_FEATURE_SSE2;
973 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
974 user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] >> 4) & 1;
975 user_shared_data->ProcessorFeatures[PF_PAE_ENABLED] = (regs2[3] >> 6) & 1;
976 user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] >> 8) & 1;
977 user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 23) & 1;
978 user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 25) & 1;
979 user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 26) & 1;
980 user_shared_data->ProcessorFeatures[PF_SSE3_INSTRUCTIONS_AVAILABLE] = regs2[2] & 1;
981 user_shared_data->ProcessorFeatures[PF_XSAVE_ENABLED] = (regs2[2] >> 27) & 1;
982 user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE128] = (regs2[2] >> 13) & 1;
984 if((regs2[3] & (1 << 26)) && (regs2[3] & (1 << 24))) /* has SSE2 and FXSAVE/FXRSTOR */
985 user_shared_data->ProcessorFeatures[PF_SSE_DAZ_MODE_AVAILABLE] = have_sse_daz_mode();
987 if (regs[1] == AUTH && regs[3] == ENTI && regs[2] == CAMD)
989 info->Level = (regs2[0] >> 8) & 0xf; /* family */
990 if (info->Level == 0xf) /* AMD says to add the extended family to the family if family is 0xf */
991 info->Level += (regs2[0] >> 20) & 0xff;
993 /* repack model and stepping to make a "revision" */
994 info->Revision = ((regs2[0] >> 16) & 0xf) << 12; /* extended model */
995 info->Revision |= ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
996 info->Revision |= regs2[0] & 0xf; /* stepping */
998 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
999 if (regs[0] >= 0x80000001)
1001 do_cpuid(0x80000001, regs2); /* get vendor features */
1002 user_shared_data->ProcessorFeatures[PF_VIRT_FIRMWARE_ENABLED] = (regs2[2] >> 2) & 1;
1003 user_shared_data->ProcessorFeatures[PF_NX_ENABLED] = (regs2[3] >> 20) & 1;
1004 user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 31) & 1;
1005 if (regs2[3] >> 31) info->FeatureSet |= CPU_FEATURE_3DNOW;
1008 else if (regs[1] == GENU && regs[3] == INEI && regs[2] == NTEL)
1010 info->Level = ((regs2[0] >> 8) & 0xf) + ((regs2[0] >> 20) & 0xff); /* family + extended family */
1011 if(info->Level == 15) info->Level = 6;
1013 /* repack model and stepping to make a "revision" */
1014 info->Revision = ((regs2[0] >> 16) & 0xf) << 12; /* extended model */
1015 info->Revision |= ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
1016 info->Revision |= regs2[0] & 0xf; /* stepping */
1018 if(regs2[3] & (1 << 21)) info->FeatureSet |= CPU_FEATURE_DS;
1019 user_shared_data->ProcessorFeatures[PF_VIRT_FIRMWARE_ENABLED] = (regs2[2] >> 5) & 1;
1021 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
1022 if (regs[0] >= 0x80000001)
1024 do_cpuid(0x80000001, regs2); /* get vendor features */
1025 user_shared_data->ProcessorFeatures[PF_NX_ENABLED] = (regs2[3] >> 20) & 1;
1028 else
1030 info->Level = (regs2[0] >> 8) & 0xf; /* family */
1032 /* repack model and stepping to make a "revision" */
1033 info->Revision = ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
1034 info->Revision |= regs2[0] & 0xf; /* stepping */
1039 #elif defined(__powerpc__) || defined(__ppc__)
1041 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
1043 #ifdef __APPLE__
1044 size_t valSize;
1045 int value;
1047 valSize = sizeof(value);
1048 if (sysctlbyname("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
1049 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = !value;
1051 valSize = sizeof(value);
1052 if (sysctlbyname("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
1054 switch (value)
1056 case CPU_SUBTYPE_POWERPC_601:
1057 case CPU_SUBTYPE_POWERPC_602: info->Level = 1; break;
1058 case CPU_SUBTYPE_POWERPC_603: info->Level = 3; break;
1059 case CPU_SUBTYPE_POWERPC_603e:
1060 case CPU_SUBTYPE_POWERPC_603ev: info->Level = 6; break;
1061 case CPU_SUBTYPE_POWERPC_604: info->Level = 4; break;
1062 case CPU_SUBTYPE_POWERPC_604e: info->Level = 9; break;
1063 case CPU_SUBTYPE_POWERPC_620: info->Level = 20; break;
1064 case CPU_SUBTYPE_POWERPC_750: /* G3/G4 derive from 603 so ... */
1065 case CPU_SUBTYPE_POWERPC_7400:
1066 case CPU_SUBTYPE_POWERPC_7450: info->Level = 6; break;
1067 case CPU_SUBTYPE_POWERPC_970: info->Level = 9;
1068 /* :o) user_shared_data->ProcessorFeatures[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
1069 break;
1070 default: break;
1073 #else
1074 FIXME("CPU Feature detection not implemented.\n");
1075 #endif
1076 info->Architecture = PROCESSOR_ARCHITECTURE_PPC;
1079 #elif defined(__arm__)
1081 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
1083 #ifdef linux
1084 char line[512];
1085 char *s, *value;
1086 FILE *f = fopen("/proc/cpuinfo", "r");
1087 if (f)
1089 while (fgets(line, sizeof(line), f) != NULL)
1091 /* NOTE: the ':' is the only character we can rely on */
1092 if (!(value = strchr(line,':')))
1093 continue;
1094 /* terminate the valuename */
1095 s = value - 1;
1096 while ((s >= line) && isspace(*s)) s--;
1097 *(s + 1) = '\0';
1098 /* and strip leading spaces from value */
1099 value += 1;
1100 while (isspace(*value)) value++;
1101 if ((s = strchr(value,'\n')))
1102 *s='\0';
1103 if (!strcasecmp(line, "CPU architecture"))
1105 if (isdigit(value[0]))
1106 info->Level = atoi(value);
1107 continue;
1109 if (!strcasecmp(line, "CPU revision"))
1111 if (isdigit(value[0]))
1112 info->Revision = atoi(value);
1113 continue;
1115 if (!strcasecmp(line, "features"))
1117 if (strstr(value, "vfpv3"))
1118 user_shared_data->ProcessorFeatures[PF_ARM_VFP_32_REGISTERS_AVAILABLE] = TRUE;
1119 if (strstr(value, "neon"))
1120 user_shared_data->ProcessorFeatures[PF_ARM_NEON_INSTRUCTIONS_AVAILABLE] = TRUE;
1121 continue;
1124 fclose(f);
1126 #elif defined(__FreeBSD__)
1127 size_t valsize;
1128 char buf[8];
1129 int value;
1131 valsize = sizeof(buf);
1132 if (!sysctlbyname("hw.machine_arch", &buf, &valsize, NULL, 0) &&
1133 sscanf(buf, "armv%i", &value) == 1)
1134 info->Level = value;
1136 valsize = sizeof(value);
1137 if (!sysctlbyname("hw.floatingpoint", &value, &valsize, NULL, 0))
1138 user_shared_data->ProcessorFeatures[PF_ARM_VFP_32_REGISTERS_AVAILABLE] = value;
1139 #else
1140 FIXME("CPU Feature detection not implemented.\n");
1141 #endif
1142 if (info->Level >= 8)
1143 user_shared_data->ProcessorFeatures[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = TRUE;
1144 info->Architecture = PROCESSOR_ARCHITECTURE_ARM;
1147 #elif defined(__aarch64__)
1149 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
1151 #ifdef linux
1152 char line[512];
1153 char *s, *value;
1154 FILE *f = fopen("/proc/cpuinfo", "r");
1155 if (f)
1157 while (fgets(line, sizeof(line), f) != NULL)
1159 /* NOTE: the ':' is the only character we can rely on */
1160 if (!(value = strchr(line,':')))
1161 continue;
1162 /* terminate the valuename */
1163 s = value - 1;
1164 while ((s >= line) && isspace(*s)) s--;
1165 *(s + 1) = '\0';
1166 /* and strip leading spaces from value */
1167 value += 1;
1168 while (isspace(*value)) value++;
1169 if ((s = strchr(value,'\n')))
1170 *s='\0';
1171 if (!strcasecmp(line, "CPU architecture"))
1173 if (isdigit(value[0]))
1174 info->Level = atoi(value);
1175 continue;
1177 if (!strcasecmp(line, "CPU revision"))
1179 if (isdigit(value[0]))
1180 info->Revision = atoi(value);
1181 continue;
1183 if (!strcasecmp(line, "Features"))
1185 if (strstr(value, "crc32"))
1186 user_shared_data->ProcessorFeatures[PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE] = TRUE;
1187 if (strstr(value, "aes"))
1188 user_shared_data->ProcessorFeatures[PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE] = TRUE;
1189 continue;
1192 fclose(f);
1194 #else
1195 FIXME("CPU Feature detection not implemented.\n");
1196 #endif
1197 info->Level = max(info->Level, 8);
1198 user_shared_data->ProcessorFeatures[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = TRUE;
1199 info->Architecture = PROCESSOR_ARCHITECTURE_ARM64;
1202 #endif /* End architecture specific feature detection for CPUs */
1204 /******************************************************************
1205 * fill_cpu_info
1207 * inits a couple of places with CPU related information:
1208 * - cached_sci in this file
1209 * - Peb->NumberOfProcessors
1210 * - SharedUserData->ProcessFeatures[] array
1212 void fill_cpu_info(void)
1214 long num;
1216 #ifdef _SC_NPROCESSORS_ONLN
1217 num = sysconf(_SC_NPROCESSORS_ONLN);
1218 if (num < 1)
1220 num = 1;
1221 WARN("Failed to detect the number of processors.\n");
1223 #elif defined(CTL_HW) && defined(HW_NCPU)
1224 int mib[2];
1225 size_t len = sizeof(num);
1226 mib[0] = CTL_HW;
1227 mib[1] = HW_NCPU;
1228 if (sysctl(mib, 2, &num, &len, NULL, 0) != 0)
1230 num = 1;
1231 WARN("Failed to detect the number of processors.\n");
1233 #else
1234 num = 1;
1235 FIXME("Detecting the number of processors is not supported.\n");
1236 #endif
1237 NtCurrentTeb()->Peb->NumberOfProcessors = num;
1239 memset(&cached_sci, 0, sizeof(cached_sci));
1240 get_cpuinfo(&cached_sci);
1242 TRACE("<- CPU arch %d, level %d, rev %d, features 0x%x\n",
1243 cached_sci.Architecture, cached_sci.Level, cached_sci.Revision, cached_sci.FeatureSet);
1246 static BOOL grow_logical_proc_buf(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1247 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *max_len)
1249 if (pdata)
1251 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *new_data;
1253 *max_len *= 2;
1254 new_data = RtlReAllocateHeap(GetProcessHeap(), 0, *pdata, *max_len*sizeof(*new_data));
1255 if (!new_data)
1256 return FALSE;
1258 *pdata = new_data;
1260 else
1262 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *new_dataex;
1264 *max_len *= 2;
1265 new_dataex = RtlReAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, *pdataex, *max_len*sizeof(*new_dataex));
1266 if (!new_dataex)
1267 return FALSE;
1269 *pdataex = new_dataex;
1272 return TRUE;
1275 static DWORD log_proc_ex_size_plus(DWORD size)
1277 /* add SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX.Relationship and .Size */
1278 return sizeof(LOGICAL_PROCESSOR_RELATIONSHIP) + sizeof(DWORD) + size;
1281 static inline BOOL logical_proc_info_add_by_id(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1282 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len, DWORD *pmax_len,
1283 LOGICAL_PROCESSOR_RELATIONSHIP rel, DWORD id, ULONG_PTR mask)
1285 if (pdata) {
1286 DWORD i;
1288 if(rel == RelationProcessorPackage){
1289 for(i=0; i<*len; i++)
1291 if ((*pdata)[i].Relationship!=rel || (*pdata)[i].u.Reserved[1]!=id)
1292 continue;
1294 (*pdata)[i].ProcessorMask |= mask;
1295 return TRUE;
1297 }else
1298 i = *len;
1300 while(*len == *pmax_len)
1302 if (!grow_logical_proc_buf(pdata, NULL, pmax_len))
1303 return FALSE;
1306 (*pdata)[i].Relationship = rel;
1307 (*pdata)[i].ProcessorMask = mask;
1308 /* TODO: set processor core flags */
1309 (*pdata)[i].u.Reserved[0] = 0;
1310 (*pdata)[i].u.Reserved[1] = id;
1311 *len = i+1;
1312 }else{
1313 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex = *pdataex;
1314 DWORD ofs = 0;
1316 while(ofs < *len)
1318 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1319 if (rel == RelationProcessorPackage && dataex->Relationship == rel && dataex->u.Processor.Reserved[1] == id)
1321 dataex->u.Processor.GroupMask[0].Mask |= mask;
1322 return TRUE;
1324 ofs += dataex->Size;
1327 /* TODO: For now, just one group. If more than 64 processors, then we
1328 * need another group. */
1330 while (ofs + log_proc_ex_size_plus(sizeof(PROCESSOR_RELATIONSHIP)) > *pmax_len)
1332 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1333 return FALSE;
1336 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1338 dataex->Relationship = rel;
1339 dataex->Size = log_proc_ex_size_plus(sizeof(PROCESSOR_RELATIONSHIP));
1340 dataex->u.Processor.Flags = 0; /* TODO */
1341 dataex->u.Processor.EfficiencyClass = 0;
1342 dataex->u.Processor.GroupCount = 1;
1343 dataex->u.Processor.GroupMask[0].Mask = mask;
1344 dataex->u.Processor.GroupMask[0].Group = 0;
1345 /* mark for future lookup */
1346 dataex->u.Processor.Reserved[0] = 0;
1347 dataex->u.Processor.Reserved[1] = id;
1349 *len += dataex->Size;
1352 return TRUE;
1355 static inline BOOL logical_proc_info_add_cache(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1356 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len,
1357 DWORD *pmax_len, ULONG_PTR mask, CACHE_DESCRIPTOR *cache)
1359 if (pdata)
1361 DWORD i;
1363 for (i=0; i<*len; i++)
1365 if ((*pdata)[i].Relationship==RelationCache && (*pdata)[i].ProcessorMask==mask
1366 && (*pdata)[i].u.Cache.Level==cache->Level && (*pdata)[i].u.Cache.Type==cache->Type)
1367 return TRUE;
1370 while (*len == *pmax_len)
1371 if (!grow_logical_proc_buf(pdata, NULL, pmax_len))
1372 return FALSE;
1374 (*pdata)[i].Relationship = RelationCache;
1375 (*pdata)[i].ProcessorMask = mask;
1376 (*pdata)[i].u.Cache = *cache;
1377 *len = i+1;
1379 else
1381 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex = *pdataex;
1382 DWORD ofs;
1384 for (ofs = 0; ofs < *len; )
1386 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1387 if (dataex->Relationship == RelationCache && dataex->u.Cache.GroupMask.Mask == mask &&
1388 dataex->u.Cache.Level == cache->Level && dataex->u.Cache.Type == cache->Type)
1389 return TRUE;
1390 ofs += dataex->Size;
1393 while (ofs + log_proc_ex_size_plus(sizeof(CACHE_RELATIONSHIP)) > *pmax_len)
1395 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1396 return FALSE;
1399 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1401 dataex->Relationship = RelationCache;
1402 dataex->Size = log_proc_ex_size_plus(sizeof(CACHE_RELATIONSHIP));
1403 dataex->u.Cache.Level = cache->Level;
1404 dataex->u.Cache.Associativity = cache->Associativity;
1405 dataex->u.Cache.LineSize = cache->LineSize;
1406 dataex->u.Cache.CacheSize = cache->Size;
1407 dataex->u.Cache.Type = cache->Type;
1408 dataex->u.Cache.GroupMask.Mask = mask;
1409 dataex->u.Cache.GroupMask.Group = 0;
1411 *len += dataex->Size;
1414 return TRUE;
1417 static inline BOOL logical_proc_info_add_numa_node(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1418 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len, DWORD *pmax_len, ULONG_PTR mask,
1419 DWORD node_id)
1421 if (pdata)
1423 while (*len == *pmax_len)
1424 if (!grow_logical_proc_buf(pdata, NULL, pmax_len))
1425 return FALSE;
1427 (*pdata)[*len].Relationship = RelationNumaNode;
1428 (*pdata)[*len].ProcessorMask = mask;
1429 (*pdata)[*len].u.NumaNode.NodeNumber = node_id;
1430 (*len)++;
1432 else
1434 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
1436 while (*len + log_proc_ex_size_plus(sizeof(NUMA_NODE_RELATIONSHIP)) > *pmax_len)
1438 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1439 return FALSE;
1442 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + *len);
1444 dataex->Relationship = RelationNumaNode;
1445 dataex->Size = log_proc_ex_size_plus(sizeof(NUMA_NODE_RELATIONSHIP));
1446 dataex->u.NumaNode.NodeNumber = node_id;
1447 dataex->u.NumaNode.GroupMask.Mask = mask;
1448 dataex->u.NumaNode.GroupMask.Group = 0;
1450 *len += dataex->Size;
1453 return TRUE;
1456 static inline BOOL logical_proc_info_add_group(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex,
1457 DWORD *len, DWORD *pmax_len, DWORD num_cpus, ULONG_PTR mask)
1459 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
1461 while (*len + log_proc_ex_size_plus(sizeof(GROUP_RELATIONSHIP)) > *pmax_len)
1463 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1464 return FALSE;
1467 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + *len);
1469 dataex->Relationship = RelationGroup;
1470 dataex->Size = log_proc_ex_size_plus(sizeof(GROUP_RELATIONSHIP));
1471 dataex->u.Group.MaximumGroupCount = 1;
1472 dataex->u.Group.ActiveGroupCount = 1;
1473 dataex->u.Group.GroupInfo[0].MaximumProcessorCount = num_cpus;
1474 dataex->u.Group.GroupInfo[0].ActiveProcessorCount = num_cpus;
1475 dataex->u.Group.GroupInfo[0].ActiveProcessorMask = mask;
1477 *len += dataex->Size;
1479 return TRUE;
1482 #ifdef linux
1483 /* for 'data', max_len is the array count. for 'dataex', max_len is in bytes */
1484 static NTSTATUS create_logical_proc_info(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
1485 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex, DWORD *max_len)
1487 static const char core_info[] = "/sys/devices/system/cpu/cpu%u/%s";
1488 static const char cache_info[] = "/sys/devices/system/cpu/cpu%u/cache/index%u/%s";
1489 static const char numa_info[] = "/sys/devices/system/node/node%u/cpumap";
1491 FILE *fcpu_list, *fnuma_list, *f;
1492 DWORD len = 0, beg, end, i, j, r, num_cpus = 0;
1493 char op, name[MAX_PATH];
1494 ULONG_PTR all_cpus_mask = 0;
1496 fcpu_list = fopen("/sys/devices/system/cpu/online", "r");
1497 if(!fcpu_list)
1498 return STATUS_NOT_IMPLEMENTED;
1500 while(!feof(fcpu_list))
1502 if(!fscanf(fcpu_list, "%u%c ", &beg, &op))
1503 break;
1504 if(op == '-') fscanf(fcpu_list, "%u%c ", &end, &op);
1505 else end = beg;
1507 for(i=beg; i<=end; i++)
1509 if(i > 8*sizeof(ULONG_PTR))
1511 FIXME("skipping logical processor %d\n", i);
1512 continue;
1515 sprintf(name, core_info, i, "physical_package_id");
1516 f = fopen(name, "r");
1517 if(f)
1519 fscanf(f, "%u", &r);
1520 fclose(f);
1522 else r = 0;
1523 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorPackage, r, (ULONG_PTR)1 << i))
1525 fclose(fcpu_list);
1526 return STATUS_NO_MEMORY;
1529 sprintf(name, core_info, i, "core_id");
1530 f = fopen(name, "r");
1531 if(f)
1533 fscanf(f, "%u", &r);
1534 fclose(f);
1536 else r = i;
1537 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorCore, r, (ULONG_PTR)1 << i))
1539 fclose(fcpu_list);
1540 return STATUS_NO_MEMORY;
1543 for(j=0; j<4; j++)
1545 CACHE_DESCRIPTOR cache;
1546 ULONG_PTR mask = 0;
1548 sprintf(name, cache_info, i, j, "shared_cpu_map");
1549 f = fopen(name, "r");
1550 if(!f) continue;
1551 while(!feof(f))
1553 if(!fscanf(f, "%x%c ", &r, &op))
1554 break;
1555 mask = (sizeof(ULONG_PTR)>sizeof(int) ? mask<<(8*sizeof(DWORD)) : 0) + r;
1557 fclose(f);
1559 sprintf(name, cache_info, i, j, "level");
1560 f = fopen(name, "r");
1561 if(!f) continue;
1562 fscanf(f, "%u", &r);
1563 fclose(f);
1564 cache.Level = r;
1566 sprintf(name, cache_info, i, j, "ways_of_associativity");
1567 f = fopen(name, "r");
1568 if(!f) continue;
1569 fscanf(f, "%u", &r);
1570 fclose(f);
1571 cache.Associativity = r;
1573 sprintf(name, cache_info, i, j, "coherency_line_size");
1574 f = fopen(name, "r");
1575 if(!f) continue;
1576 fscanf(f, "%u", &r);
1577 fclose(f);
1578 cache.LineSize = r;
1580 sprintf(name, cache_info, i, j, "size");
1581 f = fopen(name, "r");
1582 if(!f) continue;
1583 fscanf(f, "%u%c", &r, &op);
1584 fclose(f);
1585 if(op != 'K')
1586 WARN("unknown cache size %u%c\n", r, op);
1587 cache.Size = (op=='K' ? r*1024 : r);
1589 sprintf(name, cache_info, i, j, "type");
1590 f = fopen(name, "r");
1591 if(!f) continue;
1592 fscanf(f, "%s", name);
1593 fclose(f);
1594 if(!memcmp(name, "Data", 5))
1595 cache.Type = CacheData;
1596 else if(!memcmp(name, "Instruction", 11))
1597 cache.Type = CacheInstruction;
1598 else
1599 cache.Type = CacheUnified;
1601 if(!logical_proc_info_add_cache(data, dataex, &len, max_len, mask, &cache))
1603 fclose(fcpu_list);
1604 return STATUS_NO_MEMORY;
1609 fclose(fcpu_list);
1611 if(data){
1612 for(i=0; i<len; i++){
1613 if((*data)[i].Relationship == RelationProcessorCore){
1614 all_cpus_mask |= (*data)[i].ProcessorMask;
1615 ++num_cpus;
1618 }else{
1619 for(i = 0; i < len; ){
1620 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *infoex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*dataex) + i);
1621 if(infoex->Relationship == RelationProcessorCore){
1622 all_cpus_mask |= infoex->u.Processor.GroupMask[0].Mask;
1623 ++num_cpus;
1625 i += infoex->Size;
1629 fnuma_list = fopen("/sys/devices/system/node/online", "r");
1630 if(!fnuma_list)
1632 if(!logical_proc_info_add_numa_node(data, dataex, &len, max_len, all_cpus_mask, 0))
1633 return STATUS_NO_MEMORY;
1635 else
1637 while(!feof(fnuma_list))
1639 if(!fscanf(fnuma_list, "%u%c ", &beg, &op))
1640 break;
1641 if(op == '-') fscanf(fnuma_list, "%u%c ", &end, &op);
1642 else end = beg;
1644 for(i=beg; i<=end; i++)
1646 ULONG_PTR mask = 0;
1648 sprintf(name, numa_info, i);
1649 f = fopen(name, "r");
1650 if(!f) continue;
1651 while(!feof(f))
1653 if(!fscanf(f, "%x%c ", &r, &op))
1654 break;
1655 mask = (sizeof(ULONG_PTR)>sizeof(int) ? mask<<(8*sizeof(DWORD)) : 0) + r;
1657 fclose(f);
1659 if(!logical_proc_info_add_numa_node(data, dataex, &len, max_len, mask, i))
1661 fclose(fnuma_list);
1662 return STATUS_NO_MEMORY;
1666 fclose(fnuma_list);
1669 if(dataex)
1670 logical_proc_info_add_group(dataex, &len, max_len, num_cpus, all_cpus_mask);
1672 if(data)
1673 *max_len = len * sizeof(**data);
1674 else
1675 *max_len = len;
1677 return STATUS_SUCCESS;
1679 #elif defined(__APPLE__)
1680 /* for 'data', max_len is the array count. for 'dataex', max_len is in bytes */
1681 static NTSTATUS create_logical_proc_info(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
1682 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex, DWORD *max_len)
1684 DWORD pkgs_no, cores_no, lcpu_no, lcpu_per_core, cores_per_package, assoc, len = 0;
1685 DWORD cache_ctrs[10] = {0};
1686 ULONG_PTR all_cpus_mask = 0;
1687 CACHE_DESCRIPTOR cache[10];
1688 LONGLONG cache_size, cache_line_size, cache_sharing[10];
1689 size_t size;
1690 DWORD p,i,j,k;
1692 lcpu_no = NtCurrentTeb()->Peb->NumberOfProcessors;
1694 size = sizeof(pkgs_no);
1695 if(sysctlbyname("hw.packages", &pkgs_no, &size, NULL, 0))
1696 pkgs_no = 1;
1698 size = sizeof(cores_no);
1699 if(sysctlbyname("hw.physicalcpu", &cores_no, &size, NULL, 0))
1700 cores_no = lcpu_no;
1702 TRACE("%u logical CPUs from %u physical cores across %u packages\n",
1703 lcpu_no, cores_no, pkgs_no);
1705 lcpu_per_core = lcpu_no / cores_no;
1706 cores_per_package = cores_no / pkgs_no;
1708 memset(cache, 0, sizeof(cache));
1709 cache[1].Level = 1;
1710 cache[1].Type = CacheInstruction;
1711 cache[1].Associativity = 8; /* reasonable default */
1712 cache[1].LineSize = 0x40; /* reasonable default */
1713 cache[2].Level = 1;
1714 cache[2].Type = CacheData;
1715 cache[2].Associativity = 8;
1716 cache[2].LineSize = 0x40;
1717 cache[3].Level = 2;
1718 cache[3].Type = CacheUnified;
1719 cache[3].Associativity = 8;
1720 cache[3].LineSize = 0x40;
1721 cache[4].Level = 3;
1722 cache[4].Type = CacheUnified;
1723 cache[4].Associativity = 12;
1724 cache[4].LineSize = 0x40;
1726 size = sizeof(cache_line_size);
1727 if(!sysctlbyname("hw.cachelinesize", &cache_line_size, &size, NULL, 0))
1729 for(i=1; i<5; i++)
1730 cache[i].LineSize = cache_line_size;
1733 /* TODO: set actual associativity for all caches */
1734 size = sizeof(assoc);
1735 if(!sysctlbyname("machdep.cpu.cache.L2_associativity", &assoc, &size, NULL, 0))
1736 cache[3].Associativity = assoc;
1738 size = sizeof(cache_size);
1739 if(!sysctlbyname("hw.l1icachesize", &cache_size, &size, NULL, 0))
1740 cache[1].Size = cache_size;
1741 size = sizeof(cache_size);
1742 if(!sysctlbyname("hw.l1dcachesize", &cache_size, &size, NULL, 0))
1743 cache[2].Size = cache_size;
1744 size = sizeof(cache_size);
1745 if(!sysctlbyname("hw.l2cachesize", &cache_size, &size, NULL, 0))
1746 cache[3].Size = cache_size;
1747 size = sizeof(cache_size);
1748 if(!sysctlbyname("hw.l3cachesize", &cache_size, &size, NULL, 0))
1749 cache[4].Size = cache_size;
1751 size = sizeof(cache_sharing);
1752 if(sysctlbyname("hw.cacheconfig", cache_sharing, &size, NULL, 0) < 0){
1753 cache_sharing[1] = lcpu_per_core;
1754 cache_sharing[2] = lcpu_per_core;
1755 cache_sharing[3] = lcpu_per_core;
1756 cache_sharing[4] = lcpu_no;
1757 }else{
1758 /* in cache[], indexes 1 and 2 are l1 caches */
1759 cache_sharing[4] = cache_sharing[3];
1760 cache_sharing[3] = cache_sharing[2];
1761 cache_sharing[2] = cache_sharing[1];
1764 for(p = 0; p < pkgs_no; ++p){
1765 for(j = 0; j < cores_per_package && p * cores_per_package + j < cores_no; ++j){
1766 ULONG_PTR mask = 0;
1768 for(k = 0; k < lcpu_per_core; ++k)
1769 mask |= (ULONG_PTR)1 << (j * lcpu_per_core + k);
1771 all_cpus_mask |= mask;
1773 /* add to package */
1774 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorPackage, p, mask))
1775 return STATUS_NO_MEMORY;
1777 /* add new core */
1778 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorCore, p, mask))
1779 return STATUS_NO_MEMORY;
1781 for(i = 1; i < 5; ++i){
1782 if(cache_ctrs[i] == 0 && cache[i].Size > 0){
1783 mask = 0;
1784 for(k = 0; k < cache_sharing[i]; ++k)
1785 mask |= (ULONG_PTR)1 << (j * lcpu_per_core + k);
1787 if(!logical_proc_info_add_cache(data, dataex, &len, max_len, mask, &cache[i]))
1788 return STATUS_NO_MEMORY;
1791 cache_ctrs[i] += lcpu_per_core;
1793 if(cache_ctrs[i] == cache_sharing[i])
1794 cache_ctrs[i] = 0;
1799 /* OSX doesn't support NUMA, so just make one NUMA node for all CPUs */
1800 if(!logical_proc_info_add_numa_node(data, dataex, &len, max_len, all_cpus_mask, 0))
1801 return STATUS_NO_MEMORY;
1803 if(dataex)
1804 logical_proc_info_add_group(dataex, &len, max_len, lcpu_no, all_cpus_mask);
1806 if(data)
1807 *max_len = len * sizeof(**data);
1808 else
1809 *max_len = len;
1811 return STATUS_SUCCESS;
1813 #else
1814 static NTSTATUS create_logical_proc_info(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
1815 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex, DWORD *max_len)
1817 FIXME("stub\n");
1818 return STATUS_NOT_IMPLEMENTED;
1820 #endif
1822 /******************************************************************************
1823 * NtQuerySystemInformation [NTDLL.@]
1824 * ZwQuerySystemInformation [NTDLL.@]
1826 * ARGUMENTS:
1827 * SystemInformationClass Index to a certain information structure
1828 * SystemTimeAdjustmentInformation SYSTEM_TIME_ADJUSTMENT
1829 * SystemCacheInformation SYSTEM_CACHE_INFORMATION
1830 * SystemConfigurationInformation CONFIGURATION_INFORMATION
1831 * observed (class/len):
1832 * 0x0/0x2c
1833 * 0x12/0x18
1834 * 0x2/0x138
1835 * 0x8/0x600
1836 * 0x25/0xc
1837 * SystemInformation caller supplies storage for the information structure
1838 * Length size of the structure
1839 * ResultLength Data written
1841 NTSTATUS WINAPI NtQuerySystemInformation(
1842 IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
1843 OUT PVOID SystemInformation,
1844 IN ULONG Length,
1845 OUT PULONG ResultLength)
1847 NTSTATUS ret = STATUS_SUCCESS;
1848 ULONG len = 0;
1850 TRACE("(0x%08x,%p,0x%08x,%p)\n",
1851 SystemInformationClass,SystemInformation,Length,ResultLength);
1853 switch (SystemInformationClass)
1855 case SystemBasicInformation:
1857 SYSTEM_BASIC_INFORMATION sbi;
1859 virtual_get_system_info( &sbi );
1860 len = sizeof(sbi);
1862 if ( Length == len)
1864 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1865 else memcpy( SystemInformation, &sbi, len);
1867 else ret = STATUS_INFO_LENGTH_MISMATCH;
1869 break;
1870 case SystemCpuInformation:
1871 if (Length >= (len = sizeof(cached_sci)))
1873 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1874 else memcpy(SystemInformation, &cached_sci, len);
1876 else ret = STATUS_INFO_LENGTH_MISMATCH;
1877 break;
1878 case SystemPerformanceInformation:
1880 SYSTEM_PERFORMANCE_INFORMATION spi;
1881 static BOOL fixme_written = FALSE;
1882 FILE *fp;
1884 memset(&spi, 0 , sizeof(spi));
1885 len = sizeof(spi);
1887 spi.Reserved3 = 0x7fffffff; /* Available paged pool memory? */
1889 if ((fp = fopen("/proc/uptime", "r")))
1891 double uptime, idle_time;
1893 fscanf(fp, "%lf %lf", &uptime, &idle_time);
1894 fclose(fp);
1895 spi.IdleTime.QuadPart = 10000000 * idle_time;
1897 else
1899 static ULONGLONG idle;
1900 /* many programs expect IdleTime to change so fake change */
1901 spi.IdleTime.QuadPart = ++idle;
1904 if (Length >= len)
1906 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1907 else memcpy( SystemInformation, &spi, len);
1909 else ret = STATUS_INFO_LENGTH_MISMATCH;
1910 if(!fixme_written) {
1911 FIXME("info_class SYSTEM_PERFORMANCE_INFORMATION\n");
1912 fixme_written = TRUE;
1915 break;
1916 case SystemTimeOfDayInformation:
1918 SYSTEM_TIMEOFDAY_INFORMATION sti;
1920 memset(&sti, 0 , sizeof(sti));
1922 /* liKeSystemTime, liExpTimeZoneBias, uCurrentTimeZoneId */
1923 sti.liKeBootTime.QuadPart = server_start_time;
1925 if (Length <= sizeof(sti))
1927 len = Length;
1928 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1929 else memcpy( SystemInformation, &sti, Length);
1931 else ret = STATUS_INFO_LENGTH_MISMATCH;
1933 break;
1934 case SystemProcessInformation:
1936 SYSTEM_PROCESS_INFORMATION* spi = SystemInformation;
1937 SYSTEM_PROCESS_INFORMATION* last = NULL;
1938 HANDLE hSnap = 0;
1939 WCHAR procname[1024];
1940 WCHAR* exename;
1941 DWORD wlen = 0;
1942 DWORD procstructlen = 0;
1944 SERVER_START_REQ( create_snapshot )
1946 req->flags = SNAP_PROCESS | SNAP_THREAD;
1947 req->attributes = 0;
1948 if (!(ret = wine_server_call( req )))
1949 hSnap = wine_server_ptr_handle( reply->handle );
1951 SERVER_END_REQ;
1952 len = 0;
1953 while (ret == STATUS_SUCCESS)
1955 SERVER_START_REQ( next_process )
1957 req->handle = wine_server_obj_handle( hSnap );
1958 req->reset = (len == 0);
1959 wine_server_set_reply( req, procname, sizeof(procname)-sizeof(WCHAR) );
1960 if (!(ret = wine_server_call( req )))
1962 /* Make sure procname is 0 terminated */
1963 procname[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
1965 /* Get only the executable name, not the path */
1966 if ((exename = strrchrW(procname, '\\')) != NULL) exename++;
1967 else exename = procname;
1969 wlen = (strlenW(exename) + 1) * sizeof(WCHAR);
1971 procstructlen = sizeof(*spi) + wlen + ((reply->threads - 1) * sizeof(SYSTEM_THREAD_INFORMATION));
1973 if (Length >= len + procstructlen)
1975 /* ftCreationTime, ftUserTime, ftKernelTime;
1976 * vmCounters, ioCounters
1979 memset(spi, 0, sizeof(*spi));
1981 spi->NextEntryOffset = procstructlen - wlen;
1982 spi->dwThreadCount = reply->threads;
1984 /* spi->pszProcessName will be set later on */
1986 spi->dwBasePriority = reply->priority;
1987 spi->UniqueProcessId = UlongToHandle(reply->pid);
1988 spi->ParentProcessId = UlongToHandle(reply->ppid);
1989 spi->HandleCount = reply->handles;
1991 /* spi->ti will be set later on */
1994 len += procstructlen;
1997 SERVER_END_REQ;
1999 if (ret != STATUS_SUCCESS)
2001 if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
2002 break;
2005 if (Length >= len)
2007 int i, j;
2009 /* set thread info */
2010 i = j = 0;
2011 while (ret == STATUS_SUCCESS)
2013 SERVER_START_REQ( next_thread )
2015 req->handle = wine_server_obj_handle( hSnap );
2016 req->reset = (j == 0);
2017 if (!(ret = wine_server_call( req )))
2019 j++;
2020 if (UlongToHandle(reply->pid) == spi->UniqueProcessId)
2022 /* ftKernelTime, ftUserTime, ftCreateTime;
2023 * dwTickCount, dwStartAddress
2026 memset(&spi->ti[i], 0, sizeof(spi->ti));
2028 spi->ti[i].CreateTime.QuadPart = 0xdeadbeef;
2029 spi->ti[i].ClientId.UniqueProcess = UlongToHandle(reply->pid);
2030 spi->ti[i].ClientId.UniqueThread = UlongToHandle(reply->tid);
2031 spi->ti[i].dwCurrentPriority = reply->base_pri + reply->delta_pri;
2032 spi->ti[i].dwBasePriority = reply->base_pri;
2033 i++;
2037 SERVER_END_REQ;
2039 if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
2041 /* now append process name */
2042 spi->ProcessName.Buffer = (WCHAR*)((char*)spi + spi->NextEntryOffset);
2043 spi->ProcessName.Length = wlen - sizeof(WCHAR);
2044 spi->ProcessName.MaximumLength = wlen;
2045 memcpy( spi->ProcessName.Buffer, exename, wlen );
2046 spi->NextEntryOffset += wlen;
2048 last = spi;
2049 spi = (SYSTEM_PROCESS_INFORMATION*)((char*)spi + spi->NextEntryOffset);
2052 if (ret == STATUS_SUCCESS && last) last->NextEntryOffset = 0;
2053 if (len > Length) ret = STATUS_INFO_LENGTH_MISMATCH;
2054 if (hSnap) NtClose(hSnap);
2056 break;
2057 case SystemProcessorPerformanceInformation:
2059 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi = NULL;
2060 unsigned int cpus = 0;
2061 int out_cpus = Length / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
2063 if (out_cpus == 0)
2065 len = 0;
2066 ret = STATUS_INFO_LENGTH_MISMATCH;
2067 break;
2069 else
2070 #ifdef __APPLE__
2072 processor_cpu_load_info_data_t *pinfo;
2073 mach_msg_type_number_t info_count;
2075 if (host_processor_info (mach_host_self (),
2076 PROCESSOR_CPU_LOAD_INFO,
2077 &cpus,
2078 (processor_info_array_t*)&pinfo,
2079 &info_count) == 0)
2081 int i;
2082 cpus = min(cpus,out_cpus);
2083 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
2084 sppi = RtlAllocateHeap(GetProcessHeap(), 0,len);
2085 for (i = 0; i < cpus; i++)
2087 sppi[i].IdleTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_IDLE];
2088 sppi[i].KernelTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_SYSTEM];
2089 sppi[i].UserTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_USER];
2091 vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count * sizeof(natural_t));
2094 #else
2096 FILE *cpuinfo = fopen("/proc/stat", "r");
2097 if (cpuinfo)
2099 unsigned long clk_tck = sysconf(_SC_CLK_TCK);
2100 unsigned long usr,nice,sys,idle,remainder[8];
2101 int i, count;
2102 char name[32];
2103 char line[255];
2105 /* first line is combined usage */
2106 while (fgets(line,255,cpuinfo))
2108 count = sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
2109 name, &usr, &nice, &sys, &idle,
2110 &remainder[0], &remainder[1], &remainder[2], &remainder[3],
2111 &remainder[4], &remainder[5], &remainder[6], &remainder[7]);
2113 if (count < 5 || strncmp( name, "cpu", 3 )) break;
2114 for (i = 0; i + 5 < count; ++i) sys += remainder[i];
2115 sys += idle;
2116 usr += nice;
2117 cpus = atoi( name + 3 ) + 1;
2118 if (cpus > out_cpus) break;
2119 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
2120 if (sppi)
2121 sppi = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sppi, len );
2122 else
2123 sppi = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, len );
2125 sppi[cpus-1].IdleTime.QuadPart = (ULONGLONG)idle * 10000000 / clk_tck;
2126 sppi[cpus-1].KernelTime.QuadPart = (ULONGLONG)sys * 10000000 / clk_tck;
2127 sppi[cpus-1].UserTime.QuadPart = (ULONGLONG)usr * 10000000 / clk_tck;
2129 fclose(cpuinfo);
2132 #endif
2134 if (cpus == 0)
2136 static int i = 1;
2137 unsigned int n;
2138 cpus = min(NtCurrentTeb()->Peb->NumberOfProcessors, out_cpus);
2139 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
2140 sppi = RtlAllocateHeap(GetProcessHeap(), 0, len);
2141 FIXME("stub info_class SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION\n");
2142 /* many programs expect these values to change so fake change */
2143 for (n = 0; n < cpus; n++)
2145 sppi[n].KernelTime.QuadPart = 1 * i;
2146 sppi[n].UserTime.QuadPart = 2 * i;
2147 sppi[n].IdleTime.QuadPart = 3 * i;
2149 i++;
2152 if (Length >= len)
2154 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2155 else memcpy( SystemInformation, sppi, len);
2157 else ret = STATUS_INFO_LENGTH_MISMATCH;
2159 RtlFreeHeap(GetProcessHeap(),0,sppi);
2161 break;
2162 case SystemModuleInformation:
2163 /* FIXME: should be system-wide */
2164 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2165 else ret = LdrQueryProcessModuleInformation( SystemInformation, Length, &len );
2166 break;
2167 case SystemHandleInformation:
2169 struct handle_info *info;
2170 DWORD i, num_handles;
2172 if (Length < sizeof(SYSTEM_HANDLE_INFORMATION))
2174 ret = STATUS_INFO_LENGTH_MISMATCH;
2175 break;
2178 if (!SystemInformation)
2180 ret = STATUS_ACCESS_VIOLATION;
2181 break;
2184 num_handles = (Length - FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handle )) / sizeof(SYSTEM_HANDLE_ENTRY);
2185 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) * num_handles )))
2186 return STATUS_NO_MEMORY;
2188 SERVER_START_REQ( get_system_handles )
2190 wine_server_set_reply( req, info, sizeof(*info) * num_handles );
2191 if (!(ret = wine_server_call( req )))
2193 SYSTEM_HANDLE_INFORMATION *shi = SystemInformation;
2194 shi->Count = wine_server_reply_size( req ) / sizeof(*info);
2195 len = FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handle[shi->Count] );
2196 for (i = 0; i < shi->Count; i++)
2198 memset( &shi->Handle[i], 0, sizeof(shi->Handle[i]) );
2199 shi->Handle[i].OwnerPid = info[i].owner;
2200 shi->Handle[i].HandleValue = info[i].handle;
2201 shi->Handle[i].AccessMask = info[i].access;
2202 /* FIXME: Fill out ObjectType, HandleFlags, ObjectPointer */
2205 else if (ret == STATUS_BUFFER_TOO_SMALL)
2207 len = FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handle[reply->count] );
2208 ret = STATUS_INFO_LENGTH_MISMATCH;
2211 SERVER_END_REQ;
2213 RtlFreeHeap( GetProcessHeap(), 0, info );
2215 break;
2216 case SystemCacheInformation:
2218 SYSTEM_CACHE_INFORMATION sci;
2220 memset(&sci, 0, sizeof(sci)); /* FIXME */
2221 len = sizeof(sci);
2223 if ( Length >= len)
2225 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2226 else memcpy( SystemInformation, &sci, len);
2228 else ret = STATUS_INFO_LENGTH_MISMATCH;
2229 FIXME("info_class SYSTEM_CACHE_INFORMATION\n");
2231 break;
2232 case SystemInterruptInformation:
2234 SYSTEM_INTERRUPT_INFORMATION sii;
2236 memset(&sii, 0, sizeof(sii));
2237 len = sizeof(sii);
2239 if ( Length >= len)
2241 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2242 else memcpy( SystemInformation, &sii, len);
2244 else ret = STATUS_INFO_LENGTH_MISMATCH;
2245 FIXME("info_class SYSTEM_INTERRUPT_INFORMATION\n");
2247 break;
2248 case SystemKernelDebuggerInformation:
2250 SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
2252 skdi.DebuggerEnabled = FALSE;
2253 skdi.DebuggerNotPresent = TRUE;
2254 len = sizeof(skdi);
2256 if ( Length >= len)
2258 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2259 else memcpy( SystemInformation, &skdi, len);
2261 else ret = STATUS_INFO_LENGTH_MISMATCH;
2263 break;
2264 case SystemRegistryQuotaInformation:
2266 /* Something to do with the size of the registry *
2267 * Since we don't have a size limitation, fake it *
2268 * This is almost certainly wrong. *
2269 * This sets each of the three words in the struct to 32 MB, *
2270 * which is enough to make the IE 5 installer happy. */
2271 SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
2273 srqi.RegistryQuotaAllowed = 0x2000000;
2274 srqi.RegistryQuotaUsed = 0x200000;
2275 srqi.Reserved1 = (void*)0x200000;
2276 len = sizeof(srqi);
2278 if ( Length >= len)
2280 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2281 else
2283 FIXME("SystemRegistryQuotaInformation: faking max registry size of 32 MB\n");
2284 memcpy( SystemInformation, &srqi, len);
2287 else ret = STATUS_INFO_LENGTH_MISMATCH;
2289 break;
2290 case SystemLogicalProcessorInformation:
2292 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf;
2294 /* Each logical processor may use up to 7 entries in returned table:
2295 * core, numa node, package, L1i, L1d, L2, L3 */
2296 len = 7 * NtCurrentTeb()->Peb->NumberOfProcessors;
2297 buf = RtlAllocateHeap(GetProcessHeap(), 0, len * sizeof(*buf));
2298 if(!buf)
2300 ret = STATUS_NO_MEMORY;
2301 break;
2304 ret = create_logical_proc_info(&buf, NULL, &len);
2305 if( ret != STATUS_SUCCESS )
2307 RtlFreeHeap(GetProcessHeap(), 0, buf);
2308 break;
2311 if( Length >= len)
2313 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2314 else memcpy( SystemInformation, buf, len);
2316 else ret = STATUS_INFO_LENGTH_MISMATCH;
2317 RtlFreeHeap(GetProcessHeap(), 0, buf);
2319 break;
2320 case SystemRecommendedSharedDataAlignment:
2322 len = sizeof(DWORD);
2323 if (Length >= len)
2325 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2326 else *((DWORD *)SystemInformation) = 64;
2328 else ret = STATUS_INFO_LENGTH_MISMATCH;
2330 break;
2331 default:
2332 FIXME("(0x%08x,%p,0x%08x,%p) stub\n",
2333 SystemInformationClass,SystemInformation,Length,ResultLength);
2335 /* Several Information Classes are not implemented on Windows and return 2 different values
2336 * STATUS_NOT_IMPLEMENTED or STATUS_INVALID_INFO_CLASS
2337 * in 95% of the cases it's STATUS_INVALID_INFO_CLASS, so use this as the default
2339 ret = STATUS_INVALID_INFO_CLASS;
2342 if (ResultLength) *ResultLength = len;
2344 return ret;
2347 /******************************************************************************
2348 * NtQuerySystemInformationEx [NTDLL.@]
2349 * ZwQuerySystemInformationEx [NTDLL.@]
2351 NTSTATUS WINAPI NtQuerySystemInformationEx(SYSTEM_INFORMATION_CLASS SystemInformationClass,
2352 void *Query, ULONG QueryLength, void *SystemInformation, ULONG Length, ULONG *ResultLength)
2354 ULONG len = 0;
2355 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
2357 TRACE("(0x%08x,%p,%u,%p,%u,%p) stub\n", SystemInformationClass, Query, QueryLength, SystemInformation,
2358 Length, ResultLength);
2360 switch (SystemInformationClass) {
2361 case SystemLogicalProcessorInformationEx:
2363 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buf;
2365 if (!Query || QueryLength < sizeof(DWORD))
2367 ret = STATUS_INVALID_PARAMETER;
2368 break;
2371 if (*(DWORD*)Query != RelationAll)
2372 FIXME("Relationship filtering not implemented: 0x%x\n", *(DWORD*)Query);
2374 len = 3 * sizeof(*buf);
2375 buf = RtlAllocateHeap(GetProcessHeap(), 0, len);
2376 if (!buf)
2378 ret = STATUS_NO_MEMORY;
2379 break;
2382 ret = create_logical_proc_info(NULL, &buf, &len);
2383 if (ret != STATUS_SUCCESS)
2385 RtlFreeHeap(GetProcessHeap(), 0, buf);
2386 break;
2389 if (Length >= len)
2391 if (!SystemInformation)
2392 ret = STATUS_ACCESS_VIOLATION;
2393 else
2394 memcpy( SystemInformation, buf, len);
2396 else
2397 ret = STATUS_INFO_LENGTH_MISMATCH;
2399 RtlFreeHeap(GetProcessHeap(), 0, buf);
2401 break;
2403 default:
2404 FIXME("(0x%08x,%p,%u,%p,%u,%p) stub\n", SystemInformationClass, Query, QueryLength, SystemInformation,
2405 Length, ResultLength);
2406 break;
2409 if (ResultLength)
2410 *ResultLength = len;
2412 return ret;
2415 /******************************************************************************
2416 * NtSetSystemInformation [NTDLL.@]
2417 * ZwSetSystemInformation [NTDLL.@]
2419 NTSTATUS WINAPI NtSetSystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG Length)
2421 FIXME("(0x%08x,%p,0x%08x) stub\n",SystemInformationClass,SystemInformation,Length);
2422 return STATUS_SUCCESS;
2425 /******************************************************************************
2426 * NtCreatePagingFile [NTDLL.@]
2427 * ZwCreatePagingFile [NTDLL.@]
2429 NTSTATUS WINAPI NtCreatePagingFile(
2430 PUNICODE_STRING PageFileName,
2431 PLARGE_INTEGER MinimumSize,
2432 PLARGE_INTEGER MaximumSize,
2433 PLARGE_INTEGER ActualSize)
2435 FIXME("(%p %p %p %p) stub\n", PageFileName, MinimumSize, MaximumSize, ActualSize);
2436 return STATUS_SUCCESS;
2439 /******************************************************************************
2440 * NtDisplayString [NTDLL.@]
2442 * writes a string to the nt-textmode screen eg. during startup
2444 NTSTATUS WINAPI NtDisplayString ( PUNICODE_STRING string )
2446 STRING stringA;
2447 NTSTATUS ret;
2449 if (!(ret = RtlUnicodeStringToAnsiString( &stringA, string, TRUE )))
2451 MESSAGE( "%.*s", stringA.Length, stringA.Buffer );
2452 RtlFreeAnsiString( &stringA );
2454 return ret;
2457 /******************************************************************************
2458 * NtInitiatePowerAction [NTDLL.@]
2461 NTSTATUS WINAPI NtInitiatePowerAction(
2462 IN POWER_ACTION SystemAction,
2463 IN SYSTEM_POWER_STATE MinSystemState,
2464 IN ULONG Flags,
2465 IN BOOLEAN Asynchronous)
2467 FIXME("(%d,%d,0x%08x,%d),stub\n",
2468 SystemAction,MinSystemState,Flags,Asynchronous);
2469 return STATUS_NOT_IMPLEMENTED;
2472 #ifdef linux
2473 /* Fallback using /proc/cpuinfo for Linux systems without cpufreq. For
2474 * most distributions on recent enough hardware, this is only likely to
2475 * happen while running in virtualized environments such as QEMU. */
2476 static ULONG mhz_from_cpuinfo(void)
2478 char line[512];
2479 char *s, *value;
2480 double cmz = 0;
2481 FILE* f = fopen("/proc/cpuinfo", "r");
2482 if(f) {
2483 while (fgets(line, sizeof(line), f) != NULL) {
2484 if (!(value = strchr(line,':')))
2485 continue;
2486 s = value - 1;
2487 while ((s >= line) && isspace(*s)) s--;
2488 *(s + 1) = '\0';
2489 value++;
2490 if (!strcasecmp(line, "cpu MHz")) {
2491 sscanf(value, " %lf", &cmz);
2492 break;
2495 fclose(f);
2497 return cmz;
2499 #endif
2501 /******************************************************************************
2502 * NtPowerInformation [NTDLL.@]
2505 NTSTATUS WINAPI NtPowerInformation(
2506 IN POWER_INFORMATION_LEVEL InformationLevel,
2507 IN PVOID lpInputBuffer,
2508 IN ULONG nInputBufferSize,
2509 IN PVOID lpOutputBuffer,
2510 IN ULONG nOutputBufferSize)
2512 TRACE("(%d,%p,%d,%p,%d)\n",
2513 InformationLevel,lpInputBuffer,nInputBufferSize,lpOutputBuffer,nOutputBufferSize);
2514 switch(InformationLevel) {
2515 case SystemPowerCapabilities: {
2516 PSYSTEM_POWER_CAPABILITIES PowerCaps = lpOutputBuffer;
2517 FIXME("semi-stub: SystemPowerCapabilities\n");
2518 if (nOutputBufferSize < sizeof(SYSTEM_POWER_CAPABILITIES))
2519 return STATUS_BUFFER_TOO_SMALL;
2520 /* FIXME: These values are based off a native XP desktop, should probably use APM/ACPI to get the 'real' values */
2521 PowerCaps->PowerButtonPresent = TRUE;
2522 PowerCaps->SleepButtonPresent = FALSE;
2523 PowerCaps->LidPresent = FALSE;
2524 PowerCaps->SystemS1 = TRUE;
2525 PowerCaps->SystemS2 = FALSE;
2526 PowerCaps->SystemS3 = FALSE;
2527 PowerCaps->SystemS4 = TRUE;
2528 PowerCaps->SystemS5 = TRUE;
2529 PowerCaps->HiberFilePresent = TRUE;
2530 PowerCaps->FullWake = TRUE;
2531 PowerCaps->VideoDimPresent = FALSE;
2532 PowerCaps->ApmPresent = FALSE;
2533 PowerCaps->UpsPresent = FALSE;
2534 PowerCaps->ThermalControl = FALSE;
2535 PowerCaps->ProcessorThrottle = FALSE;
2536 PowerCaps->ProcessorMinThrottle = 100;
2537 PowerCaps->ProcessorMaxThrottle = 100;
2538 PowerCaps->DiskSpinDown = TRUE;
2539 PowerCaps->SystemBatteriesPresent = FALSE;
2540 PowerCaps->BatteriesAreShortTerm = FALSE;
2541 PowerCaps->BatteryScale[0].Granularity = 0;
2542 PowerCaps->BatteryScale[0].Capacity = 0;
2543 PowerCaps->BatteryScale[1].Granularity = 0;
2544 PowerCaps->BatteryScale[1].Capacity = 0;
2545 PowerCaps->BatteryScale[2].Granularity = 0;
2546 PowerCaps->BatteryScale[2].Capacity = 0;
2547 PowerCaps->AcOnLineWake = PowerSystemUnspecified;
2548 PowerCaps->SoftLidWake = PowerSystemUnspecified;
2549 PowerCaps->RtcWake = PowerSystemSleeping1;
2550 PowerCaps->MinDeviceWakeState = PowerSystemUnspecified;
2551 PowerCaps->DefaultLowLatencyWake = PowerSystemUnspecified;
2552 return STATUS_SUCCESS;
2554 case SystemExecutionState: {
2555 PULONG ExecutionState = lpOutputBuffer;
2556 WARN("semi-stub: SystemExecutionState\n"); /* Needed for .NET Framework, but using a FIXME is really noisy. */
2557 if (lpInputBuffer != NULL)
2558 return STATUS_INVALID_PARAMETER;
2559 /* FIXME: The actual state should be the value set by SetThreadExecutionState which is not currently implemented. */
2560 *ExecutionState = ES_USER_PRESENT;
2561 return STATUS_SUCCESS;
2563 case ProcessorInformation: {
2564 const int cannedMHz = 1000; /* We fake a 1GHz processor if we can't conjure up real values */
2565 PROCESSOR_POWER_INFORMATION* cpu_power = lpOutputBuffer;
2566 int i, out_cpus;
2568 if ((lpOutputBuffer == NULL) || (nOutputBufferSize == 0))
2569 return STATUS_INVALID_PARAMETER;
2570 out_cpus = NtCurrentTeb()->Peb->NumberOfProcessors;
2571 if ((nOutputBufferSize / sizeof(PROCESSOR_POWER_INFORMATION)) < out_cpus)
2572 return STATUS_BUFFER_TOO_SMALL;
2573 #if defined(linux)
2575 char filename[128];
2576 FILE* f;
2578 for(i = 0; i < out_cpus; i++) {
2579 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
2580 f = fopen(filename, "r");
2581 if (f && (fscanf(f, "%d", &cpu_power[i].CurrentMhz) == 1)) {
2582 cpu_power[i].CurrentMhz /= 1000;
2583 fclose(f);
2585 else {
2586 if(i == 0) {
2587 cpu_power[0].CurrentMhz = mhz_from_cpuinfo();
2588 if(cpu_power[0].CurrentMhz == 0)
2589 cpu_power[0].CurrentMhz = cannedMHz;
2591 else
2592 cpu_power[i].CurrentMhz = cpu_power[0].CurrentMhz;
2593 if(f) fclose(f);
2596 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", i);
2597 f = fopen(filename, "r");
2598 if (f && (fscanf(f, "%d", &cpu_power[i].MaxMhz) == 1)) {
2599 cpu_power[i].MaxMhz /= 1000;
2600 fclose(f);
2602 else {
2603 cpu_power[i].MaxMhz = cpu_power[i].CurrentMhz;
2604 if(f) fclose(f);
2607 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", i);
2608 f = fopen(filename, "r");
2609 if(f && (fscanf(f, "%d", &cpu_power[i].MhzLimit) == 1)) {
2610 cpu_power[i].MhzLimit /= 1000;
2611 fclose(f);
2613 else
2615 cpu_power[i].MhzLimit = cpu_power[i].MaxMhz;
2616 if(f) fclose(f);
2619 cpu_power[i].Number = i;
2620 cpu_power[i].MaxIdleState = 0; /* FIXME */
2621 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2624 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
2626 int num;
2627 size_t valSize = sizeof(num);
2628 if (sysctlbyname("hw.clockrate", &num, &valSize, NULL, 0))
2629 num = cannedMHz;
2630 for(i = 0; i < out_cpus; i++) {
2631 cpu_power[i].CurrentMhz = num;
2632 cpu_power[i].MaxMhz = num;
2633 cpu_power[i].MhzLimit = num;
2634 cpu_power[i].Number = i;
2635 cpu_power[i].MaxIdleState = 0; /* FIXME */
2636 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2639 #elif defined (__APPLE__)
2641 size_t valSize;
2642 unsigned long long currentMhz;
2643 unsigned long long maxMhz;
2645 valSize = sizeof(currentMhz);
2646 if (!sysctlbyname("hw.cpufrequency", &currentMhz, &valSize, NULL, 0))
2647 currentMhz /= 1000000;
2648 else
2649 currentMhz = cannedMHz;
2651 valSize = sizeof(maxMhz);
2652 if (!sysctlbyname("hw.cpufrequency_max", &maxMhz, &valSize, NULL, 0))
2653 maxMhz /= 1000000;
2654 else
2655 maxMhz = currentMhz;
2657 for(i = 0; i < out_cpus; i++) {
2658 cpu_power[i].CurrentMhz = currentMhz;
2659 cpu_power[i].MaxMhz = maxMhz;
2660 cpu_power[i].MhzLimit = maxMhz;
2661 cpu_power[i].Number = i;
2662 cpu_power[i].MaxIdleState = 0; /* FIXME */
2663 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2666 #else
2667 for(i = 0; i < out_cpus; i++) {
2668 cpu_power[i].CurrentMhz = cannedMHz;
2669 cpu_power[i].MaxMhz = cannedMHz;
2670 cpu_power[i].MhzLimit = cannedMHz;
2671 cpu_power[i].Number = i;
2672 cpu_power[i].MaxIdleState = 0; /* FIXME */
2673 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2675 WARN("Unable to detect CPU MHz for this platform. Reporting %d MHz.\n", cannedMHz);
2676 #endif
2677 for(i = 0; i < out_cpus; i++) {
2678 TRACE("cpu_power[%d] = %u %u %u %u %u %u\n", i, cpu_power[i].Number,
2679 cpu_power[i].MaxMhz, cpu_power[i].CurrentMhz, cpu_power[i].MhzLimit,
2680 cpu_power[i].MaxIdleState, cpu_power[i].CurrentIdleState);
2682 return STATUS_SUCCESS;
2684 default:
2685 /* FIXME: Needed by .NET Framework */
2686 WARN("Unimplemented NtPowerInformation action: %d\n", InformationLevel);
2687 return STATUS_NOT_IMPLEMENTED;
2691 /******************************************************************************
2692 * NtShutdownSystem [NTDLL.@]
2695 NTSTATUS WINAPI NtShutdownSystem(SHUTDOWN_ACTION Action)
2697 FIXME("%d\n",Action);
2698 return STATUS_SUCCESS;
2701 /******************************************************************************
2702 * NtAllocateLocallyUniqueId (NTDLL.@)
2704 NTSTATUS WINAPI NtAllocateLocallyUniqueId(PLUID Luid)
2706 NTSTATUS status;
2708 TRACE("%p\n", Luid);
2710 if (!Luid)
2711 return STATUS_ACCESS_VIOLATION;
2713 SERVER_START_REQ( allocate_locally_unique_id )
2715 status = wine_server_call( req );
2716 if (!status)
2718 Luid->LowPart = reply->luid.low_part;
2719 Luid->HighPart = reply->luid.high_part;
2722 SERVER_END_REQ;
2724 return status;
2727 /******************************************************************************
2728 * VerSetConditionMask (NTDLL.@)
2730 ULONGLONG WINAPI VerSetConditionMask( ULONGLONG dwlConditionMask, DWORD dwTypeBitMask,
2731 BYTE dwConditionMask)
2733 if(dwTypeBitMask == 0)
2734 return dwlConditionMask;
2735 dwConditionMask &= 0x07;
2736 if(dwConditionMask == 0)
2737 return dwlConditionMask;
2739 if(dwTypeBitMask & VER_PRODUCT_TYPE)
2740 dwlConditionMask |= dwConditionMask << 7*3;
2741 else if (dwTypeBitMask & VER_SUITENAME)
2742 dwlConditionMask |= dwConditionMask << 6*3;
2743 else if (dwTypeBitMask & VER_SERVICEPACKMAJOR)
2744 dwlConditionMask |= dwConditionMask << 5*3;
2745 else if (dwTypeBitMask & VER_SERVICEPACKMINOR)
2746 dwlConditionMask |= dwConditionMask << 4*3;
2747 else if (dwTypeBitMask & VER_PLATFORMID)
2748 dwlConditionMask |= dwConditionMask << 3*3;
2749 else if (dwTypeBitMask & VER_BUILDNUMBER)
2750 dwlConditionMask |= dwConditionMask << 2*3;
2751 else if (dwTypeBitMask & VER_MAJORVERSION)
2752 dwlConditionMask |= dwConditionMask << 1*3;
2753 else if (dwTypeBitMask & VER_MINORVERSION)
2754 dwlConditionMask |= dwConditionMask << 0*3;
2755 return dwlConditionMask;
2758 /******************************************************************************
2759 * NtAccessCheckAndAuditAlarm (NTDLL.@)
2760 * ZwAccessCheckAndAuditAlarm (NTDLL.@)
2762 NTSTATUS WINAPI NtAccessCheckAndAuditAlarm(PUNICODE_STRING SubsystemName, HANDLE HandleId, PUNICODE_STRING ObjectTypeName,
2763 PUNICODE_STRING ObjectName, PSECURITY_DESCRIPTOR SecurityDescriptor,
2764 ACCESS_MASK DesiredAccess, PGENERIC_MAPPING GenericMapping, BOOLEAN ObjectCreation,
2765 PACCESS_MASK GrantedAccess, PBOOLEAN AccessStatus, PBOOLEAN GenerateOnClose)
2767 FIXME("(%s, %p, %s, %p, 0x%08x, %p, %d, %p, %p, %p), stub\n", debugstr_us(SubsystemName), HandleId,
2768 debugstr_us(ObjectTypeName), SecurityDescriptor, DesiredAccess, GenericMapping, ObjectCreation,
2769 GrantedAccess, AccessStatus, GenerateOnClose);
2771 return STATUS_NOT_IMPLEMENTED;
2774 /******************************************************************************
2775 * NtSystemDebugControl (NTDLL.@)
2776 * ZwSystemDebugControl (NTDLL.@)
2778 NTSTATUS WINAPI NtSystemDebugControl(SYSDBG_COMMAND command, PVOID inbuffer, ULONG inbuflength, PVOID outbuffer,
2779 ULONG outbuflength, PULONG retlength)
2781 FIXME("(%d, %p, %d, %p, %d, %p), stub\n", command, inbuffer, inbuflength, outbuffer, outbuflength, retlength);
2783 return STATUS_NOT_IMPLEMENTED;
2786 /******************************************************************************
2787 * NtSetLdtEntries (NTDLL.@)
2788 * ZwSetLdtEntries (NTDLL.@)
2790 NTSTATUS WINAPI NtSetLdtEntries(ULONG selector1, ULONG entry1_low, ULONG entry1_high,
2791 ULONG selector2, ULONG entry2_low, ULONG entry2_high)
2793 FIXME("(%u, %u, %u, %u, %u, %u): stub\n", selector1, entry1_low, entry1_high, selector2, entry2_low, entry2_high);
2795 return STATUS_NOT_IMPLEMENTED;