mirrored from https://gitlab.winehq.org/wine/wine.git
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
system.c
3848 lines (3365 loc) · 132 KB
/
system.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* System information APIs
*
* Copyright 1996-1998 Marcus Meissner
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
# include <sys/sysctl.h>
#endif
#ifdef HAVE_SYS_UTSNAME_H
# include <sys/utsname.h>
#endif
#ifdef HAVE_MACHINE_CPU_H
# include <machine/cpu.h>
#endif
#ifdef HAVE_SYS_RANDOM_H
# include <sys/random.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_IOKIT_IOKITLIB_H
# include <CoreFoundation/CoreFoundation.h>
# include <IOKit/IOKitLib.h>
# include <IOKit/pwr_mgt/IOPM.h>
# include <IOKit/pwr_mgt/IOPMLib.h>
# include <IOKit/ps/IOPowerSources.h>
#endif
#ifdef __APPLE__
# include <mach/mach.h>
# include <mach/machine.h>
# include <mach/mach_init.h>
# include <mach/mach_host.h>
# include <mach/vm_map.h>
#endif
#define NONAMELESSUNION
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "ddk/wdm.h"
#include "wine/asm.h"
#include "unix_private.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
#include "pshpack1.h"
struct smbios_prologue
{
BYTE calling_method;
BYTE major_version;
BYTE minor_version;
BYTE revision;
DWORD length;
};
struct smbios_header
{
BYTE type;
BYTE length;
WORD handle;
};
struct smbios_bios
{
struct smbios_header hdr;
BYTE vendor;
BYTE version;
WORD start;
BYTE date;
BYTE size;
UINT64 characteristics;
BYTE characteristics_ext[2];
BYTE system_bios_major_release;
BYTE system_bios_minor_release;
BYTE ec_firmware_major_release;
BYTE ec_firmware_minor_release;
};
struct smbios_system
{
struct smbios_header hdr;
BYTE vendor;
BYTE product;
BYTE version;
BYTE serial;
BYTE uuid[16];
BYTE wake_up_type;
BYTE sku_number;
BYTE family;
};
struct smbios_board
{
struct smbios_header hdr;
BYTE vendor;
BYTE product;
BYTE version;
BYTE serial;
BYTE asset_tag;
BYTE feature_flags;
BYTE location;
WORD chassis_handle;
BYTE board_type;
BYTE num_contained_handles;
};
struct smbios_chassis
{
struct smbios_header hdr;
BYTE vendor;
BYTE type;
BYTE version;
BYTE serial;
BYTE asset_tag;
BYTE boot_state;
BYTE power_supply_state;
BYTE thermal_state;
BYTE security_status;
DWORD oem_defined;
BYTE height;
BYTE num_power_cords;
BYTE num_contained_elements;
BYTE contained_element_rec_length;
};
struct smbios_boot_info
{
struct smbios_header hdr;
BYTE reserved[6];
BYTE boot_status[10];
};
#include "poppack.h"
struct smbios_bios_args
{
const char *vendor;
size_t vendor_len;
const char *version;
size_t version_len;
const char *date;
size_t date_len;
};
struct smbios_system_args
{
const char *vendor;
size_t vendor_len;
const char *product;
size_t product_len;
const char *version;
size_t version_len;
const char *serial;
size_t serial_len;
GUID uuid;
const char *sku;
size_t sku_len;
const char *family;
size_t family_len;
};
struct smbios_board_args
{
const char *vendor;
size_t vendor_len;
const char *product;
size_t product_len;
const char *version;
size_t version_len;
const char *serial;
size_t serial_len;
const char *asset_tag;
size_t asset_tag_len;
};
struct smbios_chassis_args
{
const char *vendor;
size_t vendor_len;
BYTE type;
const char *version;
size_t version_len;
const char *serial;
size_t serial_len;
const char *asset_tag;
size_t asset_tag_len;
};
/* Firmware table providers */
#define ACPI 0x41435049
#define FIRM 0x4649524D
#define RSMB 0x52534D42
SYSTEM_CPU_INFORMATION cpu_info = { 0 };
/*******************************************************************************
* Architecture specific feature detection for CPUs
*
* This a set of mutually exclusive #if define()s each providing its own get_cpuinfo() to be called
* from init_cpu_info();
*/
#if defined(__i386__) || defined(__x86_64__)
BOOL xstate_compaction_enabled = FALSE;
#define AUTH 0x68747541 /* "Auth" */
#define ENTI 0x69746e65 /* "enti" */
#define CAMD 0x444d4163 /* "cAMD" */
#define GENU 0x756e6547 /* "Genu" */
#define INEI 0x49656e69 /* "ineI" */
#define NTEL 0x6c65746e /* "ntel" */
extern void do_cpuid( unsigned int ax, unsigned int cx, unsigned int *p ) DECLSPEC_HIDDEN;
#ifdef __i386__
__ASM_GLOBAL_FUNC( do_cpuid,
"pushl %esi\n\t"
"pushl %ebx\n\t"
"movl 12(%esp),%eax\n\t"
"movl 16(%esp),%ecx\n\t"
"movl 20(%esp),%esi\n\t"
"cpuid\n\t"
"movl %eax,(%esi)\n\t"
"movl %ebx,4(%esi)\n\t"
"movl %ecx,8(%esi)\n\t"
"movl %edx,12(%esi)\n\t"
"popl %ebx\n\t"
"popl %esi\n\t"
"ret" )
#else
__ASM_GLOBAL_FUNC( do_cpuid,
"pushq %rbx\n\t"
"movl %edi,%eax\n\t"
"movl %esi,%ecx\n\t"
"movq %rdx,%r8\n\t"
"cpuid\n\t"
"movl %eax,(%r8)\n\t"
"movl %ebx,4(%r8)\n\t"
"movl %ecx,8(%r8)\n\t"
"movl %edx,12(%r8)\n\t"
"popq %rbx\n\t"
"ret" )
#endif
#ifdef __i386__
extern int have_cpuid(void) DECLSPEC_HIDDEN;
__ASM_GLOBAL_FUNC( have_cpuid,
"pushfl\n\t"
"pushfl\n\t"
"movl (%esp),%ecx\n\t"
"xorl $0x00200000,(%esp)\n\t"
"popfl\n\t"
"pushfl\n\t"
"popl %eax\n\t"
"popfl\n\t"
"xorl %ecx,%eax\n\t"
"andl $0x00200000,%eax\n\t"
"ret" )
#else
static int have_cpuid(void)
{
return 1;
}
#endif
/* Detect if a SSE2 processor is capable of Denormals Are Zero (DAZ) mode.
*
* This function assumes you have already checked for SSE2/FXSAVE support. */
static inline BOOL have_sse_daz_mode(void)
{
#ifdef __i386__
/* Intel says we need a zeroed 16-byte aligned buffer */
char buffer[512 + 16];
XSAVE_FORMAT *state = (XSAVE_FORMAT *)(((ULONG_PTR)buffer + 15) & ~15);
memset(buffer, 0, sizeof(buffer));
__asm__ __volatile__( "fxsave %0" : "=m" (*state) : "m" (*state) );
return (state->MxCsr_Mask & (1 << 6)) >> 6;
#else /* all x86_64 processors include SSE2 with DAZ mode */
return TRUE;
#endif
}
static void get_cpuinfo( SYSTEM_CPU_INFORMATION *info )
{
unsigned int regs[4], regs2[4], regs3[4];
#if defined(__i386__)
info->ProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
#elif defined(__x86_64__)
info->ProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64;
#endif
/* We're at least a 386 */
info->ProcessorFeatureBits = CPU_FEATURE_VME | CPU_FEATURE_X86 | CPU_FEATURE_PGE;
info->ProcessorLevel = 3;
if (!have_cpuid()) return;
do_cpuid( 0x00000000, 0, regs ); /* get standard cpuid level and vendor name */
if (regs[0]>=0x00000001) /* Check for supported cpuid version */
{
do_cpuid( 0x00000001, 0, regs2 ); /* get cpu features */
if (regs2[3] & (1 << 3 )) info->ProcessorFeatureBits |= CPU_FEATURE_PSE;
if (regs2[3] & (1 << 4 )) info->ProcessorFeatureBits |= CPU_FEATURE_TSC;
if (regs2[3] & (1 << 6 )) info->ProcessorFeatureBits |= CPU_FEATURE_PAE;
if (regs2[3] & (1 << 8 )) info->ProcessorFeatureBits |= CPU_FEATURE_CX8;
if (regs2[3] & (1 << 11)) info->ProcessorFeatureBits |= CPU_FEATURE_SEP;
if (regs2[3] & (1 << 12)) info->ProcessorFeatureBits |= CPU_FEATURE_MTRR;
if (regs2[3] & (1 << 15)) info->ProcessorFeatureBits |= CPU_FEATURE_CMOV;
if (regs2[3] & (1 << 16)) info->ProcessorFeatureBits |= CPU_FEATURE_PAT;
if (regs2[3] & (1 << 23)) info->ProcessorFeatureBits |= CPU_FEATURE_MMX;
if (regs2[3] & (1 << 24)) info->ProcessorFeatureBits |= CPU_FEATURE_FXSR;
if (regs2[3] & (1 << 25)) info->ProcessorFeatureBits |= CPU_FEATURE_SSE;
if (regs2[3] & (1 << 26)) info->ProcessorFeatureBits |= CPU_FEATURE_SSE2;
if (regs2[2] & (1 << 0 )) info->ProcessorFeatureBits |= CPU_FEATURE_SSE3;
if (regs2[2] & (1 << 9 )) info->ProcessorFeatureBits |= CPU_FEATURE_SSSE3;
if (regs2[2] & (1 << 13)) info->ProcessorFeatureBits |= CPU_FEATURE_CX128;
if (regs2[2] & (1 << 19)) info->ProcessorFeatureBits |= CPU_FEATURE_SSE41;
if (regs2[2] & (1 << 20)) info->ProcessorFeatureBits |= CPU_FEATURE_SSE42;
if (regs2[2] & (1 << 27)) info->ProcessorFeatureBits |= CPU_FEATURE_XSAVE;
if (regs2[2] & (1 << 28)) info->ProcessorFeatureBits |= CPU_FEATURE_AVX;
if((regs2[3] & (1 << 26)) && (regs2[3] & (1 << 24)) && have_sse_daz_mode()) /* has SSE2 and FXSAVE/FXRSTOR */
info->ProcessorFeatureBits |= CPU_FEATURE_DAZ;
if (regs[0] >= 0x00000007)
{
do_cpuid( 0x00000007, 0, regs3 ); /* get extended features */
if (regs3[1] & (1 << 5)) info->ProcessorFeatureBits |= CPU_FEATURE_AVX2;
}
if (info->ProcessorFeatureBits & CPU_FEATURE_XSAVE)
{
do_cpuid( 0x0000000d, 1, regs3 ); /* get XSAVE details */
if (regs3[0] & 2) xstate_compaction_enabled = TRUE;
}
if (regs[1] == AUTH && regs[3] == ENTI && regs[2] == CAMD)
{
info->ProcessorLevel = (regs2[0] >> 8) & 0xf; /* family */
if (info->ProcessorLevel == 0xf) /* AMD says to add the extended family to the family if family is 0xf */
info->ProcessorLevel += (regs2[0] >> 20) & 0xff;
/* repack model and stepping to make a "revision" */
info->ProcessorRevision = ((regs2[0] >> 16) & 0xf) << 12; /* extended model */
info->ProcessorRevision |= ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
info->ProcessorRevision |= regs2[0] & 0xf; /* stepping */
do_cpuid( 0x80000000, 0, regs ); /* get vendor cpuid level */
if (regs[0] >= 0x80000001)
{
do_cpuid( 0x80000001, 0, regs2 ); /* get vendor features */
if (regs2[2] & (1 << 2)) info->ProcessorFeatureBits |= CPU_FEATURE_VIRT;
if (regs2[3] & (1 << 20)) info->ProcessorFeatureBits |= CPU_FEATURE_NX;
if (regs2[3] & (1 << 27)) info->ProcessorFeatureBits |= CPU_FEATURE_TSC;
if (regs2[3] & (1u << 31)) info->ProcessorFeatureBits |= CPU_FEATURE_3DNOW;
}
}
else if (regs[1] == GENU && regs[3] == INEI && regs[2] == NTEL)
{
info->ProcessorLevel = ((regs2[0] >> 8) & 0xf) + ((regs2[0] >> 20) & 0xff); /* family + extended family */
if(info->ProcessorLevel == 15) info->ProcessorLevel = 6;
/* repack model and stepping to make a "revision" */
info->ProcessorRevision = ((regs2[0] >> 16) & 0xf) << 12; /* extended model */
info->ProcessorRevision |= ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
info->ProcessorRevision |= regs2[0] & 0xf; /* stepping */
if(regs2[2] & (1 << 5)) info->ProcessorFeatureBits |= CPU_FEATURE_VIRT;
if(regs2[3] & (1 << 21)) info->ProcessorFeatureBits |= CPU_FEATURE_DS;
do_cpuid( 0x80000000, 0, regs ); /* get vendor cpuid level */
if (regs[0] >= 0x80000001)
{
do_cpuid( 0x80000001, 0, regs2 ); /* get vendor features */
if (regs2[3] & (1 << 20)) info->ProcessorFeatureBits |= CPU_FEATURE_NX;
if (regs2[3] & (1 << 27)) info->ProcessorFeatureBits |= CPU_FEATURE_TSC;
}
}
else
{
info->ProcessorLevel = (regs2[0] >> 8) & 0xf; /* family */
/* repack model and stepping to make a "revision" */
info->ProcessorRevision = ((regs2[0] >> 4 ) & 0xf) << 8; /* model */
info->ProcessorRevision |= regs2[0] & 0xf; /* stepping */
}
}
}
#elif defined(__arm__)
static inline void get_cpuinfo( SYSTEM_CPU_INFORMATION *info )
{
#ifdef linux
char line[512];
char *s, *value;
FILE *f = fopen("/proc/cpuinfo", "r");
if (f)
{
while (fgets( line, sizeof(line), f ))
{
/* NOTE: the ':' is the only character we can rely on */
if (!(value = strchr(line,':'))) continue;
/* terminate the valuename */
s = value - 1;
while ((s >= line) && (*s == ' ' || *s == '\t')) s--;
s[1] = 0;
/* and strip leading spaces from value */
value += 1;
while (*value == ' ' || *value == '\t') value++;
if ((s = strchr( value,'\n' ))) *s = 0;
if (!strcmp( line, "CPU architecture" ))
{
info->ProcessorLevel = atoi(value);
continue;
}
if (!strcmp( line, "CPU revision" ))
{
info->ProcessorRevision = atoi(value);
continue;
}
if (!strcmp( line, "Features" ))
{
if (strstr(value, "crc32")) info->ProcessorFeatureBits |= CPU_FEATURE_ARM_V8_CRC32;
if (strstr(value, "aes")) info->ProcessorFeatureBits |= CPU_FEATURE_ARM_V8_CRYPTO;
continue;
}
}
fclose( f );
}
#elif defined(__FreeBSD__)
size_t valsize;
char buf[8];
int value;
valsize = sizeof(buf);
if (!sysctlbyname("hw.machine_arch", &buf, &valsize, NULL, 0) && sscanf(buf, "armv%i", &value) == 1)
info->ProcessorLevel = value;
valsize = sizeof(value);
if (!sysctlbyname("hw.floatingpoint", &value, &valsize, NULL, 0))
info->ProcessorFeatureBits |= CPU_FEATURE_ARM_VFP_32;
#else
FIXME("CPU Feature detection not implemented.\n");
#endif
info->ProcessorArchitecture = PROCESSOR_ARCHITECTURE_ARM;
}
#elif defined(__aarch64__)
static void get_cpuinfo( SYSTEM_CPU_INFORMATION *info )
{
#ifdef linux
char line[512];
char *s, *value;
FILE *f = fopen("/proc/cpuinfo", "r");
if (f)
{
while (fgets( line, sizeof(line), f ))
{
/* NOTE: the ':' is the only character we can rely on */
if (!(value = strchr(line,':'))) continue;
/* terminate the valuename */
s = value - 1;
while ((s >= line) && (*s == ' ' || *s == '\t')) s--;
s[1] = 0;
/* and strip leading spaces from value */
value += 1;
while (*value == ' ' || *value == '\t') value++;
if ((s = strchr( value,'\n' ))) *s = 0;
if (!strcmp( line, "CPU architecture" ))
{
info->ProcessorLevel = atoi(value);
continue;
}
if (!strcmp( line, "CPU revision" ))
{
info->ProcessorRevision = atoi(value);
continue;
}
if (!strcmp( line, "Features" ))
{
if (strstr(value, "crc32")) info->ProcessorFeatureBits |= CPU_FEATURE_ARM_V8_CRC32;
if (strstr(value, "aes")) info->ProcessorFeatureBits |= CPU_FEATURE_ARM_V8_CRYPTO;
continue;
}
}
fclose( f );
}
#else
FIXME("CPU Feature detection not implemented.\n");
#endif
info->ProcessorLevel = max(info->ProcessorLevel, 8);
info->ProcessorArchitecture = PROCESSOR_ARCHITECTURE_ARM64;
}
#endif /* End architecture specific feature detection for CPUs */
/******************************************************************
* init_cpu_info
*
* inits a couple of places with CPU related information:
* - cpu_info in this file
* - Peb->NumberOfProcessors
* - SharedUserData->ProcessFeatures[] array
*/
void init_cpu_info(void)
{
long num;
#ifdef _SC_NPROCESSORS_ONLN
num = sysconf(_SC_NPROCESSORS_ONLN);
if (num < 1)
{
num = 1;
WARN("Failed to detect the number of processors.\n");
}
#elif defined(CTL_HW) && defined(HW_NCPU)
int mib[2];
size_t len = sizeof(num);
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
if (sysctl(mib, 2, &num, &len, NULL, 0) != 0)
{
num = 1;
WARN("Failed to detect the number of processors.\n");
}
#else
num = 1;
FIXME("Detecting the number of processors is not supported.\n");
#endif
peb->NumberOfProcessors = num;
get_cpuinfo( &cpu_info );
TRACE( "<- CPU arch %d, level %d, rev %d, features 0x%x\n",
cpu_info.ProcessorArchitecture, cpu_info.ProcessorLevel, cpu_info.ProcessorRevision,
cpu_info.ProcessorFeatureBits );
}
static BOOL grow_logical_proc_buf( SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata, DWORD *max_len )
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION *new_data;
*max_len *= 2;
if (!(new_data = realloc( *pdata, *max_len*sizeof(*new_data) ))) return FALSE;
*pdata = new_data;
return TRUE;
}
static BOOL grow_logical_proc_ex_buf( SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *max_len )
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *new_dataex;
DWORD new_len = *max_len * 2;
if (!(new_dataex = realloc( *pdataex, new_len * sizeof(*new_dataex) ))) return FALSE;
memset( new_dataex + *max_len, 0, (new_len - *max_len) * sizeof(*new_dataex) );
*pdataex = new_dataex;
*max_len = new_len;
return TRUE;
}
static DWORD log_proc_ex_size_plus(DWORD size)
{
/* add SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX.Relationship and .Size */
return sizeof(LOGICAL_PROCESSOR_RELATIONSHIP) + sizeof(DWORD) + size;
}
static DWORD count_bits(ULONG_PTR mask)
{
DWORD count = 0;
while (mask > 0)
{
if (mask & 1) ++count;
mask >>= 1;
}
return count;
}
/* Store package and core information for a logical processor. Parsing of processor
* data may happen in multiple passes; the 'id' parameter is then used to locate
* previously stored data. The type of data stored in 'id' depends on 'rel':
* - RelationProcessorPackage: package id ('CPU socket').
* - RelationProcessorCore: physical core number.
*/
static BOOL logical_proc_info_add_by_id( SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len,
DWORD *pmax_len, LOGICAL_PROCESSOR_RELATIONSHIP rel,
DWORD id, ULONG_PTR mask )
{
if (pdata)
{
DWORD i;
for (i = 0; i < *len; i++)
{
if (rel == RelationProcessorPackage && (*pdata)[i].Relationship == rel && (*pdata)[i].u.Reserved[1] == id)
{
(*pdata)[i].ProcessorMask |= mask;
return TRUE;
}
else if (rel == RelationProcessorCore && (*pdata)[i].Relationship == rel && (*pdata)[i].u.Reserved[1] == id)
return TRUE;
}
while (*len == *pmax_len)
{
if (!grow_logical_proc_buf(pdata, pmax_len)) return FALSE;
}
(*pdata)[i].Relationship = rel;
(*pdata)[i].ProcessorMask = mask;
if (rel == RelationProcessorCore)
(*pdata)[i].u.ProcessorCore.Flags = count_bits(mask) > 1 ? LTP_PC_SMT : 0;
(*pdata)[i].u.Reserved[0] = 0;
(*pdata)[i].u.Reserved[1] = id;
*len = i+1;
}
else
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
DWORD ofs = 0;
while (ofs < *len)
{
dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
if (rel == RelationProcessorPackage && dataex->Relationship == rel && dataex->u.Processor.Reserved[1] == id)
{
dataex->u.Processor.GroupMask[0].Mask |= mask;
return TRUE;
}
else if (rel == RelationProcessorCore && dataex->Relationship == rel && dataex->u.Processor.Reserved[1] == id)
{
return TRUE;
}
ofs += dataex->Size;
}
/* TODO: For now, just one group. If more than 64 processors, then we
* need another group. */
while (ofs + log_proc_ex_size_plus(sizeof(PROCESSOR_RELATIONSHIP)) > *pmax_len)
{
if (!grow_logical_proc_ex_buf(pdataex, pmax_len)) return FALSE;
}
dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
dataex->Relationship = rel;
dataex->Size = log_proc_ex_size_plus(sizeof(PROCESSOR_RELATIONSHIP));
if (rel == RelationProcessorCore)
dataex->u.Processor.Flags = count_bits(mask) > 1 ? LTP_PC_SMT : 0;
else
dataex->u.Processor.Flags = 0;
dataex->u.Processor.EfficiencyClass = 0;
dataex->u.Processor.GroupCount = 1;
dataex->u.Processor.GroupMask[0].Mask = mask;
dataex->u.Processor.GroupMask[0].Group = 0;
/* mark for future lookup */
dataex->u.Processor.Reserved[0] = 0;
dataex->u.Processor.Reserved[1] = id;
*len += dataex->Size;
}
return TRUE;
}
static BOOL logical_proc_info_add_cache( SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len,
DWORD *pmax_len, ULONG_PTR mask, CACHE_DESCRIPTOR *cache )
{
if (pdata)
{
DWORD i;
for (i = 0; i < *len; i++)
{
if ((*pdata)[i].Relationship==RelationCache && (*pdata)[i].ProcessorMask==mask
&& (*pdata)[i].u.Cache.Level==cache->Level && (*pdata)[i].u.Cache.Type==cache->Type)
return TRUE;
}
while (*len == *pmax_len)
if (!grow_logical_proc_buf(pdata, pmax_len)) return FALSE;
(*pdata)[i].Relationship = RelationCache;
(*pdata)[i].ProcessorMask = mask;
(*pdata)[i].u.Cache = *cache;
*len = i+1;
}
else
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
DWORD ofs;
for (ofs = 0; ofs < *len; )
{
dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
if (dataex->Relationship == RelationCache && dataex->u.Cache.GroupMask.Mask == mask &&
dataex->u.Cache.Level == cache->Level && dataex->u.Cache.Type == cache->Type)
return TRUE;
ofs += dataex->Size;
}
while (ofs + log_proc_ex_size_plus(sizeof(CACHE_RELATIONSHIP)) > *pmax_len)
{
if (!grow_logical_proc_ex_buf(pdataex, pmax_len)) return FALSE;
}
dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + ofs);
dataex->Relationship = RelationCache;
dataex->Size = log_proc_ex_size_plus(sizeof(CACHE_RELATIONSHIP));
dataex->u.Cache.Level = cache->Level;
dataex->u.Cache.Associativity = cache->Associativity;
dataex->u.Cache.LineSize = cache->LineSize;
dataex->u.Cache.CacheSize = cache->Size;
dataex->u.Cache.Type = cache->Type;
dataex->u.Cache.GroupMask.Mask = mask;
dataex->u.Cache.GroupMask.Group = 0;
*len += dataex->Size;
}
return TRUE;
}
static BOOL logical_proc_info_add_numa_node( SYSTEM_LOGICAL_PROCESSOR_INFORMATION **pdata,
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex, DWORD *len,
DWORD *pmax_len, ULONG_PTR mask, DWORD node_id )
{
if (pdata)
{
while (*len == *pmax_len)
if (!grow_logical_proc_buf(pdata, pmax_len)) return FALSE;
(*pdata)[*len].Relationship = RelationNumaNode;
(*pdata)[*len].ProcessorMask = mask;
(*pdata)[*len].u.NumaNode.NodeNumber = node_id;
(*len)++;
}
else
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
while (*len + log_proc_ex_size_plus(sizeof(NUMA_NODE_RELATIONSHIP)) > *pmax_len)
{
if (!grow_logical_proc_ex_buf(pdataex, pmax_len)) return FALSE;
}
dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + *len);
dataex->Relationship = RelationNumaNode;
dataex->Size = log_proc_ex_size_plus(sizeof(NUMA_NODE_RELATIONSHIP));
dataex->u.NumaNode.NodeNumber = node_id;
dataex->u.NumaNode.GroupMask.Mask = mask;
dataex->u.NumaNode.GroupMask.Group = 0;
*len += dataex->Size;
}
return TRUE;
}
static BOOL logical_proc_info_add_group( SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **pdataex,
DWORD *len, DWORD *pmax_len, DWORD num_cpus, ULONG_PTR mask )
{
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *dataex;
while (*len + log_proc_ex_size_plus(sizeof(GROUP_RELATIONSHIP)) > *pmax_len)
if (!grow_logical_proc_ex_buf(pdataex, pmax_len)) return FALSE;
dataex = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(((char *)*pdataex) + *len);
dataex->Relationship = RelationGroup;
dataex->Size = log_proc_ex_size_plus(sizeof(GROUP_RELATIONSHIP));
dataex->u.Group.MaximumGroupCount = 1;
dataex->u.Group.ActiveGroupCount = 1;
dataex->u.Group.GroupInfo[0].MaximumProcessorCount = num_cpus;
dataex->u.Group.GroupInfo[0].ActiveProcessorCount = num_cpus;
dataex->u.Group.GroupInfo[0].ActiveProcessorMask = mask;
*len += dataex->Size;
return TRUE;
}
#ifdef linux
/* Helper function for counting bitmap values as commonly used by the Linux kernel
* for storing CPU masks in sysfs. The format is comma separated lists of hex values
* each max 32-bit e.g. "00ff" or even "00,00000000,0000ffff".
*
* Example files include:
* - /sys/devices/system/cpu/cpu0/cache/index0/shared_cpu_map
* - /sys/devices/system/cpu/cpu0/topology/thread_siblings
*/
static BOOL sysfs_parse_bitmap(const char *filename, ULONG_PTR *mask)
{
FILE *f;
DWORD r;
f = fopen(filename, "r");
if (!f) return FALSE;
while (!feof(f))
{
char op;
if (!fscanf(f, "%x%c ", &r, &op)) break;
*mask = (sizeof(ULONG_PTR)>sizeof(int) ? *mask << (8 * sizeof(DWORD)) : 0) + r;
}
fclose( f );
return TRUE;
}
/* Helper function for counting number of elements in interval lists as used by
* the Linux kernel. The format is comma separated list of intervals of which
* each interval has the format of "begin-end" where begin and end are decimal
* numbers. E.g. "0-7", "0-7,16-23"
*
* Example files include:
* - /sys/devices/system/cpu/online
* - /sys/devices/system/cpu/cpu0/cache/index0/shared_cpu_list
* - /sys/devices/system/cpu/cpu0/topology/thread_siblings_list.
*/
static BOOL sysfs_count_list_elements(const char *filename, DWORD *result)
{
FILE *f;
f = fopen(filename, "r");
if (!f) return FALSE;
while (!feof(f))
{
char op;
DWORD beg, end;
if (!fscanf(f, "%u%c ", &beg, &op)) break;
if(op == '-')
fscanf(f, "%u%c ", &end, &op);
else
end = beg;
*result += end - beg + 1;
}
fclose( f );
return TRUE;
}
/* for 'data', max_len is the array count. for 'dataex', max_len is in bytes */
static NTSTATUS create_logical_proc_info( SYSTEM_LOGICAL_PROCESSOR_INFORMATION **data,
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX **dataex,
DWORD *max_len, DWORD relation )
{
static const char core_info[] = "/sys/devices/system/cpu/cpu%u/topology/%s";
static const char cache_info[] = "/sys/devices/system/cpu/cpu%u/cache/index%u/%s";
static const char numa_info[] = "/sys/devices/system/node/node%u/cpumap";
FILE *fcpu_list, *fnuma_list, *f;
DWORD len = 0, beg, end, i, j, r, num_cpus = 0, max_cpus = 0;
char op, name[MAX_PATH];
ULONG_PTR all_cpus_mask = 0;
/* On systems with a large number of CPU cores (32 or 64 depending on 32-bit or 64-bit),
* we have issues parsing processor information:
* - ULONG_PTR masks as used in data structures can't hold all cores. Requires splitting
* data appropriately into "processor groups". We are hard coding 1.
* - Thread affinity code in wineserver and our CPU parsing code here work independently.
* So far the Windows mask applied directly to Linux, but process groups break that.
* (NUMA systems you may have multiple non-full groups.)
*/
if(sysfs_count_list_elements("/sys/devices/system/cpu/present", &max_cpus) && max_cpus > MAXIMUM_PROCESSORS)
{
FIXME("Improve CPU info reporting: system supports %u logical cores, but only %u supported!\n",
max_cpus, MAXIMUM_PROCESSORS);
}
fcpu_list = fopen("/sys/devices/system/cpu/online", "r");
if (!fcpu_list) return STATUS_NOT_IMPLEMENTED;
while (!feof(fcpu_list))
{
if (!fscanf(fcpu_list, "%u%c ", &beg, &op)) break;
if (op == '-') fscanf(fcpu_list, "%u%c ", &end, &op);
else end = beg;
for(i = beg; i <= end; i++)
{
DWORD phys_core = 0;
ULONG_PTR thread_mask = 0;
if (i > 8*sizeof(ULONG_PTR))
{
FIXME("skipping logical processor %d\n", i);
continue;
}
if (relation == RelationAll || relation == RelationProcessorPackage)
{
sprintf(name, core_info, i, "physical_package_id");
f = fopen(name, "r");
if (f)
{
fscanf(f, "%u", &r);
fclose(f);
}
else r = 0;
if (!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorPackage, r, (ULONG_PTR)1 << i))
{
fclose(fcpu_list);
return STATUS_NO_MEMORY;
}
}
/* Sysfs enumerates logical cores (and not physical cores), but Windows enumerates
* by physical core. Upon enumerating a logical core in sysfs, we register a physical
* core and all its logical cores. In order to not report physical cores multiple
* times, we pass a unique physical core ID to logical_proc_info_add_by_id and let
* that call figure out any duplication.
* Obtain a unique physical core ID from the first element of thread_siblings_list.
* This list provides logical cores sharing the same physical core. The IDs are based
* on kernel cpu core numbering as opposed to a hardware core ID like provided through
* 'core_id', so are suitable as a unique ID.
*/
if(relation == RelationAll || relation == RelationProcessorCore ||
relation == RelationNumaNode || relation == RelationGroup)
{
/* Mask of logical threads sharing same physical core in kernel core numbering. */
sprintf(name, core_info, i, "thread_siblings");
if(!sysfs_parse_bitmap(name, &thread_mask)) thread_mask = 1<<i;
/* Needed later for NumaNode and Group. */
all_cpus_mask |= thread_mask;
if (relation == RelationAll || relation == RelationProcessorCore)
{
sprintf(name, core_info, i, "thread_siblings_list");
f = fopen(name, "r");
if (f)
{
fscanf(f, "%d%c", &phys_core, &op);
fclose(f);
}
else phys_core = i;
if (!logical_proc_info_add_by_id(data, dataex, &len, max_len, RelationProcessorCore, phys_core, thread_mask))
{
fclose(fcpu_list);
return STATUS_NO_MEMORY;
}
}
}
if (relation == RelationAll || relation == RelationCache)
{
for(j = 0; j < 4; j++)
{
CACHE_DESCRIPTOR cache;
ULONG_PTR mask = 0;
sprintf(name, cache_info, i, j, "shared_cpu_map");
if(!sysfs_parse_bitmap(name, &mask)) continue;