This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
xfreq-intel.c
2382 lines (2195 loc) · 80 KB
/
xfreq-intel.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
/*
* xfreq-intel.c by CyrIng
*
* XFreq
* Copyright (C) 2013-2015 CYRIL INGENIERIE
* Licenses: GPL2
*/
#if defined(Linux)
#define _GNU_SOURCE
#include <sched.h>
#include <sys/io.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <libgen.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/stat.h>
#if defined(FreeBSD)
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <dev/io/iodev.h>
#include <sys/cpuctl.h>
#include <sys/cpuset.h>
#include <pthread_np.h>
#endif
#define _APPNAME "XFreq-Intel"
#include "xfreq-smbios.h"
#include "xfreq-api.h"
#include "xfreq-intel.h"
static char Version[] = AutoDate;
// Initialize MSR based on Architecture.
Bool32 Init_MSR_GenuineIntel(void *uArg)
{
uARG *A=(uARG *) uArg;
ssize_t retval=0;
int tmpFD=open(CPU_BP, O_RDONLY);
Bool32 rc=TRUE;
// Read the minimum, maximum & the turbo ratios from Core number 0
if(tmpFD != -1)
{
rc=((retval=Read_MSR(tmpFD, IA32_MISC_ENABLE, (MISC_PROC_FEATURES *) &A->SHM->P.MiscFeatures)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_MTRR_DEF_TYPE,(MTRR_DEF_TYPE *) &A->SHM->P.MTRRdefType)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_POWER_CTL, (POWER_CONTROL *) &A->SHM->P.PowerControl)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_PKG_CST_CONFIG_CTRL, (CSTATE_CONFIG *) &A->SHM->P.CStateConfig)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PLATFORM_ID, (PLATFORM_ID *) &A->SHM->P.PlatformId)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PERF_STATUS, (PERF_STATUS *) &A->SHM->P.PerfStatus)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PERF_CTL, (PERF_CONTROL *) &A->SHM->P.PerfControl)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_EFER, (EXT_FEATURE_ENABLE *) &A->SHM->P.ExtFeature)) != -1);
// MSR_PLATFORM_INFO may be available in this Intel Architecture ?
if((rc=((retval=Read_MSR(tmpFD, MSR_PLATFORM_INFO, (PLATFORM_INFO *) &A->SHM->P.PlatformInfo)) != -1)) == TRUE)
{ // Then, we get the Min & Max non Turbo Ratios which might inverted.
A->SHM->P.Boost[0]=MIN(A->SHM->P.PlatformInfo.MinimumRatio, A->SHM->P.PlatformInfo.MaxNonTurboRatio);
A->SHM->P.Boost[1]=MAX(A->SHM->P.PlatformInfo.MinimumRatio, A->SHM->P.PlatformInfo.MaxNonTurboRatio);
}
else // Otherwise, set the Min & Max Ratios to the first non zero value found in the following MSR order:
// IA32_PLATFORM_ID[MaxBusRatio] , IA32_PERF_STATUS[MaxBusRatio] , IA32_PERF_STATUS[CurrentRatio]
A->SHM->P.Boost[0]=A->SHM->P.Boost[1]=(A->SHM->P.PlatformId.MaxBusRatio) ? A->SHM->P.PlatformId.MaxBusRatio:(A->SHM->P.PerfStatus.MaxBusRatio) ? A->SHM->P.PerfStatus.MaxBusRatio:A->SHM->P.PerfStatus.CurrentRatio;
// MSR_TURBO_RATIO_LIMIT may also be available ?
if((rc=((retval=Read_MSR(tmpFD, MSR_TURBO_RATIO_LIMIT, (TURBO *) &A->SHM->P.Turbo)) != -1)) == TRUE)
{
A->SHM->P.Boost[2]=A->SHM->P.Turbo.MaxRatio_8C;
A->SHM->P.Boost[3]=A->SHM->P.Turbo.MaxRatio_7C;
A->SHM->P.Boost[4]=A->SHM->P.Turbo.MaxRatio_6C;
A->SHM->P.Boost[5]=A->SHM->P.Turbo.MaxRatio_5C;
A->SHM->P.Boost[6]=A->SHM->P.Turbo.MaxRatio_4C;
A->SHM->P.Boost[7]=A->SHM->P.Turbo.MaxRatio_3C;
A->SHM->P.Boost[8]=A->SHM->P.Turbo.MaxRatio_2C;
A->SHM->P.Boost[9]=A->SHM->P.Turbo.MaxRatio_1C;
}
else // In any case, last Ratio is equal to the maximum non Turbo Ratio.
A->SHM->P.Boost[9]=A->SHM->P.Boost[1];
close(tmpFD);
}
else rc=FALSE;
char pathname[]=CPU_AP;
int cpu=0;
for(cpu=0; cpu < A->SHM->P.CPU; cpu++)
if(A->SHM->C[cpu].T.Offline != TRUE)
{
sprintf(pathname, CPU_DEV, cpu);
if( (rc=((A->SHM->C[cpu].FD=open(pathname, O_RDWR)) != -1)) )
{
// Initialize C-States.
A->Arch[A->SHM->P.ArchID].uCycle(A, cpu, 0);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_THERM_INTERRUPT, (THERM_INTERRUPT *) &A->SHM->C[cpu].ThermIntr)) != -1);
}
A->SHM->C[cpu].TjMax.Target=100;
}
return(rc);
}
Bool32 Init_MSR_Core(void *uArg)
{
uARG *A=(uARG *) uArg;
ssize_t retval=0;
int tmpFD=open(CPU_BP, O_RDONLY);
Bool32 rc=TRUE;
// Read the minimum, maximum & the turbo ratios from Core number 0
if(tmpFD != -1)
{
rc=((retval=Read_MSR(tmpFD, IA32_MISC_ENABLE, (MISC_PROC_FEATURES *) &A->SHM->P.MiscFeatures)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_MTRR_DEF_TYPE,(MTRR_DEF_TYPE *) &A->SHM->P.MTRRdefType)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_POWER_CTL, (POWER_CONTROL *) &A->SHM->P.PowerControl)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_PKG_CST_CONFIG_CTRL, (CSTATE_CONFIG *) &A->SHM->P.CStateConfig)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PLATFORM_ID, (PLATFORM_ID *) &A->SHM->P.PlatformId)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PERF_STATUS, (PERF_STATUS *) &A->SHM->P.PerfStatus)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PERF_CTL, (PERF_CONTROL *) &A->SHM->P.PerfControl)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_EFER, (EXT_FEATURE_ENABLE *) &A->SHM->P.ExtFeature)) != -1);
// MSR_PLATFORM_INFO may be available with Core2 ?
if((rc=((retval=Read_MSR(tmpFD, MSR_PLATFORM_INFO, (PLATFORM_INFO *) &A->SHM->P.PlatformInfo)) != -1)) == TRUE)
{
A->SHM->P.Boost[0]=MIN(A->SHM->P.PlatformInfo.MinimumRatio, A->SHM->P.PlatformInfo.MaxNonTurboRatio);
A->SHM->P.Boost[1]=MAX(A->SHM->P.PlatformInfo.MinimumRatio, A->SHM->P.PlatformInfo.MaxNonTurboRatio);
}
else
A->SHM->P.Boost[0]=A->SHM->P.Boost[1]=(A->SHM->P.PlatformId.MaxBusRatio) ? A->SHM->P.PlatformId.MaxBusRatio:(A->SHM->P.PerfStatus.MaxBusRatio) ? A->SHM->P.PerfStatus.MaxBusRatio:A->SHM->P.PerfStatus.CurrentRatio;
// MSR_TURBO_RATIO_LIMIT may also be available with Core2 ?
if((rc=((retval=Read_MSR(tmpFD, MSR_TURBO_RATIO_LIMIT, (TURBO *) &A->SHM->P.Turbo)) != -1)) == TRUE)
{
A->SHM->P.Boost[2]=A->SHM->P.Turbo.MaxRatio_8C;
A->SHM->P.Boost[3]=A->SHM->P.Turbo.MaxRatio_7C;
A->SHM->P.Boost[4]=A->SHM->P.Turbo.MaxRatio_6C;
A->SHM->P.Boost[5]=A->SHM->P.Turbo.MaxRatio_5C;
A->SHM->P.Boost[6]=A->SHM->P.Turbo.MaxRatio_4C;
A->SHM->P.Boost[7]=A->SHM->P.Turbo.MaxRatio_3C;
A->SHM->P.Boost[8]=A->SHM->P.Turbo.MaxRatio_2C;
A->SHM->P.Boost[9]=A->SHM->P.Turbo.MaxRatio_1C;
}
else
A->SHM->P.Boost[9]=A->SHM->P.Boost[1];
close(tmpFD);
}
else rc=FALSE;
char pathname[]=CPU_AP, warning[64];
int cpu=0;
for(cpu=0; cpu < A->SHM->P.CPU; cpu++)
if(A->SHM->C[cpu].T.Offline != TRUE)
{
sprintf(pathname, CPU_DEV, cpu);
if( (rc=((A->SHM->C[cpu].FD=open(pathname, O_RDWR)) != -1)) )
{
// Enable the Performance Counters 0, 1 and 2 :
// - Set the global counter bits
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_CTRL, (GLOBAL_PERF_COUNTER *) &A->SHM->C[cpu].GlobalPerfCounter)) != -1);
A->SaveArea[cpu].GlobalPerfCounter=A->SHM->C[cpu].GlobalPerfCounter;
#if defined(DEBUG)
if(A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR0 != 0)
{
sprintf(warning, "Warning: CPU#%02d: Fixed Counter #0 is already activated", cpu);
tracerr(warning);
}
if(A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR1 != 0)
{
sprintf(warning, "Warning: CPU#%02d: Fixed Counter #1 is already activated", cpu);
tracerr(warning);
}
if(A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR2 != 0)
{
sprintf(warning, "Warning: CPU#%02d: Fixed Counter #2 is already activated", cpu);
tracerr(warning);
}
#endif
A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR0=1;
A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR1=1;
A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR2=1;
rc=((retval=Write_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_CTRL, (GLOBAL_PERF_COUNTER *) &A->SHM->C[cpu].GlobalPerfCounter)) != -1);
// - Set the fixed counter bits
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR_CTRL, (FIXED_PERF_COUNTER *) &A->SHM->C[cpu].FixedPerfCounter)) != -1);
A->SaveArea[cpu].FixedPerfCounter=A->SHM->C[cpu].FixedPerfCounter;
A->SHM->C[cpu].FixedPerfCounter.EN0_OS=1;
A->SHM->C[cpu].FixedPerfCounter.EN1_OS=1;
A->SHM->C[cpu].FixedPerfCounter.EN2_OS=1;
A->SHM->C[cpu].FixedPerfCounter.EN0_Usr=1;
A->SHM->C[cpu].FixedPerfCounter.EN1_Usr=1;
A->SHM->C[cpu].FixedPerfCounter.EN2_Usr=1;
if(A->SHM->P.PerCore)
{ // Not available but keep it as a workarround.
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN0=1;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN1=1;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN2=1;
}
else {
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN0=0;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN1=0;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN2=0;
}
rc=((retval=Write_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR_CTRL, (FIXED_PERF_COUNTER *) &A->SHM->C[cpu].FixedPerfCounter)) != -1);
// Check & fixe Counter Overflow.
GLOBAL_PERF_STATUS Overflow={0};
GLOBAL_PERF_OVF_CTRL OvfControl={0};
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_STATUS, (GLOBAL_PERF_STATUS *) &Overflow)) != -1);
if(Overflow.Overflow_CTR0)
{
sprintf(warning, "Remark CPU#%02d: INST Counter #0 is overflowed", cpu);
tracerr(warning);
OvfControl.Clear_Ovf_CTR0=1;
}
if(Overflow.Overflow_CTR1)
{
sprintf(warning, "Remark CPU#%02d: UCC Counter #1 is overflowed", cpu);
tracerr(warning);
OvfControl.Clear_Ovf_CTR1=1;
}
if(Overflow.Overflow_CTR2)
{
sprintf(warning, "Remark CPU#%02d: URC Counter #1 is overflowed", cpu);
tracerr(warning);
OvfControl.Clear_Ovf_CTR2=1;
}
if(Overflow.Overflow_CTR0|Overflow.Overflow_CTR1|Overflow.Overflow_CTR2)
{
sprintf(warning, "Remark CPU#%02d: Resetting Counters", cpu);
tracerr(warning);
rc=((retval=Write_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_OVF_CTRL, (GLOBAL_PERF_OVF_CTRL *) &OvfControl)) != -1);
}
// Initialize C-States.
A->Arch[A->SHM->P.ArchID].uCycle(A, cpu, 0);
// Retreive the Thermal Junction Max. Fallback to 100°C if not available.
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, MSR_TEMPERATURE_TARGET, (TJMAX *) &A->SHM->C[cpu].TjMax)) != -1);
if(A->SHM->C[cpu].TjMax.Target == 0)
{
tracerr("Warning: Thermal Junction Max unavailable");
A->SHM->C[cpu].TjMax.Target=100;
}
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_THERM_INTERRUPT, (THERM_INTERRUPT *) &A->SHM->C[cpu].ThermIntr)) != -1);
}
else // Fallback to an arbitrary & commom value.
{
sprintf(warning, "Remark CPU#%02d: Thermal Junction Max defaults to 100C", cpu);
tracerr(warning);
A->SHM->C[cpu].TjMax.Target=100;
}
}
return(rc);
}
Bool32 Init_MSR_Nehalem(void *uArg)
{
uARG *A=(uARG *) uArg;
ssize_t retval=0;
int tmpFD=open(CPU_BP, O_RDONLY);
Bool32 rc=TRUE;
// Read the minimum, maximum & the turbo ratios from Core number 0
if(tmpFD != -1)
{
rc=((retval=Read_MSR(tmpFD, IA32_MISC_ENABLE, (MISC_PROC_FEATURES *) &A->SHM->P.MiscFeatures)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_MTRR_DEF_TYPE,(MTRR_DEF_TYPE *) &A->SHM->P.MTRRdefType)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_PLATFORM_INFO, (PLATFORM_INFO *) &A->SHM->P.PlatformInfo)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_TURBO_RATIO_LIMIT, (TURBO *) &A->SHM->P.Turbo)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_POWER_CTL, (POWER_CONTROL *) &A->SHM->P.PowerControl)) != -1);
rc=((retval=Read_MSR(tmpFD, MSR_PKG_CST_CONFIG_CTRL, (CSTATE_CONFIG *) &A->SHM->P.CStateConfig)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PLATFORM_ID, (PLATFORM_ID *) &A->SHM->P.PlatformId)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PERF_STATUS, (PERF_STATUS *) &A->SHM->P.PerfStatus)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_PERF_CTL, (PERF_CONTROL *) &A->SHM->P.PerfControl)) != -1);
rc=((retval=Read_MSR(tmpFD, IA32_EFER, (EXT_FEATURE_ENABLE *) &A->SHM->P.ExtFeature)) != -1);
close(tmpFD);
A->SHM->P.Boost[0]=A->SHM->P.PlatformInfo.MinimumRatio;
A->SHM->P.Boost[1]=A->SHM->P.PlatformInfo.MaxNonTurboRatio;
A->SHM->P.Boost[2]=A->SHM->P.Turbo.MaxRatio_8C;
A->SHM->P.Boost[3]=A->SHM->P.Turbo.MaxRatio_7C;
A->SHM->P.Boost[4]=A->SHM->P.Turbo.MaxRatio_6C;
A->SHM->P.Boost[5]=A->SHM->P.Turbo.MaxRatio_5C;
A->SHM->P.Boost[6]=A->SHM->P.Turbo.MaxRatio_4C;
A->SHM->P.Boost[7]=A->SHM->P.Turbo.MaxRatio_3C;
A->SHM->P.Boost[8]=A->SHM->P.Turbo.MaxRatio_2C;
A->SHM->P.Boost[9]=A->SHM->P.Turbo.MaxRatio_1C;
}
else rc=FALSE;
char pathname[]=CPU_AP, warning[64];
int cpu=0;
for(cpu=0; cpu < A->SHM->P.CPU; cpu++)
if(A->SHM->C[cpu].T.Offline != TRUE)
{
sprintf(pathname, CPU_DEV, cpu);
if( (rc=((A->SHM->C[cpu].FD=open(pathname, O_RDWR)) != -1)) )
{
// Enable the Performance Counters 0, 1 and 2 :
// - Set the global counter bits
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_CTRL, (GLOBAL_PERF_COUNTER *) &A->SHM->C[cpu].GlobalPerfCounter)) != -1);
A->SaveArea[cpu].GlobalPerfCounter=A->SHM->C[cpu].GlobalPerfCounter;
#if defined(DEBUG)
if(A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR0 != 0)
{
sprintf(warning, "Warning: CPU#%02d: Fixed Counter #0 is already activated", cpu);
tracerr(warning);
}
if(A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR1 != 0)
{
sprintf(warning, "Warning: CPU#%02d: Fixed Counter #1 is already activated", cpu);
tracerr(warning);
}
if(A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR2 != 0)
{
sprintf(warning, "Warning: CPU#%02d: Fixed Counter #2 is already activated", cpu);
tracerr(warning);
}
#endif
A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR0=1;
A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR1=1;
A->SHM->C[cpu].GlobalPerfCounter.EN_FIXED_CTR2=1;
rc=((retval=Write_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_CTRL, (GLOBAL_PERF_COUNTER *) &A->SHM->C[cpu].GlobalPerfCounter)) != -1);
// - Set the fixed counter bits
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR_CTRL, (FIXED_PERF_COUNTER *) &A->SHM->C[cpu].FixedPerfCounter)) != -1);
A->SaveArea[cpu].FixedPerfCounter=A->SHM->C[cpu].FixedPerfCounter;
A->SHM->C[cpu].FixedPerfCounter.EN0_OS=1;
A->SHM->C[cpu].FixedPerfCounter.EN1_OS=1;
A->SHM->C[cpu].FixedPerfCounter.EN2_OS=1;
A->SHM->C[cpu].FixedPerfCounter.EN0_Usr=1;
A->SHM->C[cpu].FixedPerfCounter.EN1_Usr=1;
A->SHM->C[cpu].FixedPerfCounter.EN2_Usr=1;
if(A->SHM->P.PerCore)
{
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN0=1;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN1=1;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN2=1;
}
else {
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN0=0;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN1=0;
A->SHM->C[cpu].FixedPerfCounter.AnyThread_EN2=0;
}
rc=((retval=Write_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR_CTRL, (FIXED_PERF_COUNTER *) &A->SHM->C[cpu].FixedPerfCounter)) != -1);
// Check & fixe Counter Overflow.
GLOBAL_PERF_STATUS Overflow={0};
GLOBAL_PERF_OVF_CTRL OvfControl={0};
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_STATUS, (GLOBAL_PERF_STATUS *) &Overflow)) != -1);
if(Overflow.Overflow_CTR0)
{
sprintf(warning, "Remark CPU#%02d: INST Counter #0 is overflowed", cpu);
tracerr(warning);
OvfControl.Clear_Ovf_CTR0=1;
}
if(Overflow.Overflow_CTR1)
{
sprintf(warning, "Remark CPU#%02d: UCC Counter #1 is overflowed", cpu);
tracerr(warning);
OvfControl.Clear_Ovf_CTR1=1;
}
if(Overflow.Overflow_CTR2)
{
sprintf(warning, "Remark CPU#%02d: URC Counter #1 is overflowed", cpu);
tracerr(warning);
OvfControl.Clear_Ovf_CTR2=1;
}
if(Overflow.Overflow_CTR0|Overflow.Overflow_CTR1|Overflow.Overflow_CTR2)
{
sprintf(warning, "Remark CPU#%02d: Resetting Counters", cpu);
tracerr(warning);
rc=((retval=Write_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_OVF_CTRL, (GLOBAL_PERF_OVF_CTRL *) &OvfControl)) != -1);
}
// Initialize C-States.
A->Arch[A->SHM->P.ArchID].uCycle(A, cpu, 0);
// Retreive the Thermal Junction Max. Fallback to 100°C if not available.
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, MSR_TEMPERATURE_TARGET, (TJMAX *) &A->SHM->C[cpu].TjMax)) != -1);
if(A->SHM->C[cpu].TjMax.Target == 0)
{
tracerr("Warning: Thermal Junction Max unavailable");
A->SHM->C[cpu].TjMax.Target=100;
}
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_THERM_INTERRUPT, (THERM_INTERRUPT *) &A->SHM->C[cpu].ThermIntr)) != -1);
}
else // Fallback to an arbitrary & commom value.
{
sprintf(warning, "Remark CPU#%02d: Thermal Junction Max defaults to 100C", cpu);
tracerr(warning);
A->SHM->C[cpu].TjMax.Target=100;
}
}
return(rc);
}
// Close all MSR handles.
Bool32 Close_MSR_Only(void *uArg)
{
uARG *A=(uARG *) uArg;
int cpu=0;
for(cpu=0; cpu < A->SHM->P.CPU; cpu++)
if(A->SHM->C[cpu].T.Offline != TRUE)
{
// Release the MSR handle associated to the Core.
if(A->SHM->C[cpu].FD != -1)
close(A->SHM->C[cpu].FD);
}
return(FALSE);
}
Bool32 Close_MSR_Counters(void *uArg)
{
uARG *A=(uARG *) uArg;
int cpu=0;
for(cpu=0; cpu < A->SHM->P.CPU; cpu++)
if(A->SHM->C[cpu].T.Offline != TRUE)
{
// Reset the fixed and the global counters.
if(A->SHM->CPL.RESET == TRUE)
{
A->SaveArea[cpu].FixedPerfCounter.EN0_Usr=0;
A->SaveArea[cpu].FixedPerfCounter.EN1_Usr=0;
A->SaveArea[cpu].FixedPerfCounter.EN2_Usr=0;
A->SaveArea[cpu].FixedPerfCounter.EN0_OS=0;
A->SaveArea[cpu].FixedPerfCounter.EN1_OS=0;
A->SaveArea[cpu].FixedPerfCounter.EN2_OS=0;
A->SaveArea[cpu].FixedPerfCounter.AnyThread_EN0=0;
A->SaveArea[cpu].FixedPerfCounter.AnyThread_EN1=0;
A->SaveArea[cpu].FixedPerfCounter.AnyThread_EN2=0;
A->SaveArea[cpu].GlobalPerfCounter.EN_FIXED_CTR0=0;
A->SaveArea[cpu].GlobalPerfCounter.EN_FIXED_CTR1=0;
A->SaveArea[cpu].GlobalPerfCounter.EN_FIXED_CTR2=0;
}
Write_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR_CTRL, &A->SaveArea[cpu].FixedPerfCounter);
Write_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_CTRL, &A->SaveArea[cpu].GlobalPerfCounter);
// Release the MSR handle associated to the Core.
if(A->SHM->C[cpu].FD != -1)
close(A->SHM->C[cpu].FD);
}
return(FALSE);
}
// Refresh the SHM data structure from updated MSR registers.
Bool32 Refresh_SHM(void *uArg)
{
uARG *A=(uARG *) uArg;
ssize_t retval=0;
int cpu=0;
Bool32 rc=TRUE;
if(A->SHM->C[cpu].T.Offline != TRUE)
{
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_MISC_ENABLE, (MISC_PROC_FEATURES *) &A->SHM->P.MiscFeatures)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_MTRR_DEF_TYPE,(MTRR_DEF_TYPE *) &A->SHM->P.MTRRdefType)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, MSR_PLATFORM_INFO, (PLATFORM_INFO *) &A->SHM->P.PlatformInfo)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, MSR_TURBO_RATIO_LIMIT, (TURBO *) &A->SHM->P.Turbo)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, MSR_POWER_CTL, (POWER_CONTROL *) &A->SHM->P.PowerControl)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, MSR_PKG_CST_CONFIG_CTRL, (CSTATE_CONFIG *) &A->SHM->P.CStateConfig)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PLATFORM_ID, (PLATFORM_ID *) &A->SHM->P.PlatformId)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_STATUS, (PERF_STATUS *) &A->SHM->P.PerfStatus)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_CTL, (PERF_CONTROL *) &A->SHM->P.PerfControl)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_EFER, (EXT_FEATURE_ENABLE *) &A->SHM->P.ExtFeature)) != -1);
}
for(cpu=0; cpu < A->SHM->P.CPU; cpu++)
if(A->SHM->C[cpu].T.Offline != TRUE)
{
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_PERF_GLOBAL_CTRL, (GLOBAL_PERF_COUNTER *) &A->SHM->C[cpu].GlobalPerfCounter)) != -1);
rc=((retval=Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR_CTRL, (FIXED_PERF_COUNTER *) &A->SHM->C[cpu].FixedPerfCounter)) != -1);
}
return(rc);
}
// Read the Time Stamp Counter.
static __inline__ unsigned long long int RDTSC(void)
{
unsigned Hi, Lo;
__asm__ volatile
(
"rdtsc"
:"=a" (Lo),
"=d" (Hi)
);
return ((unsigned long long int) Lo) | (((unsigned long long int) Hi) << 32);
}
static __inline__ unsigned long long int RDTSCP(void)
{
unsigned Hi, Lo, Aux;
__asm__ volatile
(
"rdtscp"
:"=a" (Lo),
"=d" (Hi),
"=c" (Aux)
);
return ((unsigned long long int) Lo) | (((unsigned long long int) Hi) << 32);
}
// [Genuine Intel]
double ClockSpeed_GenuineIntel()
{
return(100.00f);
};
// [Core]
double ClockSpeed_Core()
{
int FD=0;
if( (FD=open(CPU_BP, O_RDONLY)) != -1) {
FSB_FREQ FSB={0};
Read_MSR(FD, MSR_FSB_FREQ, (unsigned long long int *) &FSB);
close(FD);
switch(FSB.Bus_Speed)
{
case 0b101:
return(100.00f);
break;
case 0b001:
return(133.33f);
break;
case 0b011:
return(166.67f);
break;
default:
return(100.00f);
break;
}
}
else
return(100.00f);
};
// [Core2]
double ClockSpeed_Core2()
{
int FD=0;
if( (FD=open(CPU_BP, O_RDONLY)) != -1) {
FSB_FREQ FSB={0};
Read_MSR(FD, MSR_FSB_FREQ, (unsigned long long int *) &FSB);
close(FD);
switch(FSB.Bus_Speed)
{
case 0b101:
return(100.00f);
break;
case 0b001:
return(133.33f);
break;
case 0b011:
return(166.67f);
break;
case 0b010:
return(200.00f);
break;
case 0b000:
return(266.67f);
break;
case 0b100:
return(333.33f);
break;
case 0b110:
return(400.00f);
break;
default:
return(100.00f);
break;
}
}
else
return(100.00f);
};
// [Atom]. See Linux:tsc_msr.c
double ClockSpeed_Atom()
{
int FD=0;
if( (FD=open(CPU_BP, O_RDONLY)) != -1) {
FSB_FREQ FSB={0};
Read_MSR(FD, MSR_FSB_FREQ, (unsigned long long int *) &FSB);
close(FD);
switch(FSB.Bus_Speed)
{
case 0b111:
return(83.20f);
break;
case 0b101:
return(99.84f);
break;
case 0b001:
return(133.20f);
break;
case 0b011:
return(166.40f);
break;
default:
return(100.00f);
break;
}
}
else
return(100.00f);
};
// [Silvermont]
double ClockSpeed_Silvermont()
{
int FD=0;
if( (FD=open(CPU_BP, O_RDONLY)) != -1) {
FSB_FREQ FSB={0};
Read_MSR(FD, MSR_FSB_FREQ, (unsigned long long int *) &FSB);
close(FD);
switch(FSB.Bus_Speed)
{
case 0b100:
return(80.0f);
break;
case 0b000:
return(83.3f);
break;
case 0b001:
return(100.0f);
break;
case 0b010:
return(133.33f);
break;
case 0b011:
return(116.7f);
break;
default:
return(83.3f);
break;
}
}
else
return(83.3f);
};
// [GenuineIntel]
void *uCycle_GenuineIntel(void *uA, int cpu, int T)
{
uARG *A=(uARG *) uA;
// Unhalted Core & the Reference Cycles.
Read_MSR(A->SHM->C[cpu].FD, IA32_APERF, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].UCC);
Read_MSR(A->SHM->C[cpu].FD, IA32_MPERF, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].URC);
// TSC.
Read_MSR(A->SHM->C[cpu].FD, IA32_TIME_STAMP_COUNTER, (unsigned long long int *) &A->SHM->C[cpu].Cycles.TSC[T]);
// Derive C1
A->SHM->C[cpu].Cycles.C1[T]=(A->SHM->C[cpu].Cycles.TSC[T] > A->SHM->C[cpu].Cycles.C0[T].URC) ?
A->SHM->C[cpu].Cycles.TSC[T] - A->SHM->C[cpu].Cycles.C0[T].URC : 0;
return(NULL);
}
// [Core]
void *uCycle_Core(void *uA, int cpu, int T)
{
uARG *A=(uARG *) uA;
// Instructions Retired
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR0, (unsigned long long int *) &A->SHM->C[cpu].Cycles.INST[T]);
// Unhalted Core & the Reference Cycles.
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR1, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].UCC);
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR2, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].URC);
// TSC.
Read_MSR(A->SHM->C[cpu].FD, IA32_TIME_STAMP_COUNTER, (unsigned long long int *) &A->SHM->C[cpu].Cycles.TSC[T]);
// Derive C1
A->SHM->C[cpu].Cycles.C1[T]=(A->SHM->C[cpu].Cycles.TSC[T] > A->SHM->C[cpu].Cycles.C0[T].URC) ?
A->SHM->C[cpu].Cycles.TSC[T] - A->SHM->C[cpu].Cycles.C0[T].URC : 0;
return(NULL);
}
// [Nehalem]
double ClockSpeed_Nehalem_Bloomfield()
{
return(133.33f);
};
#define ClockSpeed_Nehalem_Lynnfield ClockSpeed_Nehalem_Bloomfield
#define ClockSpeed_Nehalem_MB ClockSpeed_Nehalem_Bloomfield
#define ClockSpeed_Nehalem_EX ClockSpeed_Nehalem_Bloomfield
void *uCycle_Nehalem(void *uA, int cpu, int T)
{
uARG *A=(uARG *) uA;
// Instructions Retired
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR0, (unsigned long long int *) &A->SHM->C[cpu].Cycles.INST[T]);
// Unhalted Core & Reference Cycles.
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR1, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].UCC);
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR2, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].URC);
// TSC in relation to the Logical Core.
Read_MSR(A->SHM->C[cpu].FD, IA32_TIME_STAMP_COUNTER, (unsigned long long int *) &A->SHM->C[cpu].Cycles.TSC[T]);
// C-States.
Read_MSR(A->SHM->C[cpu].FD, MSR_CORE_C3_RESIDENCY, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C3[T]);
Read_MSR(A->SHM->C[cpu].FD, MSR_CORE_C6_RESIDENCY, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C6[T]);
// Derive C1
register unsigned long long int Cx=A->SHM->C[cpu].Cycles.C6[T] + A->SHM->C[cpu].Cycles.C3[T] + A->SHM->C[cpu].Cycles.C0[T].URC;
A->SHM->C[cpu].Cycles.C1[T]=(A->SHM->C[cpu].Cycles.TSC[T] > Cx) ? A->SHM->C[cpu].Cycles.TSC[T] - Cx : 0;
return(NULL);
}
// [Westmere]
double ClockSpeed_Westmere()
{
return(133.33f);
};
#define ClockSpeed_Westmere_EP ClockSpeed_Westmere
#define ClockSpeed_Westmere_EX ClockSpeed_Westmere
// [SandyBridge]
double ClockSpeed_SandyBridge_EP()
{
return(100.00f);
};
#define ClockSpeed_SandyBridge ClockSpeed_SandyBridge_EP
void *uCycle_SandyBridge(void *uA, int cpu, int T)
{
uARG *A=(uARG *) uA;
// Instructions Retired
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR0, (unsigned long long int *) &A->SHM->C[cpu].Cycles.INST[T]);
// Unhalted Core & Reference Cycles.
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR1, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].UCC);
Read_MSR(A->SHM->C[cpu].FD, IA32_FIXED_CTR2, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C0[T].URC);
// TSC in relation to the Logical Core.
Read_MSR(A->SHM->C[cpu].FD, IA32_TIME_STAMP_COUNTER, (unsigned long long int *) &A->SHM->C[cpu].Cycles.TSC[T]);
// C-States.
Read_MSR(A->SHM->C[cpu].FD, MSR_CORE_C3_RESIDENCY, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C3[T]);
Read_MSR(A->SHM->C[cpu].FD, MSR_CORE_C6_RESIDENCY, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C6[T]);
Read_MSR(A->SHM->C[cpu].FD, MSR_CORE_C7_RESIDENCY, (unsigned long long int *) &A->SHM->C[cpu].Cycles.C7[T]);
// Derive C1
register unsigned long long int Cx=A->SHM->C[cpu].Cycles.C7[T] + A->SHM->C[cpu].Cycles.C6[T] + A->SHM->C[cpu].Cycles.C3[T] + A->SHM->C[cpu].Cycles.C0[T].URC;
A->SHM->C[cpu].Cycles.C1[T]=(A->SHM->C[cpu].Cycles.TSC[T] > Cx) ? A->SHM->C[cpu].Cycles.TSC[T] - Cx : 0;
return(NULL);
}
// [IvyBridge]
double ClockSpeed_IvyBridge()
{
return(100.00f);
};
#define ClockSpeed_IvyBridge_EP ClockSpeed_IvyBridge
// [Haswell]
double ClockSpeed_Haswell_DT()
{
return(100.00f);
};
#define ClockSpeed_Haswell_MB ClockSpeed_Haswell_DT
#define ClockSpeed_Haswell_ULT ClockSpeed_Haswell_DT
#define ClockSpeed_Haswell_ULX ClockSpeed_Haswell_DT
// Estimate the Bus Clock Frequency from the TSC.
double Compute_ExtClock(int Coef)
{
unsigned long long int TSC[2];
TSC[0]=RDTSC();
usleep(IDLE_BASE_USEC * Coef);
TSC[1]=RDTSC();
return((double) (TSC[1] - TSC[0]) / (IDLE_BASE_USEC * Coef));
}
double Compute64_ExtClock(int Coef)
{
unsigned long long int TSC[2];
TSC[0]=RDTSCP();
usleep(IDLE_BASE_USEC * Coef);
TSC[1]=RDTSCP();
return((double) (TSC[1] - TSC[0]) / (IDLE_BASE_USEC * Coef));
}
// Read the Base Clock in ROM memory.
int Read_ROM_BCLK(off_t addr)
{
int fd=-1;
ssize_t br=1;
char buf[2]={0};
int bclk=0;
if( (fd=open("/dev/mem", O_RDONLY)) != -1 )
{
if( (lseek(fd, addr, SEEK_SET ) != -1) && ((br=read(fd, buf, 2)) != 1) )
bclk=((unsigned char) (buf[0])) + ((unsigned char) (buf[1] << 8));
close(fd);
}
return(bclk);
}
// Retreive the Processor features through a call to the CPUID instruction.
void Read_Features(FEATURES *features)
{
int BX=0, DX=0, CX=0;
__asm__ volatile
(
"cpuid"
: "=b" (BX),
"=d" (DX),
"=c" (CX)
: "a" (0x0)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
features->VendorID[0]=BX; features->VendorID[1]=(BX >> 8); features->VendorID[2]= (BX >> 16); features->VendorID[3]= (BX >> 24);
features->VendorID[4]=DX; features->VendorID[5]=(DX >> 8); features->VendorID[6]= (DX >> 16); features->VendorID[7]= (DX >> 24);
features->VendorID[8]=CX; features->VendorID[9]=(CX >> 8); features->VendorID[10]=(CX >> 16); features->VendorID[11]=(CX >> 24);
__asm__ volatile
(
"cpuid"
: "=a" (features->Std.AX),
"=b" (features->Std.BX),
"=c" (features->Std.CX),
"=d" (features->Std.DX)
: "a" (0x1)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"xorq %%rcx, %%rcx \n\t"
"cpuid \n\t"
"shr $26, %%rax \n\t"
"and $0x3f, %%rax \n\t"
"add $1, %%rax"
: "=a" (features->ThreadCount)
: "a" (0x4)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"cpuid"
: "=a" (features->MONITOR_MWAIT_Leaf.AX),
"=b" (features->MONITOR_MWAIT_Leaf.BX),
"=c" (features->MONITOR_MWAIT_Leaf.CX),
"=d" (features->MONITOR_MWAIT_Leaf.DX)
: "a" (0x5)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"cpuid"
: "=a" (features->Thermal_Power_Leaf.AX),
"=b" (features->Thermal_Power_Leaf.BX),
"=c" (features->Thermal_Power_Leaf.CX),
"=d" (features->Thermal_Power_Leaf.DX)
: "a" (0x6)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"xorq %%rbx, %%rbx \n\t"
"xorq %%rcx, %%rcx \n\t"
"xorq %%rdx, %%rdx \n\t"
"cpuid"
: "=a" (features->ExtFeature.AX),
"=b" (features->ExtFeature.BX),
"=c" (features->ExtFeature.CX),
"=d" (features->ExtFeature.DX)
: "a" (0x7)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"cpuid"
: "=a" (features->Perf_Monitoring_Leaf.AX),
"=b" (features->Perf_Monitoring_Leaf.BX),
"=c" (features->Perf_Monitoring_Leaf.CX),
"=d" (features->Perf_Monitoring_Leaf.DX)
: "a" (0xa)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"cpuid"
: "=a" (features->LargestExtFunc)
: "a" (0x80000000)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
if(features->LargestExtFunc >= 0x80000004 && features->LargestExtFunc <= 0x80000008)
{
__asm__ volatile
(
"cpuid \n\t"
"and $0x100, %%rdx \n\t"
"shr $8, %%rdx"
: "=d" (features->InvariantTSC)
: "a" (0x80000007)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
__asm__ volatile
(
"cpuid"
: "=c" (features->ExtFunc.CX),
"=d" (features->ExtFunc.DX)
: "a" (0x80000001)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
struct
{
struct
{
unsigned char Chr[4];
} AX, BX, CX, DX;
} Brand;
char tmpString[48];
int ix=0, jx=0, px=0;
for(ix=0; ix<3; ix++)
{
__asm__ volatile
(
"cpuid"
: "=a" (Brand.AX),
"=b" (Brand.BX),
"=c" (Brand.CX),
"=d" (Brand.DX)
: "a" (0x80000002 + ix)
#if defined(FreeBSD)
: "rcx", "rbx"
#endif
);
for(jx=0; jx<4; jx++, px++)
tmpString[px]=Brand.AX.Chr[jx];
for(jx=0; jx<4; jx++, px++)
tmpString[px]=Brand.BX.Chr[jx];
for(jx=0; jx<4; jx++, px++)
tmpString[px]=Brand.CX.Chr[jx];
for(jx=0; jx<4; jx++, px++)
tmpString[px]=Brand.DX.Chr[jx];
}
for(ix=jx=0; jx < px; jx++)
if(!(tmpString[jx] == 0x20 && tmpString[jx+1] == 0x20))
features->BrandString[ix++]=tmpString[jx];
}
}
void Cache_Topology(TOPOLOGY *T)
{
unsigned int level=0x0;
for(level=0; level < CACHE_MAX_LEVEL; level++)
{
asm volatile
(
"cpuid"
: "=a" (T->Cache[level].AX),
"=b" (T->Cache[level].BX),
"=c" (T->Cache[level].Sets),
"=d" (T->Cache[level].DX)
: "a" (0x4),
"c" (level)
#if defined(FreeBSD)
: "rax", "rbx", "rcx", "rdx"
#endif
);
T->Cache[level].Size = (T->Cache[level].Sets + 1)
* (T->Cache[level].Linez + 1)
* (T->Cache[level].Parts + 1)
* (T->Cache[level].Ways + 1);
}
}
// Enumerate the Processor's Cores and Threads topology.
static void *uReadAPIC(void *uApic)
{
uAPIC *pApic=(uAPIC *) uApic;
uARG *A=(uARG *) pApic->A;
unsigned int cpu=pApic->cpu;