comdlg32: Detach file dialog data after child windows were destroyed.
[wine.git] / dlls / ntdll / nt.c
blob8995c362a9aaa2ed690f7c58a4e0c7d6265349e4
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;
86 data_size_t len;
87 struct object_attributes *objattr;
89 TRACE("(%p,0x%08x,%s,0x%08x,0x%08x,%p)\n",
90 ExistingToken, DesiredAccess, debugstr_ObjectAttributes(ObjectAttributes),
91 ImpersonationLevel, TokenType, NewToken);
93 if ((status = alloc_object_attributes( ObjectAttributes, &objattr, &len ))) return status;
95 if (ObjectAttributes && ObjectAttributes->SecurityQualityOfService)
97 SECURITY_QUALITY_OF_SERVICE *SecurityQOS = ObjectAttributes->SecurityQualityOfService;
98 TRACE("ObjectAttributes->SecurityQualityOfService = {%d, %d, %d, %s}\n",
99 SecurityQOS->Length, SecurityQOS->ImpersonationLevel,
100 SecurityQOS->ContextTrackingMode,
101 SecurityQOS->EffectiveOnly ? "TRUE" : "FALSE");
102 ImpersonationLevel = SecurityQOS->ImpersonationLevel;
105 SERVER_START_REQ( duplicate_token )
107 req->handle = wine_server_obj_handle( ExistingToken );
108 req->access = DesiredAccess;
109 req->primary = (TokenType == TokenPrimary);
110 req->impersonation_level = ImpersonationLevel;
111 wine_server_add_data( req, objattr, len );
112 status = wine_server_call( req );
113 if (!status) *NewToken = wine_server_ptr_handle( reply->new_handle );
115 SERVER_END_REQ;
117 RtlFreeHeap( GetProcessHeap(), 0, objattr );
118 return status;
121 /******************************************************************************
122 * NtOpenProcessToken [NTDLL.@]
123 * ZwOpenProcessToken [NTDLL.@]
125 NTSTATUS WINAPI NtOpenProcessToken(
126 HANDLE ProcessHandle,
127 DWORD DesiredAccess,
128 HANDLE *TokenHandle)
130 return NtOpenProcessTokenEx( ProcessHandle, DesiredAccess, 0, TokenHandle );
133 /******************************************************************************
134 * NtOpenProcessTokenEx [NTDLL.@]
135 * ZwOpenProcessTokenEx [NTDLL.@]
137 NTSTATUS WINAPI NtOpenProcessTokenEx( HANDLE process, DWORD access, DWORD attributes,
138 HANDLE *handle )
140 NTSTATUS ret;
142 TRACE("(%p,0x%08x,0x%08x,%p)\n", process, access, attributes, handle);
144 SERVER_START_REQ( open_token )
146 req->handle = wine_server_obj_handle( process );
147 req->access = access;
148 req->attributes = attributes;
149 req->flags = 0;
150 ret = wine_server_call( req );
151 if (!ret) *handle = wine_server_ptr_handle( reply->token );
153 SERVER_END_REQ;
154 return ret;
157 /******************************************************************************
158 * NtOpenThreadToken [NTDLL.@]
159 * ZwOpenThreadToken [NTDLL.@]
161 NTSTATUS WINAPI NtOpenThreadToken(
162 HANDLE ThreadHandle,
163 DWORD DesiredAccess,
164 BOOLEAN OpenAsSelf,
165 HANDLE *TokenHandle)
167 return NtOpenThreadTokenEx( ThreadHandle, DesiredAccess, OpenAsSelf, 0, TokenHandle );
170 /******************************************************************************
171 * NtOpenThreadTokenEx [NTDLL.@]
172 * ZwOpenThreadTokenEx [NTDLL.@]
174 NTSTATUS WINAPI NtOpenThreadTokenEx( HANDLE thread, DWORD access, BOOLEAN as_self, DWORD attributes,
175 HANDLE *handle )
177 NTSTATUS ret;
179 TRACE("(%p,0x%08x,%u,0x%08x,%p)\n", thread, access, as_self, attributes, handle );
181 SERVER_START_REQ( open_token )
183 req->handle = wine_server_obj_handle( thread );
184 req->access = access;
185 req->attributes = attributes;
186 req->flags = OPEN_TOKEN_THREAD;
187 if (as_self) req->flags |= OPEN_TOKEN_AS_SELF;
188 ret = wine_server_call( req );
189 if (!ret) *handle = wine_server_ptr_handle( reply->token );
191 SERVER_END_REQ;
193 return ret;
196 /******************************************************************************
197 * NtAdjustPrivilegesToken [NTDLL.@]
198 * ZwAdjustPrivilegesToken [NTDLL.@]
200 * FIXME: parameters unsafe
202 NTSTATUS WINAPI NtAdjustPrivilegesToken(
203 IN HANDLE TokenHandle,
204 IN BOOLEAN DisableAllPrivileges,
205 IN PTOKEN_PRIVILEGES NewState,
206 IN DWORD BufferLength,
207 OUT PTOKEN_PRIVILEGES PreviousState,
208 OUT PDWORD ReturnLength)
210 NTSTATUS ret;
212 TRACE("(%p,0x%08x,%p,0x%08x,%p,%p)\n",
213 TokenHandle, DisableAllPrivileges, NewState, BufferLength, PreviousState, ReturnLength);
215 SERVER_START_REQ( adjust_token_privileges )
217 req->handle = wine_server_obj_handle( TokenHandle );
218 req->disable_all = DisableAllPrivileges;
219 req->get_modified_state = (PreviousState != NULL);
220 if (!DisableAllPrivileges)
222 wine_server_add_data( req, NewState->Privileges,
223 NewState->PrivilegeCount * sizeof(NewState->Privileges[0]) );
225 if (PreviousState && BufferLength >= FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ))
226 wine_server_set_reply( req, PreviousState->Privileges,
227 BufferLength - FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) );
228 ret = wine_server_call( req );
229 if (PreviousState)
231 if (ReturnLength) *ReturnLength = reply->len + FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges );
232 PreviousState->PrivilegeCount = reply->len / sizeof(LUID_AND_ATTRIBUTES);
235 SERVER_END_REQ;
237 return ret;
240 /******************************************************************************
241 * NtQueryInformationToken [NTDLL.@]
242 * ZwQueryInformationToken [NTDLL.@]
244 * NOTES
245 * Buffer for TokenUser:
246 * 0x00 TOKEN_USER the PSID field points to the SID
247 * 0x08 SID
250 NTSTATUS WINAPI NtQueryInformationToken(
251 HANDLE token,
252 TOKEN_INFORMATION_CLASS tokeninfoclass,
253 PVOID tokeninfo,
254 ULONG tokeninfolength,
255 PULONG retlen )
257 static const ULONG info_len [] =
260 0, /* TokenUser */
261 0, /* TokenGroups */
262 0, /* TokenPrivileges */
263 0, /* TokenOwner */
264 0, /* TokenPrimaryGroup */
265 0, /* TokenDefaultDacl */
266 sizeof(TOKEN_SOURCE), /* TokenSource */
267 sizeof(TOKEN_TYPE), /* TokenType */
268 sizeof(SECURITY_IMPERSONATION_LEVEL), /* TokenImpersonationLevel */
269 sizeof(TOKEN_STATISTICS), /* TokenStatistics */
270 0, /* TokenRestrictedSids */
271 sizeof(DWORD), /* TokenSessionId */
272 0, /* TokenGroupsAndPrivileges */
273 0, /* TokenSessionReference */
274 0, /* TokenSandBoxInert */
275 0, /* TokenAuditPolicy */
276 0, /* TokenOrigin */
277 sizeof(TOKEN_ELEVATION_TYPE), /* TokenElevationType */
278 0, /* TokenLinkedToken */
279 sizeof(TOKEN_ELEVATION), /* TokenElevation */
280 0, /* TokenHasRestrictions */
281 0, /* TokenAccessInformation */
282 0, /* TokenVirtualizationAllowed */
283 0, /* TokenVirtualizationEnabled */
284 sizeof(TOKEN_MANDATORY_LABEL) + sizeof(SID), /* TokenIntegrityLevel [sizeof(SID) includes one SubAuthority] */
285 0, /* TokenUIAccess */
286 0, /* TokenMandatoryPolicy */
287 0, /* TokenLogonSid */
288 sizeof(DWORD), /* TokenIsAppContainer */
289 0, /* TokenCapabilities */
290 sizeof(TOKEN_APPCONTAINER_INFORMATION) + sizeof(SID), /* TokenAppContainerSid */
291 0, /* TokenAppContainerNumber */
292 0, /* TokenUserClaimAttributes*/
293 0, /* TokenDeviceClaimAttributes */
294 0, /* TokenRestrictedUserClaimAttributes */
295 0, /* TokenRestrictedDeviceClaimAttributes */
296 0, /* TokenDeviceGroups */
297 0, /* TokenRestrictedDeviceGroups */
298 0, /* TokenSecurityAttributes */
299 0, /* TokenIsRestricted */
300 0 /* TokenProcessTrustLevel */
303 ULONG len = 0;
304 NTSTATUS status = STATUS_SUCCESS;
306 TRACE("(%p,%d,%p,%d,%p)\n",
307 token,tokeninfoclass,tokeninfo,tokeninfolength,retlen);
309 if (tokeninfoclass < MaxTokenInfoClass)
310 len = info_len[tokeninfoclass];
312 if (retlen) *retlen = len;
314 if (tokeninfolength < len)
315 return STATUS_BUFFER_TOO_SMALL;
317 switch (tokeninfoclass)
319 case TokenUser:
320 SERVER_START_REQ( get_token_sid )
322 TOKEN_USER * tuser = tokeninfo;
323 PSID sid = tuser + 1;
324 DWORD sid_len = tokeninfolength < sizeof(TOKEN_USER) ? 0 : tokeninfolength - sizeof(TOKEN_USER);
326 req->handle = wine_server_obj_handle( token );
327 req->which_sid = tokeninfoclass;
328 wine_server_set_reply( req, sid, sid_len );
329 status = wine_server_call( req );
330 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_USER);
331 if (status == STATUS_SUCCESS)
333 tuser->User.Sid = sid;
334 tuser->User.Attributes = 0;
337 SERVER_END_REQ;
338 break;
339 case TokenGroups:
341 void *buffer;
343 /* reply buffer is always shorter than output one */
344 buffer = tokeninfolength ? RtlAllocateHeap(GetProcessHeap(), 0, tokeninfolength) : NULL;
346 SERVER_START_REQ( get_token_groups )
348 TOKEN_GROUPS *groups = tokeninfo;
350 req->handle = wine_server_obj_handle( token );
351 wine_server_set_reply( req, buffer, tokeninfolength );
352 status = wine_server_call( req );
353 if (status == STATUS_BUFFER_TOO_SMALL)
355 if (retlen) *retlen = reply->user_len;
357 else if (status == STATUS_SUCCESS)
359 struct token_groups *tg = buffer;
360 unsigned int *attr = (unsigned int *)(tg + 1);
361 ULONG i;
362 const int non_sid_portion = (sizeof(struct token_groups) + tg->count * sizeof(unsigned int));
363 SID *sids = (SID *)((char *)tokeninfo + FIELD_OFFSET( TOKEN_GROUPS, Groups[tg->count] ));
365 if (retlen) *retlen = reply->user_len;
367 groups->GroupCount = tg->count;
368 memcpy( sids, (char *)buffer + non_sid_portion,
369 reply->user_len - FIELD_OFFSET( TOKEN_GROUPS, Groups[tg->count] ));
371 for (i = 0; i < tg->count; i++)
373 groups->Groups[i].Attributes = attr[i];
374 groups->Groups[i].Sid = sids;
375 sids = (SID *)((char *)sids + RtlLengthSid(sids));
378 else if (retlen) *retlen = 0;
380 SERVER_END_REQ;
382 RtlFreeHeap(GetProcessHeap(), 0, buffer);
383 break;
385 case TokenPrimaryGroup:
386 SERVER_START_REQ( get_token_sid )
388 TOKEN_PRIMARY_GROUP *tgroup = tokeninfo;
389 PSID sid = tgroup + 1;
390 DWORD sid_len = tokeninfolength < sizeof(TOKEN_PRIMARY_GROUP) ? 0 : tokeninfolength - sizeof(TOKEN_PRIMARY_GROUP);
392 req->handle = wine_server_obj_handle( token );
393 req->which_sid = tokeninfoclass;
394 wine_server_set_reply( req, sid, sid_len );
395 status = wine_server_call( req );
396 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_PRIMARY_GROUP);
397 if (status == STATUS_SUCCESS)
398 tgroup->PrimaryGroup = sid;
400 SERVER_END_REQ;
401 break;
402 case TokenPrivileges:
403 SERVER_START_REQ( get_token_privileges )
405 TOKEN_PRIVILEGES *tpriv = tokeninfo;
406 req->handle = wine_server_obj_handle( token );
407 if (tpriv && tokeninfolength > FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ))
408 wine_server_set_reply( req, tpriv->Privileges, tokeninfolength - FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) );
409 status = wine_server_call( req );
410 if (retlen) *retlen = FIELD_OFFSET( TOKEN_PRIVILEGES, Privileges ) + reply->len;
411 if (tpriv) tpriv->PrivilegeCount = reply->len / sizeof(LUID_AND_ATTRIBUTES);
413 SERVER_END_REQ;
414 break;
415 case TokenOwner:
416 SERVER_START_REQ( get_token_sid )
418 TOKEN_OWNER *towner = tokeninfo;
419 PSID sid = towner + 1;
420 DWORD sid_len = tokeninfolength < sizeof(TOKEN_OWNER) ? 0 : tokeninfolength - sizeof(TOKEN_OWNER);
422 req->handle = wine_server_obj_handle( token );
423 req->which_sid = tokeninfoclass;
424 wine_server_set_reply( req, sid, sid_len );
425 status = wine_server_call( req );
426 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_OWNER);
427 if (status == STATUS_SUCCESS)
428 towner->Owner = sid;
430 SERVER_END_REQ;
431 break;
432 case TokenImpersonationLevel:
433 SERVER_START_REQ( get_token_impersonation_level )
435 SECURITY_IMPERSONATION_LEVEL *impersonation_level = tokeninfo;
436 req->handle = wine_server_obj_handle( token );
437 status = wine_server_call( req );
438 if (status == STATUS_SUCCESS)
439 *impersonation_level = reply->impersonation_level;
441 SERVER_END_REQ;
442 break;
443 case TokenStatistics:
444 SERVER_START_REQ( get_token_statistics )
446 TOKEN_STATISTICS *statistics = tokeninfo;
447 req->handle = wine_server_obj_handle( token );
448 status = wine_server_call( req );
449 if (status == STATUS_SUCCESS)
451 statistics->TokenId.LowPart = reply->token_id.low_part;
452 statistics->TokenId.HighPart = reply->token_id.high_part;
453 statistics->AuthenticationId.LowPart = 0; /* FIXME */
454 statistics->AuthenticationId.HighPart = 0; /* FIXME */
455 statistics->ExpirationTime.u.HighPart = 0x7fffffff;
456 statistics->ExpirationTime.u.LowPart = 0xffffffff;
457 statistics->TokenType = reply->primary ? TokenPrimary : TokenImpersonation;
458 statistics->ImpersonationLevel = reply->impersonation_level;
460 /* kernel information not relevant to us */
461 statistics->DynamicCharged = 0;
462 statistics->DynamicAvailable = 0;
464 statistics->GroupCount = reply->group_count;
465 statistics->PrivilegeCount = reply->privilege_count;
466 statistics->ModifiedId.LowPart = reply->modified_id.low_part;
467 statistics->ModifiedId.HighPart = reply->modified_id.high_part;
470 SERVER_END_REQ;
471 break;
472 case TokenType:
473 SERVER_START_REQ( get_token_statistics )
475 TOKEN_TYPE *token_type = tokeninfo;
476 req->handle = wine_server_obj_handle( token );
477 status = wine_server_call( req );
478 if (status == STATUS_SUCCESS)
479 *token_type = reply->primary ? TokenPrimary : TokenImpersonation;
481 SERVER_END_REQ;
482 break;
483 case TokenDefaultDacl:
484 SERVER_START_REQ( get_token_default_dacl )
486 TOKEN_DEFAULT_DACL *default_dacl = tokeninfo;
487 ACL *acl = (ACL *)(default_dacl + 1);
488 DWORD acl_len;
490 if (tokeninfolength < sizeof(TOKEN_DEFAULT_DACL)) acl_len = 0;
491 else acl_len = tokeninfolength - sizeof(TOKEN_DEFAULT_DACL);
493 req->handle = wine_server_obj_handle( token );
494 wine_server_set_reply( req, acl, acl_len );
495 status = wine_server_call( req );
497 if (retlen) *retlen = reply->acl_len + sizeof(TOKEN_DEFAULT_DACL);
498 if (status == STATUS_SUCCESS)
500 if (reply->acl_len)
501 default_dacl->DefaultDacl = acl;
502 else
503 default_dacl->DefaultDacl = NULL;
506 SERVER_END_REQ;
507 break;
508 case TokenElevationType:
510 TOKEN_ELEVATION_TYPE *elevation_type = tokeninfo;
511 FIXME("QueryInformationToken( ..., TokenElevationType, ...) semi-stub\n");
512 *elevation_type = TokenElevationTypeFull;
514 break;
515 case TokenElevation:
517 TOKEN_ELEVATION *elevation = tokeninfo;
518 FIXME("QueryInformationToken( ..., TokenElevation, ...) semi-stub\n");
519 elevation->TokenIsElevated = TRUE;
521 break;
522 case TokenSessionId:
524 *((DWORD*)tokeninfo) = 0;
525 FIXME("QueryInformationToken( ..., TokenSessionId, ...) semi-stub\n");
527 break;
528 case TokenIntegrityLevel:
530 /* report always "S-1-16-12288" (high mandatory level) for now */
531 static const SID high_level = {SID_REVISION, 1, {SECURITY_MANDATORY_LABEL_AUTHORITY},
532 {SECURITY_MANDATORY_HIGH_RID}};
534 TOKEN_MANDATORY_LABEL *tml = tokeninfo;
535 PSID psid = tml + 1;
537 tml->Label.Sid = psid;
538 tml->Label.Attributes = SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED;
539 memcpy(psid, &high_level, sizeof(SID));
541 break;
542 case TokenAppContainerSid:
544 TOKEN_APPCONTAINER_INFORMATION *container = tokeninfo;
545 FIXME("QueryInformationToken( ..., TokenAppContainerSid, ...) semi-stub\n");
546 container->TokenAppContainer = NULL;
548 break;
549 case TokenIsAppContainer:
551 TRACE("TokenIsAppContainer semi-stub\n");
552 *(DWORD*)tokeninfo = 0;
553 break;
555 case TokenLogonSid:
556 SERVER_START_REQ( get_token_sid )
558 TOKEN_GROUPS * groups = tokeninfo;
559 PSID sid = groups + 1;
560 DWORD sid_len = tokeninfolength < sizeof(TOKEN_GROUPS) ? 0 : tokeninfolength - sizeof(TOKEN_GROUPS);
562 req->handle = wine_server_obj_handle( token );
563 req->which_sid = tokeninfoclass;
564 wine_server_set_reply( req, sid, sid_len );
565 status = wine_server_call( req );
566 if (retlen) *retlen = reply->sid_len + sizeof(TOKEN_GROUPS);
567 if (status == STATUS_SUCCESS)
569 groups->GroupCount = 1;
570 groups->Groups[0].Sid = sid;
571 groups->Groups[0].Attributes = 0;
574 SERVER_END_REQ;
575 break;
576 default:
578 ERR("Unhandled Token Information class %d!\n", tokeninfoclass);
579 return STATUS_NOT_IMPLEMENTED;
582 return status;
585 /******************************************************************************
586 * NtSetInformationToken [NTDLL.@]
587 * ZwSetInformationToken [NTDLL.@]
589 NTSTATUS WINAPI NtSetInformationToken(
590 HANDLE TokenHandle,
591 TOKEN_INFORMATION_CLASS TokenInformationClass,
592 PVOID TokenInformation,
593 ULONG TokenInformationLength)
595 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
597 TRACE("%p %d %p %u\n", TokenHandle, TokenInformationClass,
598 TokenInformation, TokenInformationLength);
600 switch (TokenInformationClass)
602 case TokenDefaultDacl:
603 if (TokenInformationLength < sizeof(TOKEN_DEFAULT_DACL))
605 ret = STATUS_INFO_LENGTH_MISMATCH;
606 break;
608 if (!TokenInformation)
610 ret = STATUS_ACCESS_VIOLATION;
611 break;
613 SERVER_START_REQ( set_token_default_dacl )
615 ACL *acl = ((TOKEN_DEFAULT_DACL *)TokenInformation)->DefaultDacl;
616 WORD size;
618 if (acl) size = acl->AclSize;
619 else size = 0;
621 req->handle = wine_server_obj_handle( TokenHandle );
622 wine_server_add_data( req, acl, size );
623 ret = wine_server_call( req );
625 SERVER_END_REQ;
626 break;
627 default:
628 FIXME("unimplemented class %u\n", TokenInformationClass);
629 break;
632 return ret;
635 /******************************************************************************
636 * NtAdjustGroupsToken [NTDLL.@]
637 * ZwAdjustGroupsToken [NTDLL.@]
639 NTSTATUS WINAPI NtAdjustGroupsToken(
640 HANDLE TokenHandle,
641 BOOLEAN ResetToDefault,
642 PTOKEN_GROUPS NewState,
643 ULONG BufferLength,
644 PTOKEN_GROUPS PreviousState,
645 PULONG ReturnLength)
647 FIXME("%p %d %p %u %p %p\n", TokenHandle, ResetToDefault,
648 NewState, BufferLength, PreviousState, ReturnLength);
649 return STATUS_NOT_IMPLEMENTED;
652 /******************************************************************************
653 * NtPrivilegeCheck [NTDLL.@]
654 * ZwPrivilegeCheck [NTDLL.@]
656 NTSTATUS WINAPI NtPrivilegeCheck(
657 HANDLE ClientToken,
658 PPRIVILEGE_SET RequiredPrivileges,
659 PBOOLEAN Result)
661 NTSTATUS status;
662 SERVER_START_REQ( check_token_privileges )
664 req->handle = wine_server_obj_handle( ClientToken );
665 req->all_required = (RequiredPrivileges->Control & PRIVILEGE_SET_ALL_NECESSARY) != 0;
666 wine_server_add_data( req, RequiredPrivileges->Privilege,
667 RequiredPrivileges->PrivilegeCount * sizeof(RequiredPrivileges->Privilege[0]) );
668 wine_server_set_reply( req, RequiredPrivileges->Privilege,
669 RequiredPrivileges->PrivilegeCount * sizeof(RequiredPrivileges->Privilege[0]) );
671 status = wine_server_call( req );
673 if (status == STATUS_SUCCESS)
674 *Result = reply->has_privileges != 0;
676 SERVER_END_REQ;
677 return status;
681 * ports
684 /******************************************************************************
685 * NtCreatePort [NTDLL.@]
686 * ZwCreatePort [NTDLL.@]
688 NTSTATUS WINAPI NtCreatePort(PHANDLE PortHandle,POBJECT_ATTRIBUTES ObjectAttributes,
689 ULONG MaxConnectInfoLength,ULONG MaxDataLength,PULONG reserved)
691 FIXME("(%p,%p,%u,%u,%p),stub!\n",PortHandle,ObjectAttributes,
692 MaxConnectInfoLength,MaxDataLength,reserved);
693 return STATUS_NOT_IMPLEMENTED;
696 /******************************************************************************
697 * NtConnectPort [NTDLL.@]
698 * ZwConnectPort [NTDLL.@]
700 NTSTATUS WINAPI NtConnectPort(
701 PHANDLE PortHandle,
702 PUNICODE_STRING PortName,
703 PSECURITY_QUALITY_OF_SERVICE SecurityQos,
704 PLPC_SECTION_WRITE WriteSection,
705 PLPC_SECTION_READ ReadSection,
706 PULONG MaximumMessageLength,
707 PVOID ConnectInfo,
708 PULONG pConnectInfoLength)
710 FIXME("(%p,%s,%p,%p,%p,%p,%p,%p),stub!\n",
711 PortHandle,debugstr_w(PortName->Buffer),SecurityQos,
712 WriteSection,ReadSection,MaximumMessageLength,ConnectInfo,
713 pConnectInfoLength);
714 if (ConnectInfo && pConnectInfoLength)
715 TRACE("\tMessage = %s\n",debugstr_an(ConnectInfo,*pConnectInfoLength));
716 return STATUS_NOT_IMPLEMENTED;
719 /******************************************************************************
720 * NtSecureConnectPort (NTDLL.@)
721 * ZwSecureConnectPort (NTDLL.@)
723 NTSTATUS WINAPI NtSecureConnectPort(
724 PHANDLE PortHandle,
725 PUNICODE_STRING PortName,
726 PSECURITY_QUALITY_OF_SERVICE SecurityQos,
727 PLPC_SECTION_WRITE WriteSection,
728 PSID pSid,
729 PLPC_SECTION_READ ReadSection,
730 PULONG MaximumMessageLength,
731 PVOID ConnectInfo,
732 PULONG pConnectInfoLength)
734 FIXME("(%p,%s,%p,%p,%p,%p,%p,%p,%p),stub!\n",
735 PortHandle,debugstr_w(PortName->Buffer),SecurityQos,
736 WriteSection,pSid,ReadSection,MaximumMessageLength,ConnectInfo,
737 pConnectInfoLength);
738 return STATUS_NOT_IMPLEMENTED;
741 /******************************************************************************
742 * NtListenPort [NTDLL.@]
743 * ZwListenPort [NTDLL.@]
745 NTSTATUS WINAPI NtListenPort(HANDLE PortHandle,PLPC_MESSAGE pLpcMessage)
747 FIXME("(%p,%p),stub!\n",PortHandle,pLpcMessage);
748 return STATUS_NOT_IMPLEMENTED;
751 /******************************************************************************
752 * NtAcceptConnectPort [NTDLL.@]
753 * ZwAcceptConnectPort [NTDLL.@]
755 NTSTATUS WINAPI NtAcceptConnectPort(
756 PHANDLE PortHandle,
757 ULONG PortIdentifier,
758 PLPC_MESSAGE pLpcMessage,
759 BOOLEAN Accept,
760 PLPC_SECTION_WRITE WriteSection,
761 PLPC_SECTION_READ ReadSection)
763 FIXME("(%p,%u,%p,%d,%p,%p),stub!\n",
764 PortHandle,PortIdentifier,pLpcMessage,Accept,WriteSection,ReadSection);
765 return STATUS_NOT_IMPLEMENTED;
768 /******************************************************************************
769 * NtCompleteConnectPort [NTDLL.@]
770 * ZwCompleteConnectPort [NTDLL.@]
772 NTSTATUS WINAPI NtCompleteConnectPort(HANDLE PortHandle)
774 FIXME("(%p),stub!\n",PortHandle);
775 return STATUS_NOT_IMPLEMENTED;
778 /******************************************************************************
779 * NtRegisterThreadTerminatePort [NTDLL.@]
780 * ZwRegisterThreadTerminatePort [NTDLL.@]
782 NTSTATUS WINAPI NtRegisterThreadTerminatePort(HANDLE PortHandle)
784 FIXME("(%p),stub!\n",PortHandle);
785 return STATUS_NOT_IMPLEMENTED;
788 /******************************************************************************
789 * NtRequestWaitReplyPort [NTDLL.@]
790 * ZwRequestWaitReplyPort [NTDLL.@]
792 NTSTATUS WINAPI NtRequestWaitReplyPort(
793 HANDLE PortHandle,
794 PLPC_MESSAGE pLpcMessageIn,
795 PLPC_MESSAGE pLpcMessageOut)
797 FIXME("(%p,%p,%p),stub!\n",PortHandle,pLpcMessageIn,pLpcMessageOut);
798 if(pLpcMessageIn)
800 TRACE("Message to send:\n");
801 TRACE("\tDataSize = %u\n",pLpcMessageIn->DataSize);
802 TRACE("\tMessageSize = %u\n",pLpcMessageIn->MessageSize);
803 TRACE("\tMessageType = %u\n",pLpcMessageIn->MessageType);
804 TRACE("\tVirtualRangesOffset = %u\n",pLpcMessageIn->VirtualRangesOffset);
805 TRACE("\tClientId.UniqueProcess = %p\n",pLpcMessageIn->ClientId.UniqueProcess);
806 TRACE("\tClientId.UniqueThread = %p\n",pLpcMessageIn->ClientId.UniqueThread);
807 TRACE("\tMessageId = %lu\n",pLpcMessageIn->MessageId);
808 TRACE("\tSectionSize = %lu\n",pLpcMessageIn->SectionSize);
809 TRACE("\tData = %s\n",
810 debugstr_an((const char*)pLpcMessageIn->Data,pLpcMessageIn->DataSize));
812 return STATUS_NOT_IMPLEMENTED;
815 /******************************************************************************
816 * NtReplyWaitReceivePort [NTDLL.@]
817 * ZwReplyWaitReceivePort [NTDLL.@]
819 NTSTATUS WINAPI NtReplyWaitReceivePort(
820 HANDLE PortHandle,
821 PULONG PortIdentifier,
822 PLPC_MESSAGE ReplyMessage,
823 PLPC_MESSAGE Message)
825 FIXME("(%p,%p,%p,%p),stub!\n",PortHandle,PortIdentifier,ReplyMessage,Message);
826 return STATUS_NOT_IMPLEMENTED;
830 * Misc
833 /******************************************************************************
834 * NtSetIntervalProfile [NTDLL.@]
835 * ZwSetIntervalProfile [NTDLL.@]
837 NTSTATUS WINAPI NtSetIntervalProfile(
838 ULONG Interval,
839 KPROFILE_SOURCE Source)
841 FIXME("%u,%d\n", Interval, Source);
842 return STATUS_SUCCESS;
845 static SYSTEM_CPU_INFORMATION cached_sci;
847 /*******************************************************************************
848 * Architecture specific feature detection for CPUs
850 * This a set of mutually exclusive #if define()s each providing its own get_cpuinfo() to be called
851 * from fill_cpu_info();
853 #if defined(__i386__) || defined(__x86_64__)
855 #define AUTH 0x68747541 /* "Auth" */
856 #define ENTI 0x69746e65 /* "enti" */
857 #define CAMD 0x444d4163 /* "cAMD" */
859 #define GENU 0x756e6547 /* "Genu" */
860 #define INEI 0x49656e69 /* "ineI" */
861 #define NTEL 0x6c65746e /* "ntel" */
863 /* Calls cpuid with an eax of 'ax' and returns the 16 bytes in *p
864 * We are compiled with -fPIC, so we can't clobber ebx.
866 static inline void do_cpuid(unsigned int ax, unsigned int *p)
868 #ifdef __i386__
869 __asm__("pushl %%ebx\n\t"
870 "cpuid\n\t"
871 "movl %%ebx, %%esi\n\t"
872 "popl %%ebx"
873 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
874 : "0" (ax));
875 #elif defined(__x86_64__)
876 __asm__("push %%rbx\n\t"
877 "cpuid\n\t"
878 "movq %%rbx, %%rsi\n\t"
879 "pop %%rbx"
880 : "=a" (p[0]), "=S" (p[1]), "=c" (p[2]), "=d" (p[3])
881 : "0" (ax));
882 #endif
885 /* From xf86info havecpuid.c 1.11 */
886 static inline BOOL have_cpuid(void)
888 #ifdef __i386__
889 unsigned int f1, f2;
890 __asm__("pushfl\n\t"
891 "pushfl\n\t"
892 "popl %0\n\t"
893 "movl %0,%1\n\t"
894 "xorl %2,%0\n\t"
895 "pushl %0\n\t"
896 "popfl\n\t"
897 "pushfl\n\t"
898 "popl %0\n\t"
899 "popfl"
900 : "=&r" (f1), "=&r" (f2)
901 : "ir" (0x00200000));
902 return ((f1^f2) & 0x00200000) != 0;
903 #elif defined(__x86_64__)
904 return TRUE;
905 #else
906 return FALSE;
907 #endif
910 /* Detect if a SSE2 processor is capable of Denormals Are Zero (DAZ) mode.
912 * This function assumes you have already checked for SSE2/FXSAVE support. */
913 static inline BOOL have_sse_daz_mode(void)
915 #ifdef __i386__
916 typedef struct DECLSPEC_ALIGN(16) _M128A {
917 ULONGLONG Low;
918 LONGLONG High;
919 } M128A;
921 typedef struct _XMM_SAVE_AREA32 {
922 WORD ControlWord;
923 WORD StatusWord;
924 BYTE TagWord;
925 BYTE Reserved1;
926 WORD ErrorOpcode;
927 DWORD ErrorOffset;
928 WORD ErrorSelector;
929 WORD Reserved2;
930 DWORD DataOffset;
931 WORD DataSelector;
932 WORD Reserved3;
933 DWORD MxCsr;
934 DWORD MxCsr_Mask;
935 M128A FloatRegisters[8];
936 M128A XmmRegisters[16];
937 BYTE Reserved4[96];
938 } XMM_SAVE_AREA32;
940 /* Intel says we need a zeroed 16-byte aligned buffer */
941 char buffer[512 + 16];
942 XMM_SAVE_AREA32 *state = (XMM_SAVE_AREA32 *)(((ULONG_PTR)buffer + 15) & ~15);
943 memset(buffer, 0, sizeof(buffer));
945 __asm__ __volatile__( "fxsave %0" : "=m" (*state) : "m" (*state) );
947 return (state->MxCsr_Mask & (1 << 6)) >> 6;
948 #else /* all x86_64 processors include SSE2 with DAZ mode */
949 return TRUE;
950 #endif
953 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
955 unsigned int regs[4], regs2[4];
957 #if defined(__i386__)
958 info->Architecture = PROCESSOR_ARCHITECTURE_INTEL;
959 #elif defined(__x86_64__)
960 info->Architecture = PROCESSOR_ARCHITECTURE_AMD64;
961 #endif
963 /* We're at least a 386 */
964 info->FeatureSet = CPU_FEATURE_VME | CPU_FEATURE_X86 | CPU_FEATURE_PGE;
965 info->Level = 3;
967 if (!have_cpuid()) return;
969 do_cpuid(0x00000000, regs); /* get standard cpuid level and vendor name */
970 if (regs[0]>=0x00000001) /* Check for supported cpuid version */
972 do_cpuid(0x00000001, regs2); /* get cpu features */
974 if(regs2[3] & (1 << 3 )) info->FeatureSet |= CPU_FEATURE_PSE;
975 if(regs2[3] & (1 << 4 )) info->FeatureSet |= CPU_FEATURE_TSC;
976 if(regs2[3] & (1 << 8 )) info->FeatureSet |= CPU_FEATURE_CX8;
977 if(regs2[3] & (1 << 11)) info->FeatureSet |= CPU_FEATURE_SEP;
978 if(regs2[3] & (1 << 12)) info->FeatureSet |= CPU_FEATURE_MTRR;
979 if(regs2[3] & (1 << 15)) info->FeatureSet |= CPU_FEATURE_CMOV;
980 if(regs2[3] & (1 << 16)) info->FeatureSet |= CPU_FEATURE_PAT;
981 if(regs2[3] & (1 << 23)) info->FeatureSet |= CPU_FEATURE_MMX;
982 if(regs2[3] & (1 << 24)) info->FeatureSet |= CPU_FEATURE_FXSR;
983 if(regs2[3] & (1 << 25)) info->FeatureSet |= CPU_FEATURE_SSE;
984 if(regs2[3] & (1 << 26)) info->FeatureSet |= CPU_FEATURE_SSE2;
986 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = !(regs2[3] & 1);
987 user_shared_data->ProcessorFeatures[PF_RDTSC_INSTRUCTION_AVAILABLE] = (regs2[3] >> 4) & 1;
988 user_shared_data->ProcessorFeatures[PF_PAE_ENABLED] = (regs2[3] >> 6) & 1;
989 user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE_DOUBLE] = (regs2[3] >> 8) & 1;
990 user_shared_data->ProcessorFeatures[PF_MMX_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 23) & 1;
991 user_shared_data->ProcessorFeatures[PF_XMMI_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 25) & 1;
992 user_shared_data->ProcessorFeatures[PF_XMMI64_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 26) & 1;
993 user_shared_data->ProcessorFeatures[PF_SSE3_INSTRUCTIONS_AVAILABLE] = regs2[2] & 1;
994 user_shared_data->ProcessorFeatures[PF_XSAVE_ENABLED] = (regs2[2] >> 27) & 1;
995 user_shared_data->ProcessorFeatures[PF_COMPARE_EXCHANGE128] = (regs2[2] >> 13) & 1;
997 if((regs2[3] & (1 << 26)) && (regs2[3] & (1 << 24))) /* has SSE2 and FXSAVE/FXRSTOR */
998 user_shared_data->ProcessorFeatures[PF_SSE_DAZ_MODE_AVAILABLE] = have_sse_daz_mode();
1000 if (regs[1] == AUTH && regs[3] == ENTI && regs[2] == CAMD)
1002 info->Level = (regs2[0] >> 8) & 0xf; /* family */
1003 if (info->Level == 0xf) /* AMD says to add the extended family to the family if family is 0xf */
1004 info->Level += (regs2[0] >> 20) & 0xff;
1006 /* repack model and stepping to make a "revision" */
1007 info->Revision = ((regs2[0] >> 16) & 0xf) << 12; /* extended model */
1008 info->Revision |= ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
1009 info->Revision |= regs2[0] & 0xf; /* stepping */
1011 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
1012 if (regs[0] >= 0x80000001)
1014 do_cpuid(0x80000001, regs2); /* get vendor features */
1015 user_shared_data->ProcessorFeatures[PF_VIRT_FIRMWARE_ENABLED] = (regs2[2] >> 2) & 1;
1016 user_shared_data->ProcessorFeatures[PF_NX_ENABLED] = (regs2[3] >> 20) & 1;
1017 user_shared_data->ProcessorFeatures[PF_3DNOW_INSTRUCTIONS_AVAILABLE] = (regs2[3] >> 31) & 1;
1018 if (regs2[3] >> 31) info->FeatureSet |= CPU_FEATURE_3DNOW;
1021 else if (regs[1] == GENU && regs[3] == INEI && regs[2] == NTEL)
1023 info->Level = ((regs2[0] >> 8) & 0xf) + ((regs2[0] >> 20) & 0xff); /* family + extended family */
1024 if(info->Level == 15) info->Level = 6;
1026 /* repack model and stepping to make a "revision" */
1027 info->Revision = ((regs2[0] >> 16) & 0xf) << 12; /* extended model */
1028 info->Revision |= ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
1029 info->Revision |= regs2[0] & 0xf; /* stepping */
1031 if(regs2[3] & (1 << 21)) info->FeatureSet |= CPU_FEATURE_DS;
1032 user_shared_data->ProcessorFeatures[PF_VIRT_FIRMWARE_ENABLED] = (regs2[2] >> 5) & 1;
1034 do_cpuid(0x80000000, regs); /* get vendor cpuid level */
1035 if (regs[0] >= 0x80000001)
1037 do_cpuid(0x80000001, regs2); /* get vendor features */
1038 user_shared_data->ProcessorFeatures[PF_NX_ENABLED] = (regs2[3] >> 20) & 1;
1041 else
1043 info->Level = (regs2[0] >> 8) & 0xf; /* family */
1045 /* repack model and stepping to make a "revision" */
1046 info->Revision = ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
1047 info->Revision |= regs2[0] & 0xf; /* stepping */
1052 #elif defined(__powerpc__) || defined(__ppc__)
1054 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
1056 #ifdef __APPLE__
1057 size_t valSize;
1058 int value;
1060 valSize = sizeof(value);
1061 if (sysctlbyname("hw.optional.floatingpoint", &value, &valSize, NULL, 0) == 0)
1062 user_shared_data->ProcessorFeatures[PF_FLOATING_POINT_EMULATED] = !value;
1064 valSize = sizeof(value);
1065 if (sysctlbyname("hw.cpusubtype", &value, &valSize, NULL, 0) == 0)
1067 switch (value)
1069 case CPU_SUBTYPE_POWERPC_601:
1070 case CPU_SUBTYPE_POWERPC_602: info->Level = 1; break;
1071 case CPU_SUBTYPE_POWERPC_603: info->Level = 3; break;
1072 case CPU_SUBTYPE_POWERPC_603e:
1073 case CPU_SUBTYPE_POWERPC_603ev: info->Level = 6; break;
1074 case CPU_SUBTYPE_POWERPC_604: info->Level = 4; break;
1075 case CPU_SUBTYPE_POWERPC_604e: info->Level = 9; break;
1076 case CPU_SUBTYPE_POWERPC_620: info->Level = 20; break;
1077 case CPU_SUBTYPE_POWERPC_750: /* G3/G4 derive from 603 so ... */
1078 case CPU_SUBTYPE_POWERPC_7400:
1079 case CPU_SUBTYPE_POWERPC_7450: info->Level = 6; break;
1080 case CPU_SUBTYPE_POWERPC_970: info->Level = 9;
1081 /* :o) user_shared_data->ProcessorFeatures[PF_ALTIVEC_INSTRUCTIONS_AVAILABLE] ;-) */
1082 break;
1083 default: break;
1086 #else
1087 FIXME("CPU Feature detection not implemented.\n");
1088 #endif
1089 info->Architecture = PROCESSOR_ARCHITECTURE_PPC;
1092 #elif defined(__arm__)
1094 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
1096 #ifdef linux
1097 char line[512];
1098 char *s, *value;
1099 FILE *f = fopen("/proc/cpuinfo", "r");
1100 if (f)
1102 while (fgets(line, sizeof(line), f) != NULL)
1104 /* NOTE: the ':' is the only character we can rely on */
1105 if (!(value = strchr(line,':')))
1106 continue;
1107 /* terminate the valuename */
1108 s = value - 1;
1109 while ((s >= line) && isspace(*s)) s--;
1110 *(s + 1) = '\0';
1111 /* and strip leading spaces from value */
1112 value += 1;
1113 while (isspace(*value)) value++;
1114 if ((s = strchr(value,'\n')))
1115 *s='\0';
1116 if (!strcasecmp(line, "CPU architecture"))
1118 if (isdigit(value[0]))
1119 info->Level = atoi(value);
1120 continue;
1122 if (!strcasecmp(line, "CPU revision"))
1124 if (isdigit(value[0]))
1125 info->Revision = atoi(value);
1126 continue;
1128 if (!strcasecmp(line, "features"))
1130 if (strstr(value, "vfpv3"))
1131 user_shared_data->ProcessorFeatures[PF_ARM_VFP_32_REGISTERS_AVAILABLE] = TRUE;
1132 if (strstr(value, "neon"))
1133 user_shared_data->ProcessorFeatures[PF_ARM_NEON_INSTRUCTIONS_AVAILABLE] = TRUE;
1134 continue;
1137 fclose(f);
1139 #elif defined(__FreeBSD__)
1140 size_t valsize;
1141 char buf[8];
1142 int value;
1144 valsize = sizeof(buf);
1145 if (!sysctlbyname("hw.machine_arch", &buf, &valsize, NULL, 0) &&
1146 sscanf(buf, "armv%i", &value) == 1)
1147 info->Level = value;
1149 valsize = sizeof(value);
1150 if (!sysctlbyname("hw.floatingpoint", &value, &valsize, NULL, 0))
1151 user_shared_data->ProcessorFeatures[PF_ARM_VFP_32_REGISTERS_AVAILABLE] = value;
1152 #else
1153 FIXME("CPU Feature detection not implemented.\n");
1154 #endif
1155 if (info->Level >= 8)
1156 user_shared_data->ProcessorFeatures[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = TRUE;
1157 info->Architecture = PROCESSOR_ARCHITECTURE_ARM;
1160 #elif defined(__aarch64__)
1162 static inline void get_cpuinfo(SYSTEM_CPU_INFORMATION* info)
1164 #ifdef linux
1165 char line[512];
1166 char *s, *value;
1167 FILE *f = fopen("/proc/cpuinfo", "r");
1168 if (f)
1170 while (fgets(line, sizeof(line), f) != NULL)
1172 /* NOTE: the ':' is the only character we can rely on */
1173 if (!(value = strchr(line,':')))
1174 continue;
1175 /* terminate the valuename */
1176 s = value - 1;
1177 while ((s >= line) && isspace(*s)) s--;
1178 *(s + 1) = '\0';
1179 /* and strip leading spaces from value */
1180 value += 1;
1181 while (isspace(*value)) value++;
1182 if ((s = strchr(value,'\n')))
1183 *s='\0';
1184 if (!strcasecmp(line, "CPU architecture"))
1186 if (isdigit(value[0]))
1187 info->Level = atoi(value);
1188 continue;
1190 if (!strcasecmp(line, "CPU revision"))
1192 if (isdigit(value[0]))
1193 info->Revision = atoi(value);
1194 continue;
1196 if (!strcasecmp(line, "Features"))
1198 if (strstr(value, "crc32"))
1199 user_shared_data->ProcessorFeatures[PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE] = TRUE;
1200 if (strstr(value, "aes"))
1201 user_shared_data->ProcessorFeatures[PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE] = TRUE;
1202 continue;
1205 fclose(f);
1207 #else
1208 FIXME("CPU Feature detection not implemented.\n");
1209 #endif
1210 info->Level = max(info->Level, 8);
1211 user_shared_data->ProcessorFeatures[PF_ARM_V8_INSTRUCTIONS_AVAILABLE] = TRUE;
1212 info->Architecture = PROCESSOR_ARCHITECTURE_ARM64;
1215 #endif /* End architecture specific feature detection for CPUs */
1217 /******************************************************************
1218 * fill_cpu_info
1220 * inits a couple of places with CPU related information:
1221 * - cached_sci in this file
1222 * - Peb->NumberOfProcessors
1223 * - SharedUserData->ProcessFeatures[] array
1225 void fill_cpu_info(void)
1227 long num;
1229 #ifdef _SC_NPROCESSORS_ONLN
1230 num = sysconf(_SC_NPROCESSORS_ONLN);
1231 if (num < 1)
1233 num = 1;
1234 WARN("Failed to detect the number of processors.\n");
1236 #elif defined(CTL_HW) && defined(HW_NCPU)
1237 int mib[2];
1238 size_t len = sizeof(num);
1239 mib[0] = CTL_HW;
1240 mib[1] = HW_NCPU;
1241 if (sysctl(mib, 2, &num, &len, NULL, 0) != 0)
1243 num = 1;
1244 WARN("Failed to detect the number of processors.\n");
1246 #else
1247 num = 1;
1248 FIXME("Detecting the number of processors is not supported.\n");
1249 #endif
1250 NtCurrentTeb()->Peb->NumberOfProcessors = num;
1252 memset(&cached_sci, 0, sizeof(cached_sci));
1253 get_cpuinfo(&cached_sci);
1255 TRACE("<- CPU arch %d, level %d, rev %d, features 0x%x\n",
1256 cached_sci.Architecture, cached_sci.Level, cached_sci.Revision, cached_sci.FeatureSet);
1259 static BOOL grow_logical_proc_buf(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1260 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *max_len)
1262 if (pdata)
1264 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *new_data;
1266 *max_len *= 2;
1267 new_data = RtlReAllocateHeap(GetProcessHeap(), 0, *pdata, *max_len*sizeof(*new_data));
1268 if (!new_data)
1269 return FALSE;
1271 *pdata = new_data;
1273 else
1275 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *new_dataex;
1277 *max_len *= 2;
1278 new_dataex = RtlReAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, *pdataex, *max_len*sizeof(*new_dataex));
1279 if (!new_dataex)
1280 return FALSE;
1282 *pdataex = new_dataex;
1285 return TRUE;
1288 static DWORD log_proc_ex_size_plus(DWORD size)
1290 /* add SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX.Relationship and .Size */
1291 return sizeof(LOGICAL_PROCESSOR_RELATIONSHIP) + sizeof(DWORD) + size;
1294 static inline BOOL logical_proc_info_add_by_id(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1295 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len, DWORD *pmax_len,
1296 LOGICAL_PROCESSOR_RELATIONSHIP rel, DWORD id, ULONG_PTR mask)
1298 if (pdata) {
1299 DWORD i;
1301 if(rel == RelationProcessorPackage){
1302 for(i=0; i<*len; i++)
1304 if ((*pdata)[i].Relationship!=rel || (*pdata)[i].u.Reserved[1]!=id)
1305 continue;
1307 (*pdata)[i].ProcessorMask |= mask;
1308 return TRUE;
1310 }else
1311 i = *len;
1313 while(*len == *pmax_len)
1315 if (!grow_logical_proc_buf(pdata, NULL, pmax_len))
1316 return FALSE;
1319 (*pdata)[i].Relationship = rel;
1320 (*pdata)[i].ProcessorMask = mask;
1321 /* TODO: set processor core flags */
1322 (*pdata)[i].u.Reserved[0] = 0;
1323 (*pdata)[i].u.Reserved[1] = id;
1324 *len = i+1;
1325 }else{
1326 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
1327 DWORD ofs = 0;
1329 while(ofs < *len)
1331 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1332 if (rel == RelationProcessorPackage && dataex->Relationship == rel && dataex->u.Processor.Reserved[1] == id)
1334 dataex->u.Processor.GroupMask[0].Mask |= mask;
1335 return TRUE;
1337 ofs += dataex->Size;
1340 /* TODO: For now, just one group. If more than 64 processors, then we
1341 * need another group. */
1343 while (ofs + log_proc_ex_size_plus(sizeof(PROCESSOR_RELATIONSHIP)) > *pmax_len)
1345 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1346 return FALSE;
1349 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1351 dataex->Relationship = rel;
1352 dataex->Size = log_proc_ex_size_plus(sizeof(PROCESSOR_RELATIONSHIP));
1353 dataex->u.Processor.Flags = 0; /* TODO */
1354 dataex->u.Processor.EfficiencyClass = 0;
1355 dataex->u.Processor.GroupCount = 1;
1356 dataex->u.Processor.GroupMask[0].Mask = mask;
1357 dataex->u.Processor.GroupMask[0].Group = 0;
1358 /* mark for future lookup */
1359 dataex->u.Processor.Reserved[0] = 0;
1360 dataex->u.Processor.Reserved[1] = id;
1362 *len += dataex->Size;
1365 return TRUE;
1368 static inline BOOL logical_proc_info_add_cache(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1369 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len,
1370 DWORD *pmax_len, ULONG_PTR mask, CACHE_DESCRIPTOR *cache)
1372 if (pdata)
1374 DWORD i;
1376 for (i=0; i<*len; i++)
1378 if ((*pdata)[i].Relationship==RelationCache && (*pdata)[i].ProcessorMask==mask
1379 && (*pdata)[i].u.Cache.Level==cache->Level && (*pdata)[i].u.Cache.Type==cache->Type)
1380 return TRUE;
1383 while (*len == *pmax_len)
1384 if (!grow_logical_proc_buf(pdata, NULL, pmax_len))
1385 return FALSE;
1387 (*pdata)[i].Relationship = RelationCache;
1388 (*pdata)[i].ProcessorMask = mask;
1389 (*pdata)[i].u.Cache = *cache;
1390 *len = i+1;
1392 else
1394 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
1395 DWORD ofs;
1397 for (ofs = 0; ofs < *len; )
1399 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1400 if (dataex->Relationship == RelationCache && dataex->u.Cache.GroupMask.Mask == mask &&
1401 dataex->u.Cache.Level == cache->Level && dataex->u.Cache.Type == cache->Type)
1402 return TRUE;
1403 ofs += dataex->Size;
1406 while (ofs + log_proc_ex_size_plus(sizeof(CACHE_RELATIONSHIP)) > *pmax_len)
1408 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1409 return FALSE;
1412 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
1414 dataex->Relationship = RelationCache;
1415 dataex->Size = log_proc_ex_size_plus(sizeof(CACHE_RELATIONSHIP));
1416 dataex->u.Cache.Level = cache->Level;
1417 dataex->u.Cache.Associativity = cache->Associativity;
1418 dataex->u.Cache.LineSize = cache->LineSize;
1419 dataex->u.Cache.CacheSize = cache->Size;
1420 dataex->u.Cache.Type = cache->Type;
1421 dataex->u.Cache.GroupMask.Mask = mask;
1422 dataex->u.Cache.GroupMask.Group = 0;
1424 *len += dataex->Size;
1427 return TRUE;
1430 static inline BOOL logical_proc_info_add_numa_node(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
1431 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len, DWORD *pmax_len, ULONG_PTR mask,
1432 DWORD node_id)
1434 if (pdata)
1436 while (*len == *pmax_len)
1437 if (!grow_logical_proc_buf(pdata, NULL, pmax_len))
1438 return FALSE;
1440 (*pdata)[*len].Relationship = RelationNumaNode;
1441 (*pdata)[*len].ProcessorMask = mask;
1442 (*pdata)[*len].u.NumaNode.NodeNumber = node_id;
1443 (*len)++;
1445 else
1447 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
1449 while (*len + log_proc_ex_size_plus(sizeof(NUMA_NODE_RELATIONSHIP)) > *pmax_len)
1451 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1452 return FALSE;
1455 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + *len);
1457 dataex->Relationship = RelationNumaNode;
1458 dataex->Size = log_proc_ex_size_plus(sizeof(NUMA_NODE_RELATIONSHIP));
1459 dataex->u.NumaNode.NodeNumber = node_id;
1460 dataex->u.NumaNode.GroupMask.Mask = mask;
1461 dataex->u.NumaNode.GroupMask.Group = 0;
1463 *len += dataex->Size;
1466 return TRUE;
1469 static inline BOOL logical_proc_info_add_group(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex,
1470 DWORD *len, DWORD *pmax_len, DWORD num_cpus, ULONG_PTR mask)
1472 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
1474 while (*len + log_proc_ex_size_plus(sizeof(GROUP_RELATIONSHIP)) > *pmax_len)
1476 if (!grow_logical_proc_buf(NULL, pdataex, pmax_len))
1477 return FALSE;
1480 dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + *len);
1482 dataex->Relationship = RelationGroup;
1483 dataex->Size = log_proc_ex_size_plus(sizeof(GROUP_RELATIONSHIP));
1484 dataex->u.Group.MaximumGroupCount = 1;
1485 dataex->u.Group.ActiveGroupCount = 1;
1486 dataex->u.Group.GroupInfo[0].MaximumProcessorCount = num_cpus;
1487 dataex->u.Group.GroupInfo[0].ActiveProcessorCount = num_cpus;
1488 dataex->u.Group.GroupInfo[0].ActiveProcessorMask = mask;
1490 *len += dataex->Size;
1492 return TRUE;
1495 #ifdef linux
1496 /* for 'data', max_len is the array count. for 'dataex', max_len is in bytes */
1497 static NTSTATUS create_logical_proc_info(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
1498 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex, DWORD *max_len)
1500 static const char core_info[] = "/sys/devices/system/cpu/cpu%u/%s";
1501 static const char cache_info[] = "/sys/devices/system/cpu/cpu%u/cache/index%u/%s";
1502 static const char numa_info[] = "/sys/devices/system/node/node%u/cpumap";
1504 FILE *fcpu_list, *fnuma_list, *f;
1505 DWORD len = 0, beg, end, i, j, r, num_cpus = 0;
1506 char op, name[MAX_PATH];
1507 ULONG_PTR all_cpus_mask = 0;
1509 fcpu_list = fopen("/sys/devices/system/cpu/online", "r");
1510 if(!fcpu_list)
1511 return STATUS_NOT_IMPLEMENTED;
1513 while(!feof(fcpu_list))
1515 if(!fscanf(fcpu_list, "%u%c ", &beg, &op))
1516 break;
1517 if(op == '-') fscanf(fcpu_list, "%u%c ", &end, &op);
1518 else end = beg;
1520 for(i=beg; i<=end; i++)
1522 if(i > 8*sizeof(ULONG_PTR))
1524 FIXME("skipping logical processor %d\n", i);
1525 continue;
1528 sprintf(name, core_info, i, "physical_package_id");
1529 f = fopen(name, "r");
1530 if(f)
1532 fscanf(f, "%u", &r);
1533 fclose(f);
1535 else r = 0;
1536 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorPackage, r, (ULONG_PTR)1 << i))
1538 fclose(fcpu_list);
1539 return STATUS_NO_MEMORY;
1542 sprintf(name, core_info, i, "core_id");
1543 f = fopen(name, "r");
1544 if(f)
1546 fscanf(f, "%u", &r);
1547 fclose(f);
1549 else r = i;
1550 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorCore, r, (ULONG_PTR)1 << i))
1552 fclose(fcpu_list);
1553 return STATUS_NO_MEMORY;
1556 for(j=0; j<4; j++)
1558 CACHE_DESCRIPTOR cache;
1559 ULONG_PTR mask = 0;
1561 sprintf(name, cache_info, i, j, "shared_cpu_map");
1562 f = fopen(name, "r");
1563 if(!f) continue;
1564 while(!feof(f))
1566 if(!fscanf(f, "%x%c ", &r, &op))
1567 break;
1568 mask = (sizeof(ULONG_PTR)>sizeof(int) ? mask<<(8*sizeof(DWORD)) : 0) + r;
1570 fclose(f);
1572 sprintf(name, cache_info, i, j, "level");
1573 f = fopen(name, "r");
1574 if(!f) continue;
1575 fscanf(f, "%u", &r);
1576 fclose(f);
1577 cache.Level = r;
1579 sprintf(name, cache_info, i, j, "ways_of_associativity");
1580 f = fopen(name, "r");
1581 if(!f) continue;
1582 fscanf(f, "%u", &r);
1583 fclose(f);
1584 cache.Associativity = r;
1586 sprintf(name, cache_info, i, j, "coherency_line_size");
1587 f = fopen(name, "r");
1588 if(!f) continue;
1589 fscanf(f, "%u", &r);
1590 fclose(f);
1591 cache.LineSize = r;
1593 sprintf(name, cache_info, i, j, "size");
1594 f = fopen(name, "r");
1595 if(!f) continue;
1596 fscanf(f, "%u%c", &r, &op);
1597 fclose(f);
1598 if(op != 'K')
1599 WARN("unknown cache size %u%c\n", r, op);
1600 cache.Size = (op=='K' ? r*1024 : r);
1602 sprintf(name, cache_info, i, j, "type");
1603 f = fopen(name, "r");
1604 if(!f) continue;
1605 fscanf(f, "%s", name);
1606 fclose(f);
1607 if(!memcmp(name, "Data", 5))
1608 cache.Type = CacheData;
1609 else if(!memcmp(name, "Instruction", 11))
1610 cache.Type = CacheInstruction;
1611 else
1612 cache.Type = CacheUnified;
1614 if(!logical_proc_info_add_cache(data, dataex, &len, max_len, mask, &cache))
1616 fclose(fcpu_list);
1617 return STATUS_NO_MEMORY;
1622 fclose(fcpu_list);
1624 if(data){
1625 for(i=0; i<len; i++){
1626 if((*data)[i].Relationship == RelationProcessorCore){
1627 all_cpus_mask |= (*data)[i].ProcessorMask;
1628 ++num_cpus;
1631 }else{
1632 for(i = 0; i < len; ){
1633 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *infoex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*dataex) + i);
1634 if(infoex->Relationship == RelationProcessorCore){
1635 all_cpus_mask |= infoex->u.Processor.GroupMask[0].Mask;
1636 ++num_cpus;
1638 i += infoex->Size;
1642 fnuma_list = fopen("/sys/devices/system/node/online", "r");
1643 if(!fnuma_list)
1645 if(!logical_proc_info_add_numa_node(data, dataex, &len, max_len, all_cpus_mask, 0))
1646 return STATUS_NO_MEMORY;
1648 else
1650 while(!feof(fnuma_list))
1652 if(!fscanf(fnuma_list, "%u%c ", &beg, &op))
1653 break;
1654 if(op == '-') fscanf(fnuma_list, "%u%c ", &end, &op);
1655 else end = beg;
1657 for(i=beg; i<=end; i++)
1659 ULONG_PTR mask = 0;
1661 sprintf(name, numa_info, i);
1662 f = fopen(name, "r");
1663 if(!f) continue;
1664 while(!feof(f))
1666 if(!fscanf(f, "%x%c ", &r, &op))
1667 break;
1668 mask = (sizeof(ULONG_PTR)>sizeof(int) ? mask<<(8*sizeof(DWORD)) : 0) + r;
1670 fclose(f);
1672 if(!logical_proc_info_add_numa_node(data, dataex, &len, max_len, mask, i))
1674 fclose(fnuma_list);
1675 return STATUS_NO_MEMORY;
1679 fclose(fnuma_list);
1682 if(dataex)
1683 logical_proc_info_add_group(dataex, &len, max_len, num_cpus, all_cpus_mask);
1685 if(data)
1686 *max_len = len * sizeof(**data);
1687 else
1688 *max_len = len;
1690 return STATUS_SUCCESS;
1692 #elif defined(__APPLE__)
1693 /* for 'data', max_len is the array count. for 'dataex', max_len is in bytes */
1694 static NTSTATUS create_logical_proc_info(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
1695 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex, DWORD *max_len)
1697 DWORD pkgs_no, cores_no, lcpu_no, lcpu_per_core, cores_per_package, assoc, len = 0;
1698 DWORD cache_ctrs[10] = {0};
1699 ULONG_PTR all_cpus_mask = 0;
1700 CACHE_DESCRIPTOR cache[10];
1701 LONGLONG cache_size, cache_line_size, cache_sharing[10];
1702 size_t size;
1703 DWORD p,i,j,k;
1705 lcpu_no = NtCurrentTeb()->Peb->NumberOfProcessors;
1707 size = sizeof(pkgs_no);
1708 if(sysctlbyname("hw.packages", &pkgs_no, &size, NULL, 0))
1709 pkgs_no = 1;
1711 size = sizeof(cores_no);
1712 if(sysctlbyname("hw.physicalcpu", &cores_no, &size, NULL, 0))
1713 cores_no = lcpu_no;
1715 TRACE("%u logical CPUs from %u physical cores across %u packages\n",
1716 lcpu_no, cores_no, pkgs_no);
1718 lcpu_per_core = lcpu_no / cores_no;
1719 cores_per_package = cores_no / pkgs_no;
1721 memset(cache, 0, sizeof(cache));
1722 cache[1].Level = 1;
1723 cache[1].Type = CacheInstruction;
1724 cache[1].Associativity = 8; /* reasonable default */
1725 cache[1].LineSize = 0x40; /* reasonable default */
1726 cache[2].Level = 1;
1727 cache[2].Type = CacheData;
1728 cache[2].Associativity = 8;
1729 cache[2].LineSize = 0x40;
1730 cache[3].Level = 2;
1731 cache[3].Type = CacheUnified;
1732 cache[3].Associativity = 8;
1733 cache[3].LineSize = 0x40;
1734 cache[4].Level = 3;
1735 cache[4].Type = CacheUnified;
1736 cache[4].Associativity = 12;
1737 cache[4].LineSize = 0x40;
1739 size = sizeof(cache_line_size);
1740 if(!sysctlbyname("hw.cachelinesize", &cache_line_size, &size, NULL, 0))
1742 for(i=1; i<5; i++)
1743 cache[i].LineSize = cache_line_size;
1746 /* TODO: set actual associativity for all caches */
1747 size = sizeof(assoc);
1748 if(!sysctlbyname("machdep.cpu.cache.L2_associativity", &assoc, &size, NULL, 0))
1749 cache[3].Associativity = assoc;
1751 size = sizeof(cache_size);
1752 if(!sysctlbyname("hw.l1icachesize", &cache_size, &size, NULL, 0))
1753 cache[1].Size = cache_size;
1754 size = sizeof(cache_size);
1755 if(!sysctlbyname("hw.l1dcachesize", &cache_size, &size, NULL, 0))
1756 cache[2].Size = cache_size;
1757 size = sizeof(cache_size);
1758 if(!sysctlbyname("hw.l2cachesize", &cache_size, &size, NULL, 0))
1759 cache[3].Size = cache_size;
1760 size = sizeof(cache_size);
1761 if(!sysctlbyname("hw.l3cachesize", &cache_size, &size, NULL, 0))
1762 cache[4].Size = cache_size;
1764 size = sizeof(cache_sharing);
1765 if(sysctlbyname("hw.cacheconfig", cache_sharing, &size, NULL, 0) < 0){
1766 cache_sharing[1] = lcpu_per_core;
1767 cache_sharing[2] = lcpu_per_core;
1768 cache_sharing[3] = lcpu_per_core;
1769 cache_sharing[4] = lcpu_no;
1770 }else{
1771 /* in cache[], indexes 1 and 2 are l1 caches */
1772 cache_sharing[4] = cache_sharing[3];
1773 cache_sharing[3] = cache_sharing[2];
1774 cache_sharing[2] = cache_sharing[1];
1777 for(p = 0; p < pkgs_no; ++p){
1778 for(j = 0; j < cores_per_package && p * cores_per_package + j < cores_no; ++j){
1779 ULONG_PTR mask = 0;
1781 for(k = 0; k < lcpu_per_core; ++k)
1782 mask |= (ULONG_PTR)1 << (j * lcpu_per_core + k);
1784 all_cpus_mask |= mask;
1786 /* add to package */
1787 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorPackage, p, mask))
1788 return STATUS_NO_MEMORY;
1790 /* add new core */
1791 if(!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorCore, p, mask))
1792 return STATUS_NO_MEMORY;
1794 for(i = 1; i < 5; ++i){
1795 if(cache_ctrs[i] == 0 && cache[i].Size > 0){
1796 mask = 0;
1797 for(k = 0; k < cache_sharing[i]; ++k)
1798 mask |= (ULONG_PTR)1 << (j * lcpu_per_core + k);
1800 if(!logical_proc_info_add_cache(data, dataex, &len, max_len, mask, &cache[i]))
1801 return STATUS_NO_MEMORY;
1804 cache_ctrs[i] += lcpu_per_core;
1806 if(cache_ctrs[i] == cache_sharing[i])
1807 cache_ctrs[i] = 0;
1812 /* OSX doesn't support NUMA, so just make one NUMA node for all CPUs */
1813 if(!logical_proc_info_add_numa_node(data, dataex, &len, max_len, all_cpus_mask, 0))
1814 return STATUS_NO_MEMORY;
1816 if(dataex)
1817 logical_proc_info_add_group(dataex, &len, max_len, lcpu_no, all_cpus_mask);
1819 if(data)
1820 *max_len = len * sizeof(**data);
1821 else
1822 *max_len = len;
1824 return STATUS_SUCCESS;
1826 #else
1827 static NTSTATUS create_logical_proc_info(SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
1828 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex, DWORD *max_len)
1830 FIXME("stub\n");
1831 return STATUS_NOT_IMPLEMENTED;
1833 #endif
1835 /******************************************************************************
1836 * NtQuerySystemInformation [NTDLL.@]
1837 * ZwQuerySystemInformation [NTDLL.@]
1839 * ARGUMENTS:
1840 * SystemInformationClass Index to a certain information structure
1841 * SystemTimeAdjustmentInformation SYSTEM_TIME_ADJUSTMENT
1842 * SystemCacheInformation SYSTEM_CACHE_INFORMATION
1843 * SystemConfigurationInformation CONFIGURATION_INFORMATION
1844 * observed (class/len):
1845 * 0x0/0x2c
1846 * 0x12/0x18
1847 * 0x2/0x138
1848 * 0x8/0x600
1849 * 0x25/0xc
1850 * SystemInformation caller supplies storage for the information structure
1851 * Length size of the structure
1852 * ResultLength Data written
1854 NTSTATUS WINAPI NtQuerySystemInformation(
1855 IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
1856 OUT PVOID SystemInformation,
1857 IN ULONG Length,
1858 OUT PULONG ResultLength)
1860 NTSTATUS ret = STATUS_SUCCESS;
1861 ULONG len = 0;
1863 TRACE("(0x%08x,%p,0x%08x,%p)\n",
1864 SystemInformationClass,SystemInformation,Length,ResultLength);
1866 switch (SystemInformationClass)
1868 case SystemBasicInformation:
1870 SYSTEM_BASIC_INFORMATION sbi;
1872 virtual_get_system_info( &sbi );
1873 len = sizeof(sbi);
1875 if ( Length == len)
1877 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1878 else memcpy( SystemInformation, &sbi, len);
1880 else ret = STATUS_INFO_LENGTH_MISMATCH;
1882 break;
1883 case SystemCpuInformation:
1884 if (Length >= (len = sizeof(cached_sci)))
1886 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1887 else memcpy(SystemInformation, &cached_sci, len);
1889 else ret = STATUS_INFO_LENGTH_MISMATCH;
1890 break;
1891 case SystemPerformanceInformation:
1893 SYSTEM_PERFORMANCE_INFORMATION spi;
1894 static BOOL fixme_written = FALSE;
1895 FILE *fp;
1897 memset(&spi, 0 , sizeof(spi));
1898 len = sizeof(spi);
1900 spi.Reserved3 = 0x7fffffff; /* Available paged pool memory? */
1902 if ((fp = fopen("/proc/uptime", "r")))
1904 double uptime, idle_time;
1906 fscanf(fp, "%lf %lf", &uptime, &idle_time);
1907 fclose(fp);
1908 spi.IdleTime.QuadPart = 10000000 * idle_time;
1910 else
1912 static ULONGLONG idle;
1913 /* many programs expect IdleTime to change so fake change */
1914 spi.IdleTime.QuadPart = ++idle;
1917 if (Length >= len)
1919 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1920 else memcpy( SystemInformation, &spi, len);
1922 else ret = STATUS_INFO_LENGTH_MISMATCH;
1923 if(!fixme_written) {
1924 FIXME("info_class SYSTEM_PERFORMANCE_INFORMATION\n");
1925 fixme_written = TRUE;
1928 break;
1929 case SystemTimeOfDayInformation:
1931 SYSTEM_TIMEOFDAY_INFORMATION sti;
1933 memset(&sti, 0 , sizeof(sti));
1935 /* liKeSystemTime, liExpTimeZoneBias, uCurrentTimeZoneId */
1936 sti.liKeBootTime.QuadPart = server_start_time;
1938 if (Length <= sizeof(sti))
1940 len = Length;
1941 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
1942 else memcpy( SystemInformation, &sti, Length);
1944 else ret = STATUS_INFO_LENGTH_MISMATCH;
1946 break;
1947 case SystemProcessInformation:
1949 SYSTEM_PROCESS_INFORMATION* spi = SystemInformation;
1950 SYSTEM_PROCESS_INFORMATION* last = NULL;
1951 HANDLE hSnap = 0;
1952 WCHAR procname[1024];
1953 WCHAR* exename;
1954 DWORD wlen = 0;
1955 DWORD procstructlen = 0;
1957 SERVER_START_REQ( create_snapshot )
1959 req->flags = SNAP_PROCESS | SNAP_THREAD;
1960 req->attributes = 0;
1961 if (!(ret = wine_server_call( req )))
1962 hSnap = wine_server_ptr_handle( reply->handle );
1964 SERVER_END_REQ;
1965 len = 0;
1966 while (ret == STATUS_SUCCESS)
1968 SERVER_START_REQ( next_process )
1970 req->handle = wine_server_obj_handle( hSnap );
1971 req->reset = (len == 0);
1972 wine_server_set_reply( req, procname, sizeof(procname)-sizeof(WCHAR) );
1973 if (!(ret = wine_server_call( req )))
1975 /* Make sure procname is 0 terminated */
1976 procname[wine_server_reply_size(reply) / sizeof(WCHAR)] = 0;
1978 /* Get only the executable name, not the path */
1979 if ((exename = strrchrW(procname, '\\')) != NULL) exename++;
1980 else exename = procname;
1982 wlen = (strlenW(exename) + 1) * sizeof(WCHAR);
1984 procstructlen = sizeof(*spi) + wlen + ((reply->threads - 1) * sizeof(SYSTEM_THREAD_INFORMATION));
1986 if (Length >= len + procstructlen)
1988 /* ftCreationTime, ftUserTime, ftKernelTime;
1989 * vmCounters, ioCounters
1992 memset(spi, 0, sizeof(*spi));
1994 spi->NextEntryOffset = procstructlen - wlen;
1995 spi->dwThreadCount = reply->threads;
1997 /* spi->pszProcessName will be set later on */
1999 spi->dwBasePriority = reply->priority;
2000 spi->UniqueProcessId = UlongToHandle(reply->pid);
2001 spi->ParentProcessId = UlongToHandle(reply->ppid);
2002 spi->HandleCount = reply->handles;
2004 /* spi->ti will be set later on */
2007 len += procstructlen;
2010 SERVER_END_REQ;
2012 if (ret != STATUS_SUCCESS)
2014 if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
2015 break;
2018 if (Length >= len)
2020 int i, j;
2022 /* set thread info */
2023 i = j = 0;
2024 while (ret == STATUS_SUCCESS)
2026 SERVER_START_REQ( next_thread )
2028 req->handle = wine_server_obj_handle( hSnap );
2029 req->reset = (j == 0);
2030 if (!(ret = wine_server_call( req )))
2032 j++;
2033 if (UlongToHandle(reply->pid) == spi->UniqueProcessId)
2035 /* ftKernelTime, ftUserTime, ftCreateTime;
2036 * dwTickCount, dwStartAddress
2039 memset(&spi->ti[i], 0, sizeof(spi->ti));
2041 spi->ti[i].CreateTime.QuadPart = 0xdeadbeef;
2042 spi->ti[i].ClientId.UniqueProcess = UlongToHandle(reply->pid);
2043 spi->ti[i].ClientId.UniqueThread = UlongToHandle(reply->tid);
2044 spi->ti[i].dwCurrentPriority = reply->base_pri + reply->delta_pri;
2045 spi->ti[i].dwBasePriority = reply->base_pri;
2046 i++;
2050 SERVER_END_REQ;
2052 if (ret == STATUS_NO_MORE_FILES) ret = STATUS_SUCCESS;
2054 /* now append process name */
2055 spi->ProcessName.Buffer = (WCHAR*)((char*)spi + spi->NextEntryOffset);
2056 spi->ProcessName.Length = wlen - sizeof(WCHAR);
2057 spi->ProcessName.MaximumLength = wlen;
2058 memcpy( spi->ProcessName.Buffer, exename, wlen );
2059 spi->NextEntryOffset += wlen;
2061 last = spi;
2062 spi = (SYSTEM_PROCESS_INFORMATION*)((char*)spi + spi->NextEntryOffset);
2065 if (ret == STATUS_SUCCESS && last) last->NextEntryOffset = 0;
2066 if (len > Length) ret = STATUS_INFO_LENGTH_MISMATCH;
2067 if (hSnap) NtClose(hSnap);
2069 break;
2070 case SystemProcessorPerformanceInformation:
2072 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi = NULL;
2073 unsigned int cpus = 0;
2074 int out_cpus = Length / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
2076 if (out_cpus == 0)
2078 len = 0;
2079 ret = STATUS_INFO_LENGTH_MISMATCH;
2080 break;
2082 else
2083 #ifdef __APPLE__
2085 processor_cpu_load_info_data_t *pinfo;
2086 mach_msg_type_number_t info_count;
2088 if (host_processor_info (mach_host_self (),
2089 PROCESSOR_CPU_LOAD_INFO,
2090 &cpus,
2091 (processor_info_array_t*)&pinfo,
2092 &info_count) == 0)
2094 int i;
2095 cpus = min(cpus,out_cpus);
2096 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
2097 sppi = RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
2098 for (i = 0; i < cpus; i++)
2100 sppi[i].IdleTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_IDLE];
2101 sppi[i].KernelTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_SYSTEM];
2102 sppi[i].UserTime.QuadPart = pinfo[i].cpu_ticks[CPU_STATE_USER];
2104 vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count * sizeof(natural_t));
2107 #else
2109 FILE *cpuinfo = fopen("/proc/stat", "r");
2110 if (cpuinfo)
2112 unsigned long clk_tck = sysconf(_SC_CLK_TCK);
2113 unsigned long usr,nice,sys,idle,remainder[8];
2114 int i, count;
2115 char name[32];
2116 char line[255];
2118 /* first line is combined usage */
2119 while (fgets(line,255,cpuinfo))
2121 count = sscanf(line, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
2122 name, &usr, &nice, &sys, &idle,
2123 &remainder[0], &remainder[1], &remainder[2], &remainder[3],
2124 &remainder[4], &remainder[5], &remainder[6], &remainder[7]);
2126 if (count < 5 || strncmp( name, "cpu", 3 )) break;
2127 for (i = 0; i + 5 < count; ++i) sys += remainder[i];
2128 sys += idle;
2129 usr += nice;
2130 cpus = atoi( name + 3 ) + 1;
2131 if (cpus > out_cpus) break;
2132 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
2133 if (sppi)
2134 sppi = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sppi, len );
2135 else
2136 sppi = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, len );
2138 sppi[cpus-1].IdleTime.QuadPart = (ULONGLONG)idle * 10000000 / clk_tck;
2139 sppi[cpus-1].KernelTime.QuadPart = (ULONGLONG)sys * 10000000 / clk_tck;
2140 sppi[cpus-1].UserTime.QuadPart = (ULONGLONG)usr * 10000000 / clk_tck;
2142 fclose(cpuinfo);
2145 #endif
2147 if (cpus == 0)
2149 static int i = 1;
2150 unsigned int n;
2151 cpus = min(NtCurrentTeb()->Peb->NumberOfProcessors, out_cpus);
2152 len = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * cpus;
2153 sppi = RtlAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
2154 FIXME("stub info_class SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION\n");
2155 /* many programs expect these values to change so fake change */
2156 for (n = 0; n < cpus; n++)
2158 sppi[n].KernelTime.QuadPart = 1 * i;
2159 sppi[n].UserTime.QuadPart = 2 * i;
2160 sppi[n].IdleTime.QuadPart = 3 * i;
2162 i++;
2165 if (Length >= len)
2167 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2168 else memcpy( SystemInformation, sppi, len);
2170 else ret = STATUS_INFO_LENGTH_MISMATCH;
2172 RtlFreeHeap(GetProcessHeap(),0,sppi);
2174 break;
2175 case SystemModuleInformation:
2176 /* FIXME: should be system-wide */
2177 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2178 else ret = LdrQueryProcessModuleInformation( SystemInformation, Length, &len );
2179 break;
2180 case SystemHandleInformation:
2182 struct handle_info *info;
2183 DWORD i, num_handles;
2185 if (Length < sizeof(SYSTEM_HANDLE_INFORMATION))
2187 ret = STATUS_INFO_LENGTH_MISMATCH;
2188 break;
2191 if (!SystemInformation)
2193 ret = STATUS_ACCESS_VIOLATION;
2194 break;
2197 num_handles = (Length - FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handle )) / sizeof(SYSTEM_HANDLE_ENTRY);
2198 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) * num_handles )))
2199 return STATUS_NO_MEMORY;
2201 SERVER_START_REQ( get_system_handles )
2203 wine_server_set_reply( req, info, sizeof(*info) * num_handles );
2204 if (!(ret = wine_server_call( req )))
2206 SYSTEM_HANDLE_INFORMATION *shi = SystemInformation;
2207 shi->Count = wine_server_reply_size( req ) / sizeof(*info);
2208 len = FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handle[shi->Count] );
2209 for (i = 0; i < shi->Count; i++)
2211 memset( &shi->Handle[i], 0, sizeof(shi->Handle[i]) );
2212 shi->Handle[i].OwnerPid = info[i].owner;
2213 shi->Handle[i].HandleValue = info[i].handle;
2214 shi->Handle[i].AccessMask = info[i].access;
2215 /* FIXME: Fill out ObjectType, HandleFlags, ObjectPointer */
2218 else if (ret == STATUS_BUFFER_TOO_SMALL)
2220 len = FIELD_OFFSET( SYSTEM_HANDLE_INFORMATION, Handle[reply->count] );
2221 ret = STATUS_INFO_LENGTH_MISMATCH;
2224 SERVER_END_REQ;
2226 RtlFreeHeap( GetProcessHeap(), 0, info );
2228 break;
2229 case SystemCacheInformation:
2231 SYSTEM_CACHE_INFORMATION sci;
2233 memset(&sci, 0, sizeof(sci)); /* FIXME */
2234 len = sizeof(sci);
2236 if ( Length >= len)
2238 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2239 else memcpy( SystemInformation, &sci, len);
2241 else ret = STATUS_INFO_LENGTH_MISMATCH;
2242 FIXME("info_class SYSTEM_CACHE_INFORMATION\n");
2244 break;
2245 case SystemInterruptInformation:
2247 SYSTEM_INTERRUPT_INFORMATION sii;
2249 memset(&sii, 0, sizeof(sii));
2250 len = sizeof(sii);
2252 if ( Length >= len)
2254 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2255 else memcpy( SystemInformation, &sii, len);
2257 else ret = STATUS_INFO_LENGTH_MISMATCH;
2258 FIXME("info_class SYSTEM_INTERRUPT_INFORMATION\n");
2260 break;
2261 case SystemKernelDebuggerInformation:
2263 SYSTEM_KERNEL_DEBUGGER_INFORMATION skdi;
2265 skdi.DebuggerEnabled = FALSE;
2266 skdi.DebuggerNotPresent = TRUE;
2267 len = sizeof(skdi);
2269 if ( Length >= len)
2271 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2272 else memcpy( SystemInformation, &skdi, len);
2274 else ret = STATUS_INFO_LENGTH_MISMATCH;
2276 break;
2277 case SystemRegistryQuotaInformation:
2279 /* Something to do with the size of the registry *
2280 * Since we don't have a size limitation, fake it *
2281 * This is almost certainly wrong. *
2282 * This sets each of the three words in the struct to 32 MB, *
2283 * which is enough to make the IE 5 installer happy. */
2284 SYSTEM_REGISTRY_QUOTA_INFORMATION srqi;
2286 srqi.RegistryQuotaAllowed = 0x2000000;
2287 srqi.RegistryQuotaUsed = 0x200000;
2288 srqi.Reserved1 = (void*)0x200000;
2289 len = sizeof(srqi);
2291 if ( Length >= len)
2293 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2294 else
2296 FIXME("SystemRegistryQuotaInformation: faking max registry size of 32 MB\n");
2297 memcpy( SystemInformation, &srqi, len);
2300 else ret = STATUS_INFO_LENGTH_MISMATCH;
2302 break;
2303 case SystemLogicalProcessorInformation:
2305 SYSTEM_LOGICAL_PROCESSOR_INFORMATION *buf;
2307 /* Each logical processor may use up to 7 entries in returned table:
2308 * core, numa node, package, L1i, L1d, L2, L3 */
2309 len = 7 * NtCurrentTeb()->Peb->NumberOfProcessors;
2310 buf = RtlAllocateHeap(GetProcessHeap(), 0, len * sizeof(*buf));
2311 if(!buf)
2313 ret = STATUS_NO_MEMORY;
2314 break;
2317 ret = create_logical_proc_info(&buf, NULL, &len);
2318 if( ret != STATUS_SUCCESS )
2320 RtlFreeHeap(GetProcessHeap(), 0, buf);
2321 break;
2324 if( Length >= len)
2326 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2327 else memcpy( SystemInformation, buf, len);
2329 else ret = STATUS_INFO_LENGTH_MISMATCH;
2330 RtlFreeHeap(GetProcessHeap(), 0, buf);
2332 break;
2333 case SystemRecommendedSharedDataAlignment:
2335 len = sizeof(DWORD);
2336 if (Length >= len)
2338 if (!SystemInformation) ret = STATUS_ACCESS_VIOLATION;
2339 else *((DWORD *)SystemInformation) = 64;
2341 else ret = STATUS_INFO_LENGTH_MISMATCH;
2343 break;
2344 default:
2345 FIXME("(0x%08x,%p,0x%08x,%p) stub\n",
2346 SystemInformationClass,SystemInformation,Length,ResultLength);
2348 /* Several Information Classes are not implemented on Windows and return 2 different values
2349 * STATUS_NOT_IMPLEMENTED or STATUS_INVALID_INFO_CLASS
2350 * in 95% of the cases it's STATUS_INVALID_INFO_CLASS, so use this as the default
2352 ret = STATUS_INVALID_INFO_CLASS;
2355 if (ResultLength) *ResultLength = len;
2357 return ret;
2360 /******************************************************************************
2361 * NtQuerySystemInformationEx [NTDLL.@]
2362 * ZwQuerySystemInformationEx [NTDLL.@]
2364 NTSTATUS WINAPI NtQuerySystemInformationEx(SYSTEM_INFORMATION_CLASS SystemInformationClass,
2365 void *Query, ULONG QueryLength, void *SystemInformation, ULONG Length, ULONG *ResultLength)
2367 ULONG len = 0;
2368 NTSTATUS ret = STATUS_NOT_IMPLEMENTED;
2370 TRACE("(0x%08x,%p,%u,%p,%u,%p) stub\n", SystemInformationClass, Query, QueryLength, SystemInformation,
2371 Length, ResultLength);
2373 switch (SystemInformationClass) {
2374 case SystemLogicalProcessorInformationEx:
2376 SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *buf;
2378 if (!Query || QueryLength < sizeof(DWORD))
2380 ret = STATUS_INVALID_PARAMETER;
2381 break;
2384 if (*(DWORD*)Query != RelationAll)
2385 FIXME("Relationship filtering not implemented: 0x%x\n", *(DWORD*)Query);
2387 len = 3 * sizeof(*buf);
2388 buf = RtlAllocateHeap(GetProcessHeap(), 0, len);
2389 if (!buf)
2391 ret = STATUS_NO_MEMORY;
2392 break;
2395 ret = create_logical_proc_info(NULL, &buf, &len);
2396 if (ret != STATUS_SUCCESS)
2398 RtlFreeHeap(GetProcessHeap(), 0, buf);
2399 break;
2402 if (Length >= len)
2404 if (!SystemInformation)
2405 ret = STATUS_ACCESS_VIOLATION;
2406 else
2407 memcpy( SystemInformation, buf, len);
2409 else
2410 ret = STATUS_INFO_LENGTH_MISMATCH;
2412 RtlFreeHeap(GetProcessHeap(), 0, buf);
2414 break;
2416 default:
2417 FIXME("(0x%08x,%p,%u,%p,%u,%p) stub\n", SystemInformationClass, Query, QueryLength, SystemInformation,
2418 Length, ResultLength);
2419 break;
2422 if (ResultLength)
2423 *ResultLength = len;
2425 return ret;
2428 /******************************************************************************
2429 * NtSetSystemInformation [NTDLL.@]
2430 * ZwSetSystemInformation [NTDLL.@]
2432 NTSTATUS WINAPI NtSetSystemInformation(SYSTEM_INFORMATION_CLASS SystemInformationClass, PVOID SystemInformation, ULONG Length)
2434 FIXME("(0x%08x,%p,0x%08x) stub\n",SystemInformationClass,SystemInformation,Length);
2435 return STATUS_SUCCESS;
2438 /******************************************************************************
2439 * NtCreatePagingFile [NTDLL.@]
2440 * ZwCreatePagingFile [NTDLL.@]
2442 NTSTATUS WINAPI NtCreatePagingFile(
2443 PUNICODE_STRING PageFileName,
2444 PLARGE_INTEGER MinimumSize,
2445 PLARGE_INTEGER MaximumSize,
2446 PLARGE_INTEGER ActualSize)
2448 FIXME("(%p %p %p %p) stub\n", PageFileName, MinimumSize, MaximumSize, ActualSize);
2449 return STATUS_SUCCESS;
2452 /******************************************************************************
2453 * NtDisplayString [NTDLL.@]
2455 * writes a string to the nt-textmode screen eg. during startup
2457 NTSTATUS WINAPI NtDisplayString ( PUNICODE_STRING string )
2459 STRING stringA;
2460 NTSTATUS ret;
2462 if (!(ret = RtlUnicodeStringToAnsiString( &stringA, string, TRUE )))
2464 MESSAGE( "%.*s", stringA.Length, stringA.Buffer );
2465 RtlFreeAnsiString( &stringA );
2467 return ret;
2470 /******************************************************************************
2471 * NtInitiatePowerAction [NTDLL.@]
2474 NTSTATUS WINAPI NtInitiatePowerAction(
2475 IN POWER_ACTION SystemAction,
2476 IN SYSTEM_POWER_STATE MinSystemState,
2477 IN ULONG Flags,
2478 IN BOOLEAN Asynchronous)
2480 FIXME("(%d,%d,0x%08x,%d),stub\n",
2481 SystemAction,MinSystemState,Flags,Asynchronous);
2482 return STATUS_NOT_IMPLEMENTED;
2485 #ifdef linux
2486 /* Fallback using /proc/cpuinfo for Linux systems without cpufreq. For
2487 * most distributions on recent enough hardware, this is only likely to
2488 * happen while running in virtualized environments such as QEMU. */
2489 static ULONG mhz_from_cpuinfo(void)
2491 char line[512];
2492 char *s, *value;
2493 double cmz = 0;
2494 FILE* f = fopen("/proc/cpuinfo", "r");
2495 if(f) {
2496 while (fgets(line, sizeof(line), f) != NULL) {
2497 if (!(value = strchr(line,':')))
2498 continue;
2499 s = value - 1;
2500 while ((s >= line) && isspace(*s)) s--;
2501 *(s + 1) = '\0';
2502 value++;
2503 if (!strcasecmp(line, "cpu MHz")) {
2504 sscanf(value, " %lf", &cmz);
2505 break;
2508 fclose(f);
2510 return cmz;
2512 #endif
2514 /******************************************************************************
2515 * NtPowerInformation [NTDLL.@]
2518 NTSTATUS WINAPI NtPowerInformation(
2519 IN POWER_INFORMATION_LEVEL InformationLevel,
2520 IN PVOID lpInputBuffer,
2521 IN ULONG nInputBufferSize,
2522 IN PVOID lpOutputBuffer,
2523 IN ULONG nOutputBufferSize)
2525 TRACE("(%d,%p,%d,%p,%d)\n",
2526 InformationLevel,lpInputBuffer,nInputBufferSize,lpOutputBuffer,nOutputBufferSize);
2527 switch(InformationLevel) {
2528 case SystemPowerCapabilities: {
2529 PSYSTEM_POWER_CAPABILITIES PowerCaps = lpOutputBuffer;
2530 FIXME("semi-stub: SystemPowerCapabilities\n");
2531 if (nOutputBufferSize < sizeof(SYSTEM_POWER_CAPABILITIES))
2532 return STATUS_BUFFER_TOO_SMALL;
2533 /* FIXME: These values are based off a native XP desktop, should probably use APM/ACPI to get the 'real' values */
2534 PowerCaps->PowerButtonPresent = TRUE;
2535 PowerCaps->SleepButtonPresent = FALSE;
2536 PowerCaps->LidPresent = FALSE;
2537 PowerCaps->SystemS1 = TRUE;
2538 PowerCaps->SystemS2 = FALSE;
2539 PowerCaps->SystemS3 = FALSE;
2540 PowerCaps->SystemS4 = TRUE;
2541 PowerCaps->SystemS5 = TRUE;
2542 PowerCaps->HiberFilePresent = TRUE;
2543 PowerCaps->FullWake = TRUE;
2544 PowerCaps->VideoDimPresent = FALSE;
2545 PowerCaps->ApmPresent = FALSE;
2546 PowerCaps->UpsPresent = FALSE;
2547 PowerCaps->ThermalControl = FALSE;
2548 PowerCaps->ProcessorThrottle = FALSE;
2549 PowerCaps->ProcessorMinThrottle = 100;
2550 PowerCaps->ProcessorMaxThrottle = 100;
2551 PowerCaps->DiskSpinDown = TRUE;
2552 PowerCaps->SystemBatteriesPresent = FALSE;
2553 PowerCaps->BatteriesAreShortTerm = FALSE;
2554 PowerCaps->BatteryScale[0].Granularity = 0;
2555 PowerCaps->BatteryScale[0].Capacity = 0;
2556 PowerCaps->BatteryScale[1].Granularity = 0;
2557 PowerCaps->BatteryScale[1].Capacity = 0;
2558 PowerCaps->BatteryScale[2].Granularity = 0;
2559 PowerCaps->BatteryScale[2].Capacity = 0;
2560 PowerCaps->AcOnLineWake = PowerSystemUnspecified;
2561 PowerCaps->SoftLidWake = PowerSystemUnspecified;
2562 PowerCaps->RtcWake = PowerSystemSleeping1;
2563 PowerCaps->MinDeviceWakeState = PowerSystemUnspecified;
2564 PowerCaps->DefaultLowLatencyWake = PowerSystemUnspecified;
2565 return STATUS_SUCCESS;
2567 case SystemExecutionState: {
2568 PULONG ExecutionState = lpOutputBuffer;
2569 WARN("semi-stub: SystemExecutionState\n"); /* Needed for .NET Framework, but using a FIXME is really noisy. */
2570 if (lpInputBuffer != NULL)
2571 return STATUS_INVALID_PARAMETER;
2572 /* FIXME: The actual state should be the value set by SetThreadExecutionState which is not currently implemented. */
2573 *ExecutionState = ES_USER_PRESENT;
2574 return STATUS_SUCCESS;
2576 case ProcessorInformation: {
2577 const int cannedMHz = 1000; /* We fake a 1GHz processor if we can't conjure up real values */
2578 PROCESSOR_POWER_INFORMATION* cpu_power = lpOutputBuffer;
2579 int i, out_cpus;
2581 if ((lpOutputBuffer == NULL) || (nOutputBufferSize == 0))
2582 return STATUS_INVALID_PARAMETER;
2583 out_cpus = NtCurrentTeb()->Peb->NumberOfProcessors;
2584 if ((nOutputBufferSize / sizeof(PROCESSOR_POWER_INFORMATION)) < out_cpus)
2585 return STATUS_BUFFER_TOO_SMALL;
2586 #if defined(linux)
2588 char filename[128];
2589 FILE* f;
2591 for(i = 0; i < out_cpus; i++) {
2592 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
2593 f = fopen(filename, "r");
2594 if (f && (fscanf(f, "%d", &cpu_power[i].CurrentMhz) == 1)) {
2595 cpu_power[i].CurrentMhz /= 1000;
2596 fclose(f);
2598 else {
2599 if(i == 0) {
2600 cpu_power[0].CurrentMhz = mhz_from_cpuinfo();
2601 if(cpu_power[0].CurrentMhz == 0)
2602 cpu_power[0].CurrentMhz = cannedMHz;
2604 else
2605 cpu_power[i].CurrentMhz = cpu_power[0].CurrentMhz;
2606 if(f) fclose(f);
2609 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", i);
2610 f = fopen(filename, "r");
2611 if (f && (fscanf(f, "%d", &cpu_power[i].MaxMhz) == 1)) {
2612 cpu_power[i].MaxMhz /= 1000;
2613 fclose(f);
2615 else {
2616 cpu_power[i].MaxMhz = cpu_power[i].CurrentMhz;
2617 if(f) fclose(f);
2620 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_max_freq", i);
2621 f = fopen(filename, "r");
2622 if(f && (fscanf(f, "%d", &cpu_power[i].MhzLimit) == 1)) {
2623 cpu_power[i].MhzLimit /= 1000;
2624 fclose(f);
2626 else
2628 cpu_power[i].MhzLimit = cpu_power[i].MaxMhz;
2629 if(f) fclose(f);
2632 cpu_power[i].Number = i;
2633 cpu_power[i].MaxIdleState = 0; /* FIXME */
2634 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2637 #elif defined(__FreeBSD__) || defined (__FreeBSD_kernel__) || defined(__DragonFly__)
2639 int num;
2640 size_t valSize = sizeof(num);
2641 if (sysctlbyname("hw.clockrate", &num, &valSize, NULL, 0))
2642 num = cannedMHz;
2643 for(i = 0; i < out_cpus; i++) {
2644 cpu_power[i].CurrentMhz = num;
2645 cpu_power[i].MaxMhz = num;
2646 cpu_power[i].MhzLimit = num;
2647 cpu_power[i].Number = i;
2648 cpu_power[i].MaxIdleState = 0; /* FIXME */
2649 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2652 #elif defined (__APPLE__)
2654 size_t valSize;
2655 unsigned long long currentMhz;
2656 unsigned long long maxMhz;
2658 valSize = sizeof(currentMhz);
2659 if (!sysctlbyname("hw.cpufrequency", &currentMhz, &valSize, NULL, 0))
2660 currentMhz /= 1000000;
2661 else
2662 currentMhz = cannedMHz;
2664 valSize = sizeof(maxMhz);
2665 if (!sysctlbyname("hw.cpufrequency_max", &maxMhz, &valSize, NULL, 0))
2666 maxMhz /= 1000000;
2667 else
2668 maxMhz = currentMhz;
2670 for(i = 0; i < out_cpus; i++) {
2671 cpu_power[i].CurrentMhz = currentMhz;
2672 cpu_power[i].MaxMhz = maxMhz;
2673 cpu_power[i].MhzLimit = maxMhz;
2674 cpu_power[i].Number = i;
2675 cpu_power[i].MaxIdleState = 0; /* FIXME */
2676 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2679 #else
2680 for(i = 0; i < out_cpus; i++) {
2681 cpu_power[i].CurrentMhz = cannedMHz;
2682 cpu_power[i].MaxMhz = cannedMHz;
2683 cpu_power[i].MhzLimit = cannedMHz;
2684 cpu_power[i].Number = i;
2685 cpu_power[i].MaxIdleState = 0; /* FIXME */
2686 cpu_power[i].CurrentIdleState = 0; /* FIXME */
2688 WARN("Unable to detect CPU MHz for this platform. Reporting %d MHz.\n", cannedMHz);
2689 #endif
2690 for(i = 0; i < out_cpus; i++) {
2691 TRACE("cpu_power[%d] = %u %u %u %u %u %u\n", i, cpu_power[i].Number,
2692 cpu_power[i].MaxMhz, cpu_power[i].CurrentMhz, cpu_power[i].MhzLimit,
2693 cpu_power[i].MaxIdleState, cpu_power[i].CurrentIdleState);
2695 return STATUS_SUCCESS;
2697 default:
2698 /* FIXME: Needed by .NET Framework */
2699 WARN("Unimplemented NtPowerInformation action: %d\n", InformationLevel);
2700 return STATUS_NOT_IMPLEMENTED;
2704 /******************************************************************************
2705 * NtShutdownSystem [NTDLL.@]
2708 NTSTATUS WINAPI NtShutdownSystem(SHUTDOWN_ACTION Action)
2710 FIXME("%d\n",Action);
2711 return STATUS_SUCCESS;
2714 /******************************************************************************
2715 * NtAllocateLocallyUniqueId (NTDLL.@)
2717 NTSTATUS WINAPI NtAllocateLocallyUniqueId(PLUID Luid)
2719 NTSTATUS status;
2721 TRACE("%p\n", Luid);
2723 if (!Luid)
2724 return STATUS_ACCESS_VIOLATION;
2726 SERVER_START_REQ( allocate_locally_unique_id )
2728 status = wine_server_call( req );
2729 if (!status)
2731 Luid->LowPart = reply->luid.low_part;
2732 Luid->HighPart = reply->luid.high_part;
2735 SERVER_END_REQ;
2737 return status;
2740 /******************************************************************************
2741 * VerSetConditionMask (NTDLL.@)
2743 ULONGLONG WINAPI VerSetConditionMask( ULONGLONG dwlConditionMask, DWORD dwTypeBitMask,
2744 BYTE dwConditionMask)
2746 if(dwTypeBitMask == 0)
2747 return dwlConditionMask;
2748 dwConditionMask &= 0x07;
2749 if(dwConditionMask == 0)
2750 return dwlConditionMask;
2752 if(dwTypeBitMask & VER_PRODUCT_TYPE)
2753 dwlConditionMask |= dwConditionMask << 7*3;
2754 else if (dwTypeBitMask & VER_SUITENAME)
2755 dwlConditionMask |= dwConditionMask << 6*3;
2756 else if (dwTypeBitMask & VER_SERVICEPACKMAJOR)
2757 dwlConditionMask |= dwConditionMask << 5*3;
2758 else if (dwTypeBitMask & VER_SERVICEPACKMINOR)
2759 dwlConditionMask |= dwConditionMask << 4*3;
2760 else if (dwTypeBitMask & VER_PLATFORMID)
2761 dwlConditionMask |= dwConditionMask << 3*3;
2762 else if (dwTypeBitMask & VER_BUILDNUMBER)
2763 dwlConditionMask |= dwConditionMask << 2*3;
2764 else if (dwTypeBitMask & VER_MAJORVERSION)
2765 dwlConditionMask |= dwConditionMask << 1*3;
2766 else if (dwTypeBitMask & VER_MINORVERSION)
2767 dwlConditionMask |= dwConditionMask << 0*3;
2768 return dwlConditionMask;
2771 /******************************************************************************
2772 * NtAccessCheckAndAuditAlarm (NTDLL.@)
2773 * ZwAccessCheckAndAuditAlarm (NTDLL.@)
2775 NTSTATUS WINAPI NtAccessCheckAndAuditAlarm(PUNICODE_STRING SubsystemName, HANDLE HandleId, PUNICODE_STRING ObjectTypeName,
2776 PUNICODE_STRING ObjectName, PSECURITY_DESCRIPTOR SecurityDescriptor,
2777 ACCESS_MASK DesiredAccess, PGENERIC_MAPPING GenericMapping, BOOLEAN ObjectCreation,
2778 PACCESS_MASK GrantedAccess, PBOOLEAN AccessStatus, PBOOLEAN GenerateOnClose)
2780 FIXME("(%s, %p, %s, %p, 0x%08x, %p, %d, %p, %p, %p), stub\n", debugstr_us(SubsystemName), HandleId,
2781 debugstr_us(ObjectTypeName), SecurityDescriptor, DesiredAccess, GenericMapping, ObjectCreation,
2782 GrantedAccess, AccessStatus, GenerateOnClose);
2784 return STATUS_NOT_IMPLEMENTED;
2787 /******************************************************************************
2788 * NtSystemDebugControl (NTDLL.@)
2789 * ZwSystemDebugControl (NTDLL.@)
2791 NTSTATUS WINAPI NtSystemDebugControl(SYSDBG_COMMAND command, PVOID inbuffer, ULONG inbuflength, PVOID outbuffer,
2792 ULONG outbuflength, PULONG retlength)
2794 FIXME("(%d, %p, %d, %p, %d, %p), stub\n", command, inbuffer, inbuflength, outbuffer, outbuflength, retlength);
2796 return STATUS_NOT_IMPLEMENTED;
2799 /******************************************************************************
2800 * NtSetLdtEntries (NTDLL.@)
2801 * ZwSetLdtEntries (NTDLL.@)
2803 NTSTATUS WINAPI NtSetLdtEntries(ULONG selector1, ULONG entry1_low, ULONG entry1_high,
2804 ULONG selector2, ULONG entry2_low, ULONG entry2_high)
2806 FIXME("(%u, %u, %u, %u, %u, %u): stub\n", selector1, entry1_low, entry1_high, selector2, entry2_low, entry2_high);
2808 return STATUS_NOT_IMPLEMENTED;